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