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