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