Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | 0c1f3f4 | 2011-02-25 15:18:50 +0100 | [diff] [blame] | 25 | /* uncomment this if used with the debug version of python */ |
| 26 | /* #define Py_DEBUG */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 27 | |
| 28 | #include "vim.h" |
| 29 | |
| 30 | #include <limits.h> |
| 31 | |
| 32 | /* Python.h defines _POSIX_THREADS itself (if needed) */ |
| 33 | #ifdef _POSIX_THREADS |
| 34 | # undef _POSIX_THREADS |
| 35 | #endif |
| 36 | |
Bram Moolenaar | d68554d | 2010-07-25 13:43:20 +0200 | [diff] [blame] | 37 | #if defined(_WIN32) && defined(HAVE_FCNTL_H) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 38 | # undef HAVE_FCNTL_H |
| 39 | #endif |
| 40 | |
| 41 | #ifdef _DEBUG |
| 42 | # undef _DEBUG |
| 43 | #endif |
| 44 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 45 | #ifdef F_BLANK |
| 46 | # undef F_BLANK |
| 47 | #endif |
| 48 | |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 49 | #ifdef HAVE_STDARG_H |
| 50 | # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ |
| 51 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 52 | #ifdef _POSIX_C_SOURCE /* defined in feature.h */ |
| 53 | # undef _POSIX_C_SOURCE |
| 54 | #endif |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 55 | #ifdef _XOPEN_SOURCE |
| 56 | # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ |
| 57 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 58 | |
| 59 | #include <Python.h> |
| 60 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 61 | # include "macglue.h" |
| 62 | # include <CodeFragments.h> |
| 63 | #endif |
| 64 | #undef main /* Defined in python.h - aargh */ |
| 65 | #undef HAVE_FCNTL_H /* Clash with os_win32.h */ |
| 66 | |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 67 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
| 68 | # define PY_SSIZE_T_CLEAN |
| 69 | #endif |
| 70 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 71 | static void init_structs(void); |
| 72 | |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 73 | /* The "surrogateescape" error handler is new in Python 3.1 */ |
| 74 | #if PY_VERSION_HEX >= 0x030100f0 |
| 75 | # define CODEC_ERROR_HANDLER "surrogateescape" |
| 76 | #else |
| 77 | # define CODEC_ERROR_HANDLER NULL |
| 78 | #endif |
| 79 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 80 | /* Python 3 does not support CObjects, always use Capsules */ |
| 81 | #define PY_USE_CAPSULE |
| 82 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 83 | #define PyInt Py_ssize_t |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 84 | #define PyString_Check(obj) PyUnicode_Check(obj) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 85 | #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 86 | #define PyString_FreeBytes(obj) Py_XDECREF(bytes) |
| 87 | #define PyString_AsString(obj) PyBytes_AsString(obj) |
| 88 | #define PyString_Size(obj) PyBytes_GET_SIZE(bytes) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 89 | #define PyString_FromString(repr) PyUnicode_FromString(repr) |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 90 | #define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len) |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 91 | #define PyInt_Check(obj) PyLong_Check(obj) |
| 92 | #define PyInt_FromLong(i) PyLong_FromLong(i) |
| 93 | #define PyInt_AsLong(obj) PyLong_AsLong(obj) |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 94 | #define Py_ssize_t_fmt "n" |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 95 | |
Bram Moolenaar | 0c1f3f4 | 2011-02-25 15:18:50 +0100 | [diff] [blame] | 96 | #if defined(DYNAMIC_PYTHON3) || defined(PROTO) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 97 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 98 | # ifndef WIN3264 |
| 99 | # include <dlfcn.h> |
| 100 | # define FARPROC void* |
| 101 | # define HINSTANCE void* |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 102 | # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 103 | # define load_dll(n) dlopen((n), RTLD_LAZY) |
| 104 | # else |
| 105 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 106 | # endif |
| 107 | # define close_dll dlclose |
| 108 | # define symbol_from_dll dlsym |
| 109 | # else |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 110 | # define load_dll vimLoadLib |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 111 | # define close_dll FreeLibrary |
| 112 | # define symbol_from_dll GetProcAddress |
| 113 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 114 | /* |
| 115 | * Wrapper defines |
| 116 | */ |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 117 | # undef PyArg_Parse |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 118 | # define PyArg_Parse py3_PyArg_Parse |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 119 | # undef PyArg_ParseTuple |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 120 | # define PyArg_ParseTuple py3_PyArg_ParseTuple |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 121 | # define PyMem_Free py3_PyMem_Free |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 122 | # define PyMem_Malloc py3_PyMem_Malloc |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 123 | # define PyDict_SetItemString py3_PyDict_SetItemString |
| 124 | # define PyErr_BadArgument py3_PyErr_BadArgument |
| 125 | # define PyErr_Clear py3_PyErr_Clear |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 126 | # define PyErr_PrintEx py3_PyErr_PrintEx |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 127 | # define PyErr_NoMemory py3_PyErr_NoMemory |
| 128 | # define PyErr_Occurred py3_PyErr_Occurred |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 129 | # define PyErr_PrintEx py3_PyErr_PrintEx |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 130 | # define PyErr_SetNone py3_PyErr_SetNone |
| 131 | # define PyErr_SetString py3_PyErr_SetString |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 132 | # define PyErr_SetObject py3_PyErr_SetObject |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 133 | # define PyEval_InitThreads py3_PyEval_InitThreads |
| 134 | # define PyEval_RestoreThread py3_PyEval_RestoreThread |
| 135 | # define PyEval_SaveThread py3_PyEval_SaveThread |
| 136 | # define PyGILState_Ensure py3_PyGILState_Ensure |
| 137 | # define PyGILState_Release py3_PyGILState_Release |
| 138 | # define PyLong_AsLong py3_PyLong_AsLong |
| 139 | # define PyLong_FromLong py3_PyLong_FromLong |
| 140 | # define PyList_GetItem py3_PyList_GetItem |
| 141 | # define PyList_Append py3_PyList_Append |
| 142 | # define PyList_New py3_PyList_New |
| 143 | # define PyList_SetItem py3_PyList_SetItem |
| 144 | # define PyList_Size py3_PyList_Size |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 145 | # define PySequence_Check py3_PySequence_Check |
| 146 | # define PySequence_Size py3_PySequence_Size |
| 147 | # define PySequence_GetItem py3_PySequence_GetItem |
| 148 | # define PyTuple_Size py3_PyTuple_Size |
| 149 | # define PyTuple_GetItem py3_PyTuple_GetItem |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 150 | # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx |
| 151 | # define PyImport_ImportModule py3_PyImport_ImportModule |
| 152 | # define PyObject_Init py3__PyObject_Init |
| 153 | # define PyDict_New py3_PyDict_New |
| 154 | # define PyDict_GetItemString py3_PyDict_GetItemString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 155 | # define PyDict_Next py3_PyDict_Next |
| 156 | # define PyMapping_Check py3_PyMapping_Check |
| 157 | # define PyMapping_Items py3_PyMapping_Items |
| 158 | # define PyIter_Next py3_PyIter_Next |
| 159 | # define PyObject_GetIter py3_PyObject_GetIter |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 160 | # define PyObject_IsTrue py3_PyObject_IsTrue |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 161 | # define PyModule_GetDict py3_PyModule_GetDict |
| 162 | #undef PyRun_SimpleString |
| 163 | # define PyRun_SimpleString py3_PyRun_SimpleString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 164 | #undef PyRun_String |
| 165 | # define PyRun_String py3_PyRun_String |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 166 | # define PyObject_GetAttrString py3_PyObject_GetAttrString |
| 167 | # define PyObject_SetAttrString py3_PyObject_SetAttrString |
| 168 | # define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs |
| 169 | # define PyEval_GetLocals py3_PyEval_GetLocals |
| 170 | # define PyEval_GetGlobals py3_PyEval_GetGlobals |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 171 | # define PySys_SetObject py3_PySys_SetObject |
| 172 | # define PySys_SetArgv py3_PySys_SetArgv |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 173 | # define PyType_Ready py3_PyType_Ready |
| 174 | #undef Py_BuildValue |
| 175 | # define Py_BuildValue py3_Py_BuildValue |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 176 | # define Py_SetPythonHome py3_Py_SetPythonHome |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 177 | # define Py_Initialize py3_Py_Initialize |
| 178 | # define Py_Finalize py3_Py_Finalize |
| 179 | # define Py_IsInitialized py3_Py_IsInitialized |
| 180 | # define _Py_NoneStruct (*py3__Py_NoneStruct) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 181 | # define _Py_FalseStruct (*py3__Py_FalseStruct) |
| 182 | # define _Py_TrueStruct (*py3__Py_TrueStruct) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 183 | # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 184 | # define PyModule_AddObject py3_PyModule_AddObject |
| 185 | # define PyImport_AppendInittab py3_PyImport_AppendInittab |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 186 | # define PyImport_AddModule py3_PyImport_AddModule |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 187 | # if PY_VERSION_HEX >= 0x030300f0 |
| 188 | # undef _PyUnicode_AsString |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 189 | # define _PyUnicode_AsString py3_PyUnicode_AsUTF8 |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 190 | # else |
| 191 | # define _PyUnicode_AsString py3__PyUnicode_AsString |
| 192 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 193 | # undef PyUnicode_AsEncodedString |
| 194 | # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString |
| 195 | # undef PyBytes_AsString |
| 196 | # define PyBytes_AsString py3_PyBytes_AsString |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 197 | # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 198 | # undef PyBytes_FromString |
| 199 | # define PyBytes_FromString py3_PyBytes_FromString |
| 200 | # define PyFloat_FromDouble py3_PyFloat_FromDouble |
| 201 | # define PyFloat_AsDouble py3_PyFloat_AsDouble |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 202 | # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 203 | # define PyType_Type (*py3_PyType_Type) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 204 | # define PySlice_Type (*py3_PySlice_Type) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 205 | # define PyFloat_Type (*py3_PyFloat_Type) |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 206 | # define PyBool_Type (*py3_PyBool_Type) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 207 | # define PyErr_NewException py3_PyErr_NewException |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 208 | # ifdef Py_DEBUG |
| 209 | # define _Py_NegativeRefcount py3__Py_NegativeRefcount |
| 210 | # define _Py_RefTotal (*py3__Py_RefTotal) |
| 211 | # define _Py_Dealloc py3__Py_Dealloc |
| 212 | # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc |
| 213 | # define _PyObject_DebugFree py3__PyObject_DebugFree |
| 214 | # else |
| 215 | # define PyObject_Malloc py3_PyObject_Malloc |
| 216 | # define PyObject_Free py3_PyObject_Free |
| 217 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 218 | # define PyType_GenericAlloc py3_PyType_GenericAlloc |
| 219 | # define PyType_GenericNew py3_PyType_GenericNew |
| 220 | # define PyModule_Create2 py3_PyModule_Create2 |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 221 | # undef PyUnicode_FromString |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 222 | # define PyUnicode_FromString py3_PyUnicode_FromString |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 223 | # undef PyUnicode_Decode |
| 224 | # define PyUnicode_Decode py3_PyUnicode_Decode |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 225 | # define PyType_IsSubtype py3_PyType_IsSubtype |
| 226 | # define PyCapsule_New py3_PyCapsule_New |
| 227 | # define PyCapsule_GetPointer py3_PyCapsule_GetPointer |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 228 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 229 | # ifdef Py_DEBUG |
| 230 | # undef PyObject_NEW |
| 231 | # define PyObject_NEW(type, typeobj) \ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 232 | ( (type *) PyObject_Init( \ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 233 | (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 234 | # endif |
| 235 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 236 | /* |
| 237 | * Pointers for dynamic link |
| 238 | */ |
| 239 | static int (*py3_PySys_SetArgv)(int, wchar_t **); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 240 | static void (*py3_Py_SetPythonHome)(wchar_t *home); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 241 | static void (*py3_Py_Initialize)(void); |
| 242 | static PyObject* (*py3_PyList_New)(Py_ssize_t size); |
| 243 | static PyGILState_STATE (*py3_PyGILState_Ensure)(void); |
| 244 | static void (*py3_PyGILState_Release)(PyGILState_STATE); |
| 245 | static int (*py3_PySys_SetObject)(char *, PyObject *); |
| 246 | static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *); |
| 247 | static Py_ssize_t (*py3_PyList_Size)(PyObject *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 248 | static int (*py3_PySequence_Check)(PyObject *); |
| 249 | static Py_ssize_t (*py3_PySequence_Size)(PyObject *); |
| 250 | static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t); |
| 251 | static Py_ssize_t (*py3_PyTuple_Size)(PyObject *); |
| 252 | static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t); |
| 253 | static int (*py3_PyMapping_Check)(PyObject *); |
| 254 | static PyObject* (*py3_PyMapping_Items)(PyObject *); |
Bram Moolenaar | 314ed4b | 2011-09-14 18:59:39 +0200 | [diff] [blame] | 255 | static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 256 | Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 257 | static PyObject* (*py3_PyErr_NoMemory)(void); |
| 258 | static void (*py3_Py_Finalize)(void); |
| 259 | static void (*py3_PyErr_SetString)(PyObject *, const char *); |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 260 | static void (*py3_PyErr_SetObject)(PyObject *, PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 261 | static int (*py3_PyRun_SimpleString)(char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 262 | static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *); |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 263 | static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *); |
| 264 | static PyObject* (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *); |
| 265 | static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...); |
| 266 | static PyObject* (*py3_PyEval_GetGlobals)(); |
| 267 | static PyObject* (*py3_PyEval_GetLocals)(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 268 | static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); |
| 269 | static PyObject* (*py3_PyImport_ImportModule)(const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 270 | static PyObject* (*py3_PyImport_AddModule)(const char *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 271 | static int (*py3_PyErr_BadArgument)(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 272 | static PyObject* (*py3_PyErr_Occurred)(void); |
| 273 | static PyObject* (*py3_PyModule_GetDict)(PyObject *); |
| 274 | static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); |
| 275 | static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 276 | static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 277 | static PyObject* (*py3_PyLong_FromLong)(long); |
| 278 | static PyObject* (*py3_PyDict_New)(void); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 279 | static PyObject* (*py3_PyIter_Next)(PyObject *); |
| 280 | static PyObject* (*py3_PyObject_GetIter)(PyObject *); |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 281 | static int (*py3_PyObject_IsTrue)(PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 282 | static PyObject* (*py3_Py_BuildValue)(char *, ...); |
| 283 | static int (*py3_PyType_Ready)(PyTypeObject *type); |
| 284 | static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 285 | static PyObject* (*py3_PyUnicode_FromString)(const char *u); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 286 | static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size, |
| 287 | const char *encoding, const char *errors); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 288 | static long (*py3_PyLong_AsLong)(PyObject *); |
| 289 | static void (*py3_PyErr_SetNone)(PyObject *); |
| 290 | static void (*py3_PyEval_InitThreads)(void); |
| 291 | static void(*py3_PyEval_RestoreThread)(PyThreadState *); |
| 292 | static PyThreadState*(*py3_PyEval_SaveThread)(void); |
| 293 | static int (*py3_PyArg_Parse)(PyObject *, char *, ...); |
| 294 | static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 295 | static int (*py3_PyMem_Free)(void *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 296 | static void* (*py3_PyMem_Malloc)(size_t); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 297 | static int (*py3_Py_IsInitialized)(void); |
| 298 | static void (*py3_PyErr_Clear)(void); |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 299 | static void (*py3_PyErr_PrintEx)(int); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 300 | static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 301 | static iternextfunc py3__PyObject_NextNotImplemented; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 302 | static PyObject* py3__Py_NoneStruct; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 303 | static PyObject* py3__Py_FalseStruct; |
| 304 | static PyObject* py3__Py_TrueStruct; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 305 | static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); |
| 306 | static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 307 | # if PY_VERSION_HEX >= 0x030300f0 |
| 308 | static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode); |
| 309 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 310 | static char* (*py3__PyUnicode_AsString)(PyObject *unicode); |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 311 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 312 | static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); |
| 313 | static char* (*py3_PyBytes_AsString)(PyObject *bytes); |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 314 | static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 315 | static PyObject* (*py3_PyBytes_FromString)(char *str); |
| 316 | static PyObject* (*py3_PyFloat_FromDouble)(double num); |
| 317 | static double (*py3_PyFloat_AsDouble)(PyObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 318 | static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); |
| 319 | static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); |
| 320 | static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); |
| 321 | static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 322 | static PyTypeObject* py3_PyType_Type; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 323 | static PyTypeObject* py3_PySlice_Type; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 324 | static PyTypeObject* py3_PyFloat_Type; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 325 | static PyTypeObject* py3_PyBool_Type; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 326 | static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 327 | static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor); |
| 328 | static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 329 | # ifdef Py_DEBUG |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 330 | static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); |
| 331 | static Py_ssize_t* py3__Py_RefTotal; |
| 332 | static void (*py3__Py_Dealloc)(PyObject *obj); |
| 333 | static void (*py3__PyObject_DebugFree)(void*); |
| 334 | static void* (*py3__PyObject_DebugMalloc)(size_t); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 335 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 336 | static void (*py3_PyObject_Free)(void*); |
| 337 | static void* (*py3_PyObject_Malloc)(size_t); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 338 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 339 | static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 340 | |
| 341 | static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ |
| 342 | |
| 343 | /* Imported exception objects */ |
| 344 | static PyObject *p3imp_PyExc_AttributeError; |
| 345 | static PyObject *p3imp_PyExc_IndexError; |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 346 | static PyObject *p3imp_PyExc_KeyError; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 347 | static PyObject *p3imp_PyExc_KeyboardInterrupt; |
| 348 | static PyObject *p3imp_PyExc_TypeError; |
| 349 | static PyObject *p3imp_PyExc_ValueError; |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 350 | static PyObject *p3imp_PyExc_RuntimeError; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 351 | |
| 352 | # define PyExc_AttributeError p3imp_PyExc_AttributeError |
| 353 | # define PyExc_IndexError p3imp_PyExc_IndexError |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 354 | # define PyExc_KeyError p3imp_PyExc_KeyError |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 355 | # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt |
| 356 | # define PyExc_TypeError p3imp_PyExc_TypeError |
| 357 | # define PyExc_ValueError p3imp_PyExc_ValueError |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 358 | # define PyExc_RuntimeError p3imp_PyExc_RuntimeError |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * Table of name to function pointer of python. |
| 362 | */ |
| 363 | # define PYTHON_PROC FARPROC |
| 364 | static struct |
| 365 | { |
| 366 | char *name; |
| 367 | PYTHON_PROC *ptr; |
| 368 | } py3_funcname_table[] = |
| 369 | { |
| 370 | {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 371 | {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 372 | {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 373 | # ifndef PY_SSIZE_T_CLEAN |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 374 | {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 375 | {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue}, |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 376 | # else |
Bram Moolenaar | e8cdcef | 2012-09-12 20:21:43 +0200 | [diff] [blame] | 377 | {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
| 378 | {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue}, |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 379 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 380 | {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 381 | {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 382 | {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, |
| 383 | {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, |
| 384 | {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, |
| 385 | {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, |
| 386 | {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, |
| 387 | {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 388 | {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check}, |
| 389 | {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size}, |
| 390 | {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem}, |
| 391 | {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size}, |
| 392 | {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 393 | {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, |
| 394 | {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, |
| 395 | {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, |
| 396 | {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, |
Bram Moolenaar | 4d188da | 2013-05-15 15:35:09 +0200 | [diff] [blame] | 397 | {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 398 | {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 399 | {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String}, |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 400 | {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString}, |
| 401 | {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString}, |
| 402 | {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs}, |
| 403 | {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals}, |
| 404 | {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 405 | {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, |
| 406 | {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 407 | {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 408 | {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 409 | {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, |
| 410 | {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, |
| 411 | {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, |
| 412 | {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 413 | {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next}, |
| 414 | {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check}, |
| 415 | {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items}, |
| 416 | {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next}, |
| 417 | {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter}, |
Bram Moolenaar | 03db85b | 2013-05-15 14:51:35 +0200 | [diff] [blame] | 418 | {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 419 | {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, |
| 420 | {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 421 | {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, |
| 422 | {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, |
| 423 | {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, |
| 424 | {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, |
| 425 | {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, |
| 426 | {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, |
| 427 | {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, |
| 428 | {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 429 | {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 430 | {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 431 | {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 432 | {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct}, |
| 433 | {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 434 | {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 435 | {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 436 | {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, |
| 437 | {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, |
| 438 | {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 439 | # if PY_VERSION_HEX >= 0x030300f0 |
| 440 | {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8}, |
| 441 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 442 | {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 443 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 444 | {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString}, |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 445 | {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 446 | {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString}, |
| 447 | {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble}, |
| 448 | {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 449 | {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, |
| 450 | {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, |
| 451 | {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, |
| 452 | {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 453 | {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 454 | {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 455 | {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type}, |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 456 | {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 457 | {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 458 | # ifdef Py_DEBUG |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 459 | {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, |
| 460 | {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, |
| 461 | {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, |
| 462 | {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, |
| 463 | {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 464 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 465 | {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, |
| 466 | {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 467 | # endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 468 | {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype}, |
| 469 | {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New}, |
| 470 | {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 471 | {"", NULL}, |
| 472 | }; |
| 473 | |
| 474 | /* |
| 475 | * Free python.dll |
| 476 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 477 | static void |
| 478 | end_dynamic_python3(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 479 | { |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 480 | if (hinstPy3 != 0) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 481 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 482 | close_dll(hinstPy3); |
| 483 | hinstPy3 = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
| 487 | /* |
| 488 | * Load library and get all pointers. |
| 489 | * Parameter 'libname' provides name of DLL. |
| 490 | * Return OK or FAIL. |
| 491 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 492 | static int |
| 493 | py3_runtime_link_init(char *libname, int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 494 | { |
| 495 | int i; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 496 | void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 497 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 498 | # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON) |
Bram Moolenaar | b744b2f | 2010-08-13 16:22:57 +0200 | [diff] [blame] | 499 | /* Can't have Python and Python3 loaded at the same time. |
| 500 | * It cause a crash, because RTLD_GLOBAL is needed for |
| 501 | * standard C extension libraries of one or both python versions. */ |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 502 | if (python_loaded()) |
| 503 | { |
Bram Moolenaar | 9dc93ae | 2011-08-28 16:00:19 +0200 | [diff] [blame] | 504 | if (verbose) |
| 505 | EMSG(_("E837: This Vim cannot execute :py3 after using :python")); |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 506 | return FAIL; |
| 507 | } |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 508 | # endif |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 509 | |
| 510 | if (hinstPy3 != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 511 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 512 | hinstPy3 = load_dll(libname); |
| 513 | |
| 514 | if (!hinstPy3) |
| 515 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 516 | if (verbose) |
| 517 | EMSG2(_(e_loadlib), libname); |
| 518 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | for (i = 0; py3_funcname_table[i].ptr; ++i) |
| 522 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 523 | if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, |
| 524 | py3_funcname_table[i].name)) == NULL) |
| 525 | { |
| 526 | close_dll(hinstPy3); |
| 527 | hinstPy3 = 0; |
| 528 | if (verbose) |
| 529 | EMSG2(_(e_loadfunc), py3_funcname_table[i].name); |
| 530 | return FAIL; |
| 531 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 534 | /* Load unicode functions separately as only the ucs2 or the ucs4 functions |
| 535 | * will be present in the library. */ |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 536 | # if PY_VERSION_HEX >= 0x030300f0 |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 537 | ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString"); |
| 538 | ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode"); |
| 539 | ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
| 540 | "PyUnicode_AsEncodedString"); |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 541 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 542 | ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 543 | ucs_decode = symbol_from_dll(hinstPy3, |
| 544 | "PyUnicodeUCS2_Decode"); |
| 545 | ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
| 546 | "PyUnicodeUCS2_AsEncodedString"); |
| 547 | if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 548 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 549 | ucs_from_string = symbol_from_dll(hinstPy3, |
| 550 | "PyUnicodeUCS4_FromString"); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 551 | ucs_decode = symbol_from_dll(hinstPy3, |
| 552 | "PyUnicodeUCS4_Decode"); |
| 553 | ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
| 554 | "PyUnicodeUCS4_AsEncodedString"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 555 | } |
Bram Moolenaar | 9c9cbf1 | 2012-10-23 05:17:37 +0200 | [diff] [blame] | 556 | # endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 557 | if (ucs_from_string && ucs_decode && ucs_as_encoded_string) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 558 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 559 | py3_PyUnicode_FromString = ucs_from_string; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 560 | py3_PyUnicode_Decode = ucs_decode; |
| 561 | py3_PyUnicode_AsEncodedString = ucs_as_encoded_string; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 562 | } |
| 563 | else |
| 564 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 565 | close_dll(hinstPy3); |
| 566 | hinstPy3 = 0; |
| 567 | if (verbose) |
| 568 | EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); |
| 569 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return OK; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * If python is enabled (there is installed python on Windows system) return |
| 577 | * TRUE, else FALSE. |
| 578 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 579 | int |
| 580 | python3_enabled(int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 581 | { |
| 582 | return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; |
| 583 | } |
| 584 | |
| 585 | /* Load the standard Python exceptions - don't import the symbols from the |
| 586 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 587 | */ |
| 588 | static void get_py3_exceptions __ARGS((void)); |
| 589 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 590 | static void |
| 591 | get_py3_exceptions() |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 592 | { |
| 593 | PyObject *exmod = PyImport_ImportModule("builtins"); |
| 594 | PyObject *exdict = PyModule_GetDict(exmod); |
| 595 | p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 596 | p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 597 | p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 598 | p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 599 | p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 600 | p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 601 | p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 602 | Py_XINCREF(p3imp_PyExc_AttributeError); |
| 603 | Py_XINCREF(p3imp_PyExc_IndexError); |
Bram Moolenaar | af6abb9 | 2013-04-24 13:04:26 +0200 | [diff] [blame] | 604 | Py_XINCREF(p3imp_PyExc_KeyError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 605 | Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); |
| 606 | Py_XINCREF(p3imp_PyExc_TypeError); |
| 607 | Py_XINCREF(p3imp_PyExc_ValueError); |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 608 | Py_XINCREF(p3imp_PyExc_RuntimeError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 609 | Py_XDECREF(exmod); |
| 610 | } |
| 611 | #endif /* DYNAMIC_PYTHON3 */ |
| 612 | |
Bram Moolenaar | 7f85d29 | 2012-02-04 20:17:26 +0100 | [diff] [blame] | 613 | static PyObject *BufferDir(PyObject *, PyObject *); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 614 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 615 | static int py3initialised = 0; |
| 616 | |
| 617 | #define PYINITIALISED py3initialised |
| 618 | |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 619 | #define DICTKEY_DECL PyObject *bytes = NULL; |
| 620 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 621 | #define DICTKEY_GET(err) \ |
| 622 | if (PyBytes_Check(keyObject)) \ |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 623 | { \ |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 624 | if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \ |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 625 | return err; \ |
| 626 | } \ |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 627 | else if (PyUnicode_Check(keyObject)) \ |
| 628 | { \ |
| 629 | bytes = PyString_AsBytes(keyObject); \ |
| 630 | if (bytes == NULL) \ |
| 631 | return err; \ |
Bram Moolenaar | afa6b9a | 2012-09-05 19:09:11 +0200 | [diff] [blame] | 632 | if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \ |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 633 | return err; \ |
| 634 | } \ |
| 635 | else \ |
| 636 | { \ |
| 637 | PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \ |
| 638 | return err; \ |
| 639 | } |
Bram Moolenaar | cdab905 | 2012-09-05 19:03:56 +0200 | [diff] [blame] | 640 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 641 | #define DICTKEY_UNREF \ |
| 642 | if (bytes != NULL) \ |
| 643 | Py_XDECREF(bytes); |
| 644 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 645 | #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 646 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 647 | #define WIN_PYTHON_REF(win) win->w_python3_ref |
| 648 | #define BUF_PYTHON_REF(buf) buf->b_python3_ref |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 649 | #define TAB_PYTHON_REF(tab) tab->tp_python3_ref |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 650 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 651 | static void |
| 652 | call_PyObject_Free(void *p) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 653 | { |
| 654 | #ifdef Py_DEBUG |
| 655 | _PyObject_DebugFree(p); |
| 656 | #else |
| 657 | PyObject_Free(p); |
| 658 | #endif |
| 659 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 660 | |
| 661 | static PyObject * |
| 662 | call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 663 | { |
| 664 | return PyType_GenericNew(type,args,kwds); |
| 665 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 666 | |
| 667 | static PyObject * |
| 668 | call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 669 | { |
| 670 | return PyType_GenericAlloc(type,nitems); |
| 671 | } |
| 672 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 673 | static PyObject *OutputGetattro(PyObject *, PyObject *); |
| 674 | static int OutputSetattro(PyObject *, PyObject *, PyObject *); |
| 675 | static PyObject *BufferGetattro(PyObject *, PyObject *); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 676 | static PyObject *TabPageGetattro(PyObject *, PyObject *); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 677 | static PyObject *WindowGetattro(PyObject *, PyObject *); |
| 678 | static int WindowSetattro(PyObject *, PyObject *, PyObject *); |
| 679 | static PyObject *RangeGetattro(PyObject *, PyObject *); |
| 680 | static PyObject *CurrentGetattro(PyObject *, PyObject *); |
| 681 | static int CurrentSetattro(PyObject *, PyObject *, PyObject *); |
| 682 | static PyObject *DictionaryGetattro(PyObject *, PyObject *); |
| 683 | static int DictionarySetattro(PyObject *, PyObject *, PyObject *); |
| 684 | static PyObject *ListGetattro(PyObject *, PyObject *); |
| 685 | static int ListSetattro(PyObject *, PyObject *, PyObject *); |
| 686 | static PyObject *FunctionGetattro(PyObject *, PyObject *); |
| 687 | |
| 688 | static struct PyModuleDef vimmodule; |
| 689 | |
| 690 | /* |
| 691 | * Include the code shared with if_python.c |
| 692 | */ |
| 693 | #include "if_py_both.h" |
| 694 | |
| 695 | #define GET_ATTR_STRING(name, nameobj) \ |
| 696 | char *name = ""; \ |
| 697 | if (PyUnicode_Check(nameobj)) \ |
| 698 | name = _PyUnicode_AsString(nameobj) |
| 699 | |
| 700 | #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0) |
| 701 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 702 | /****************************************************** |
| 703 | * Internal function prototypes. |
| 704 | */ |
| 705 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 706 | static PyObject *globals; |
| 707 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 708 | static int PythonIO_Init(void); |
Bram Moolenaar | 7854e3a | 2012-11-28 15:33:14 +0100 | [diff] [blame] | 709 | static PyObject *Py3Init_vim(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 710 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 711 | /****************************************************** |
| 712 | * 1. Python interpreter main program. |
| 713 | */ |
| 714 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 715 | void |
| 716 | python3_end() |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 717 | { |
| 718 | static int recurse = 0; |
| 719 | |
| 720 | /* If a crash occurs while doing this, don't try again. */ |
| 721 | if (recurse != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 722 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 723 | |
| 724 | ++recurse; |
| 725 | |
| 726 | #ifdef DYNAMIC_PYTHON3 |
| 727 | if (hinstPy3) |
| 728 | #endif |
| 729 | if (Py_IsInitialized()) |
| 730 | { |
| 731 | // acquire lock before finalizing |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 732 | PyGILState_Ensure(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 733 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 734 | Py_Finalize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | #ifdef DYNAMIC_PYTHON3 |
| 738 | end_dynamic_python3(); |
| 739 | #endif |
| 740 | |
| 741 | --recurse; |
| 742 | } |
| 743 | |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 744 | #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO) |
| 745 | int |
| 746 | python3_loaded() |
| 747 | { |
| 748 | return (hinstPy3 != 0); |
| 749 | } |
| 750 | #endif |
| 751 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 752 | static int |
| 753 | Python3_Init(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 754 | { |
| 755 | if (!py3initialised) |
| 756 | { |
| 757 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 758 | if (!python3_enabled(TRUE)) |
| 759 | { |
| 760 | EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); |
| 761 | goto fail; |
| 762 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 763 | #endif |
| 764 | |
| 765 | init_structs(); |
| 766 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 767 | |
| 768 | #ifdef PYTHON3_HOME |
| 769 | Py_SetPythonHome(PYTHON3_HOME); |
| 770 | #endif |
| 771 | |
Bram Moolenaar | 7bc4f93 | 2012-10-14 03:22:56 +0200 | [diff] [blame] | 772 | PyImport_AppendInittab("vim", Py3Init_vim); |
| 773 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 774 | #if !defined(MACOS) || defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 775 | Py_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 776 | #else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 777 | PyMac_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 778 | #endif |
Bram Moolenaar | 76d711c | 2013-02-13 14:17:08 +0100 | [diff] [blame] | 779 | /* Initialise threads, and below save the state using |
| 780 | * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread |
| 781 | * specific state (such as the system trace hook), will be lost |
| 782 | * between invocations of Python code. */ |
Bram Moolenaar | 456f2bb | 2011-06-12 21:37:13 +0200 | [diff] [blame] | 783 | PyEval_InitThreads(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 784 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 785 | get_py3_exceptions(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 786 | #endif |
| 787 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 788 | if (PythonIO_Init()) |
| 789 | goto fail; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 790 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 791 | globals = PyModule_GetDict(PyImport_AddModule("__main__")); |
| 792 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 793 | /* Remove the element from sys.path that was added because of our |
| 794 | * argv[0] value in Py3Init_vim(). Previously we used an empty |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 795 | * string, but depending on the OS we then get an empty entry or |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 796 | * the current directory in sys.path. |
| 797 | * Only after vim has been imported, the element does exist in |
| 798 | * sys.path. |
| 799 | */ |
| 800 | 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] | 801 | |
Bram Moolenaar | 76d711c | 2013-02-13 14:17:08 +0100 | [diff] [blame] | 802 | /* lock is created and acquired in PyEval_InitThreads() and thread |
| 803 | * state is created in Py_Initialize() |
| 804 | * there _PyGILState_NoteThreadState() also sets gilcounter to 1 |
| 805 | * (python must have threads enabled!) |
| 806 | * so the following does both: unlock GIL and save thread state in TLS |
| 807 | * without deleting thread state |
| 808 | */ |
| 809 | PyEval_SaveThread(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 810 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 811 | py3initialised = 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return 0; |
| 815 | |
| 816 | fail: |
| 817 | /* We call PythonIO_Flush() here to print any Python errors. |
| 818 | * This is OK, as it is possible to call this function even |
| 819 | * if PythonIO_Init() has not completed successfully (it will |
| 820 | * not do anything in this case). |
| 821 | */ |
| 822 | PythonIO_Flush(); |
| 823 | return -1; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * External interface |
| 828 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 829 | static void |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 830 | DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 831 | { |
| 832 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 833 | GrafPtr oldPort; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 834 | #endif |
| 835 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 836 | char *saved_locale; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 837 | #endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 838 | PyObject *cmdstr; |
| 839 | PyObject *cmdbytes; |
Bram Moolenaar | 71700b8 | 2013-05-15 17:49:05 +0200 | [diff] [blame] | 840 | PyGILState_STATE pygilstate; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 841 | |
| 842 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 843 | GetPort(&oldPort); |
| 844 | /* Check if the Python library is available */ |
| 845 | if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 846 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 847 | #endif |
| 848 | if (Python3_Init()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 849 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 850 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 851 | if (rettv == NULL) |
| 852 | { |
| 853 | RangeStart = eap->line1; |
| 854 | RangeEnd = eap->line2; |
| 855 | } |
| 856 | else |
| 857 | { |
| 858 | RangeStart = (PyInt) curwin->w_cursor.lnum; |
| 859 | RangeEnd = RangeStart; |
| 860 | } |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 861 | Python_Release_Vim(); /* leave vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 862 | |
| 863 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 864 | /* Python only works properly when the LC_NUMERIC locale is "C". */ |
| 865 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 866 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 867 | saved_locale = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 868 | else |
| 869 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 870 | /* Need to make a copy, value may change when setting new locale. */ |
| 871 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 872 | (void)setlocale(LC_NUMERIC, "C"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 873 | } |
| 874 | #endif |
| 875 | |
| 876 | pygilstate = PyGILState_Ensure(); |
| 877 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 878 | /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause |
| 879 | * SyntaxError (unicode error). */ |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 880 | cmdstr = PyUnicode_Decode(cmd, strlen(cmd), |
| 881 | (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
| 882 | cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 883 | Py_XDECREF(cmdstr); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 884 | if (rettv == NULL) |
| 885 | PyRun_SimpleString(PyBytes_AsString(cmdbytes)); |
| 886 | else |
| 887 | { |
| 888 | PyObject *r; |
| 889 | |
| 890 | r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input, |
| 891 | globals, globals); |
| 892 | if (r == NULL) |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 893 | { |
| 894 | if (PyErr_Occurred() && !msg_silent) |
| 895 | PyErr_PrintEx(0); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 896 | EMSG(_("E860: Eval did not return a valid python 3 object")); |
Bram Moolenaar | 4d36987 | 2013-02-20 16:09:43 +0100 | [diff] [blame] | 897 | } |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 898 | else |
| 899 | { |
| 900 | if (ConvertFromPyObject(r, rettv) == -1) |
| 901 | EMSG(_("E861: Failed to convert returned python 3 object to vim value")); |
| 902 | Py_DECREF(r); |
| 903 | } |
| 904 | PyErr_Clear(); |
| 905 | } |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 906 | Py_XDECREF(cmdbytes); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 907 | |
| 908 | PyGILState_Release(pygilstate); |
| 909 | |
| 910 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 911 | if (saved_locale != NULL) |
| 912 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 913 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 914 | vim_free(saved_locale); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 915 | } |
| 916 | #endif |
| 917 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 918 | Python_Lock_Vim(); /* enter vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 919 | PythonIO_Flush(); |
| 920 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 921 | SetPort(oldPort); |
| 922 | #endif |
| 923 | |
| 924 | theend: |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 925 | return; /* keeps lint happy */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 929 | * ":py3" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 930 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 931 | void |
| 932 | ex_py3(exarg_T *eap) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 933 | { |
| 934 | char_u *script; |
| 935 | |
| 936 | script = script_get(eap, eap->arg); |
| 937 | if (!eap->skip) |
| 938 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 939 | if (script == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 940 | DoPy3Command(eap, (char *)eap->arg, NULL); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 941 | else |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 942 | DoPy3Command(eap, (char *)script, NULL); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 943 | } |
| 944 | vim_free(script); |
| 945 | } |
| 946 | |
| 947 | #define BUFFER_SIZE 2048 |
| 948 | |
| 949 | /* |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 950 | * ":py3file" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 951 | */ |
| 952 | void |
| 953 | ex_py3file(exarg_T *eap) |
| 954 | { |
| 955 | static char buffer[BUFFER_SIZE]; |
| 956 | const char *file; |
| 957 | char *p; |
| 958 | int i; |
| 959 | |
| 960 | /* Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 961 | * stdio file pointer, but Vim and the Python DLL are compiled with |
| 962 | * different options under Windows, meaning that stdio pointers aren't |
| 963 | * compatible between the two. Yuk. |
| 964 | * |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 965 | * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) |
| 966 | * |
| 967 | * Using bytes so that Python can detect the source encoding as it normally |
| 968 | * does. The doc does not say "compile" accept bytes, though. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 969 | * |
| 970 | * We need to escape any backslashes or single quotes in the file name, so that |
| 971 | * Python won't mangle the file name. |
| 972 | */ |
| 973 | |
| 974 | strcpy(buffer, "exec(compile(open('"); |
| 975 | p = buffer + 19; /* size of "exec(compile(open('" */ |
| 976 | |
| 977 | for (i=0; i<2; ++i) |
| 978 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 979 | file = (char *)eap->arg; |
| 980 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 981 | { |
| 982 | if (*file == '\\' || *file == '\'') |
| 983 | *p++ = '\\'; |
| 984 | *p++ = *file++; |
| 985 | } |
| 986 | /* If we didn't finish the file name, we hit a buffer overflow */ |
| 987 | if (*file != '\0') |
| 988 | return; |
| 989 | if (i==0) |
| 990 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 991 | strcpy(p,"','rb').read(),'"); |
| 992 | p += 16; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 993 | } |
| 994 | else |
| 995 | { |
| 996 | strcpy(p,"','exec'))"); |
| 997 | p += 10; |
| 998 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | |
| 1002 | /* Execute the file */ |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1003 | DoPy3Command(eap, buffer, NULL); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1004 | } |
| 1005 | |
Bram Moolenaar | 54e8f00 | 2013-05-15 19:44:39 +0200 | [diff] [blame] | 1006 | void |
| 1007 | ex_py3do(exarg_T *eap) |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 1008 | { |
| 1009 | linenr_T i; |
| 1010 | const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n "; |
| 1011 | const char *s = (const char *) eap->arg; |
| 1012 | size_t len; |
| 1013 | char *code; |
| 1014 | int status; |
| 1015 | PyObject *pyfunc, *pymain; |
| 1016 | PyGILState_STATE pygilstate; |
| 1017 | |
| 1018 | if (Python3_Init()) |
| 1019 | goto theend; |
| 1020 | |
| 1021 | if (u_save(eap->line1 - 1, eap->line2 + 1) != OK) |
| 1022 | { |
| 1023 | EMSG(_("cannot save undo information")); |
| 1024 | return; |
| 1025 | } |
| 1026 | len = strlen(code_hdr) + strlen(s); |
| 1027 | code = malloc(len + 1); |
| 1028 | STRCPY(code, code_hdr); |
| 1029 | STRNCAT(code, s, len + 1); |
| 1030 | pygilstate = PyGILState_Ensure(); |
| 1031 | status = PyRun_SimpleString(code); |
| 1032 | vim_free(code); |
| 1033 | if (status) |
| 1034 | { |
| 1035 | EMSG(_("failed to run the code")); |
| 1036 | return; |
| 1037 | } |
| 1038 | status = 0; /* good */ |
| 1039 | pymain = PyImport_AddModule("__main__"); |
| 1040 | pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC); |
| 1041 | PyGILState_Release(pygilstate); |
| 1042 | |
| 1043 | for (i = eap->line1; i <= eap->line2; i++) |
| 1044 | { |
| 1045 | const char *line; |
| 1046 | PyObject *pyline, *pylinenr, *pyret, *pybytes; |
| 1047 | |
| 1048 | line = (char *)ml_get(i); |
| 1049 | pygilstate = PyGILState_Ensure(); |
| 1050 | pyline = PyUnicode_Decode(line, strlen(line), |
| 1051 | (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
| 1052 | pylinenr = PyLong_FromLong(i); |
| 1053 | pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL); |
| 1054 | Py_DECREF(pyline); |
| 1055 | Py_DECREF(pylinenr); |
| 1056 | if (!pyret) |
| 1057 | { |
| 1058 | PyErr_PrintEx(0); |
| 1059 | PythonIO_Flush(); |
| 1060 | status = 1; |
| 1061 | goto out; |
| 1062 | } |
| 1063 | |
| 1064 | if (pyret && pyret != Py_None) |
| 1065 | { |
| 1066 | if (!PyUnicode_Check(pyret)) |
| 1067 | { |
Bram Moolenaar | 54e8f00 | 2013-05-15 19:44:39 +0200 | [diff] [blame] | 1068 | EMSG(_("E863: return value must be an instance of str")); |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 1069 | Py_XDECREF(pyret); |
| 1070 | status = 1; |
| 1071 | goto out; |
| 1072 | } |
| 1073 | pybytes = PyUnicode_AsEncodedString(pyret, |
| 1074 | (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
| 1075 | ml_replace(i, (char_u *) PyBytes_AsString(pybytes), 1); |
| 1076 | Py_DECREF(pybytes); |
| 1077 | changed(); |
| 1078 | #ifdef SYNTAX_HL |
| 1079 | syn_changed(i); /* recompute syntax hl. for this line */ |
| 1080 | #endif |
| 1081 | } |
| 1082 | Py_XDECREF(pyret); |
| 1083 | PythonIO_Flush(); |
| 1084 | PyGILState_Release(pygilstate); |
| 1085 | } |
| 1086 | pygilstate = PyGILState_Ensure(); |
| 1087 | out: |
| 1088 | Py_DECREF(pyfunc); |
| 1089 | PyObject_SetAttrString(pymain, DOPY_FUNC, NULL); |
| 1090 | PyGILState_Release(pygilstate); |
| 1091 | if (status) |
| 1092 | return; |
| 1093 | check_cursor(); |
| 1094 | update_curbuf(NOT_VALID); |
| 1095 | |
| 1096 | theend: |
| 1097 | return; |
| 1098 | } |
| 1099 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1100 | /****************************************************** |
| 1101 | * 2. Python output stream: writes output via [e]msg(). |
| 1102 | */ |
| 1103 | |
| 1104 | /* Implementation functions |
| 1105 | */ |
| 1106 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1107 | static PyObject * |
| 1108 | OutputGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1109 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1110 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1111 | |
| 1112 | if (strcmp(name, "softspace") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1113 | return PyLong_FromLong(((OutputObject *)(self))->softspace); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1114 | |
| 1115 | return PyObject_GenericGetAttr(self, nameobj); |
| 1116 | } |
| 1117 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1118 | static int |
| 1119 | OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1120 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1121 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1122 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1123 | return OutputSetattr(self, name, val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1126 | /***************/ |
| 1127 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1128 | static int |
| 1129 | PythonIO_Init(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1130 | { |
| 1131 | PyType_Ready(&OutputType); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1132 | return PythonIO_Init_io(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1133 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1134 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1135 | /****************************************************** |
| 1136 | * 3. Implementation of the Vim module for Python |
| 1137 | */ |
| 1138 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1139 | /* Window type - Implementation functions |
| 1140 | * -------------------------------------- |
| 1141 | */ |
| 1142 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1143 | #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) |
| 1144 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1145 | /* Buffer type - Implementation functions |
| 1146 | * -------------------------------------- |
| 1147 | */ |
| 1148 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1149 | #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) |
| 1150 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1151 | static Py_ssize_t BufferLength(PyObject *); |
| 1152 | static PyObject *BufferItem(PyObject *, Py_ssize_t); |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1153 | static PyObject* BufferSubscript(PyObject *self, PyObject *idx); |
| 1154 | static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1155 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* Line range type - Implementation functions |
| 1158 | * -------------------------------------- |
| 1159 | */ |
| 1160 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1161 | #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) |
| 1162 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1163 | static PyObject* RangeSubscript(PyObject *self, PyObject *idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1164 | static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1165 | static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1166 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1167 | /* Current objects type - Implementation functions |
| 1168 | * ----------------------------------------------- |
| 1169 | */ |
| 1170 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1171 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1172 | (lenfunc) BufferLength, /* sq_length, len(x) */ |
| 1173 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1174 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1175 | (ssizeargfunc) BufferItem, /* sq_item, x[i] */ |
| 1176 | 0, /* was_sq_slice, x[i:j] */ |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1177 | 0, /* sq_ass_item, x[i]=v */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1178 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1179 | 0, /* sq_contains */ |
| 1180 | 0, /* sq_inplace_concat */ |
| 1181 | 0, /* sq_inplace_repeat */ |
| 1182 | }; |
| 1183 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1184 | static PyMappingMethods BufferAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1185 | /* mp_length */ (lenfunc)BufferLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1186 | /* mp_subscript */ (binaryfunc)BufferSubscript, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1187 | /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1188 | }; |
| 1189 | |
| 1190 | |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1191 | /* Buffer object |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1192 | */ |
| 1193 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1194 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1195 | BufferGetattro(PyObject *self, PyObject*nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1196 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1197 | PyObject *r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1198 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1199 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1200 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1201 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1202 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1203 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1204 | r = BufferAttr((BufferObject *)(self), name); |
| 1205 | if (r || PyErr_Occurred()) |
| 1206 | return r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1207 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1208 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1211 | static PyObject * |
Bram Moolenaar | 7f85d29 | 2012-02-04 20:17:26 +0100 | [diff] [blame] | 1212 | BufferDir(PyObject *self UNUSED, PyObject *args UNUSED) |
| 1213 | { |
| 1214 | return Py_BuildValue("[sssss]", "name", "number", |
| 1215 | "append", "mark", "range"); |
| 1216 | } |
| 1217 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1218 | /******************/ |
| 1219 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1220 | static PyObject * |
| 1221 | BufferSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1222 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1223 | if (PyLong_Check(idx)) |
| 1224 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1225 | long _idx = PyLong_AsLong(idx); |
| 1226 | return BufferItem(self,_idx); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1227 | } else if (PySlice_Check(idx)) |
| 1228 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1229 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1230 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 1231 | if (CheckBuffer((BufferObject *) self)) |
| 1232 | return NULL; |
| 1233 | |
Bram Moolenaar | 9e8edf6 | 2011-09-14 15:41:58 +0200 | [diff] [blame] | 1234 | if (PySlice_GetIndicesEx((PyObject *)idx, |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 1235 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1236 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1237 | &step, &slicelen) < 0) |
| 1238 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1239 | return NULL; |
| 1240 | } |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1241 | return BufferSlice(self, start, stop); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1242 | } |
| 1243 | else |
| 1244 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1245 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1246 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1250 | static Py_ssize_t |
| 1251 | BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) |
| 1252 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1253 | if (PyLong_Check(idx)) |
| 1254 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1255 | long n = PyLong_AsLong(idx); |
| 1256 | return RBAsItem((BufferObject *)(self), n, val, 1, |
| 1257 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1258 | NULL); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1259 | } else if (PySlice_Check(idx)) |
| 1260 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1261 | Py_ssize_t start, stop, step, slicelen; |
| 1262 | |
Bram Moolenaar | 8f1723d | 2013-05-12 20:36:14 +0200 | [diff] [blame] | 1263 | if (CheckBuffer((BufferObject *) self)) |
| 1264 | return -1; |
| 1265 | |
Bram Moolenaar | 9e8edf6 | 2011-09-14 15:41:58 +0200 | [diff] [blame] | 1266 | if (PySlice_GetIndicesEx((PyObject *)idx, |
Bram Moolenaar | bd80f35 | 2013-05-12 21:16:23 +0200 | [diff] [blame] | 1267 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1268 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1269 | &step, &slicelen) < 0) |
| 1270 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1271 | return -1; |
| 1272 | } |
| 1273 | return RBAsSlice((BufferObject *)(self), start, stop, val, 1, |
| 1274 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1275 | NULL); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1276 | } |
| 1277 | else |
| 1278 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1279 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1280 | return -1; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1284 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1285 | (lenfunc) RangeLength, /* sq_length, len(x) */ |
| 1286 | (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ |
| 1287 | (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ |
| 1288 | (ssizeargfunc) RangeItem, /* sq_item, x[i] */ |
| 1289 | 0, /* was_sq_slice, x[i:j] */ |
| 1290 | (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ |
| 1291 | 0, /* sq_ass_slice, x[i:j]=v */ |
| 1292 | 0, /* sq_contains */ |
| 1293 | 0, /* sq_inplace_concat */ |
| 1294 | 0, /* sq_inplace_repeat */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1295 | }; |
| 1296 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1297 | static PyMappingMethods RangeAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1298 | /* mp_length */ (lenfunc)RangeLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1299 | /* mp_subscript */ (binaryfunc)RangeSubscript, |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1300 | /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1301 | }; |
| 1302 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1303 | /* Line range object - Implementation |
| 1304 | */ |
| 1305 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1306 | static PyObject * |
| 1307 | RangeGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1308 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1309 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1310 | |
| 1311 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1312 | return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1313 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1314 | return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1315 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1316 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1319 | /****************/ |
| 1320 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1321 | static Py_ssize_t |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1322 | RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1323 | { |
| 1324 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1325 | ((RangeObject *)(self))->start, |
| 1326 | ((RangeObject *)(self))->end, |
| 1327 | &((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1330 | static Py_ssize_t |
| 1331 | RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val) |
| 1332 | { |
| 1333 | return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
| 1334 | ((RangeObject *)(self))->start, |
| 1335 | ((RangeObject *)(self))->end, |
| 1336 | &((RangeObject *)(self))->end); |
| 1337 | } |
| 1338 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1339 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1340 | RangeSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1341 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1342 | if (PyLong_Check(idx)) |
| 1343 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1344 | long _idx = PyLong_AsLong(idx); |
| 1345 | return RangeItem(self,_idx); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1346 | } else if (PySlice_Check(idx)) |
| 1347 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1348 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1349 | |
Bram Moolenaar | 9e8edf6 | 2011-09-14 15:41:58 +0200 | [diff] [blame] | 1350 | if (PySlice_GetIndicesEx((PyObject *)idx, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1351 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1352 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1353 | &step, &slicelen) < 0) |
| 1354 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1355 | return NULL; |
| 1356 | } |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1357 | return RangeSlice(self, start, stop); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1358 | } |
| 1359 | else |
| 1360 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1361 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1362 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1366 | static Py_ssize_t |
| 1367 | RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val) |
| 1368 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1369 | if (PyLong_Check(idx)) |
| 1370 | { |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1371 | long n = PyLong_AsLong(idx); |
| 1372 | return RangeAsItem(self, n, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1373 | } else if (PySlice_Check(idx)) |
| 1374 | { |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1375 | Py_ssize_t start, stop, step, slicelen; |
| 1376 | |
Bram Moolenaar | 9e8edf6 | 2011-09-14 15:41:58 +0200 | [diff] [blame] | 1377 | if (PySlice_GetIndicesEx((PyObject *)idx, |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1378 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1379 | &start, &stop, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1380 | &step, &slicelen) < 0) |
| 1381 | { |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1382 | return -1; |
| 1383 | } |
| 1384 | return RangeAsSlice(self, start, stop, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1385 | } |
| 1386 | else |
| 1387 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1388 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | ba4897e | 2011-09-14 15:01:58 +0200 | [diff] [blame] | 1389 | return -1; |
| 1390 | } |
| 1391 | } |
| 1392 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1393 | /* TabPage object - Implementation |
| 1394 | */ |
| 1395 | |
| 1396 | static PyObject * |
| 1397 | TabPageGetattro(PyObject *self, PyObject *nameobj) |
| 1398 | { |
| 1399 | PyObject *r; |
| 1400 | |
| 1401 | GET_ATTR_STRING(name, nameobj); |
| 1402 | |
| 1403 | if (CheckTabPage((TabPageObject *)(self))) |
| 1404 | return NULL; |
| 1405 | |
| 1406 | r = TabPageAttr((TabPageObject *)(self), name); |
| 1407 | if (r || PyErr_Occurred()) |
| 1408 | return r; |
| 1409 | else |
| 1410 | return PyObject_GenericGetAttr(self, nameobj); |
| 1411 | } |
| 1412 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1413 | /* Window object - Implementation |
| 1414 | */ |
| 1415 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1416 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1417 | WindowGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1418 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1419 | PyObject *r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1420 | |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1421 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1422 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1423 | if (CheckWindow((WindowObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1424 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1425 | |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1426 | r = WindowAttr((WindowObject *)(self), name); |
| 1427 | if (r || PyErr_Occurred()) |
| 1428 | return r; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1429 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1430 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1431 | } |
| 1432 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1433 | static int |
| 1434 | WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1435 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1436 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1437 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1438 | return WindowSetattr(self, name, val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1441 | /* Tab page list object - Definitions |
| 1442 | */ |
| 1443 | |
| 1444 | static PySequenceMethods TabListAsSeq = { |
| 1445 | (lenfunc) TabListLength, /* sq_length, len(x) */ |
| 1446 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1447 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1448 | (ssizeargfunc) TabListItem, /* sq_item, x[i] */ |
| 1449 | 0, /* sq_slice, x[i:j] */ |
| 1450 | (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ |
| 1451 | 0, /* sq_ass_slice, x[i:j]=v */ |
| 1452 | 0, /* sq_contains */ |
| 1453 | 0, /* sq_inplace_concat */ |
| 1454 | 0, /* sq_inplace_repeat */ |
| 1455 | }; |
| 1456 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1457 | /* Window list object - Definitions |
| 1458 | */ |
| 1459 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1460 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1461 | (lenfunc) WinListLength, /* sq_length, len(x) */ |
| 1462 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1463 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1464 | (ssizeargfunc) WinListItem, /* sq_item, x[i] */ |
| 1465 | 0, /* sq_slice, x[i:j] */ |
| 1466 | (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ |
| 1467 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1468 | 0, /* sq_contains */ |
| 1469 | 0, /* sq_inplace_concat */ |
| 1470 | 0, /* sq_inplace_repeat */ |
| 1471 | }; |
| 1472 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1473 | /* Current items object - Implementation |
| 1474 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1475 | static PyObject * |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1476 | CurrentGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1477 | { |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1478 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1479 | return CurrentGetattr(self, name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1482 | static int |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1483 | CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1484 | { |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1485 | GET_ATTR_STRING(name, nameobj); |
| 1486 | return CurrentSetattr(self, name, value); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1489 | /* Dictionary object - Definitions |
| 1490 | */ |
| 1491 | |
| 1492 | static PyInt DictionaryLength(PyObject *); |
| 1493 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1494 | static PyObject * |
| 1495 | DictionaryGetattro(PyObject *self, PyObject *nameobj) |
| 1496 | { |
| 1497 | DictionaryObject *this = ((DictionaryObject *) (self)); |
| 1498 | |
| 1499 | GET_ATTR_STRING(name, nameobj); |
| 1500 | |
| 1501 | if (strcmp(name, "locked") == 0) |
| 1502 | return PyLong_FromLong(this->dict->dv_lock); |
| 1503 | else if (strcmp(name, "scope") == 0) |
| 1504 | return PyLong_FromLong(this->dict->dv_scope); |
| 1505 | |
| 1506 | return PyObject_GenericGetAttr(self, nameobj); |
| 1507 | } |
| 1508 | |
| 1509 | static int |
| 1510 | DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 1511 | { |
| 1512 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1513 | return DictionarySetattr(self, name, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* List object - Definitions |
| 1517 | */ |
| 1518 | |
| 1519 | static PyInt ListLength(PyObject *); |
| 1520 | static PyObject *ListItem(PyObject *, Py_ssize_t); |
| 1521 | |
| 1522 | static PySequenceMethods ListAsSeq = { |
| 1523 | (lenfunc) ListLength, /* sq_length, len(x) */ |
| 1524 | (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ |
| 1525 | (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ |
| 1526 | (ssizeargfunc) ListItem, /* sq_item, x[i] */ |
| 1527 | (void *) 0, /* was_sq_slice, x[i:j] */ |
| 1528 | (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */ |
| 1529 | (void *) 0, /* was_sq_ass_slice, x[i:j]=v */ |
| 1530 | 0, /* sq_contains */ |
| 1531 | (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */ |
| 1532 | 0, /* sq_inplace_repeat */ |
| 1533 | }; |
| 1534 | |
| 1535 | static PyObject *ListSubscript(PyObject *, PyObject *); |
| 1536 | static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *); |
| 1537 | |
| 1538 | static PyMappingMethods ListAsMapping = { |
| 1539 | /* mp_length */ (lenfunc) ListLength, |
| 1540 | /* mp_subscript */ (binaryfunc) ListSubscript, |
| 1541 | /* mp_ass_subscript */ (objobjargproc) ListAsSubscript, |
| 1542 | }; |
| 1543 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1544 | static PyObject * |
| 1545 | ListSubscript(PyObject *self, PyObject* idxObject) |
| 1546 | { |
| 1547 | if (PyLong_Check(idxObject)) |
| 1548 | { |
| 1549 | long idx = PyLong_AsLong(idxObject); |
| 1550 | return ListItem(self, idx); |
| 1551 | } |
| 1552 | else if (PySlice_Check(idxObject)) |
| 1553 | { |
| 1554 | Py_ssize_t start, stop, step, slicelen; |
| 1555 | |
| 1556 | if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop, |
| 1557 | &step, &slicelen) < 0) |
| 1558 | return NULL; |
| 1559 | return ListSlice(self, start, stop); |
| 1560 | } |
| 1561 | else |
| 1562 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1563 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1564 | return NULL; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | static Py_ssize_t |
| 1569 | ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj) |
| 1570 | { |
| 1571 | if (PyLong_Check(idxObject)) |
| 1572 | { |
| 1573 | long idx = PyLong_AsLong(idxObject); |
| 1574 | return ListAssItem(self, idx, obj); |
| 1575 | } |
| 1576 | else if (PySlice_Check(idxObject)) |
| 1577 | { |
| 1578 | Py_ssize_t start, stop, step, slicelen; |
| 1579 | |
| 1580 | if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop, |
| 1581 | &step, &slicelen) < 0) |
| 1582 | return -1; |
| 1583 | return ListAssSlice(self, start, stop, obj); |
| 1584 | } |
| 1585 | else |
| 1586 | { |
Bram Moolenaar | 8661b17 | 2013-05-15 15:44:28 +0200 | [diff] [blame] | 1587 | PyErr_SetString(PyExc_TypeError, _("index must be int or slice")); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1588 | return -1; |
| 1589 | } |
| 1590 | } |
| 1591 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1592 | static PyObject * |
| 1593 | ListGetattro(PyObject *self, PyObject *nameobj) |
| 1594 | { |
| 1595 | GET_ATTR_STRING(name, nameobj); |
| 1596 | |
| 1597 | if (strcmp(name, "locked") == 0) |
| 1598 | return PyLong_FromLong(((ListObject *) (self))->list->lv_lock); |
| 1599 | |
| 1600 | return PyObject_GenericGetAttr(self, nameobj); |
| 1601 | } |
| 1602 | |
| 1603 | static int |
| 1604 | ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
| 1605 | { |
| 1606 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | 4d1da49 | 2013-04-24 13:39:15 +0200 | [diff] [blame] | 1607 | return ListSetattr(self, name, val); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /* Function object - Definitions |
| 1611 | */ |
| 1612 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1613 | static PyObject * |
| 1614 | FunctionGetattro(PyObject *self, PyObject *nameobj) |
| 1615 | { |
| 1616 | FunctionObject *this = (FunctionObject *)(self); |
Bram Moolenaar | 7704565 | 2012-09-21 13:46:06 +0200 | [diff] [blame] | 1617 | |
| 1618 | GET_ATTR_STRING(name, nameobj); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1619 | |
| 1620 | if (strcmp(name, "name") == 0) |
| 1621 | return PyUnicode_FromString((char *)(this->name)); |
| 1622 | |
| 1623 | return PyObject_GenericGetAttr(self, nameobj); |
| 1624 | } |
| 1625 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1626 | /* External interface |
| 1627 | */ |
| 1628 | |
| 1629 | void |
| 1630 | python3_buffer_free(buf_T *buf) |
| 1631 | { |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1632 | if (BUF_PYTHON_REF(buf) != NULL) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1633 | { |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1634 | BufferObject *bp = BUF_PYTHON_REF(buf); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1635 | bp->buf = INVALID_BUFFER_VALUE; |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1636 | BUF_PYTHON_REF(buf) = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 1641 | void |
| 1642 | python3_window_free(win_T *win) |
| 1643 | { |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1644 | if (WIN_PYTHON_REF(win) != NULL) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1645 | { |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1646 | WindowObject *wp = WIN_PYTHON_REF(win); |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1647 | wp->win = INVALID_WINDOW_VALUE; |
Bram Moolenaar | 971db46 | 2013-05-12 18:44:48 +0200 | [diff] [blame] | 1648 | WIN_PYTHON_REF(win) = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1649 | } |
| 1650 | } |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1651 | |
| 1652 | void |
| 1653 | python3_tabpage_free(tabpage_T *tab) |
| 1654 | { |
| 1655 | if (TAB_PYTHON_REF(tab) != NULL) |
| 1656 | { |
| 1657 | TabPageObject *tp = TAB_PYTHON_REF(tab); |
| 1658 | tp->tab = INVALID_TABPAGE_VALUE; |
| 1659 | TAB_PYTHON_REF(tab) = NULL; |
| 1660 | } |
| 1661 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1662 | #endif |
| 1663 | |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 1664 | static BufMapObject TheBufferMap = |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1665 | { |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 1666 | PyObject_HEAD_INIT(&BufMapType) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1667 | }; |
| 1668 | |
| 1669 | static WinListObject TheWindowList = |
| 1670 | { |
| 1671 | PyObject_HEAD_INIT(&WinListType) |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1672 | NULL |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1673 | }; |
| 1674 | |
| 1675 | static CurrentObject TheCurrent = |
| 1676 | { |
| 1677 | PyObject_HEAD_INIT(&CurrentType) |
| 1678 | }; |
| 1679 | |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1680 | static TabListObject TheTabPageList = |
| 1681 | { |
| 1682 | PyObject_HEAD_INIT(&TabListType) |
| 1683 | }; |
| 1684 | |
Bram Moolenaar | 7854e3a | 2012-11-28 15:33:14 +0100 | [diff] [blame] | 1685 | static PyObject * |
| 1686 | Py3Init_vim(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1687 | { |
| 1688 | PyObject *mod; |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1689 | PyObject *tmp; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1690 | /* The special value is removed from sys.path in Python3_Init(). */ |
| 1691 | static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; |
| 1692 | |
Bram Moolenaar | b6c589a | 2013-05-15 14:39:52 +0200 | [diff] [blame] | 1693 | PyType_Ready(&IterType); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1694 | PyType_Ready(&BufferType); |
| 1695 | PyType_Ready(&RangeType); |
| 1696 | PyType_Ready(&WindowType); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1697 | PyType_Ready(&TabPageType); |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 1698 | PyType_Ready(&BufMapType); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1699 | PyType_Ready(&WinListType); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1700 | PyType_Ready(&TabListType); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1701 | PyType_Ready(&CurrentType); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1702 | PyType_Ready(&DictionaryType); |
| 1703 | PyType_Ready(&ListType); |
| 1704 | PyType_Ready(&FunctionType); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1705 | PyType_Ready(&OptionsType); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1706 | |
| 1707 | /* Set sys.argv[] to avoid a crash in warn(). */ |
| 1708 | PySys_SetArgv(1, argv); |
| 1709 | |
| 1710 | mod = PyModule_Create(&vimmodule); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1711 | if (mod == NULL) |
| 1712 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1713 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1714 | VimError = PyErr_NewException("vim.error", NULL, NULL); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1715 | |
Bram Moolenaar | d5f729c | 2013-05-15 16:04:40 +0200 | [diff] [blame] | 1716 | Py_INCREF(VimError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1717 | PyModule_AddObject(mod, "error", VimError); |
Bram Moolenaar | dfa38d4 | 2013-05-15 13:38:47 +0200 | [diff] [blame] | 1718 | Py_INCREF((PyObject *)(void *)&TheBufferMap); |
| 1719 | PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1720 | Py_INCREF((PyObject *)(void *)&TheCurrent); |
| 1721 | PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); |
| 1722 | Py_INCREF((PyObject *)(void *)&TheWindowList); |
| 1723 | PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList); |
Bram Moolenaar | 5e538ec | 2013-05-15 15:12:29 +0200 | [diff] [blame] | 1724 | Py_INCREF((PyObject *)(void *)&TheTabPageList); |
| 1725 | PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1726 | |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 1727 | PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict)); |
| 1728 | PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict)); |
Bram Moolenaar | 84e0f6c | 2013-05-06 03:52:55 +0200 | [diff] [blame] | 1729 | PyModule_AddObject(mod, "options", |
| 1730 | OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL)); |
Bram Moolenaar | 230bb3f | 2013-04-24 14:07:45 +0200 | [diff] [blame] | 1731 | |
Bram Moolenaar | 66b7985 | 2012-09-21 14:00:35 +0200 | [diff] [blame] | 1732 | #define ADD_INT_CONSTANT(name, value) \ |
| 1733 | tmp = PyLong_FromLong(value); \ |
| 1734 | Py_INCREF(tmp); \ |
| 1735 | PyModule_AddObject(mod, name, tmp) |
| 1736 | |
| 1737 | ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED); |
| 1738 | ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED); |
| 1739 | ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE); |
| 1740 | ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE); |
| 1741 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1742 | if (PyErr_Occurred()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1743 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1744 | |
| 1745 | return mod; |
| 1746 | } |
| 1747 | |
| 1748 | /************************************************************************* |
| 1749 | * 4. Utility functions for handling the interface between Vim and Python. |
| 1750 | */ |
| 1751 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1752 | /* Convert a Vim line into a Python string. |
| 1753 | * All internal newlines are replaced by null characters. |
| 1754 | * |
| 1755 | * On errors, the Python exception data is set, and NULL is returned. |
| 1756 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1757 | static PyObject * |
| 1758 | LineToString(const char *str) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1759 | { |
| 1760 | PyObject *result; |
| 1761 | Py_ssize_t len = strlen(str); |
| 1762 | char *tmp,*p; |
| 1763 | |
| 1764 | tmp = (char *)alloc((unsigned)(len+1)); |
| 1765 | p = tmp; |
| 1766 | if (p == NULL) |
| 1767 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1768 | PyErr_NoMemory(); |
| 1769 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | while (*str) |
| 1773 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1774 | if (*str == '\n') |
| 1775 | *p = '\0'; |
| 1776 | else |
| 1777 | *p = *str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1778 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1779 | ++p; |
| 1780 | ++str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1781 | } |
| 1782 | *p = '\0'; |
| 1783 | |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 1784 | result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1785 | |
| 1786 | vim_free(tmp); |
| 1787 | return result; |
| 1788 | } |
| 1789 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1790 | void |
| 1791 | do_py3eval (char_u *str, typval_T *rettv) |
| 1792 | { |
| 1793 | DoPy3Command(NULL, (char *) str, rettv); |
| 1794 | switch(rettv->v_type) |
| 1795 | { |
| 1796 | case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; |
| 1797 | case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; |
| 1798 | case VAR_FUNC: func_ref(rettv->vval.v_string); break; |
Bram Moolenaar | 77fceb8 | 2012-09-05 18:54:48 +0200 | [diff] [blame] | 1799 | case VAR_UNKNOWN: |
| 1800 | rettv->v_type = VAR_NUMBER; |
| 1801 | rettv->vval.v_number = 0; |
| 1802 | break; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | void |
| 1807 | set_ref_in_python3 (int copyID) |
| 1808 | { |
| 1809 | set_ref_in_py(copyID); |
| 1810 | } |