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