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