Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2 | * |
| 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 Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 25 | // 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 Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 31 | |
| 32 | #include "vim.h" |
| 33 | |
| 34 | #include <limits.h> |
| 35 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 36 | #if defined(MSWIN) && defined(HAVE_FCNTL_H) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 37 | # undef HAVE_FCNTL_H |
| 38 | #endif |
| 39 | |
| 40 | #ifdef _DEBUG |
| 41 | # undef _DEBUG |
| 42 | #endif |
| 43 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 44 | #ifdef F_BLANK |
| 45 | # undef F_BLANK |
| 46 | #endif |
| 47 | |
ichizok | 6c3d3e6 | 2022-11-04 22:38:11 +0000 | [diff] [blame] | 48 | #ifdef HAVE_DUP |
| 49 | # undef HAVE_DUP |
| 50 | #endif |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 51 | #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 Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 60 | #ifdef HAVE_STDARG_H |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 61 | # undef HAVE_STDARG_H // Python's config.h defines it as well. |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 62 | #endif |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 63 | #ifdef _POSIX_C_SOURCE // defined in feature.h |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 64 | # undef _POSIX_C_SOURCE |
| 65 | #endif |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 66 | #ifdef _XOPEN_SOURCE |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 67 | # undef _XOPEN_SOURCE // pyconfig.h defines it as well. |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 68 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 69 | |
Bram Moolenaar | 0bdda37 | 2013-06-10 18:36:24 +0200 | [diff] [blame] | 70 | #define PY_SSIZE_T_CLEAN |
| 71 | |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 72 | #ifdef Py_LIMITED_API |
| 73 | # define USE_LIMITED_API // Using Python 3 limited ABI |
| 74 | #endif |
| 75 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 76 | #include <Python.h> |
Bram Moolenaar | 0bdda37 | 2013-06-10 18:36:24 +0200 | [diff] [blame] | 77 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 78 | #undef main // Defined in python.h - aargh |
| 79 | #undef HAVE_FCNTL_H // Clash with os_win32.h |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 80 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 81 | // The "surrogateescape" error handler is new in Python 3.1 |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 82 | #if PY_VERSION_HEX >= 0x030100f0 |
| 83 | # define CODEC_ERROR_HANDLER "surrogateescape" |
| 84 | #else |
| 85 | # define CODEC_ERROR_HANDLER NULL |
| 86 | #endif |
| 87 | |
Philip H | 5a5f17f | 2022-11-05 14:05:31 +0000 | [diff] [blame] | 88 | // Suppress Python 3.11 depreciations to see useful warnings |
Philip H | 422b9dc | 2023-08-11 22:38:48 +0200 | [diff] [blame] | 89 | #ifdef __GNUC__ |
| 90 | # pragma GCC diagnostic push |
| 91 | # pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
Philip H | 5a5f17f | 2022-11-05 14:05:31 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 94 | // Python 3 does not support CObjects, always use Capsules |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 95 | #define PY_USE_CAPSULE |
| 96 | |
Bram Moolenaar | 2e2f52a | 2020-12-21 16:03:02 +0100 | [diff] [blame] | 97 | #define ERRORS_DECODE_ARG CODEC_ERROR_HANDLER |
| 98 | #define ERRORS_ENCODE_ARG ERRORS_DECODE_ARG |
| 99 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 100 | #define PyInt Py_ssize_t |
Bram Moolenaar | 32ac8cd | 2013-07-03 18:49:17 +0200 | [diff] [blame] | 101 | #ifndef PyString_Check |
| 102 | # define PyString_Check(obj) PyUnicode_Check(obj) |
| 103 | #endif |
Bram Moolenaar | e032461 | 2013-07-09 17:30:55 +0200 | [diff] [blame] | 104 | #define PyString_FromString(repr) \ |
Bram Moolenaar | 2e2f52a | 2020-12-21 16:03:02 +0100 | [diff] [blame] | 105 | PyUnicode_Decode(repr, STRLEN(repr), ENC_OPT, ERRORS_DECODE_ARG) |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 106 | #define PyString_FromFormat PyUnicode_FromFormat |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 107 | #ifdef PyUnicode_FromFormat |
| 108 | # define Py_UNICODE_USE_UCS_FUNCTIONS |
| 109 | #endif |
Bram Moolenaar | 32ac8cd | 2013-07-03 18:49:17 +0200 | [diff] [blame] | 110 | #ifndef PyInt_Check |
| 111 | # define PyInt_Check(obj) PyLong_Check(obj) |
| 112 | #endif |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 113 | #define PyInt_FromLong(i) PyLong_FromLong(i) |
| 114 | #define PyInt_AsLong(obj) PyLong_AsLong(obj) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 115 | #define Py_ssize_t_fmt "n" |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 116 | #define Py_bytes_fmt "y" |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 117 | |
Bram Moolenaar | 063a46b | 2014-01-14 16:36:51 +0100 | [diff] [blame] | 118 | #define PyIntArgFunc ssizeargfunc |
| 119 | #define PyIntObjArgProc ssizeobjargproc |
| 120 | |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 121 | /* |
| 122 | * PySlice_GetIndicesEx(): first argument type changed from PySliceObject |
| 123 | * to PyObject in Python 3.2 or later. |
| 124 | */ |
| 125 | #if PY_VERSION_HEX >= 0x030200f0 |
| 126 | typedef PyObject PySliceObject_T; |
| 127 | #else |
| 128 | typedef PySliceObject PySliceObject_T; |
| 129 | #endif |
| 130 | |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 131 | #ifndef MSWIN |
| 132 | # define HINSTANCE void * |
| 133 | #endif |
| 134 | #if defined(DYNAMIC_PYTHON3) || defined(MSWIN) |
| 135 | static HINSTANCE hinstPy3 = 0; // Instance of python.dll |
| 136 | #endif |
| 137 | |
Bram Moolenaar | 0c1f3f4 | 2011-02-25 15:18:50 +0100 | [diff] [blame] | 138 | #if defined(DYNAMIC_PYTHON3) || defined(PROTO) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 139 | |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 140 | # ifdef MSWIN |
| 141 | # define load_dll vimLoadLib |
| 142 | # define close_dll FreeLibrary |
| 143 | # define symbol_from_dll GetProcAddress |
| 144 | # define load_dll_error GetWin32Error |
| 145 | # else |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 146 | # include <dlfcn.h> |
| 147 | # define FARPROC void* |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 148 | # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 149 | # define load_dll(n) dlopen((n), RTLD_LAZY) |
| 150 | # else |
| 151 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 152 | # endif |
| 153 | # define close_dll dlclose |
| 154 | # define symbol_from_dll dlsym |
Martin Tournoij | 1a3e574 | 2021-07-24 13:57:29 +0200 | [diff] [blame] | 155 | # define load_dll_error dlerror |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 156 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 157 | /* |
| 158 | * Wrapper defines |
| 159 | */ |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 160 | # undef PyArg_Parse |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 161 | # define PyArg_Parse py3_PyArg_Parse |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 162 | # undef PyArg_ParseTuple |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 163 | # define PyArg_ParseTuple py3_PyArg_ParseTuple |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 164 | # define PyMem_Free py3_PyMem_Free |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 165 | # define PyMem_Malloc py3_PyMem_Malloc |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 166 | # define PyDict_SetItemString py3_PyDict_SetItemString |
| 167 | # define PyErr_BadArgument py3_PyErr_BadArgument |
| 168 | # define PyErr_Clear py3_PyErr_Clear |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 169 | # define PyErr_Format py3_PyErr_Format |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 170 | # define PyErr_PrintEx py3_PyErr_PrintEx |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 171 | # define PyErr_NoMemory py3_PyErr_NoMemory |
| 172 | # define PyErr_Occurred py3_PyErr_Occurred |
| 173 | # define PyErr_SetNone py3_PyErr_SetNone |
| 174 | # define PyErr_SetString py3_PyErr_SetString |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 175 | # define PyErr_SetObject py3_PyErr_SetObject |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 176 | # define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 177 | # define PyEval_InitThreads py3_PyEval_InitThreads |
| 178 | # define PyEval_RestoreThread py3_PyEval_RestoreThread |
| 179 | # define PyEval_SaveThread py3_PyEval_SaveThread |
| 180 | # define PyGILState_Ensure py3_PyGILState_Ensure |
| 181 | # define PyGILState_Release py3_PyGILState_Release |
| 182 | # define PyLong_AsLong py3_PyLong_AsLong |
| 183 | # define PyLong_FromLong py3_PyLong_FromLong |
| 184 | # define PyList_GetItem py3_PyList_GetItem |
| 185 | # define PyList_Append py3_PyList_Append |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 186 | # define PyList_Insert py3_PyList_Insert |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 187 | # define PyList_New py3_PyList_New |
| 188 | # define PyList_SetItem py3_PyList_SetItem |
| 189 | # define PyList_Size py3_PyList_Size |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 190 | # define PyTuple_New py3_PyTuple_New |
| 191 | # define PyTuple_GetItem py3_PyTuple_GetItem |
| 192 | # define PyTuple_SetItem py3_PyTuple_SetItem |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 193 | # define PyTuple_Size py3_PyTuple_Size |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 194 | # define PySequence_Check py3_PySequence_Check |
| 195 | # define PySequence_Size py3_PySequence_Size |
| 196 | # define PySequence_GetItem py3_PySequence_GetItem |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 197 | # define PySequence_Fast py3_PySequence_Fast |
Bram Moolenaar | 3b48b11 | 2018-07-04 22:03:25 +0200 | [diff] [blame] | 198 | # if PY_VERSION_HEX >= 0x030601f0 |
| 199 | # define PySlice_AdjustIndices py3_PySlice_AdjustIndices |
| 200 | # define PySlice_Unpack py3_PySlice_Unpack |
| 201 | # endif |
| 202 | # undef PySlice_GetIndicesEx |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 203 | # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx |
| 204 | # define PyImport_ImportModule py3_PyImport_ImportModule |
| 205 | # define PyObject_Init py3__PyObject_Init |
| 206 | # define PyDict_New py3_PyDict_New |
| 207 | # define PyDict_GetItemString py3_PyDict_GetItemString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 208 | # define PyDict_Next py3_PyDict_Next |
| 209 | # define PyMapping_Check py3_PyMapping_Check |
Bram Moolenaar | 32ac8cd | 2013-07-03 18:49:17 +0200 | [diff] [blame] | 210 | # ifndef PyMapping_Keys |
| 211 | # define PyMapping_Keys py3_PyMapping_Keys |
| 212 | # endif |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 213 | # if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \ |
| 214 | (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000) |
| 215 | # undef PyIter_Check |
Zdenek Dohnal | 90478f3 | 2021-06-14 15:08:30 +0200 | [diff] [blame] | 216 | # define PyIter_Check py3_PyIter_Check |
| 217 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 218 | # define PyIter_Next py3_PyIter_Next |
| 219 | # define PyObject_GetIter py3_PyObject_GetIter |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 220 | # define PyObject_Repr py3_PyObject_Repr |
Bram Moolenaar | bcb4097 | 2013-05-30 13:22:13 +0200 | [diff] [blame] | 221 | # define PyObject_GetItem py3_PyObject_GetItem |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 222 | # define PyObject_IsTrue py3_PyObject_IsTrue |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 223 | # define PyModule_GetDict py3_PyModule_GetDict |
Yee Cheng Chin | 5084036 | 2024-09-10 20:56:13 +0200 | [diff] [blame] | 224 | # if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000 |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 225 | # define Py_IncRef py3_Py_IncRef |
| 226 | # define Py_DecRef py3_Py_DecRef |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 227 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 228 | # ifdef USE_LIMITED_API |
| 229 | # define Py_CompileString py3_Py_CompileString |
| 230 | # define PyEval_EvalCode py3_PyEval_EvalCode |
| 231 | # else |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 232 | # undef PyRun_SimpleString |
| 233 | # define PyRun_SimpleString py3_PyRun_SimpleString |
| 234 | # undef PyRun_String |
| 235 | # define PyRun_String py3_PyRun_String |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 236 | # endif |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 237 | # define PyObject_GetAttrString py3_PyObject_GetAttrString |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 238 | # define PyObject_HasAttrString py3_PyObject_HasAttrString |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 239 | # define PyObject_SetAttrString py3_PyObject_SetAttrString |
| 240 | # define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs |
Yaakov Selkowitz | 4179f19 | 2024-07-04 16:36:05 +0200 | [diff] [blame] | 241 | # if PY_VERSION_HEX >= 0x030d0000 |
| 242 | # define PyObject_CallFunction py3_PyObject_CallFunction |
| 243 | # else |
| 244 | # define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT |
| 245 | # endif |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 246 | # define PyObject_Call py3_PyObject_Call |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 247 | # define PyEval_GetLocals py3_PyEval_GetLocals |
| 248 | # define PyEval_GetGlobals py3_PyEval_GetGlobals |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 249 | # define PySys_SetObject py3_PySys_SetObject |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 250 | # define PySys_GetObject py3_PySys_GetObject |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 251 | # define PySys_SetArgv py3_PySys_SetArgv |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 252 | # define PyType_Ready py3_PyType_Ready |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 253 | # if PY_VERSION_HEX >= 0x03040000 |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 254 | # define PyType_GetFlags py3_PyType_GetFlags |
| 255 | # endif |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 256 | # undef Py_BuildValue |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 257 | # define Py_BuildValue py3_Py_BuildValue |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 258 | # define Py_SetPythonHome py3_Py_SetPythonHome |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 259 | # define Py_Initialize py3_Py_Initialize |
| 260 | # define Py_Finalize py3_Py_Finalize |
| 261 | # define Py_IsInitialized py3_Py_IsInitialized |
| 262 | # define _Py_NoneStruct (*py3__Py_NoneStruct) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 263 | # define _Py_FalseStruct (*py3__Py_FalseStruct) |
| 264 | # define _Py_TrueStruct (*py3__Py_TrueStruct) |
Yee Cheng Chin | 97a5be4 | 2024-09-09 19:46:17 +0200 | [diff] [blame] | 265 | # if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000 |
| 266 | // Private symbol that used to be required as part of PyIter_Check. |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 267 | # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented) |
| 268 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 269 | # define PyModule_AddObject py3_PyModule_AddObject |
| 270 | # define PyImport_AppendInittab py3_PyImport_AppendInittab |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 271 | # define PyImport_AddModule py3_PyImport_AddModule |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 272 | # ifdef USE_LIMITED_API |
| 273 | # if Py_LIMITED_API >= 0x030a0000 |
| 274 | # define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize |
| 275 | # endif |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 276 | # else |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 277 | # if PY_VERSION_HEX >= 0x030300f0 |
| 278 | # define PyUnicode_AsUTF8AndSize py3_PyUnicode_AsUTF8AndSize |
| 279 | # else |
| 280 | # define _PyUnicode_AsString py3__PyUnicode_AsString |
| 281 | # endif |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 282 | # endif |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 283 | # undef PyUnicode_CompareWithASCIIString |
| 284 | # define PyUnicode_CompareWithASCIIString py3_PyUnicode_CompareWithASCIIString |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 285 | # undef PyUnicode_AsEncodedString |
| 286 | # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 287 | # undef PyUnicode_AsUTF8String |
| 288 | # define PyUnicode_AsUTF8String py3_PyUnicode_AsUTF8String |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 289 | # undef PyBytes_AsString |
| 290 | # define PyBytes_AsString py3_PyBytes_AsString |
Bram Moolenaar | 32ac8cd | 2013-07-03 18:49:17 +0200 | [diff] [blame] | 291 | # ifndef PyBytes_AsStringAndSize |
| 292 | # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize |
| 293 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 294 | # undef PyBytes_FromString |
| 295 | # define PyBytes_FromString py3_PyBytes_FromString |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 296 | # undef PyBytes_FromStringAndSize |
| 297 | # define PyBytes_FromStringAndSize py3_PyBytes_FromStringAndSize |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 298 | # define PyFloat_FromDouble py3_PyFloat_FromDouble |
| 299 | # define PyFloat_AsDouble py3_PyFloat_AsDouble |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 300 | # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 301 | # define PyType_Type (*py3_PyType_Type) |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 302 | # ifndef USE_LIMITED_API |
| 303 | # define PyStdPrinter_Type (*py3_PyStdPrinter_Type) |
| 304 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 305 | # define PySlice_Type (*py3_PySlice_Type) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 306 | # define PyFloat_Type (*py3_PyFloat_Type) |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 307 | # define PyNumber_Check (*py3_PyNumber_Check) |
| 308 | # define PyNumber_Long (*py3_PyNumber_Long) |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 309 | # define PyBool_Type (*py3_PyBool_Type) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 310 | # define PyErr_NewException py3_PyErr_NewException |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 311 | # ifdef Py_DEBUG |
| 312 | # define _Py_NegativeRefcount py3__Py_NegativeRefcount |
| 313 | # define _Py_RefTotal (*py3__Py_RefTotal) |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 314 | # define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs |
| 315 | # else |
| 316 | # define PyModule_Create2 py3_PyModule_Create2 |
| 317 | # endif |
| 318 | # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 319 | # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc |
| 320 | # define _PyObject_DebugFree py3__PyObject_DebugFree |
| 321 | # else |
| 322 | # define PyObject_Malloc py3_PyObject_Malloc |
| 323 | # define PyObject_Free py3_PyObject_Free |
| 324 | # endif |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 325 | # define _PyObject_GC_New py3__PyObject_GC_New |
| 326 | # define PyObject_GC_Del py3_PyObject_GC_Del |
| 327 | # define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 328 | # define PyType_GenericAlloc py3_PyType_GenericAlloc |
| 329 | # define PyType_GenericNew py3_PyType_GenericNew |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 330 | # undef PyUnicode_FromString |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 331 | # define PyUnicode_FromString py3_PyUnicode_FromString |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 332 | # ifdef Py_UNICODE_USE_UCS_FUNCTIONS |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 333 | # ifdef Py_UNICODE_WIDE |
| 334 | # define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat |
| 335 | # else |
| 336 | # define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat |
| 337 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 338 | # else |
| 339 | # define PyUnicode_FromFormat py3_PyUnicode_FromFormat |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 340 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 341 | # undef PyUnicode_Decode |
| 342 | # define PyUnicode_Decode py3_PyUnicode_Decode |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 343 | # define PyType_IsSubtype py3_PyType_IsSubtype |
| 344 | # define PyCapsule_New py3_PyCapsule_New |
| 345 | # define PyCapsule_GetPointer py3_PyCapsule_GetPointer |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 346 | # ifdef USE_LIMITED_API |
| 347 | # define PyType_GetSlot py3_PyType_GetSlot |
| 348 | # define PyType_FromSpec py3_PyType_FromSpec |
| 349 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 350 | |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 351 | # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 352 | # undef PyObject_NEW |
| 353 | # define PyObject_NEW(type, typeobj) \ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 354 | ( (type *) PyObject_Init( \ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 355 | (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 356 | # elif PY_VERSION_HEX >= 0x030900b0 |
| 357 | # undef PyObject_NEW |
| 358 | # define PyObject_NEW(type, typeobj) \ |
| 359 | ((type *)py3__PyObject_New(typeobj)) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 360 | # endif |
| 361 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 362 | /* |
| 363 | * Pointers for dynamic link |
| 364 | */ |
| 365 | static int (*py3_PySys_SetArgv)(int, wchar_t **); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 366 | static void (*py3_Py_SetPythonHome)(wchar_t *home); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 367 | static void (*py3_Py_Initialize)(void); |
| 368 | static PyObject* (*py3_PyList_New)(Py_ssize_t size); |
| 369 | static PyGILState_STATE (*py3_PyGILState_Ensure)(void); |
| 370 | static void (*py3_PyGILState_Release)(PyGILState_STATE); |
| 371 | static int (*py3_PySys_SetObject)(char *, PyObject *); |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 372 | static PyObject* (*py3_PySys_GetObject)(char *); |
| 373 | static int (*py3_PyList_Append)(PyObject *, PyObject *); |
| 374 | static int (*py3_PyList_Insert)(PyObject *, int, PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 375 | static Py_ssize_t (*py3_PyList_Size)(PyObject *); |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 376 | static PyObject* (*py3_PyTuple_New)(Py_ssize_t size); |
| 377 | static int (*py3_PyTuple_SetItem)(PyObject *, Py_ssize_t, PyObject *); |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 378 | static Py_ssize_t (*py3_PyTuple_Size)(PyObject *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 379 | static int (*py3_PySequence_Check)(PyObject *); |
| 380 | static Py_ssize_t (*py3_PySequence_Size)(PyObject *); |
| 381 | static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t); |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 382 | static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 383 | static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t); |
| 384 | static int (*py3_PyMapping_Check)(PyObject *); |
Bram Moolenaar | bcb4097 | 2013-05-30 13:22:13 +0200 | [diff] [blame] | 385 | static PyObject* (*py3_PyMapping_Keys)(PyObject *); |
Bram Moolenaar | 3b48b11 | 2018-07-04 22:03:25 +0200 | [diff] [blame] | 386 | # if PY_VERSION_HEX >= 0x030601f0 |
| 387 | static int (*py3_PySlice_AdjustIndices)(Py_ssize_t length, |
| 388 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step); |
| 389 | static int (*py3_PySlice_Unpack)(PyObject *slice, |
| 390 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step); |
| 391 | # endif |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 392 | static int (*py3_PySlice_GetIndicesEx)(PySliceObject_T *r, Py_ssize_t length, |
Bram Moolenaar | 063a46b | 2014-01-14 16:36:51 +0100 | [diff] [blame] | 393 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, |
| 394 | Py_ssize_t *slicelen); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 395 | static PyObject* (*py3_PyErr_NoMemory)(void); |
| 396 | static void (*py3_Py_Finalize)(void); |
| 397 | static void (*py3_PyErr_SetString)(PyObject *, const char *); |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 398 | static void (*py3_PyErr_SetObject)(PyObject *, PyObject *); |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 399 | static int (*py3_PyErr_ExceptionMatches)(PyObject *); |
Yee Cheng Chin | 5084036 | 2024-09-10 20:56:13 +0200 | [diff] [blame] | 400 | # if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000 |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 401 | static void (*py3_Py_IncRef)(PyObject *); |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 402 | static void (*py3_Py_DecRef)(PyObject *); |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 403 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 404 | # ifdef USE_LIMITED_API |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 405 | static PyObject* (*py3_Py_CompileString)(const char *, const char *, int); |
| 406 | static PyObject* (*py3_PyEval_EvalCode)(PyObject *co, PyObject *globals, PyObject *locals); |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 407 | # else |
| 408 | static int (*py3_PyRun_SimpleString)(char *); |
| 409 | static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 410 | # endif |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 411 | static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *); |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 412 | static int (*py3_PyObject_HasAttrString)(PyObject *, const char *); |
Bram Moolenaar | 0b40008 | 2013-11-03 00:28:25 +0100 | [diff] [blame] | 413 | static int (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *); |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 414 | static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...); |
Yaakov Selkowitz | 4179f19 | 2024-07-04 16:36:05 +0200 | [diff] [blame] | 415 | # if PY_VERSION_HEX >= 0x030d0000 |
| 416 | static PyObject* (*py3_PyObject_CallFunction)(PyObject *, char *, ...); |
| 417 | # else |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 418 | static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...); |
Yaakov Selkowitz | 4179f19 | 2024-07-04 16:36:05 +0200 | [diff] [blame] | 419 | # endif |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 420 | static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *); |
Yee Cheng Chin | f7f746b | 2023-09-30 12:28:50 +0200 | [diff] [blame] | 421 | static PyObject* (*py3_PyEval_GetGlobals)(void); |
| 422 | static PyObject* (*py3_PyEval_GetLocals)(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 423 | static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); |
| 424 | static PyObject* (*py3_PyImport_ImportModule)(const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 425 | static PyObject* (*py3_PyImport_AddModule)(const char *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 426 | static int (*py3_PyErr_BadArgument)(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 427 | static PyObject* (*py3_PyErr_Occurred)(void); |
| 428 | static PyObject* (*py3_PyModule_GetDict)(PyObject *); |
| 429 | static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); |
| 430 | static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 431 | static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 432 | static PyObject* (*py3_PyLong_FromLong)(long); |
| 433 | static PyObject* (*py3_PyDict_New)(void); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 434 | # if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \ |
| 435 | (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000) |
Zdenek Dohnal | 90478f3 | 2021-06-14 15:08:30 +0200 | [diff] [blame] | 436 | static int (*py3_PyIter_Check)(PyObject *o); |
| 437 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 438 | static PyObject* (*py3_PyIter_Next)(PyObject *); |
| 439 | static PyObject* (*py3_PyObject_GetIter)(PyObject *); |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 440 | static PyObject* (*py3_PyObject_Repr)(PyObject *); |
Bram Moolenaar | bcb4097 | 2013-05-30 13:22:13 +0200 | [diff] [blame] | 441 | static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *); |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 442 | static int (*py3_PyObject_IsTrue)(PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 443 | static PyObject* (*py3_Py_BuildValue)(char *, ...); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 444 | # if PY_VERSION_HEX >= 0x03040000 |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 445 | static int (*py3_PyType_GetFlags)(PyTypeObject *o); |
| 446 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 447 | static int (*py3_PyType_Ready)(PyTypeObject *type); |
| 448 | static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 449 | static PyObject* (*py3_PyUnicode_FromString)(const char *u); |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 450 | # ifdef Py_UNICODE_USE_UCS_FUNCTIONS |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 451 | # ifdef Py_UNICODE_WIDE |
| 452 | static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...); |
| 453 | # else |
| 454 | static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...); |
| 455 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 456 | # else |
| 457 | static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...); |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 458 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 459 | static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size, |
| 460 | const char *encoding, const char *errors); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 461 | static long (*py3_PyLong_AsLong)(PyObject *); |
| 462 | static void (*py3_PyErr_SetNone)(PyObject *); |
| 463 | static void (*py3_PyEval_InitThreads)(void); |
| 464 | static void(*py3_PyEval_RestoreThread)(PyThreadState *); |
| 465 | static PyThreadState*(*py3_PyEval_SaveThread)(void); |
| 466 | static int (*py3_PyArg_Parse)(PyObject *, char *, ...); |
| 467 | static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); |
Yee Cheng Chin | d606fcc | 2023-09-20 19:59:47 +0200 | [diff] [blame] | 468 | static void (*py3_PyMem_Free)(void *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 469 | static void* (*py3_PyMem_Malloc)(size_t); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 470 | static int (*py3_Py_IsInitialized)(void); |
| 471 | static void (*py3_PyErr_Clear)(void); |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 472 | static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...); |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 473 | static void (*py3_PyErr_PrintEx)(int); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 474 | static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); |
Yee Cheng Chin | 97a5be4 | 2024-09-09 19:46:17 +0200 | [diff] [blame] | 475 | # if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 476 | static iternextfunc py3__PyObject_NextNotImplemented; |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 477 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 478 | static PyObject* py3__Py_NoneStruct; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 479 | static PyObject* py3__Py_FalseStruct; |
| 480 | static PyObject* py3__Py_TrueStruct; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 481 | static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); |
| 482 | static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 483 | # ifdef USE_LIMITED_API |
| 484 | # if Py_LIMITED_API >= 0x030a0000 |
| 485 | static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size); |
| 486 | # endif |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 487 | # else |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 488 | # if PY_VERSION_HEX >= 0x030300f0 |
| 489 | static char* (*py3_PyUnicode_AsUTF8AndSize)(PyObject *unicode, Py_ssize_t *size); |
| 490 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 491 | static char* (*py3__PyUnicode_AsString)(PyObject *unicode); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 492 | # endif |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 493 | # endif |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 494 | static int (*py3_PyUnicode_CompareWithASCIIString)(PyObject *unicode, const char* string); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 495 | static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 496 | static PyObject* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 497 | static char* (*py3_PyBytes_AsString)(PyObject *bytes); |
Bram Moolenaar | 808c2bc | 2013-06-23 13:11:18 +0200 | [diff] [blame] | 498 | static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 499 | static PyObject* (*py3_PyBytes_FromString)(char *str); |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 500 | static PyObject* (*py3_PyBytes_FromStringAndSize)(char *str, Py_ssize_t length); |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 501 | # if PY_VERSION_HEX >= 0x030900b0 |
| 502 | static PyObject* (*py3__PyObject_New)(PyTypeObject *); |
| 503 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 504 | static PyObject* (*py3_PyFloat_FromDouble)(double num); |
| 505 | static double (*py3_PyFloat_AsDouble)(PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 506 | static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 507 | static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); |
| 508 | static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 509 | static PyTypeObject* py3_PyType_Type; |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 510 | # ifndef USE_LIMITED_API |
Bram Moolenaar | d4a8c98 | 2018-05-15 22:31:18 +0200 | [diff] [blame] | 511 | static PyTypeObject* py3_PyStdPrinter_Type; |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 512 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 513 | static PyTypeObject* py3_PySlice_Type; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 514 | static PyTypeObject* py3_PyFloat_Type; |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 515 | static PyTypeObject* py3_PyBool_Type; |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 516 | static int (*py3_PyNumber_Check)(PyObject *); |
| 517 | static PyObject* (*py3_PyNumber_Long)(PyObject *); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 518 | static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 519 | static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor); |
| 520 | static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 521 | # ifdef Py_DEBUG |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 522 | static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); |
| 523 | static Py_ssize_t* py3__Py_RefTotal; |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 524 | static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 525 | # else |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 526 | static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); |
| 527 | # endif |
| 528 | # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) |
| 529 | static void (*py3__PyObject_DebugFree)(void*); |
| 530 | static void* (*py3__PyObject_DebugMalloc)(size_t); |
| 531 | # else |
| 532 | static void (*py3_PyObject_Free)(void*); |
| 533 | static void* (*py3_PyObject_Malloc)(size_t); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 534 | # endif |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 535 | static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *); |
| 536 | static void(*py3_PyObject_GC_Del)(void *); |
| 537 | static void(*py3_PyObject_GC_UnTrack)(void *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 538 | static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 539 | # ifdef USE_LIMITED_API |
| 540 | static void* (*py3_PyType_GetSlot)(PyTypeObject *, int); |
| 541 | static PyObject* (*py3_PyType_FromSpec)(PyType_Spec *); |
| 542 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 543 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 544 | // Imported exception objects |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 545 | static PyObject *p3imp_PyExc_AttributeError; |
| 546 | static PyObject *p3imp_PyExc_IndexError; |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 547 | static PyObject *p3imp_PyExc_KeyError; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 548 | static PyObject *p3imp_PyExc_KeyboardInterrupt; |
| 549 | static PyObject *p3imp_PyExc_TypeError; |
| 550 | static PyObject *p3imp_PyExc_ValueError; |
Bram Moolenaar | 4100937 | 2013-07-01 22:03:04 +0200 | [diff] [blame] | 551 | static PyObject *p3imp_PyExc_SystemExit; |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 552 | static PyObject *p3imp_PyExc_RuntimeError; |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 553 | static PyObject *p3imp_PyExc_ImportError; |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 554 | static PyObject *p3imp_PyExc_OverflowError; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 555 | |
| 556 | # define PyExc_AttributeError p3imp_PyExc_AttributeError |
| 557 | # define PyExc_IndexError p3imp_PyExc_IndexError |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 558 | # define PyExc_KeyError p3imp_PyExc_KeyError |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 559 | # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt |
| 560 | # define PyExc_TypeError p3imp_PyExc_TypeError |
| 561 | # define PyExc_ValueError p3imp_PyExc_ValueError |
Bram Moolenaar | 4100937 | 2013-07-01 22:03:04 +0200 | [diff] [blame] | 562 | # define PyExc_SystemExit p3imp_PyExc_SystemExit |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 563 | # define PyExc_RuntimeError p3imp_PyExc_RuntimeError |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 564 | # define PyExc_ImportError p3imp_PyExc_ImportError |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 565 | # define PyExc_OverflowError p3imp_PyExc_OverflowError |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 566 | |
| 567 | /* |
| 568 | * Table of name to function pointer of python. |
| 569 | */ |
| 570 | # define PYTHON_PROC FARPROC |
| 571 | static struct |
| 572 | { |
| 573 | char *name; |
| 574 | PYTHON_PROC *ptr; |
| 575 | } py3_funcname_table[] = |
| 576 | { |
| 577 | {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 578 | {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 579 | {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 580 | {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
| 581 | {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 582 | {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 583 | {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 584 | {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, |
| 585 | {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, |
| 586 | {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, |
| 587 | {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 588 | {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 589 | {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 590 | {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 591 | {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 592 | {"PyTuple_New", (PYTHON_PROC*)&py3_PyTuple_New}, |
| 593 | {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem}, |
| 594 | {"PyTuple_SetItem", (PYTHON_PROC*)&py3_PyTuple_SetItem}, |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 595 | {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 596 | {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check}, |
| 597 | {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size}, |
| 598 | {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem}, |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 599 | {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast}, |
Bram Moolenaar | 3b48b11 | 2018-07-04 22:03:25 +0200 | [diff] [blame] | 600 | # if PY_VERSION_HEX >= 0x030601f0 |
| 601 | {"PySlice_AdjustIndices", (PYTHON_PROC*)&py3_PySlice_AdjustIndices}, |
| 602 | {"PySlice_Unpack", (PYTHON_PROC*)&py3_PySlice_Unpack}, |
| 603 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 604 | {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, |
| 605 | {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, |
| 606 | {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, |
| 607 | {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 608 | {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject}, |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 609 | {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches}, |
Yee Cheng Chin | 5084036 | 2024-09-10 20:56:13 +0200 | [diff] [blame] | 610 | # if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000 |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 611 | {"Py_IncRef", (PYTHON_PROC*)&py3_Py_IncRef}, |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 612 | {"Py_DecRef", (PYTHON_PROC*)&py3_Py_DecRef}, |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 613 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 614 | # ifdef USE_LIMITED_API |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 615 | {"Py_CompileString", (PYTHON_PROC*)&py3_Py_CompileString}, |
| 616 | {"PyEval_EvalCode", (PYTHON_PROC*)&PyEval_EvalCode}, |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 617 | # else |
| 618 | {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, |
| 619 | {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 620 | # endif |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 621 | {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString}, |
Bram Moolenaar | a9922d6 | 2013-05-30 13:01:18 +0200 | [diff] [blame] | 622 | {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString}, |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 623 | {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString}, |
| 624 | {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs}, |
Yaakov Selkowitz | 4179f19 | 2024-07-04 16:36:05 +0200 | [diff] [blame] | 625 | # if PY_VERSION_HEX >= 0x030d0000 |
| 626 | {"PyObject_CallFunction", (PYTHON_PROC*)&py3_PyObject_CallFunction}, |
| 627 | # else |
Bram Moolenaar | 81c40c5 | 2013-06-12 14:41:04 +0200 | [diff] [blame] | 628 | {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT}, |
Yaakov Selkowitz | 4179f19 | 2024-07-04 16:36:05 +0200 | [diff] [blame] | 629 | # endif |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 630 | {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call}, |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 631 | {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals}, |
| 632 | {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 633 | {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, |
| 634 | {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 635 | {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 636 | {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 637 | {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, |
| 638 | {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, |
| 639 | {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, |
| 640 | {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 641 | {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next}, |
| 642 | {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check}, |
Bram Moolenaar | bcb4097 | 2013-05-30 13:22:13 +0200 | [diff] [blame] | 643 | {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 644 | # if (defined(USE_LIMITED_API) && Py_LIMITED_API >= 0x03080000) || \ |
| 645 | (!defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x03080000) |
Zdenek Dohnal | 90478f3 | 2021-06-14 15:08:30 +0200 | [diff] [blame] | 646 | {"PyIter_Check", (PYTHON_PROC*)&py3_PyIter_Check}, |
| 647 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 648 | {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next}, |
| 649 | {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter}, |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 650 | {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr}, |
Bram Moolenaar | bcb4097 | 2013-05-30 13:22:13 +0200 | [diff] [blame] | 651 | {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem}, |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 652 | {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 653 | {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, |
| 654 | {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 655 | # if PY_VERSION_HEX >= 0x03040000 |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 656 | {"PyType_GetFlags", (PYTHON_PROC*)&py3_PyType_GetFlags}, |
| 657 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 658 | {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, |
| 659 | {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, |
| 660 | {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, |
| 661 | {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, |
| 662 | {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, |
| 663 | {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, |
| 664 | {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, |
Bram Moolenaar | 5f87b23 | 2013-06-13 20:57:50 +0200 | [diff] [blame] | 665 | {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 666 | {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, |
Yee Cheng Chin | 97a5be4 | 2024-09-09 19:46:17 +0200 | [diff] [blame] | 667 | # if !defined(USE_LIMITED_API) && PY_VERSION_HEX < 0x030D0000 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 668 | {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented}, |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 669 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 670 | {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 671 | {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct}, |
| 672 | {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 673 | {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 674 | {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format}, |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 675 | {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 676 | {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, |
| 677 | {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, |
| 678 | {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 679 | # ifdef USE_LIMITED_API |
| 680 | # if Py_LIMITED_API >= 0x030a0000 |
| 681 | {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize}, |
| 682 | # endif |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 683 | # else |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 684 | # if PY_VERSION_HEX >= 0x030300f0 |
| 685 | {"PyUnicode_AsUTF8AndSize", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8AndSize}, |
| 686 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 687 | {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 688 | # endif |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 689 | # endif |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 690 | {"PyUnicode_CompareWithASCIIString", (PYTHON_PROC*)&py3_PyUnicode_CompareWithASCIIString}, |
| 691 | {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String}, |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 692 | # ifdef Py_UNICODE_USE_UCS_FUNCTIONS |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 693 | # ifdef Py_UNICODE_WIDE |
| 694 | {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat}, |
| 695 | # else |
| 696 | {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat}, |
| 697 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 698 | # else |
| 699 | {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat}, |
Bram Moolenaar | 1a3b569 | 2013-05-30 12:40:39 +0200 | [diff] [blame] | 700 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 701 | {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString}, |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 702 | {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 703 | {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString}, |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 704 | {"PyBytes_FromStringAndSize", (PYTHON_PROC*)&py3_PyBytes_FromStringAndSize}, |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 705 | # if PY_VERSION_HEX >= 0x030900b0 |
| 706 | {"_PyObject_New", (PYTHON_PROC*)&py3__PyObject_New}, |
| 707 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 708 | {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble}, |
| 709 | {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 710 | {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 711 | {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, |
| 712 | {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 713 | {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 714 | # ifndef USE_LIMITED_API |
Bram Moolenaar | d4a8c98 | 2018-05-15 22:31:18 +0200 | [diff] [blame] | 715 | {"PyStdPrinter_Type", (PYTHON_PROC*)&py3_PyStdPrinter_Type}, |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 716 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 717 | {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 718 | {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 719 | {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type}, |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 720 | {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check}, |
| 721 | {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 722 | {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 723 | # ifdef Py_DEBUG |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 724 | {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, |
| 725 | {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 726 | {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs}, |
| 727 | # else |
| 728 | {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, |
| 729 | # endif |
| 730 | # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 731 | {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, |
| 732 | {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 733 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 734 | {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, |
| 735 | {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 736 | # endif |
Bram Moolenaar | 774267b | 2013-05-21 20:51:59 +0200 | [diff] [blame] | 737 | {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New}, |
| 738 | {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del}, |
| 739 | {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 740 | {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype}, |
| 741 | {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New}, |
| 742 | {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer}, |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 743 | # ifdef USE_LIMITED_API |
| 744 | # if PY_VERSION_HEX >= 0x03040000 |
| 745 | {"PyType_GetSlot", (PYTHON_PROC*)&py3_PyType_GetSlot}, |
| 746 | # endif |
| 747 | {"PyType_FromSpec", (PYTHON_PROC*)&py3_PyType_FromSpec}, |
| 748 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 749 | {"", NULL}, |
| 750 | }; |
| 751 | |
Yee Cheng Chin | 5084036 | 2024-09-10 20:56:13 +0200 | [diff] [blame] | 752 | # if defined(USE_LIMITED_API) || PY_VERSION_HEX >= 0x03080000 |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 753 | // Use stable versions of inc/dec ref. Note that these always null-check and |
| 754 | // therefore there's no difference between XINCREF and INCREF. |
Yee Cheng Chin | 5084036 | 2024-09-10 20:56:13 +0200 | [diff] [blame] | 755 | // |
| 756 | // For 3.8 or above, we also use this version even if not using limited API. |
| 757 | // The Py_DECREF macros in 3.8+ include references to internal functions which |
| 758 | // cause link errors when building Vim. The stable versions are exposed as API |
| 759 | // functions and don't have these problems (albeit slightly slower as they |
| 760 | // require function calls rather than an inlined macro). |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 761 | # undef Py_INCREF |
| 762 | # define Py_INCREF(obj) Py_IncRef((PyObject *)obj) |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 763 | # undef Py_XINCREF |
Yee Cheng Chin | c2285a8 | 2024-09-09 19:55:24 +0200 | [diff] [blame] | 764 | # define Py_XINCREF(obj) Py_IncRef((PyObject *)obj) |
| 765 | |
| 766 | # undef Py_DECREF |
| 767 | # define Py_DECREF(obj) Py_DecRef((PyObject *)obj) |
| 768 | # undef Py_XDECREF |
| 769 | # define Py_XDECREF(obj) Py_DecRef((PyObject *)obj) |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 770 | # endif |
| 771 | |
Bram Moolenaar | ee1b931 | 2020-07-16 22:15:53 +0200 | [diff] [blame] | 772 | # if PY_VERSION_HEX >= 0x030900b0 |
| 773 | static inline int |
| 774 | py3_PyType_HasFeature(PyTypeObject *type, unsigned long feature) |
| 775 | { |
| 776 | return ((PyType_GetFlags(type) & feature) != 0); |
| 777 | } |
| 778 | # define PyType_HasFeature(t,f) py3_PyType_HasFeature(t,f) |
| 779 | # endif |
| 780 | |
Zdenek Dohnal | 90478f3 | 2021-06-14 15:08:30 +0200 | [diff] [blame] | 781 | # if PY_VERSION_HEX >= 0x030a00b2 |
| 782 | static inline int |
| 783 | py3__PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) |
| 784 | { |
| 785 | return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type); |
| 786 | } |
Zdenek Dohnal | fee511c | 2022-06-27 13:59:00 +0100 | [diff] [blame] | 787 | # if PY_VERSION_HEX >= 0x030b00b3 |
| 788 | # undef PyObject_TypeCheck |
| 789 | # define PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t) |
| 790 | # else |
| 791 | # define _PyObject_TypeCheck(o,t) py3__PyObject_TypeCheck(o,t) |
| 792 | # endif |
Zdenek Dohnal | 90478f3 | 2021-06-14 15:08:30 +0200 | [diff] [blame] | 793 | # endif |
| 794 | |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 795 | # if !defined(USE_LIMITED_API) && PY_VERSION_HEX >= 0x030c00b0 |
Yee Cheng Chin | 396058a | 2023-10-17 10:38:11 +0200 | [diff] [blame] | 796 | // PyTuple_GET_SIZE/PyList_GET_SIZE are inlined functions that use Py_SIZE(), |
| 797 | // which started to introduce linkage dependency from Python 3.12. When we |
| 798 | // build Python in dynamic mode, we don't link against it in build time, and |
| 799 | // this would fail to build. Just use the non-inlined version instead. |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 800 | # undef PyTuple_GET_SIZE |
Yee Cheng Chin | 396058a | 2023-10-17 10:38:11 +0200 | [diff] [blame] | 801 | # define PyTuple_GET_SIZE(o) PyTuple_Size(o) |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 802 | # undef PyList_GET_SIZE |
Yee Cheng Chin | 396058a | 2023-10-17 10:38:11 +0200 | [diff] [blame] | 803 | # define PyList_GET_SIZE(o) PyList_Size(o) |
Ken Takata | fa145f2 | 2023-10-06 19:27:13 +0200 | [diff] [blame] | 804 | # endif |
| 805 | |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 806 | # ifdef MSWIN |
| 807 | /* |
| 808 | * Look up the library "libname" using the InstallPath registry key. |
| 809 | * Return NULL when failed. Return an allocated string when successful. |
| 810 | */ |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 811 | static WCHAR * |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 812 | py3_get_system_libname(const char *libname) |
| 813 | { |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 814 | const WCHAR *pythoncore = L"Software\\Python\\PythonCore"; |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 815 | const char *cp = libname; |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 816 | WCHAR subkey[128]; |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 817 | HKEY hKey; |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 818 | int i; |
| 819 | DWORD j, len; |
| 820 | LSTATUS ret; |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 821 | |
| 822 | while (*cp != '\0') |
| 823 | { |
| 824 | if (*cp == ':' || *cp == '\\' || *cp == '/') |
| 825 | { |
| 826 | // Bail out if "libname" contains path separator, assume it is |
| 827 | // an absolute path. |
| 828 | return NULL; |
| 829 | } |
| 830 | ++cp; |
| 831 | } |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 832 | |
| 833 | WCHAR keyfound[32]; |
| 834 | HKEY hKeyTop[] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE}; |
| 835 | HKEY hKeyFound = NULL; |
| 836 | # ifdef USE_LIMITED_API |
| 837 | long maxminor = -1; |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 838 | # endif |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 839 | for (i = 0; i < ARRAY_LENGTH(hKeyTop); i++) |
| 840 | { |
| 841 | long major, minor; |
| 842 | |
| 843 | ret = RegOpenKeyExW(hKeyTop[i], pythoncore, 0, KEY_READ, &hKey); |
| 844 | if (ret != ERROR_SUCCESS) |
| 845 | continue; |
| 846 | for (j = 0;; j++) |
| 847 | { |
| 848 | WCHAR keyname[32]; |
| 849 | WCHAR *wp; |
| 850 | |
| 851 | len = ARRAY_LENGTH(keyname); |
| 852 | ret = RegEnumKeyExW(hKey, j, keyname, &len, |
| 853 | NULL, NULL, NULL, NULL); |
| 854 | if (ret == ERROR_NO_MORE_ITEMS) |
| 855 | break; |
| 856 | |
| 857 | major = wcstol(keyname, &wp, 10); |
Ken Takata | 982ef16 | 2023-10-18 12:02:24 +0200 | [diff] [blame] | 858 | if (*wp != L'.') |
| 859 | continue; |
| 860 | minor = wcstol(wp + 1, &wp, 10); |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 861 | # ifdef _WIN64 |
| 862 | if (*wp != L'\0') |
| 863 | continue; |
| 864 | # else |
| 865 | if (wcscmp(wp, L"-32") != 0) |
| 866 | continue; |
| 867 | # endif |
| 868 | |
| 869 | if (major != PY_MAJOR_VERSION) |
| 870 | continue; |
| 871 | # ifdef USE_LIMITED_API |
| 872 | // Search the latest version. |
| 873 | if ((minor > maxminor) |
| 874 | && (minor >= ((Py_LIMITED_API >> 16) & 0xff))) |
| 875 | { |
| 876 | maxminor = minor; |
| 877 | wcscpy(keyfound, keyname); |
| 878 | hKeyFound = hKeyTop[i]; |
| 879 | } |
| 880 | # else |
| 881 | // Check if it matches with the compiled version. |
| 882 | if (minor == PY_MINOR_VERSION) |
| 883 | { |
| 884 | wcscpy(keyfound, keyname); |
| 885 | hKeyFound = hKeyTop[i]; |
| 886 | break; |
| 887 | } |
| 888 | # endif |
| 889 | } |
| 890 | RegCloseKey(hKey); |
| 891 | # ifdef USE_LIMITED_API |
| 892 | if (hKeyFound != NULL) |
| 893 | break; |
| 894 | # endif |
| 895 | } |
| 896 | if (hKeyFound == NULL) |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 897 | return NULL; |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 898 | |
| 899 | swprintf(subkey, ARRAY_LENGTH(subkey), L"%ls\\%ls\\InstallPath", |
| 900 | pythoncore, keyfound); |
| 901 | ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ, |
| 902 | NULL, NULL, &len); |
| 903 | if (ret != ERROR_MORE_DATA && ret != ERROR_SUCCESS) |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 904 | return NULL; |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 905 | size_t len2 = len / sizeof(WCHAR) + 1 + strlen(libname); |
| 906 | WCHAR *path = alloc(len2 * sizeof(WCHAR)); |
| 907 | if (path == NULL) |
| 908 | return NULL; |
| 909 | ret = RegGetValueW(hKeyFound, subkey, NULL, RRF_RT_REG_SZ, |
| 910 | NULL, path, &len); |
| 911 | if (ret != ERROR_SUCCESS) |
| 912 | { |
| 913 | vim_free(path); |
| 914 | return NULL; |
| 915 | } |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 916 | // Remove trailing path separators. |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 917 | size_t len3 = wcslen(path); |
| 918 | if ((len3 > 0) && (path[len3 - 1] == L'/' || path[len3 - 1] == L'\\')) |
| 919 | --len3; |
| 920 | swprintf(path + len3, len2 - len3, L"\\%hs", libname); |
| 921 | return path; |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 922 | } |
| 923 | # endif |
| 924 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 925 | /* |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 926 | * Load library and get all pointers. |
| 927 | * Parameter 'libname' provides name of DLL. |
| 928 | * Return OK or FAIL. |
| 929 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 930 | static int |
| 931 | py3_runtime_link_init(char *libname, int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 932 | { |
| 933 | int i; |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 934 | PYTHON_PROC *ucs_from_string = (PYTHON_PROC *)&py3_PyUnicode_FromString; |
| 935 | PYTHON_PROC *ucs_decode = (PYTHON_PROC *)&py3_PyUnicode_Decode; |
| 936 | PYTHON_PROC *ucs_as_encoded_string = |
| 937 | (PYTHON_PROC *)&py3_PyUnicode_AsEncodedString; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 938 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 939 | # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 940 | // Can't have Python and Python3 loaded at the same time. |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 941 | // It causes a crash, because RTLD_GLOBAL is needed for |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 942 | // standard C extension libraries of one or both python versions. |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 943 | if (python_loaded()) |
| 944 | { |
Bram Moolenaar | 9dc93ae | 2011-08-28 16:00:19 +0200 | [diff] [blame] | 945 | if (verbose) |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 946 | emsg(_(e_this_vim_cannot_execute_py3_after_using_python)); |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 947 | return FAIL; |
| 948 | } |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 949 | # endif |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 950 | |
| 951 | if (hinstPy3 != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 952 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 953 | hinstPy3 = load_dll(libname); |
| 954 | |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 955 | # ifdef MSWIN |
| 956 | if (!hinstPy3) |
| 957 | { |
| 958 | // Attempt to use the path from InstallPath as stored in the registry. |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 959 | WCHAR *syslibname = py3_get_system_libname(libname); |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 960 | |
| 961 | if (syslibname != NULL) |
| 962 | { |
Ken Takata | ae3cfa4 | 2023-10-14 11:49:09 +0200 | [diff] [blame] | 963 | hinstPy3 = LoadLibraryExW(syslibname, NULL, |
| 964 | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | |
| 965 | LOAD_LIBRARY_SEARCH_SYSTEM32); |
Bram Moolenaar | b2f9e0e | 2020-12-25 13:52:37 +0100 | [diff] [blame] | 966 | vim_free(syslibname); |
| 967 | } |
| 968 | } |
| 969 | # endif |
| 970 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 971 | if (!hinstPy3) |
| 972 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 973 | if (verbose) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 974 | semsg(_(e_could_not_load_library_str_str), libname, load_dll_error()); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 975 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | for (i = 0; py3_funcname_table[i].ptr; ++i) |
| 979 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 980 | if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, |
| 981 | py3_funcname_table[i].name)) == NULL) |
| 982 | { |
| 983 | close_dll(hinstPy3); |
| 984 | hinstPy3 = 0; |
| 985 | if (verbose) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 986 | semsg(_(e_could_not_load_library_function_str), py3_funcname_table[i].name); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 987 | return FAIL; |
| 988 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 989 | } |
| 990 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 991 | // Load unicode functions separately as only the ucs2 or the ucs4 functions |
| 992 | // will be present in the library. |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 993 | # if PY_VERSION_HEX >= 0x030300f0 |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 994 | *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString"); |
| 995 | *ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode"); |
| 996 | *ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 997 | "PyUnicode_AsEncodedString"); |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 998 | # else |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 999 | *ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); |
| 1000 | *ucs_decode = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1001 | "PyUnicodeUCS2_Decode"); |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1002 | *ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1003 | "PyUnicodeUCS2_AsEncodedString"); |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1004 | if (*ucs_from_string == NULL || *ucs_decode == NULL |
| 1005 | || *ucs_as_encoded_string == NULL) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1006 | { |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1007 | *ucs_from_string = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1008 | "PyUnicodeUCS4_FromString"); |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1009 | *ucs_decode = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1010 | "PyUnicodeUCS4_Decode"); |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1011 | *ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1012 | "PyUnicodeUCS4_AsEncodedString"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1013 | } |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 1014 | # endif |
Bram Moolenaar | 7b24ce0 | 2018-03-29 18:15:26 +0200 | [diff] [blame] | 1015 | if (*ucs_from_string == NULL || *ucs_decode == NULL |
| 1016 | || *ucs_as_encoded_string == NULL) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1017 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1018 | close_dll(hinstPy3); |
| 1019 | hinstPy3 = 0; |
| 1020 | if (verbose) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1021 | semsg(_(e_could_not_load_library_function_str), "PyUnicode_UCSX_*"); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1022 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | return OK; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * If python is enabled (there is installed python on Windows system) return |
| 1030 | * TRUE, else FALSE. |
| 1031 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1032 | int |
| 1033 | python3_enabled(int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1034 | { |
Bram Moolenaar | 25e4fcd | 2016-01-09 14:57:47 +0100 | [diff] [blame] | 1035 | return py3_runtime_link_init((char *)p_py3dll, verbose) == OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1038 | /* |
| 1039 | * Load the standard Python exceptions - don't import the symbols from the |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1040 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 1041 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1042 | static void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1043 | get_py3_exceptions(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1044 | { |
| 1045 | PyObject *exmod = PyImport_ImportModule("builtins"); |
| 1046 | PyObject *exdict = PyModule_GetDict(exmod); |
| 1047 | p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 1048 | p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 1049 | p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1050 | p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 1051 | p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 1052 | p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
Bram Moolenaar | 4100937 | 2013-07-01 22:03:04 +0200 | [diff] [blame] | 1053 | p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit"); |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1054 | p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError"); |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 1055 | p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError"); |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 1056 | p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1057 | Py_XINCREF(p3imp_PyExc_AttributeError); |
| 1058 | Py_XINCREF(p3imp_PyExc_IndexError); |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 1059 | Py_XINCREF(p3imp_PyExc_KeyError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1060 | Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); |
| 1061 | Py_XINCREF(p3imp_PyExc_TypeError); |
| 1062 | Py_XINCREF(p3imp_PyExc_ValueError); |
Bram Moolenaar | 4100937 | 2013-07-01 22:03:04 +0200 | [diff] [blame] | 1063 | Py_XINCREF(p3imp_PyExc_SystemExit); |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1064 | Py_XINCREF(p3imp_PyExc_RuntimeError); |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 1065 | Py_XINCREF(p3imp_PyExc_ImportError); |
Bram Moolenaar | 141be8a | 2013-06-23 14:16:57 +0200 | [diff] [blame] | 1066 | Py_XINCREF(p3imp_PyExc_OverflowError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1067 | Py_XDECREF(exmod); |
| 1068 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1069 | #endif // DYNAMIC_PYTHON3 |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1070 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1071 | static int py3initialised = 0; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1072 | #define PYINITIALISED py3initialised |
Bram Moolenaar | c4f8338 | 2017-07-07 14:50:44 +0200 | [diff] [blame] | 1073 | static int python_end_called = FALSE; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1074 | |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1075 | #ifdef USE_LIMITED_API |
| 1076 | # define DESTRUCTOR_FINISH(self) \ |
Ken Takata | 9abd715 | 2024-08-10 09:44:20 +0200 | [diff] [blame] | 1077 | ((freefunc)PyType_GetSlot(Py_TYPE((PyObject*)self), Py_tp_free))((PyObject*)self) |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1078 | #else |
| 1079 | # define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self) |
| 1080 | #endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1081 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1082 | #define WIN_PYTHON_REF(win) win->w_python3_ref |
| 1083 | #define BUF_PYTHON_REF(buf) buf->b_python3_ref |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1084 | #define TAB_PYTHON_REF(tab) tab->tp_python3_ref |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1085 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1086 | static void |
| 1087 | call_PyObject_Free(void *p) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1088 | { |
Bram Moolenaar | 0014a53 | 2013-05-29 21:33:39 +0200 | [diff] [blame] | 1089 | #if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1090 | _PyObject_DebugFree(p); |
| 1091 | #else |
| 1092 | PyObject_Free(p); |
| 1093 | #endif |
| 1094 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1095 | |
| 1096 | static PyObject * |
| 1097 | call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1098 | { |
| 1099 | return PyType_GenericNew(type,args,kwds); |
| 1100 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1101 | |
| 1102 | static PyObject * |
| 1103 | call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1104 | { |
| 1105 | return PyType_GenericAlloc(type,nitems); |
| 1106 | } |
| 1107 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1108 | static PyObject *OutputGetattro(PyObject *, PyObject *); |
| 1109 | static int OutputSetattro(PyObject *, PyObject *, PyObject *); |
| 1110 | static PyObject *BufferGetattro(PyObject *, PyObject *); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1111 | static int BufferSetattro(PyObject *, PyObject *, PyObject *); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1112 | static PyObject *TabPageGetattro(PyObject *, PyObject *); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1113 | static PyObject *WindowGetattro(PyObject *, PyObject *); |
| 1114 | static int WindowSetattro(PyObject *, PyObject *, PyObject *); |
| 1115 | static PyObject *RangeGetattro(PyObject *, PyObject *); |
| 1116 | static PyObject *CurrentGetattro(PyObject *, PyObject *); |
| 1117 | static int CurrentSetattro(PyObject *, PyObject *, PyObject *); |
| 1118 | static PyObject *DictionaryGetattro(PyObject *, PyObject *); |
| 1119 | static int DictionarySetattro(PyObject *, PyObject *, PyObject *); |
| 1120 | static PyObject *ListGetattro(PyObject *, PyObject *); |
| 1121 | static int ListSetattro(PyObject *, PyObject *, PyObject *); |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 1122 | static PyObject *TupleGetattro(PyObject *, PyObject *); |
| 1123 | static int TupleSetattro(PyObject *, PyObject *, PyObject *); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1124 | static PyObject *FunctionGetattro(PyObject *, PyObject *); |
| 1125 | |
| 1126 | static struct PyModuleDef vimmodule; |
| 1127 | |
Bram Moolenaar | 2a0f3d3 | 2013-05-21 22:23:56 +0200 | [diff] [blame] | 1128 | #define PY_CAN_RECURSE |
| 1129 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1130 | /* |
| 1131 | * Include the code shared with if_python.c |
| 1132 | */ |
| 1133 | #include "if_py_both.h" |
| 1134 | |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 1135 | #ifdef USE_LIMITED_API |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1136 | # if Py_LIMITED_API >= 0x030A0000 |
| 1137 | # define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL) |
| 1138 | # else |
| 1139 | // Python limited API before 3.10 lack easy ways to query the raw UTF-8 chars. |
| 1140 | // We need to first convert the string to bytes, and then extract the chars. |
| 1141 | // This function is only used for attribute string comparisons, which have |
| 1142 | // known short length. As such, just allocate a short static buffer to hold |
| 1143 | // the characters instead of having to allocate/deallcoate it. |
| 1144 | // |
| 1145 | // An alternative would be to convert all attribute string comparisons to use |
| 1146 | // PyUnicode_CompareWithASCIIString to skip having to extract the chars. |
| 1147 | static char py3_unicode_utf8_chars[20]; |
Yee Cheng Chin | f7f746b | 2023-09-30 12:28:50 +0200 | [diff] [blame] | 1148 | static char* PY_UNICODE_GET_UTF8_CHARS(PyObject* str) |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1149 | { |
| 1150 | py3_unicode_utf8_chars[0] = '\0'; |
| 1151 | PyObject* bytes = PyUnicode_AsUTF8String(str); |
| 1152 | if (bytes) |
| 1153 | { |
| 1154 | char *chars; |
| 1155 | Py_ssize_t len; |
| 1156 | if (PyBytes_AsStringAndSize(bytes, &chars, &len) != -1) |
| 1157 | { |
| 1158 | if (len < (Py_ssize_t)sizeof(py3_unicode_utf8_chars)) |
| 1159 | // PyBytes_AsStringAndSize guarantees null-termination |
| 1160 | memcpy(py3_unicode_utf8_chars, chars, len + 1); |
| 1161 | } |
| 1162 | Py_DECREF(bytes); |
| 1163 | } |
| 1164 | return py3_unicode_utf8_chars; |
| 1165 | } |
| 1166 | # endif |
Ken Takata | c97b3fe | 2023-10-11 21:27:06 +0200 | [diff] [blame] | 1167 | #else // !USE_LIMITED_API |
| 1168 | # if PY_VERSION_HEX >= 0x030300f0 |
| 1169 | # define PY_UNICODE_GET_UTF8_CHARS(obj) PyUnicode_AsUTF8AndSize(obj, NULL) |
| 1170 | # else |
| 1171 | # define PY_UNICODE_GET_UTF8_CHARS _PyUnicode_AsString |
| 1172 | # endif |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1173 | #endif |
| 1174 | |
Bram Moolenaar | 828bff1 | 2019-03-19 22:11:41 +0100 | [diff] [blame] | 1175 | // NOTE: Must always be used at the start of a block, since it declares "name". |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1176 | #define GET_ATTR_STRING(name, nameobj) \ |
| 1177 | char *name = ""; \ |
| 1178 | if (PyUnicode_Check(nameobj)) \ |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1179 | name = (char *)PY_UNICODE_GET_UTF8_CHARS(nameobj) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1180 | |
| 1181 | #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0) |
| 1182 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1183 | /////////////////////////////////////////////////////// |
| 1184 | // Internal function prototypes. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1185 | |
Bram Moolenaar | 7854e3a | 2012-11-28 15:33:14 +0100 | [diff] [blame] | 1186 | static PyObject *Py3Init_vim(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1187 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1188 | /////////////////////////////////////////////////////// |
| 1189 | // 1. Python interpreter main program. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1190 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1191 | void |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1192 | python3_end(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1193 | { |
| 1194 | static int recurse = 0; |
| 1195 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1196 | // If a crash occurs while doing this, don't try again. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1197 | if (recurse != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1198 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1199 | |
Bram Moolenaar | c4f8338 | 2017-07-07 14:50:44 +0200 | [diff] [blame] | 1200 | python_end_called = TRUE; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1201 | ++recurse; |
| 1202 | |
| 1203 | #ifdef DYNAMIC_PYTHON3 |
| 1204 | if (hinstPy3) |
| 1205 | #endif |
| 1206 | if (Py_IsInitialized()) |
| 1207 | { |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 1208 | #ifdef USE_LIMITED_API |
| 1209 | shutdown_types(); |
| 1210 | #endif |
| 1211 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1212 | // acquire lock before finalizing |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1213 | PyGILState_Ensure(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1214 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1215 | Py_Finalize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1218 | --recurse; |
| 1219 | } |
| 1220 | |
Bram Moolenaar | 094454f | 2015-10-07 10:39:55 +0200 | [diff] [blame] | 1221 | #if (defined(DYNAMIC_PYTHON3) && defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON) && defined(UNIX)) || defined(PROTO) |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 1222 | int |
Bram Moolenaar | 68c2f63 | 2016-01-30 17:24:07 +0100 | [diff] [blame] | 1223 | python3_loaded(void) |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 1224 | { |
| 1225 | return (hinstPy3 != 0); |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
Bram Moolenaar | 9407316 | 2018-01-31 21:49:05 +0100 | [diff] [blame] | 1229 | static wchar_t *py_home_buf = NULL; |
| 1230 | |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1231 | #if defined(MSWIN) && (PY_VERSION_HEX >= 0x030500f0) |
Bram Moolenaar | c6ed254 | 2020-10-10 23:26:28 +0200 | [diff] [blame] | 1232 | /* |
| 1233 | * Return TRUE if stdin is readable from Python 3. |
| 1234 | */ |
| 1235 | static BOOL |
| 1236 | is_stdin_readable(void) |
| 1237 | { |
| 1238 | DWORD mode, eventnum; |
| 1239 | struct _stat st; |
| 1240 | int fd = fileno(stdin); |
| 1241 | HANDLE hstdin = (HANDLE)_get_osfhandle(fd); |
| 1242 | |
| 1243 | // Check if stdin is connected to the console. |
| 1244 | if (GetConsoleMode(hstdin, &mode)) |
| 1245 | // Check if it is opened as input. |
| 1246 | return GetNumberOfConsoleInputEvents(hstdin, &eventnum); |
| 1247 | |
| 1248 | return _fstat(fd, &st) == 0; |
| 1249 | } |
| 1250 | |
| 1251 | // Python 3.5 or later will abort inside Py_Initialize() when stdin has |
| 1252 | // been closed (i.e. executed by "vim -"). Reconnect stdin to CONIN$. |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1253 | // Note that the python DLL is linked to its own stdio DLL which can be |
| 1254 | // differ from Vim's stdio. |
| 1255 | static void |
| 1256 | reset_stdin(void) |
| 1257 | { |
| 1258 | FILE *(*py__acrt_iob_func)(unsigned) = NULL; |
| 1259 | FILE *(*pyfreopen)(const char *, const char *, FILE *) = NULL; |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 1260 | HINSTANCE hinst = get_forwarded_dll(hinstPy3); |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1261 | |
Bram Moolenaar | c6ed254 | 2020-10-10 23:26:28 +0200 | [diff] [blame] | 1262 | if (hinst == NULL || is_stdin_readable()) |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1263 | return; |
| 1264 | |
| 1265 | // Get "freopen" and "stdin" which are used in the python DLL. |
| 1266 | // "stdin" is defined as "__acrt_iob_func(0)" in VC++ 2015 or later. |
| 1267 | py__acrt_iob_func = get_dll_import_func(hinst, "__acrt_iob_func"); |
| 1268 | if (py__acrt_iob_func) |
| 1269 | { |
| 1270 | HINSTANCE hpystdiodll = find_imported_module_by_funcname(hinst, |
Bram Moolenaar | 253b16a | 2020-10-06 19:59:06 +0200 | [diff] [blame] | 1271 | "__acrt_iob_func"); |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1272 | if (hpystdiodll) |
Bram Moolenaar | 253b16a | 2020-10-06 19:59:06 +0200 | [diff] [blame] | 1273 | pyfreopen = (void *)GetProcAddress(hpystdiodll, "freopen"); |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Bram Moolenaar | c6ed254 | 2020-10-10 23:26:28 +0200 | [diff] [blame] | 1276 | // Reconnect stdin to CONIN$. |
Bram Moolenaar | 253b16a | 2020-10-06 19:59:06 +0200 | [diff] [blame] | 1277 | if (pyfreopen != NULL) |
Bram Moolenaar | c6ed254 | 2020-10-10 23:26:28 +0200 | [diff] [blame] | 1278 | pyfreopen("CONIN$", "r", py__acrt_iob_func(0)); |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1279 | else |
Bram Moolenaar | c6ed254 | 2020-10-10 23:26:28 +0200 | [diff] [blame] | 1280 | freopen("CONIN$", "r", stdin); |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1281 | } |
| 1282 | #else |
| 1283 | # define reset_stdin() |
| 1284 | #endif |
| 1285 | |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1286 | // Python 3.2 or later will abort inside Py_Initialize() when mandatory |
| 1287 | // modules cannot be loaded (e.g. 'pythonthreehome' is wrongly set.). |
| 1288 | // Install a hook to python dll's exit() and recover from it. |
| 1289 | #if defined(MSWIN) && (PY_VERSION_HEX >= 0x030200f0) |
| 1290 | # define HOOK_EXIT |
| 1291 | # include <setjmp.h> |
| 1292 | |
| 1293 | static jmp_buf exit_hook_jump_buf; |
| 1294 | static void *orig_exit = NULL; |
| 1295 | |
| 1296 | /* |
| 1297 | * Function that replaces exit() while calling Py_Initialize(). |
| 1298 | */ |
| 1299 | static void |
| 1300 | hooked_exit(int ret) |
| 1301 | { |
| 1302 | // Recover from exit. |
| 1303 | longjmp(exit_hook_jump_buf, 1); |
| 1304 | } |
| 1305 | |
| 1306 | /* |
| 1307 | * Install a hook to python dll's exit(). |
| 1308 | */ |
| 1309 | static void |
| 1310 | hook_py_exit(void) |
| 1311 | { |
Ken Takata | 119fdd9 | 2023-10-04 20:05:05 +0200 | [diff] [blame] | 1312 | HINSTANCE hinst = get_forwarded_dll(hinstPy3); |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1313 | |
| 1314 | if (hinst == NULL || orig_exit != NULL) |
| 1315 | return; |
| 1316 | |
| 1317 | orig_exit = hook_dll_import_func(hinst, "exit", (void *)hooked_exit); |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * Remove the hook installed by hook_py_exit(). |
| 1322 | */ |
| 1323 | static void |
| 1324 | restore_py_exit(void) |
| 1325 | { |
| 1326 | HINSTANCE hinst = hinstPy3; |
| 1327 | |
| 1328 | if (hinst == NULL) |
| 1329 | return; |
| 1330 | |
| 1331 | if (orig_exit != NULL) |
| 1332 | hook_dll_import_func(hinst, "exit", orig_exit); |
| 1333 | orig_exit = NULL; |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1337 | static int |
| 1338 | Python3_Init(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1339 | { |
| 1340 | if (!py3initialised) |
| 1341 | { |
| 1342 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1343 | if (!python3_enabled(TRUE)) |
| 1344 | { |
Bram Moolenaar | 9a846fb | 2022-01-01 21:59:18 +0000 | [diff] [blame] | 1345 | emsg(_(e_sorry_this_command_is_disabled_python_library_could_not_be_found)); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1346 | goto fail; |
| 1347 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1348 | #endif |
Boris Staletic | 83a0670 | 2024-10-14 20:28:39 +0200 | [diff] [blame] | 1349 | // Python 3.13 introduced a really useful feature: colorized exceptions. |
| 1350 | // This is great if you're reading them from the terminal, but useless |
| 1351 | // and broken everywhere else (such as in log files, or text editors). |
| 1352 | // Opt out, forcefully. |
| 1353 | vim_setenv((char_u*)"PYTHON_COLORS", (char_u*)"0"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1354 | |
| 1355 | init_structs(); |
| 1356 | |
Bram Moolenaar | 9407316 | 2018-01-31 21:49:05 +0100 | [diff] [blame] | 1357 | if (*p_py3home != NUL) |
| 1358 | { |
| 1359 | size_t len = mbstowcs(NULL, (char *)p_py3home, 0) + 1; |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 1360 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1361 | // The string must not change later, make a copy in static memory. |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 1362 | py_home_buf = ALLOC_MULT(wchar_t, len); |
Bram Moolenaar | 9407316 | 2018-01-31 21:49:05 +0100 | [diff] [blame] | 1363 | if (py_home_buf != NULL && mbstowcs( |
| 1364 | py_home_buf, (char *)p_py3home, len) != (size_t)-1) |
| 1365 | Py_SetPythonHome(py_home_buf); |
| 1366 | } |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 1367 | #ifdef PYTHON3_HOME |
Bram Moolenaar | 9407316 | 2018-01-31 21:49:05 +0100 | [diff] [blame] | 1368 | else if (mch_getenv((char_u *)"PYTHONHOME") == NULL) |
Bram Moolenaar | 1000565 | 2015-12-31 21:03:23 +0100 | [diff] [blame] | 1369 | Py_SetPythonHome(PYTHON3_HOME); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 1370 | #endif |
| 1371 | |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 1372 | PyImport_AppendInittab("vim", Py3Init_vim); |
| 1373 | |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1374 | #if !defined(DYNAMIC_PYTHON3) && defined(MSWIN) |
| 1375 | hinstPy3 = GetModuleHandle(PYTHON3_DLL); |
| 1376 | #endif |
Bram Moolenaar | 56b8dc3 | 2020-08-06 21:47:11 +0200 | [diff] [blame] | 1377 | reset_stdin(); |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1378 | |
| 1379 | #ifdef HOOK_EXIT |
| 1380 | // Catch exit() called in Py_Initialize(). |
| 1381 | hook_py_exit(); |
| 1382 | if (setjmp(exit_hook_jump_buf) == 0) |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1383 | { |
| 1384 | Py_Initialize(); |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1385 | restore_py_exit(); |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1386 | } |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1387 | else |
| 1388 | { |
| 1389 | // exit() was called in Py_Initialize(). |
| 1390 | restore_py_exit(); |
| 1391 | emsg(_(e_critical_error_in_python3_initialization_check_your_installation)); |
| 1392 | goto fail; |
| 1393 | } |
K.Takata | a7fbaa4 | 2022-12-26 14:46:51 +0000 | [diff] [blame] | 1394 | #else |
| 1395 | Py_Initialize(); |
Bram Moolenaar | 63ff72a | 2022-02-07 13:54:01 +0000 | [diff] [blame] | 1396 | #endif |
Bram Moolenaar | d057301 | 2017-10-28 21:11:06 +0200 | [diff] [blame] | 1397 | |
Bram Moolenaar | efc0d94 | 2020-10-11 18:05:02 +0200 | [diff] [blame] | 1398 | #if PY_VERSION_HEX < 0x03090000 |
| 1399 | // Initialise threads. This is deprecated since Python 3.9. |
Bram Moolenaar | 456f2bb | 2011-06-12 21:37:13 +0200 | [diff] [blame] | 1400 | PyEval_InitThreads(); |
Bram Moolenaar | efc0d94 | 2020-10-11 18:05:02 +0200 | [diff] [blame] | 1401 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1402 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1403 | get_py3_exceptions(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1404 | #endif |
| 1405 | |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 1406 | if (PythonIO_Init_io()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1407 | goto fail; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1408 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1409 | globals = PyModule_GetDict(PyImport_AddModule("__main__")); |
| 1410 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1411 | // Remove the element from sys.path that was added because of our |
| 1412 | // argv[0] value in Py3Init_vim(). Previously we used an empty |
| 1413 | // string, but depending on the OS we then get an empty entry or |
| 1414 | // the current directory in sys.path. |
| 1415 | // Only after vim has been imported, the element does exist in |
| 1416 | // sys.path. |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1417 | PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1418 | |
Bram Moolenaar | efc0d94 | 2020-10-11 18:05:02 +0200 | [diff] [blame] | 1419 | // Without the call to PyEval_SaveThread, thread specific state (such |
| 1420 | // as the system trace hook), will be lost between invocations of |
| 1421 | // Python code. |
| 1422 | // GIL may have been created and acquired in PyEval_InitThreads() and |
| 1423 | // thread state is created in Py_Initialize(); there |
| 1424 | // _PyGILState_NoteThreadState() also sets gilcounter to 1 (python must |
| 1425 | // have threads enabled!), so the following does both: unlock GIL and |
| 1426 | // save thread state in TLS without deleting thread state |
Bram Moolenaar | 76d711c | 2013-02-13 14:17:08 +0100 | [diff] [blame] | 1427 | PyEval_SaveThread(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1428 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1429 | py3initialised = 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | return 0; |
| 1433 | |
| 1434 | fail: |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1435 | // We call PythonIO_Flush() here to print any Python errors. |
| 1436 | // This is OK, as it is possible to call this function even |
| 1437 | // if PythonIO_Init_io() has not completed successfully (it will |
| 1438 | // not do anything in this case). |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1439 | PythonIO_Flush(); |
| 1440 | return -1; |
| 1441 | } |
| 1442 | |
| 1443 | /* |
| 1444 | * External interface |
| 1445 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1446 | static void |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 1447 | DoPyCommand(const char *cmd, |
| 1448 | dict_T* locals, |
| 1449 | rangeinitializer init_range, |
| 1450 | runner run, |
| 1451 | void *arg) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1452 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1453 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1454 | char *saved_locale; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1455 | #endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1456 | PyObject *cmdstr; |
| 1457 | PyObject *cmdbytes; |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 1458 | PyGILState_STATE pygilstate; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1459 | |
Bram Moolenaar | c4f8338 | 2017-07-07 14:50:44 +0200 | [diff] [blame] | 1460 | if (python_end_called) |
| 1461 | goto theend; |
| 1462 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1463 | if (Python3_Init()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1464 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1465 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1466 | init_range(arg); |
| 1467 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1468 | Python_Release_Vim(); // leave Vim |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1469 | |
| 1470 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1471 | // Python only works properly when the LC_NUMERIC locale is "C". |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1472 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 1473 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1474 | saved_locale = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1475 | else |
| 1476 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1477 | // Need to make a copy, value may change when setting new locale. |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1478 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 1479 | (void)setlocale(LC_NUMERIC, "C"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1480 | } |
| 1481 | #endif |
| 1482 | |
| 1483 | pygilstate = PyGILState_Ensure(); |
| 1484 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1485 | // PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause |
| 1486 | // SyntaxError (unicode error). |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 1487 | cmdstr = PyUnicode_Decode(cmd, strlen(cmd), |
Bram Moolenaar | 2e2f52a | 2020-12-21 16:03:02 +0100 | [diff] [blame] | 1488 | (char *)ENC_OPT, ERRORS_DECODE_ARG); |
| 1489 | cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", ERRORS_ENCODE_ARG); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1490 | Py_XDECREF(cmdstr); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1491 | |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 1492 | run(PyBytes_AsString(cmdbytes), locals, arg, &pygilstate); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1493 | Py_XDECREF(cmdbytes); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1494 | |
| 1495 | PyGILState_Release(pygilstate); |
| 1496 | |
| 1497 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 1498 | if (saved_locale != NULL) |
| 1499 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1500 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 1501 | vim_free(saved_locale); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1502 | } |
| 1503 | #endif |
| 1504 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1505 | Python_Lock_Vim(); // enter Vim |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1506 | PythonIO_Flush(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1507 | |
| 1508 | theend: |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1509 | return; // keeps lint happy |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 1513 | * ":py3" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1514 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1515 | void |
| 1516 | ex_py3(exarg_T *eap) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1517 | { |
| 1518 | char_u *script; |
| 1519 | |
| 1520 | script = script_get(eap, eap->arg); |
| 1521 | if (!eap->skip) |
| 1522 | { |
Bram Moolenaar | 14816ad | 2019-02-18 22:04:56 +0100 | [diff] [blame] | 1523 | if (p_pyx == 0) |
| 1524 | p_pyx = 3; |
| 1525 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1526 | DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script, |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 1527 | NULL, |
Yee Cheng Chin | 2ce070c | 2023-09-19 20:30:22 +0200 | [diff] [blame] | 1528 | init_range_cmd, |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1529 | (runner) run_cmd, |
| 1530 | (void *) eap); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1531 | } |
| 1532 | vim_free(script); |
| 1533 | } |
| 1534 | |
| 1535 | #define BUFFER_SIZE 2048 |
| 1536 | |
| 1537 | /* |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 1538 | * ":py3file" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1539 | */ |
| 1540 | void |
| 1541 | ex_py3file(exarg_T *eap) |
| 1542 | { |
| 1543 | static char buffer[BUFFER_SIZE]; |
| 1544 | const char *file; |
| 1545 | char *p; |
| 1546 | int i; |
| 1547 | |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 1548 | if (p_pyx == 0) |
| 1549 | p_pyx = 3; |
| 1550 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1551 | // Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 1552 | // stdio file pointer, but Vim and the Python DLL are compiled with |
| 1553 | // different options under Windows, meaning that stdio pointers aren't |
| 1554 | // compatible between the two. Yuk. |
| 1555 | // |
| 1556 | // construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) |
| 1557 | // |
| 1558 | // Using bytes so that Python can detect the source encoding as it normally |
| 1559 | // does. The doc does not say "compile" accept bytes, though. |
| 1560 | // |
| 1561 | // We need to escape any backslashes or single quotes in the file name, so that |
| 1562 | // Python won't mangle the file name. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1563 | |
| 1564 | strcpy(buffer, "exec(compile(open('"); |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1565 | p = buffer + 19; // size of "exec(compile(open('" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1566 | |
| 1567 | for (i=0; i<2; ++i) |
| 1568 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1569 | file = (char *)eap->arg; |
| 1570 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 1571 | { |
| 1572 | if (*file == '\\' || *file == '\'') |
| 1573 | *p++ = '\\'; |
| 1574 | *p++ = *file++; |
| 1575 | } |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1576 | // If we didn't finish the file name, we hit a buffer overflow |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1577 | if (*file != '\0') |
| 1578 | return; |
| 1579 | if (i==0) |
| 1580 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1581 | strcpy(p,"','rb').read(),'"); |
| 1582 | p += 16; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1583 | } |
| 1584 | else |
| 1585 | { |
| 1586 | strcpy(p,"','exec'))"); |
| 1587 | p += 10; |
| 1588 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1592 | // Execute the file |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1593 | DoPyCommand(buffer, |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 1594 | NULL, |
Yee Cheng Chin | 2ce070c | 2023-09-19 20:30:22 +0200 | [diff] [blame] | 1595 | init_range_cmd, |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1596 | (runner) run_cmd, |
| 1597 | (void *) eap); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Bram Moolenaar | 54e8f00 | 2013-05-15 19:44:39 +0200 | [diff] [blame] | 1600 | void |
| 1601 | ex_py3do(exarg_T *eap) |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 1602 | { |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 1603 | if (p_pyx == 0) |
| 1604 | p_pyx = 3; |
| 1605 | |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1606 | DoPyCommand((char *)eap->arg, |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 1607 | NULL, |
Yee Cheng Chin | 2ce070c | 2023-09-19 20:30:22 +0200 | [diff] [blame] | 1608 | init_range_cmd, |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 1609 | (runner)run_do, |
| 1610 | (void *)eap); |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 1611 | } |
| 1612 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1613 | /////////////////////////////////////////////////////// |
| 1614 | // 2. Python output stream: writes output via [e]msg(). |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1615 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1616 | // Implementation functions |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1617 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1618 | static PyObject * |
| 1619 | OutputGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1620 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1621 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1622 | |
| 1623 | if (strcmp(name, "softspace") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1624 | return PyLong_FromLong(((OutputObject *)(self))->softspace); |
Bram Moolenaar | 6d4431e | 2016-04-21 20:00:56 +0200 | [diff] [blame] | 1625 | else if (strcmp(name, "errors") == 0) |
| 1626 | return PyString_FromString("strict"); |
| 1627 | else if (strcmp(name, "encoding") == 0) |
| 1628 | return PyString_FromString(ENC_OPT); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1629 | |
| 1630 | return PyObject_GenericGetAttr(self, nameobj); |
| 1631 | } |
| 1632 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1633 | static int |
| 1634 | OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1635 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1636 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1637 | |
Yee Cheng Chin | 02c51b1 | 2023-09-21 16:40:12 +0200 | [diff] [blame] | 1638 | return OutputSetattr(self, name, val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1639 | } |
| 1640 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1641 | /////////////////////////////////////////////////////// |
| 1642 | // 3. Implementation of the Vim module for Python |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1643 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1644 | // Window type - Implementation functions |
| 1645 | // -------------------------------------- |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1646 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1647 | #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) |
| 1648 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1649 | // Buffer type - Implementation functions |
| 1650 | // -------------------------------------- |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1651 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1652 | #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) |
| 1653 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1654 | static PyObject* BufferSubscript(PyObject *self, PyObject *idx); |
Bram Moolenaar | 4ce5fe4 | 2020-10-21 21:01:59 +0200 | [diff] [blame] | 1655 | static int BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1656 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1657 | // Line range type - Implementation functions |
| 1658 | // -------------------------------------- |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1659 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1660 | #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) |
| 1661 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1662 | static PyObject* RangeSubscript(PyObject *self, PyObject *idx); |
Bram Moolenaar | 4ce5fe4 | 2020-10-21 21:01:59 +0200 | [diff] [blame] | 1663 | static int RangeAsItem(PyObject *, Py_ssize_t, PyObject *); |
| 1664 | static int RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1665 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1666 | // Current objects type - Implementation functions |
| 1667 | // ----------------------------------------------- |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1668 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1669 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1670 | (lenfunc) BufferLength, // sq_length, len(x) |
| 1671 | (binaryfunc) 0, // sq_concat, x+y |
| 1672 | (ssizeargfunc) 0, // sq_repeat, x*n |
| 1673 | (ssizeargfunc) BufferItem, // sq_item, x[i] |
| 1674 | 0, // was_sq_slice, x[i:j] |
| 1675 | 0, // sq_ass_item, x[i]=v |
| 1676 | 0, // sq_ass_slice, x[i:j]=v |
| 1677 | 0, // sq_contains |
| 1678 | 0, // sq_inplace_concat |
| 1679 | 0, // sq_inplace_repeat |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1680 | }; |
| 1681 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1682 | static PyMappingMethods BufferAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1683 | /* mp_length */ (lenfunc)BufferLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1684 | /* mp_subscript */ (binaryfunc)BufferSubscript, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1685 | /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1686 | }; |
| 1687 | |
| 1688 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1689 | // Buffer object |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1690 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1691 | static PyObject * |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1692 | BufferGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1693 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1694 | PyObject *r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1695 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1696 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1697 | |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1698 | if ((r = BufferAttrValid((BufferObject *)(self), name))) |
| 1699 | return r; |
| 1700 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1701 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1702 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1703 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1704 | r = BufferAttr((BufferObject *)(self), name); |
| 1705 | if (r || PyErr_Occurred()) |
| 1706 | return r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1707 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1708 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1709 | } |
| 1710 | |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1711 | static int |
| 1712 | BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 1713 | { |
| 1714 | GET_ATTR_STRING(name, nameobj); |
| 1715 | |
Yee Cheng Chin | 02c51b1 | 2023-09-21 16:40:12 +0200 | [diff] [blame] | 1716 | return BufferSetattr(self, name, val); |
Bram Moolenaar | e9ba516 | 2013-05-29 22:02:22 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1719 | ////////////////// |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1720 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1721 | static PyObject * |
| 1722 | BufferSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1723 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1724 | if (PyLong_Check(idx)) |
| 1725 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1726 | long _idx = PyLong_AsLong(idx); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1727 | return BufferItem((BufferObject *)(self), _idx); |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 1728 | } |
| 1729 | else if (PySlice_Check(idx)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1730 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1731 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1732 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 1733 | if (CheckBuffer((BufferObject *) self)) |
| 1734 | return NULL; |
| 1735 | |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 1736 | if (PySlice_GetIndicesEx((PySliceObject_T *)idx, |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 1737 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1738 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1739 | &step, &slicelen) < 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1740 | return NULL; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1741 | return BufferSlice((BufferObject *)(self), start, stop); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1742 | } |
| 1743 | else |
| 1744 | { |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 1745 | RAISE_INVALID_INDEX_TYPE(idx); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1746 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
Bram Moolenaar | 4ce5fe4 | 2020-10-21 21:01:59 +0200 | [diff] [blame] | 1750 | static int |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1751 | BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) |
| 1752 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1753 | if (PyLong_Check(idx)) |
| 1754 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1755 | long n = PyLong_AsLong(idx); |
Bram Moolenaar | ab58946 | 2020-07-06 21:03:06 +0200 | [diff] [blame] | 1756 | |
| 1757 | if (CheckBuffer((BufferObject *) self)) |
| 1758 | return -1; |
| 1759 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1760 | return RBAsItem((BufferObject *)(self), n, val, 1, |
| 1761 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1762 | NULL); |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 1763 | } |
| 1764 | else if (PySlice_Check(idx)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1765 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1766 | Py_ssize_t start, stop, step, slicelen; |
| 1767 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 1768 | if (CheckBuffer((BufferObject *) self)) |
| 1769 | return -1; |
| 1770 | |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 1771 | if (PySlice_GetIndicesEx((PySliceObject_T *)idx, |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 1772 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1773 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1774 | &step, &slicelen) < 0) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1775 | return -1; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1776 | return RBAsSlice((BufferObject *)(self), start, stop, val, 1, |
| 1777 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1778 | NULL); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1779 | } |
| 1780 | else |
| 1781 | { |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 1782 | RAISE_INVALID_INDEX_TYPE(idx); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1783 | return -1; |
| 1784 | } |
| 1785 | } |
| 1786 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1787 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1788 | (lenfunc) RangeLength, // sq_length, len(x) |
| 1789 | (binaryfunc) 0, // RangeConcat, sq_concat, x+y |
| 1790 | (ssizeargfunc) 0, // RangeRepeat, sq_repeat, x*n |
| 1791 | (ssizeargfunc) RangeItem, // sq_item, x[i] |
| 1792 | 0, // was_sq_slice, x[i:j] |
| 1793 | (ssizeobjargproc) RangeAsItem, // sq_as_item, x[i]=v |
| 1794 | 0, // sq_ass_slice, x[i:j]=v |
| 1795 | 0, // sq_contains |
| 1796 | 0, // sq_inplace_concat |
| 1797 | 0, // sq_inplace_repeat |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1798 | }; |
| 1799 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1800 | static PyMappingMethods RangeAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1801 | /* mp_length */ (lenfunc)RangeLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1802 | /* mp_subscript */ (binaryfunc)RangeSubscript, |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1803 | /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1804 | }; |
| 1805 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1806 | // Line range object - Implementation |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1807 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1808 | static PyObject * |
| 1809 | RangeGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1810 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1811 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1812 | |
| 1813 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1814 | return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1815 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1816 | return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1817 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1818 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1819 | } |
| 1820 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1821 | //////////////// |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1822 | |
Bram Moolenaar | 4ce5fe4 | 2020-10-21 21:01:59 +0200 | [diff] [blame] | 1823 | static int |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1824 | RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1825 | { |
| 1826 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1827 | ((RangeObject *)(self))->start, |
| 1828 | ((RangeObject *)(self))->end, |
| 1829 | &((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1830 | } |
| 1831 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1832 | static Py_ssize_t |
| 1833 | RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val) |
| 1834 | { |
| 1835 | return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
| 1836 | ((RangeObject *)(self))->start, |
| 1837 | ((RangeObject *)(self))->end, |
| 1838 | &((RangeObject *)(self))->end); |
| 1839 | } |
| 1840 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1841 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1842 | RangeSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1843 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1844 | if (PyLong_Check(idx)) |
| 1845 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1846 | long _idx = PyLong_AsLong(idx); |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1847 | return RangeItem((RangeObject *)(self), _idx); |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 1848 | } |
| 1849 | else if (PySlice_Check(idx)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1850 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1851 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1852 | |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 1853 | if (PySlice_GetIndicesEx((PySliceObject_T *)idx, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1854 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1855 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1856 | &step, &slicelen) < 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1857 | return NULL; |
Bram Moolenaar | d6e3918 | 2013-05-21 18:30:34 +0200 | [diff] [blame] | 1858 | return RangeSlice((RangeObject *)(self), start, stop); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1859 | } |
| 1860 | else |
| 1861 | { |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 1862 | RAISE_INVALID_INDEX_TYPE(idx); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1863 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | |
Bram Moolenaar | 4ce5fe4 | 2020-10-21 21:01:59 +0200 | [diff] [blame] | 1867 | static int |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1868 | RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val) |
| 1869 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1870 | if (PyLong_Check(idx)) |
| 1871 | { |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1872 | long n = PyLong_AsLong(idx); |
| 1873 | return RangeAsItem(self, n, val); |
Bram Moolenaar | abab0b0 | 2019-03-30 18:47:01 +0100 | [diff] [blame] | 1874 | } |
| 1875 | else if (PySlice_Check(idx)) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1876 | { |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1877 | Py_ssize_t start, stop, step, slicelen; |
| 1878 | |
Bram Moolenaar | 922a466 | 2014-03-30 16:11:43 +0200 | [diff] [blame] | 1879 | if (PySlice_GetIndicesEx((PySliceObject_T *)idx, |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1880 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1881 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1882 | &step, &slicelen) < 0) |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1883 | return -1; |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1884 | return RangeAsSlice(self, start, stop, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1885 | } |
| 1886 | else |
| 1887 | { |
Bram Moolenaar | c476e52 | 2013-06-23 13:46:40 +0200 | [diff] [blame] | 1888 | RAISE_INVALID_INDEX_TYPE(idx); |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1889 | return -1; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1893 | // TabPage object - Implementation |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1894 | |
| 1895 | static PyObject * |
| 1896 | TabPageGetattro(PyObject *self, PyObject *nameobj) |
| 1897 | { |
| 1898 | PyObject *r; |
| 1899 | |
| 1900 | GET_ATTR_STRING(name, nameobj); |
| 1901 | |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1902 | if ((r = TabPageAttrValid((TabPageObject *)(self), name))) |
| 1903 | return r; |
| 1904 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1905 | if (CheckTabPage((TabPageObject *)(self))) |
| 1906 | return NULL; |
| 1907 | |
| 1908 | r = TabPageAttr((TabPageObject *)(self), name); |
| 1909 | if (r || PyErr_Occurred()) |
| 1910 | return r; |
| 1911 | else |
| 1912 | return PyObject_GenericGetAttr(self, nameobj); |
| 1913 | } |
| 1914 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1915 | // Window object - Implementation |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1916 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1917 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1918 | WindowGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1919 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1920 | PyObject *r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1921 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1922 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1923 | |
Bram Moolenaar | 9e822c0 | 2013-05-29 22:15:30 +0200 | [diff] [blame] | 1924 | if ((r = WindowAttrValid((WindowObject *)(self), name))) |
| 1925 | return r; |
| 1926 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1927 | if (CheckWindow((WindowObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1928 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1929 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1930 | r = WindowAttr((WindowObject *)(self), name); |
| 1931 | if (r || PyErr_Occurred()) |
| 1932 | return r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1933 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1934 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1935 | } |
| 1936 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1937 | static int |
| 1938 | WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1939 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1940 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1941 | |
Yee Cheng Chin | 02c51b1 | 2023-09-21 16:40:12 +0200 | [diff] [blame] | 1942 | return WindowSetattr(self, name, val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1945 | // Tab page list object - Definitions |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1946 | |
| 1947 | static PySequenceMethods TabListAsSeq = { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1948 | (lenfunc) TabListLength, // sq_length, len(x) |
| 1949 | (binaryfunc) 0, // sq_concat, x+y |
| 1950 | (ssizeargfunc) 0, // sq_repeat, x*n |
| 1951 | (ssizeargfunc) TabListItem, // sq_item, x[i] |
| 1952 | 0, // sq_slice, x[i:j] |
| 1953 | (ssizeobjargproc)0, // sq_as_item, x[i]=v |
| 1954 | 0, // sq_ass_slice, x[i:j]=v |
| 1955 | 0, // sq_contains |
| 1956 | 0, // sq_inplace_concat |
| 1957 | 0, // sq_inplace_repeat |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1958 | }; |
| 1959 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1960 | // Window list object - Definitions |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1961 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1962 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1963 | (lenfunc) WinListLength, // sq_length, len(x) |
| 1964 | (binaryfunc) 0, // sq_concat, x+y |
| 1965 | (ssizeargfunc) 0, // sq_repeat, x*n |
| 1966 | (ssizeargfunc) WinListItem, // sq_item, x[i] |
| 1967 | 0, // sq_slice, x[i:j] |
| 1968 | (ssizeobjargproc)0, // sq_as_item, x[i]=v |
| 1969 | 0, // sq_ass_slice, x[i:j]=v |
| 1970 | 0, // sq_contains |
| 1971 | 0, // sq_inplace_concat |
| 1972 | 0, // sq_inplace_repeat |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1973 | }; |
| 1974 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1975 | /* |
| 1976 | * Current items object - Implementation |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1977 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1978 | static PyObject * |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1979 | CurrentGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1980 | { |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1981 | PyObject *r; |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1982 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | dd8aca6 | 2013-05-29 22:36:10 +0200 | [diff] [blame] | 1983 | if (!(r = CurrentGetattr(self, name))) |
| 1984 | return PyObject_GenericGetAttr(self, nameobj); |
| 1985 | return r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1988 | static int |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1989 | CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1990 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1991 | GET_ATTR_STRING(name, nameobj); |
| 1992 | return CurrentSetattr(self, name, value); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1993 | } |
| 1994 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 1995 | // Dictionary object - Definitions |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1996 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1997 | static PyObject * |
| 1998 | DictionaryGetattro(PyObject *self, PyObject *nameobj) |
| 1999 | { |
| 2000 | DictionaryObject *this = ((DictionaryObject *) (self)); |
| 2001 | |
| 2002 | GET_ATTR_STRING(name, nameobj); |
| 2003 | |
| 2004 | if (strcmp(name, "locked") == 0) |
| 2005 | return PyLong_FromLong(this->dict->dv_lock); |
| 2006 | else if (strcmp(name, "scope") == 0) |
| 2007 | return PyLong_FromLong(this->dict->dv_scope); |
| 2008 | |
| 2009 | return PyObject_GenericGetAttr(self, nameobj); |
| 2010 | } |
| 2011 | |
| 2012 | static int |
| 2013 | DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 2014 | { |
| 2015 | GET_ATTR_STRING(name, nameobj); |
Yee Cheng Chin | 02c51b1 | 2023-09-21 16:40:12 +0200 | [diff] [blame] | 2016 | return DictionarySetattr(self, name, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2017 | } |
| 2018 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2019 | // List object - Definitions |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2020 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 2021 | static PyObject * |
| 2022 | ListGetattro(PyObject *self, PyObject *nameobj) |
| 2023 | { |
| 2024 | GET_ATTR_STRING(name, nameobj); |
| 2025 | |
| 2026 | if (strcmp(name, "locked") == 0) |
| 2027 | return PyLong_FromLong(((ListObject *) (self))->list->lv_lock); |
| 2028 | |
| 2029 | return PyObject_GenericGetAttr(self, nameobj); |
| 2030 | } |
| 2031 | |
| 2032 | static int |
| 2033 | ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 2034 | { |
| 2035 | GET_ATTR_STRING(name, nameobj); |
Yee Cheng Chin | 02c51b1 | 2023-09-21 16:40:12 +0200 | [diff] [blame] | 2036 | return ListSetattr(self, name, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2037 | } |
| 2038 | |
Yegappan Lakshmanan | 038be27 | 2025-03-26 18:46:21 +0100 | [diff] [blame] | 2039 | // Tuple object - Definitions |
| 2040 | |
| 2041 | static PyObject * |
| 2042 | TupleGetattro(PyObject *self, PyObject *nameobj) |
| 2043 | { |
| 2044 | GET_ATTR_STRING(name, nameobj); |
| 2045 | |
| 2046 | if (strcmp(name, "locked") == 0) |
| 2047 | return PyLong_FromLong(((TupleObject *) (self))->tuple->tv_lock); |
| 2048 | |
| 2049 | return PyObject_GenericGetAttr(self, nameobj); |
| 2050 | } |
| 2051 | |
| 2052 | static int |
| 2053 | TupleSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 2054 | { |
| 2055 | GET_ATTR_STRING(name, nameobj); |
| 2056 | return TupleSetattr(self, name, val); |
| 2057 | } |
| 2058 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2059 | // Function object - Definitions |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2060 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2061 | static PyObject * |
| 2062 | FunctionGetattro(PyObject *self, PyObject *nameobj) |
| 2063 | { |
Bram Moolenaar | 8110a09 | 2016-04-14 15:56:09 +0200 | [diff] [blame] | 2064 | PyObject *r; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2065 | FunctionObject *this = (FunctionObject *)(self); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 2066 | |
| 2067 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2068 | |
Bram Moolenaar | 8110a09 | 2016-04-14 15:56:09 +0200 | [diff] [blame] | 2069 | r = FunctionAttr(this, name); |
| 2070 | if (r || PyErr_Occurred()) |
| 2071 | return r; |
| 2072 | else |
| 2073 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2076 | // External interface |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2077 | |
| 2078 | void |
| 2079 | python3_buffer_free(buf_T *buf) |
| 2080 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2081 | BufferObject *bp = BUF_PYTHON_REF(buf); |
| 2082 | if (bp == NULL) |
| 2083 | return; |
| 2084 | bp->buf = INVALID_BUFFER_VALUE; |
| 2085 | BUF_PYTHON_REF(buf) = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2088 | void |
| 2089 | python3_window_free(win_T *win) |
| 2090 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2091 | WindowObject *wp = WIN_PYTHON_REF(win); |
| 2092 | if (wp == NULL) |
| 2093 | return; |
| 2094 | wp->win = INVALID_WINDOW_VALUE; |
| 2095 | WIN_PYTHON_REF(win) = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2096 | } |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2097 | |
| 2098 | void |
| 2099 | python3_tabpage_free(tabpage_T *tab) |
| 2100 | { |
Yegappan Lakshmanan | 0233bdf | 2023-01-12 12:33:30 +0000 | [diff] [blame] | 2101 | TabPageObject *tp = TAB_PYTHON_REF(tab); |
| 2102 | if (tp == NULL) |
| 2103 | return; |
| 2104 | tp->tab = INVALID_TABPAGE_VALUE; |
| 2105 | TAB_PYTHON_REF(tab) = NULL; |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 2106 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2107 | |
Bram Moolenaar | 7854e3a | 2012-11-28 15:33:14 +0100 | [diff] [blame] | 2108 | static PyObject * |
| 2109 | Py3Init_vim(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2110 | { |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2111 | // The special value is removed from sys.path in Python3_Init(). |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2112 | static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; |
| 2113 | |
Bram Moolenaar | 1dc2878 | 2013-05-21 19:11:01 +0200 | [diff] [blame] | 2114 | if (init_types()) |
| 2115 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2116 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2117 | // Set sys.argv[] to avoid a crash in warn(). |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2118 | PySys_SetArgv(1, argv); |
| 2119 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 2120 | if ((vim_module = PyModule_Create(&vimmodule)) == NULL) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 2121 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2122 | |
Bram Moolenaar | dee2e31 | 2013-06-23 16:35:47 +0200 | [diff] [blame] | 2123 | if (populate_module(vim_module)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2124 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2125 | |
Bram Moolenaar | c09a6d6 | 2013-06-10 21:27:29 +0200 | [diff] [blame] | 2126 | if (init_sys_path()) |
| 2127 | return NULL; |
| 2128 | |
| 2129 | return vim_module; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2130 | } |
| 2131 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2132 | ////////////////////////////////////////////////////////////////////////// |
| 2133 | // 4. Utility functions for handling the interface between Vim and Python. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2134 | |
Bram Moolenaar | 2ab2e86 | 2019-12-04 21:24:53 +0100 | [diff] [blame] | 2135 | /* |
| 2136 | * Convert a Vim line into a Python string. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2137 | * All internal newlines are replaced by null characters. |
| 2138 | * |
| 2139 | * On errors, the Python exception data is set, and NULL is returned. |
| 2140 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 2141 | static PyObject * |
| 2142 | LineToString(const char *str) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2143 | { |
| 2144 | PyObject *result; |
| 2145 | Py_ssize_t len = strlen(str); |
Bram Moolenaar | d672dde | 2020-02-26 13:43:51 +0100 | [diff] [blame] | 2146 | char *tmp, *p; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2147 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 2148 | tmp = alloc(len + 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2149 | p = tmp; |
| 2150 | if (p == NULL) |
| 2151 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2152 | PyErr_NoMemory(); |
| 2153 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | while (*str) |
| 2157 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2158 | if (*str == '\n') |
| 2159 | *p = '\0'; |
| 2160 | else |
| 2161 | *p = *str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2162 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 2163 | ++p; |
| 2164 | ++str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2165 | } |
| 2166 | *p = '\0'; |
| 2167 | |
Bram Moolenaar | 2e2f52a | 2020-12-21 16:03:02 +0100 | [diff] [blame] | 2168 | result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, ERRORS_DECODE_ARG); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 2169 | |
| 2170 | vim_free(tmp); |
| 2171 | return result; |
| 2172 | } |
| 2173 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2174 | void |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 2175 | do_py3eval(char_u *str, dict_T *locals, typval_T *rettv) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2176 | { |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 2177 | DoPyCommand((char *) str, |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 2178 | locals, |
Yee Cheng Chin | 2ce070c | 2023-09-19 20:30:22 +0200 | [diff] [blame] | 2179 | init_range_eval, |
Bram Moolenaar | b52f4c0 | 2013-05-21 18:19:38 +0200 | [diff] [blame] | 2180 | (runner) run_eval, |
| 2181 | (void *) rettv); |
Bram Moolenaar | 8e9a24a | 2019-03-19 22:22:55 +0100 | [diff] [blame] | 2182 | if (rettv->v_type == VAR_UNKNOWN) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2183 | { |
Bram Moolenaar | 8e9a24a | 2019-03-19 22:22:55 +0100 | [diff] [blame] | 2184 | rettv->v_type = VAR_NUMBER; |
| 2185 | rettv->vval.v_number = 0; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
Bram Moolenaar | 2459a5e | 2015-02-03 12:55:18 +0100 | [diff] [blame] | 2189 | int |
Bram Moolenaar | 8e9a24a | 2019-03-19 22:22:55 +0100 | [diff] [blame] | 2190 | set_ref_in_python3(int copyID) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2191 | { |
Bram Moolenaar | b641df4 | 2015-02-03 13:16:04 +0100 | [diff] [blame] | 2192 | return set_ref_in_py(copyID); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 2193 | } |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 2194 | |
| 2195 | int |
Dominique Pellé | 4927bc7 | 2023-09-24 16:12:07 +0200 | [diff] [blame] | 2196 | python3_version(void) |
Yee Cheng Chin | c13b3d1 | 2023-08-20 21:18:38 +0200 | [diff] [blame] | 2197 | { |
| 2198 | #ifdef USE_LIMITED_API |
| 2199 | return Py_LIMITED_API; |
| 2200 | #else |
| 2201 | return PY_VERSION_HEX; |
| 2202 | #endif |
| 2203 | } |