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 | #define PY_SSIZE_T_CLEAN |
| 46 | |
| 47 | #ifdef F_BLANK |
| 48 | # undef F_BLANK |
| 49 | #endif |
| 50 | |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 51 | #ifdef HAVE_STDARG_H |
| 52 | # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ |
| 53 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 54 | #ifdef _POSIX_C_SOURCE /* defined in feature.h */ |
| 55 | # undef _POSIX_C_SOURCE |
| 56 | #endif |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 57 | #ifdef _XOPEN_SOURCE |
| 58 | # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ |
| 59 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 60 | |
| 61 | #include <Python.h> |
| 62 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 63 | # include "macglue.h" |
| 64 | # include <CodeFragments.h> |
| 65 | #endif |
| 66 | #undef main /* Defined in python.h - aargh */ |
| 67 | #undef HAVE_FCNTL_H /* Clash with os_win32.h */ |
| 68 | |
| 69 | static void init_structs(void); |
| 70 | |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 71 | /* The "surrogateescape" error handler is new in Python 3.1 */ |
| 72 | #if PY_VERSION_HEX >= 0x030100f0 |
| 73 | # define CODEC_ERROR_HANDLER "surrogateescape" |
| 74 | #else |
| 75 | # define CODEC_ERROR_HANDLER NULL |
| 76 | #endif |
| 77 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 78 | #define PyInt Py_ssize_t |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 79 | #define PyString_Check(obj) PyUnicode_Check(obj) |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 80 | #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] | 81 | #define PyString_FreeBytes(obj) Py_XDECREF(bytes) |
| 82 | #define PyString_AsString(obj) PyBytes_AsString(obj) |
| 83 | #define PyString_Size(obj) PyBytes_GET_SIZE(bytes) |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 84 | #define PyString_FromString(repr) PyUnicode_FromString(repr) |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 85 | |
Bram Moolenaar | 0c1f3f4 | 2011-02-25 15:18:50 +0100 | [diff] [blame] | 86 | #if defined(DYNAMIC_PYTHON3) || defined(PROTO) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 87 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 88 | # ifndef WIN3264 |
| 89 | # include <dlfcn.h> |
| 90 | # define FARPROC void* |
| 91 | # define HINSTANCE void* |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 92 | # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 93 | # define load_dll(n) dlopen((n), RTLD_LAZY) |
| 94 | # else |
| 95 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 96 | # endif |
| 97 | # define close_dll dlclose |
| 98 | # define symbol_from_dll dlsym |
| 99 | # else |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 100 | # define load_dll vimLoadLib |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 101 | # define close_dll FreeLibrary |
| 102 | # define symbol_from_dll GetProcAddress |
| 103 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 104 | /* |
| 105 | * Wrapper defines |
| 106 | */ |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 107 | # undef PyArg_Parse |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 108 | # define PyArg_Parse py3_PyArg_Parse |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 109 | # undef PyArg_ParseTuple |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 110 | # define PyArg_ParseTuple py3_PyArg_ParseTuple |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 111 | # define PyMem_Free py3_PyMem_Free |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 112 | # define PyDict_SetItemString py3_PyDict_SetItemString |
| 113 | # define PyErr_BadArgument py3_PyErr_BadArgument |
| 114 | # define PyErr_Clear py3_PyErr_Clear |
| 115 | # define PyErr_NoMemory py3_PyErr_NoMemory |
| 116 | # define PyErr_Occurred py3_PyErr_Occurred |
| 117 | # define PyErr_SetNone py3_PyErr_SetNone |
| 118 | # define PyErr_SetString py3_PyErr_SetString |
| 119 | # define PyEval_InitThreads py3_PyEval_InitThreads |
| 120 | # define PyEval_RestoreThread py3_PyEval_RestoreThread |
| 121 | # define PyEval_SaveThread py3_PyEval_SaveThread |
| 122 | # define PyGILState_Ensure py3_PyGILState_Ensure |
| 123 | # define PyGILState_Release py3_PyGILState_Release |
| 124 | # define PyLong_AsLong py3_PyLong_AsLong |
| 125 | # define PyLong_FromLong py3_PyLong_FromLong |
| 126 | # define PyList_GetItem py3_PyList_GetItem |
| 127 | # define PyList_Append py3_PyList_Append |
| 128 | # define PyList_New py3_PyList_New |
| 129 | # define PyList_SetItem py3_PyList_SetItem |
| 130 | # define PyList_Size py3_PyList_Size |
| 131 | # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx |
| 132 | # define PyImport_ImportModule py3_PyImport_ImportModule |
| 133 | # define PyObject_Init py3__PyObject_Init |
| 134 | # define PyDict_New py3_PyDict_New |
| 135 | # define PyDict_GetItemString py3_PyDict_GetItemString |
| 136 | # define PyModule_GetDict py3_PyModule_GetDict |
| 137 | #undef PyRun_SimpleString |
| 138 | # define PyRun_SimpleString py3_PyRun_SimpleString |
| 139 | # define PySys_SetObject py3_PySys_SetObject |
| 140 | # define PySys_SetArgv py3_PySys_SetArgv |
| 141 | # define PyType_Type (*py3_PyType_Type) |
| 142 | # define PyType_Ready py3_PyType_Ready |
| 143 | #undef Py_BuildValue |
| 144 | # define Py_BuildValue py3_Py_BuildValue |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 145 | # define Py_SetPythonHome py3_Py_SetPythonHome |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 146 | # define Py_Initialize py3_Py_Initialize |
| 147 | # define Py_Finalize py3_Py_Finalize |
| 148 | # define Py_IsInitialized py3_Py_IsInitialized |
| 149 | # define _Py_NoneStruct (*py3__Py_NoneStruct) |
| 150 | # define PyModule_AddObject py3_PyModule_AddObject |
| 151 | # define PyImport_AppendInittab py3_PyImport_AppendInittab |
| 152 | # define _PyUnicode_AsString py3__PyUnicode_AsString |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 153 | # undef PyUnicode_AsEncodedString |
| 154 | # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString |
| 155 | # undef PyBytes_AsString |
| 156 | # define PyBytes_AsString py3_PyBytes_AsString |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 157 | # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr |
| 158 | # define PySlice_Type (*py3_PySlice_Type) |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 159 | # define PyErr_NewException py3_PyErr_NewException |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 160 | # ifdef Py_DEBUG |
| 161 | # define _Py_NegativeRefcount py3__Py_NegativeRefcount |
| 162 | # define _Py_RefTotal (*py3__Py_RefTotal) |
| 163 | # define _Py_Dealloc py3__Py_Dealloc |
| 164 | # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc |
| 165 | # define _PyObject_DebugFree py3__PyObject_DebugFree |
| 166 | # else |
| 167 | # define PyObject_Malloc py3_PyObject_Malloc |
| 168 | # define PyObject_Free py3_PyObject_Free |
| 169 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 170 | # define PyType_GenericAlloc py3_PyType_GenericAlloc |
| 171 | # define PyType_GenericNew py3_PyType_GenericNew |
| 172 | # define PyModule_Create2 py3_PyModule_Create2 |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 173 | # undef PyUnicode_FromString |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 174 | # define PyUnicode_FromString py3_PyUnicode_FromString |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 175 | # undef PyUnicode_Decode |
| 176 | # define PyUnicode_Decode py3_PyUnicode_Decode |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 177 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 178 | # ifdef Py_DEBUG |
| 179 | # undef PyObject_NEW |
| 180 | # define PyObject_NEW(type, typeobj) \ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 181 | ( (type *) PyObject_Init( \ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 182 | (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 183 | # endif |
| 184 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 185 | /* |
| 186 | * Pointers for dynamic link |
| 187 | */ |
| 188 | static int (*py3_PySys_SetArgv)(int, wchar_t **); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 189 | static void (*py3_Py_SetPythonHome)(wchar_t *home); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 190 | static void (*py3_Py_Initialize)(void); |
| 191 | static PyObject* (*py3_PyList_New)(Py_ssize_t size); |
| 192 | static PyGILState_STATE (*py3_PyGILState_Ensure)(void); |
| 193 | static void (*py3_PyGILState_Release)(PyGILState_STATE); |
| 194 | static int (*py3_PySys_SetObject)(char *, PyObject *); |
| 195 | static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *); |
| 196 | static Py_ssize_t (*py3_PyList_Size)(PyObject *); |
| 197 | static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 198 | 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] | 199 | static PyObject* (*py3_PyErr_NoMemory)(void); |
| 200 | static void (*py3_Py_Finalize)(void); |
| 201 | static void (*py3_PyErr_SetString)(PyObject *, const char *); |
| 202 | static int (*py3_PyRun_SimpleString)(char *); |
| 203 | static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); |
| 204 | static PyObject* (*py3_PyImport_ImportModule)(const char *); |
| 205 | static int (*py3_PyErr_BadArgument)(void); |
| 206 | static PyTypeObject* py3_PyType_Type; |
| 207 | static PyObject* (*py3_PyErr_Occurred)(void); |
| 208 | static PyObject* (*py3_PyModule_GetDict)(PyObject *); |
| 209 | static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); |
| 210 | static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); |
| 211 | static PyObject* (*py3_PyLong_FromLong)(long); |
| 212 | static PyObject* (*py3_PyDict_New)(void); |
| 213 | static PyObject* (*py3_Py_BuildValue)(char *, ...); |
| 214 | static int (*py3_PyType_Ready)(PyTypeObject *type); |
| 215 | static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 216 | static PyObject* (*py3_PyUnicode_FromString)(const char *u); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 217 | static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size, |
| 218 | const char *encoding, const char *errors); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 219 | static long (*py3_PyLong_AsLong)(PyObject *); |
| 220 | static void (*py3_PyErr_SetNone)(PyObject *); |
| 221 | static void (*py3_PyEval_InitThreads)(void); |
| 222 | static void(*py3_PyEval_RestoreThread)(PyThreadState *); |
| 223 | static PyThreadState*(*py3_PyEval_SaveThread)(void); |
| 224 | static int (*py3_PyArg_Parse)(PyObject *, char *, ...); |
| 225 | static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 226 | static int (*py3_PyMem_Free)(void *); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 227 | static int (*py3_Py_IsInitialized)(void); |
| 228 | static void (*py3_PyErr_Clear)(void); |
| 229 | static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); |
| 230 | static PyObject* py3__Py_NoneStruct; |
| 231 | static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); |
| 232 | static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); |
| 233 | static char* (*py3__PyUnicode_AsString)(PyObject *unicode); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 234 | static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); |
| 235 | static char* (*py3_PyBytes_AsString)(PyObject *bytes); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 236 | static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); |
| 237 | static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); |
| 238 | static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); |
| 239 | static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 240 | static PyTypeObject* py3_PySlice_Type; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 241 | static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 242 | # ifdef Py_DEBUG |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 243 | static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); |
| 244 | static Py_ssize_t* py3__Py_RefTotal; |
| 245 | static void (*py3__Py_Dealloc)(PyObject *obj); |
| 246 | static void (*py3__PyObject_DebugFree)(void*); |
| 247 | static void* (*py3__PyObject_DebugMalloc)(size_t); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 248 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 249 | static void (*py3_PyObject_Free)(void*); |
| 250 | static void* (*py3_PyObject_Malloc)(size_t); |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 251 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 252 | |
| 253 | static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ |
| 254 | |
| 255 | /* Imported exception objects */ |
| 256 | static PyObject *p3imp_PyExc_AttributeError; |
| 257 | static PyObject *p3imp_PyExc_IndexError; |
| 258 | static PyObject *p3imp_PyExc_KeyboardInterrupt; |
| 259 | static PyObject *p3imp_PyExc_TypeError; |
| 260 | static PyObject *p3imp_PyExc_ValueError; |
| 261 | |
| 262 | # define PyExc_AttributeError p3imp_PyExc_AttributeError |
| 263 | # define PyExc_IndexError p3imp_PyExc_IndexError |
| 264 | # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt |
| 265 | # define PyExc_TypeError p3imp_PyExc_TypeError |
| 266 | # define PyExc_ValueError p3imp_PyExc_ValueError |
| 267 | |
| 268 | /* |
| 269 | * Table of name to function pointer of python. |
| 270 | */ |
| 271 | # define PYTHON_PROC FARPROC |
| 272 | static struct |
| 273 | { |
| 274 | char *name; |
| 275 | PYTHON_PROC *ptr; |
| 276 | } py3_funcname_table[] = |
| 277 | { |
| 278 | {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 279 | {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 280 | {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, |
| 281 | {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 282 | {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 283 | {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, |
| 284 | {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, |
| 285 | {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, |
| 286 | {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, |
| 287 | {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, |
| 288 | {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, |
| 289 | {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, |
| 290 | {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, |
| 291 | {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, |
| 292 | {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, |
| 293 | {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, |
| 294 | {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, |
| 295 | {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, |
| 296 | {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, |
| 297 | {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, |
| 298 | {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, |
| 299 | {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, |
| 300 | {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, |
| 301 | {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, |
| 302 | {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, |
| 303 | {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, |
| 304 | {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue}, |
| 305 | {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, |
| 306 | {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, |
| 307 | {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, |
| 308 | {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, |
| 309 | {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, |
| 310 | {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, |
| 311 | {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, |
| 312 | {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 313 | {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, |
| 314 | {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, |
| 315 | {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, |
| 316 | {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, |
| 317 | {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, |
| 318 | {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, |
| 319 | {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 320 | {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString}, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 321 | {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, |
| 322 | {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, |
| 323 | {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, |
| 324 | {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, |
| 325 | {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 326 | {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 327 | # ifdef Py_DEBUG |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 328 | {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, |
| 329 | {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, |
| 330 | {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, |
| 331 | {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, |
| 332 | {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 333 | # else |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 334 | {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, |
| 335 | {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 336 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 337 | {"", NULL}, |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * Free python.dll |
| 342 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 343 | static void |
| 344 | end_dynamic_python3(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 345 | { |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 346 | if (hinstPy3 != 0) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 347 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 348 | close_dll(hinstPy3); |
| 349 | hinstPy3 = 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Load library and get all pointers. |
| 355 | * Parameter 'libname' provides name of DLL. |
| 356 | * Return OK or FAIL. |
| 357 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 358 | static int |
| 359 | py3_runtime_link_init(char *libname, int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 360 | { |
| 361 | int i; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 362 | void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 363 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 364 | # 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] | 365 | /* Can't have Python and Python3 loaded at the same time. |
| 366 | * It cause a crash, because RTLD_GLOBAL is needed for |
| 367 | * standard C extension libraries of one or both python versions. */ |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 368 | if (python_loaded()) |
| 369 | { |
Bram Moolenaar | b744b2f | 2010-08-13 16:22:57 +0200 | [diff] [blame] | 370 | EMSG(_("E837: This Vim cannot execute :py3 after using :python")); |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 371 | return FAIL; |
| 372 | } |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 373 | # endif |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 374 | |
| 375 | if (hinstPy3 != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 376 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 377 | hinstPy3 = load_dll(libname); |
| 378 | |
| 379 | if (!hinstPy3) |
| 380 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 381 | if (verbose) |
| 382 | EMSG2(_(e_loadlib), libname); |
| 383 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | for (i = 0; py3_funcname_table[i].ptr; ++i) |
| 387 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 388 | if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, |
| 389 | py3_funcname_table[i].name)) == NULL) |
| 390 | { |
| 391 | close_dll(hinstPy3); |
| 392 | hinstPy3 = 0; |
| 393 | if (verbose) |
| 394 | EMSG2(_(e_loadfunc), py3_funcname_table[i].name); |
| 395 | return FAIL; |
| 396 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 399 | /* Load unicode functions separately as only the ucs2 or the ucs4 functions |
| 400 | * will be present in the library. */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 401 | ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 402 | ucs_decode = symbol_from_dll(hinstPy3, |
| 403 | "PyUnicodeUCS2_Decode"); |
| 404 | ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
| 405 | "PyUnicodeUCS2_AsEncodedString"); |
| 406 | if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 407 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 408 | ucs_from_string = symbol_from_dll(hinstPy3, |
| 409 | "PyUnicodeUCS4_FromString"); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 410 | ucs_decode = symbol_from_dll(hinstPy3, |
| 411 | "PyUnicodeUCS4_Decode"); |
| 412 | ucs_as_encoded_string = symbol_from_dll(hinstPy3, |
| 413 | "PyUnicodeUCS4_AsEncodedString"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 414 | } |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 415 | if (ucs_from_string && ucs_decode && ucs_as_encoded_string) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 416 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 417 | py3_PyUnicode_FromString = ucs_from_string; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 418 | py3_PyUnicode_Decode = ucs_decode; |
| 419 | py3_PyUnicode_AsEncodedString = ucs_as_encoded_string; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 420 | } |
| 421 | else |
| 422 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 423 | close_dll(hinstPy3); |
| 424 | hinstPy3 = 0; |
| 425 | if (verbose) |
| 426 | EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); |
| 427 | return FAIL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return OK; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * If python is enabled (there is installed python on Windows system) return |
| 435 | * TRUE, else FALSE. |
| 436 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 437 | int |
| 438 | python3_enabled(int verbose) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 439 | { |
| 440 | return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; |
| 441 | } |
| 442 | |
| 443 | /* Load the standard Python exceptions - don't import the symbols from the |
| 444 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 445 | */ |
| 446 | static void get_py3_exceptions __ARGS((void)); |
| 447 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 448 | static void |
| 449 | get_py3_exceptions() |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 450 | { |
| 451 | PyObject *exmod = PyImport_ImportModule("builtins"); |
| 452 | PyObject *exdict = PyModule_GetDict(exmod); |
| 453 | p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 454 | p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
| 455 | p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 456 | p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 457 | p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
| 458 | Py_XINCREF(p3imp_PyExc_AttributeError); |
| 459 | Py_XINCREF(p3imp_PyExc_IndexError); |
| 460 | Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); |
| 461 | Py_XINCREF(p3imp_PyExc_TypeError); |
| 462 | Py_XINCREF(p3imp_PyExc_ValueError); |
| 463 | Py_XDECREF(exmod); |
| 464 | } |
| 465 | #endif /* DYNAMIC_PYTHON3 */ |
| 466 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 467 | static PyObject *BufferNew (buf_T *); |
| 468 | static PyObject *WindowNew(win_T *); |
| 469 | static PyObject *LineToString(const char *); |
| 470 | |
| 471 | static PyTypeObject RangeType; |
| 472 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 473 | /* |
| 474 | * Include the code shared with if_python.c |
| 475 | */ |
| 476 | #include "if_py_both.h" |
| 477 | |
| 478 | static void |
| 479 | call_PyObject_Free(void *p) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 480 | { |
| 481 | #ifdef Py_DEBUG |
| 482 | _PyObject_DebugFree(p); |
| 483 | #else |
| 484 | PyObject_Free(p); |
| 485 | #endif |
| 486 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 487 | |
| 488 | static PyObject * |
| 489 | call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 490 | { |
| 491 | return PyType_GenericNew(type,args,kwds); |
| 492 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 493 | |
| 494 | static PyObject * |
| 495 | call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 496 | { |
| 497 | return PyType_GenericAlloc(type,nitems); |
| 498 | } |
| 499 | |
| 500 | /****************************************************** |
| 501 | * Internal function prototypes. |
| 502 | */ |
| 503 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 504 | static Py_ssize_t RangeStart; |
| 505 | static Py_ssize_t RangeEnd; |
| 506 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 507 | static int PythonIO_Init(void); |
| 508 | static void PythonIO_Fini(void); |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 509 | PyMODINIT_FUNC Py3Init_vim(void); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 510 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 511 | /****************************************************** |
| 512 | * 1. Python interpreter main program. |
| 513 | */ |
| 514 | |
| 515 | static int py3initialised = 0; |
| 516 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 517 | static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; |
| 518 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 519 | void |
| 520 | python3_end() |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 521 | { |
| 522 | static int recurse = 0; |
| 523 | |
| 524 | /* If a crash occurs while doing this, don't try again. */ |
| 525 | if (recurse != 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 526 | return; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 527 | |
| 528 | ++recurse; |
| 529 | |
| 530 | #ifdef DYNAMIC_PYTHON3 |
| 531 | if (hinstPy3) |
| 532 | #endif |
| 533 | if (Py_IsInitialized()) |
| 534 | { |
| 535 | // acquire lock before finalizing |
| 536 | pygilstate = PyGILState_Ensure(); |
| 537 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 538 | PythonIO_Fini(); |
| 539 | Py_Finalize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | #ifdef DYNAMIC_PYTHON3 |
| 543 | end_dynamic_python3(); |
| 544 | #endif |
| 545 | |
| 546 | --recurse; |
| 547 | } |
| 548 | |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 549 | #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO) |
| 550 | int |
| 551 | python3_loaded() |
| 552 | { |
| 553 | return (hinstPy3 != 0); |
| 554 | } |
| 555 | #endif |
| 556 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 557 | static int |
| 558 | Python3_Init(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 559 | { |
| 560 | if (!py3initialised) |
| 561 | { |
| 562 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 563 | if (!python3_enabled(TRUE)) |
| 564 | { |
| 565 | EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); |
| 566 | goto fail; |
| 567 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 568 | #endif |
| 569 | |
| 570 | init_structs(); |
| 571 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 572 | |
| 573 | #ifdef PYTHON3_HOME |
| 574 | Py_SetPythonHome(PYTHON3_HOME); |
| 575 | #endif |
| 576 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 577 | #if !defined(MACOS) || defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 578 | Py_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 579 | #else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 580 | PyMac_Initialize(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 581 | #endif |
Bram Moolenaar | 456f2bb | 2011-06-12 21:37:13 +0200 | [diff] [blame] | 582 | /* initialise threads, must be after Py_Initialize() */ |
| 583 | PyEval_InitThreads(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 584 | |
| 585 | #ifdef DYNAMIC_PYTHON3 |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 586 | get_py3_exceptions(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 587 | #endif |
| 588 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 589 | if (PythonIO_Init()) |
| 590 | goto fail; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 591 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 592 | PyImport_AppendInittab("vim", Py3Init_vim); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 593 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 594 | /* Remove the element from sys.path that was added because of our |
| 595 | * argv[0] value in Py3Init_vim(). Previously we used an empty |
| 596 | * 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] | 597 | * the current directory in sys.path. |
| 598 | * Only after vim has been imported, the element does exist in |
| 599 | * sys.path. |
| 600 | */ |
| 601 | 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] | 602 | |
| 603 | // lock is created and acquired in PyEval_InitThreads() and thread |
| 604 | // state is created in Py_Initialize() |
| 605 | // there _PyGILState_NoteThreadState() also sets gilcounter to 1 |
| 606 | // (python must have threads enabled!) |
| 607 | // so the following does both: unlock GIL and save thread state in TLS |
| 608 | // without deleting thread state |
| 609 | PyGILState_Release(pygilstate); |
| 610 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 611 | py3initialised = 1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | |
| 616 | fail: |
| 617 | /* We call PythonIO_Flush() here to print any Python errors. |
| 618 | * This is OK, as it is possible to call this function even |
| 619 | * if PythonIO_Init() has not completed successfully (it will |
| 620 | * not do anything in this case). |
| 621 | */ |
| 622 | PythonIO_Flush(); |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * External interface |
| 628 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 629 | static void |
| 630 | DoPy3Command(exarg_T *eap, const char *cmd) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 631 | { |
| 632 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 633 | GrafPtr oldPort; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 634 | #endif |
| 635 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 636 | char *saved_locale; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 637 | #endif |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 638 | PyObject *cmdstr; |
| 639 | PyObject *cmdbytes; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 640 | |
| 641 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 642 | GetPort(&oldPort); |
| 643 | /* Check if the Python library is available */ |
| 644 | if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 645 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 646 | #endif |
| 647 | if (Python3_Init()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 648 | goto theend; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 649 | |
| 650 | RangeStart = eap->line1; |
| 651 | RangeEnd = eap->line2; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 652 | Python_Release_Vim(); /* leave vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 653 | |
| 654 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 655 | /* Python only works properly when the LC_NUMERIC locale is "C". */ |
| 656 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 657 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 658 | saved_locale = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 659 | else |
| 660 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 661 | /* Need to make a copy, value may change when setting new locale. */ |
| 662 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 663 | (void)setlocale(LC_NUMERIC, "C"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 664 | } |
| 665 | #endif |
| 666 | |
| 667 | pygilstate = PyGILState_Ensure(); |
| 668 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 669 | /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause |
| 670 | * SyntaxError (unicode error). */ |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 671 | cmdstr = PyUnicode_Decode(cmd, strlen(cmd), |
| 672 | (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
| 673 | cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 674 | Py_XDECREF(cmdstr); |
| 675 | PyRun_SimpleString(PyBytes_AsString(cmdbytes)); |
| 676 | Py_XDECREF(cmdbytes); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 677 | |
| 678 | PyGILState_Release(pygilstate); |
| 679 | |
| 680 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 681 | if (saved_locale != NULL) |
| 682 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 683 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 684 | vim_free(saved_locale); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 685 | } |
| 686 | #endif |
| 687 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 688 | Python_Lock_Vim(); /* enter vim */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 689 | PythonIO_Flush(); |
| 690 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 691 | SetPort(oldPort); |
| 692 | #endif |
| 693 | |
| 694 | theend: |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 695 | return; /* keeps lint happy */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 699 | * ":py3" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 700 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 701 | void |
| 702 | ex_py3(exarg_T *eap) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 703 | { |
| 704 | char_u *script; |
| 705 | |
| 706 | script = script_get(eap, eap->arg); |
| 707 | if (!eap->skip) |
| 708 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 709 | if (script == NULL) |
| 710 | DoPy3Command(eap, (char *)eap->arg); |
| 711 | else |
| 712 | DoPy3Command(eap, (char *)script); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 713 | } |
| 714 | vim_free(script); |
| 715 | } |
| 716 | |
| 717 | #define BUFFER_SIZE 2048 |
| 718 | |
| 719 | /* |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 720 | * ":py3file" |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 721 | */ |
| 722 | void |
| 723 | ex_py3file(exarg_T *eap) |
| 724 | { |
| 725 | static char buffer[BUFFER_SIZE]; |
| 726 | const char *file; |
| 727 | char *p; |
| 728 | int i; |
| 729 | |
| 730 | /* Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 731 | * stdio file pointer, but Vim and the Python DLL are compiled with |
| 732 | * different options under Windows, meaning that stdio pointers aren't |
| 733 | * compatible between the two. Yuk. |
| 734 | * |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 735 | * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) |
| 736 | * |
| 737 | * Using bytes so that Python can detect the source encoding as it normally |
| 738 | * does. The doc does not say "compile" accept bytes, though. |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 739 | * |
| 740 | * We need to escape any backslashes or single quotes in the file name, so that |
| 741 | * Python won't mangle the file name. |
| 742 | */ |
| 743 | |
| 744 | strcpy(buffer, "exec(compile(open('"); |
| 745 | p = buffer + 19; /* size of "exec(compile(open('" */ |
| 746 | |
| 747 | for (i=0; i<2; ++i) |
| 748 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 749 | file = (char *)eap->arg; |
| 750 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 751 | { |
| 752 | if (*file == '\\' || *file == '\'') |
| 753 | *p++ = '\\'; |
| 754 | *p++ = *file++; |
| 755 | } |
| 756 | /* If we didn't finish the file name, we hit a buffer overflow */ |
| 757 | if (*file != '\0') |
| 758 | return; |
| 759 | if (i==0) |
| 760 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 761 | strcpy(p,"','rb').read(),'"); |
| 762 | p += 16; |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 763 | } |
| 764 | else |
| 765 | { |
| 766 | strcpy(p,"','exec'))"); |
| 767 | p += 10; |
| 768 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
| 772 | /* Execute the file */ |
| 773 | DoPy3Command(eap, buffer); |
| 774 | } |
| 775 | |
| 776 | /****************************************************** |
| 777 | * 2. Python output stream: writes output via [e]msg(). |
| 778 | */ |
| 779 | |
| 780 | /* Implementation functions |
| 781 | */ |
| 782 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 783 | static PyObject * |
| 784 | OutputGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 785 | { |
| 786 | char *name = ""; |
| 787 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 788 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 789 | |
| 790 | if (strcmp(name, "softspace") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 791 | return PyLong_FromLong(((OutputObject *)(self))->softspace); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 792 | |
| 793 | return PyObject_GenericGetAttr(self, nameobj); |
| 794 | } |
| 795 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 796 | static int |
| 797 | OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 798 | { |
| 799 | char *name = ""; |
| 800 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 801 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 802 | |
| 803 | if (val == NULL) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 804 | PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
| 805 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | if (strcmp(name, "softspace") == 0) |
| 809 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 810 | if (!PyLong_Check(val)) { |
| 811 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 812 | return -1; |
| 813 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 814 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 815 | ((OutputObject *)(self))->softspace = PyLong_AsLong(val); |
| 816 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 820 | return -1; |
| 821 | } |
| 822 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 823 | /***************/ |
| 824 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 825 | static int |
| 826 | PythonIO_Init(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 827 | { |
| 828 | PyType_Ready(&OutputType); |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 829 | return PythonIO_Init_io(); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 830 | } |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 831 | |
| 832 | static void |
| 833 | PythonIO_Fini(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 834 | { |
| 835 | PySys_SetObject("stdout", NULL); |
| 836 | PySys_SetObject("stderr", NULL); |
| 837 | } |
| 838 | |
| 839 | /****************************************************** |
| 840 | * 3. Implementation of the Vim module for Python |
| 841 | */ |
| 842 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 843 | /* Window type - Implementation functions |
| 844 | * -------------------------------------- |
| 845 | */ |
| 846 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 847 | #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) |
| 848 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 849 | /* Buffer type - Implementation functions |
| 850 | * -------------------------------------- |
| 851 | */ |
| 852 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 853 | #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) |
| 854 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 855 | static Py_ssize_t BufferLength(PyObject *); |
| 856 | static PyObject *BufferItem(PyObject *, Py_ssize_t); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 857 | static PyObject* BufferSubscript(PyObject *self, PyObject* idx); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 858 | static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 859 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 860 | |
| 861 | /* Line range type - Implementation functions |
| 862 | * -------------------------------------- |
| 863 | */ |
| 864 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 865 | #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) |
| 866 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 867 | static PyObject* RangeSubscript(PyObject *self, PyObject* idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 868 | static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); |
| 869 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 870 | /* Current objects type - Implementation functions |
| 871 | * ----------------------------------------------- |
| 872 | */ |
| 873 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 874 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 875 | (lenfunc) BufferLength, /* sq_length, len(x) */ |
| 876 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 877 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 878 | (ssizeargfunc) BufferItem, /* sq_item, x[i] */ |
| 879 | 0, /* was_sq_slice, x[i:j] */ |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 880 | 0, /* sq_ass_item, x[i]=v */ |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 881 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 882 | 0, /* sq_contains */ |
| 883 | 0, /* sq_inplace_concat */ |
| 884 | 0, /* sq_inplace_repeat */ |
| 885 | }; |
| 886 | |
| 887 | PyMappingMethods BufferAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 888 | /* mp_length */ (lenfunc)BufferLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 889 | /* mp_subscript */ (binaryfunc)BufferSubscript, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 890 | /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 891 | }; |
| 892 | |
| 893 | |
| 894 | /* Buffer object - Definitions |
| 895 | */ |
| 896 | |
| 897 | static PyTypeObject BufferType; |
| 898 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 899 | static PyObject * |
| 900 | BufferNew(buf_T *buf) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 901 | { |
| 902 | /* We need to handle deletion of buffers underneath us. |
| 903 | * If we add a "b_python3_ref" field to the buf_T structure, |
| 904 | * then we can get at it in buf_freeall() in vim. We then |
| 905 | * need to create only ONE Python object per buffer - if |
| 906 | * we try to create a second, just INCREF the existing one |
| 907 | * and return it. The (single) Python object referring to |
| 908 | * the buffer is stored in "b_python3_ref". |
| 909 | * Question: what to do on a buf_freeall(). We'll probably |
| 910 | * have to either delete the Python object (DECREF it to |
| 911 | * zero - a bad idea, as it leaves dangling refs!) or |
| 912 | * set the buf_T * value to an invalid value (-1?), which |
| 913 | * means we need checks in all access functions... Bah. |
| 914 | */ |
| 915 | |
| 916 | BufferObject *self; |
| 917 | |
| 918 | if (buf->b_python3_ref != NULL) |
| 919 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 920 | self = buf->b_python3_ref; |
| 921 | Py_INCREF(self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 922 | } |
| 923 | else |
| 924 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 925 | self = PyObject_NEW(BufferObject, &BufferType); |
| 926 | buf->b_python3_ref = self; |
| 927 | if (self == NULL) |
| 928 | return NULL; |
| 929 | self->buf = buf; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | return (PyObject *)(self); |
| 933 | } |
| 934 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 935 | static void |
| 936 | BufferDestructor(PyObject *self) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 937 | { |
| 938 | BufferObject *this = (BufferObject *)(self); |
| 939 | |
| 940 | if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 941 | this->buf->b_python3_ref = NULL; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 942 | |
| 943 | Py_TYPE(self)->tp_free((PyObject*)self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 946 | static PyObject * |
| 947 | BufferGetattro(PyObject *self, PyObject*nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 948 | { |
| 949 | BufferObject *this = (BufferObject *)(self); |
| 950 | |
| 951 | char *name = ""; |
| 952 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 953 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 954 | |
| 955 | if (CheckBuffer(this)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 956 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 957 | |
| 958 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 959 | return Py_BuildValue("s", this->buf->b_ffname); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 960 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 961 | return Py_BuildValue("n", this->buf->b_fnum); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 962 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 963 | return Py_BuildValue("[ss]", "name", "number"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 964 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 965 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 968 | static PyObject * |
| 969 | BufferRepr(PyObject *self) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 970 | { |
| 971 | static char repr[100]; |
| 972 | BufferObject *this = (BufferObject *)(self); |
| 973 | |
| 974 | if (this->buf == INVALID_BUFFER_VALUE) |
| 975 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 976 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
| 977 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 978 | } |
| 979 | else |
| 980 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 981 | char *name = (char *)this->buf->b_fname; |
| 982 | Py_ssize_t len; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 983 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 984 | if (name == NULL) |
| 985 | name = ""; |
| 986 | len = strlen(name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 987 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 988 | if (len > 35) |
| 989 | name = name + (35 - len); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 990 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 991 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 992 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 993 | return PyUnicode_FromString(repr); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
| 997 | /******************/ |
| 998 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 999 | static Py_ssize_t |
| 1000 | BufferLength(PyObject *self) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1001 | { |
| 1002 | if (CheckBuffer((BufferObject *)(self))) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1003 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1004 | |
| 1005 | return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1006 | } |
| 1007 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1008 | static PyObject * |
| 1009 | BufferItem(PyObject *self, Py_ssize_t n) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1010 | { |
| 1011 | return RBItem((BufferObject *)(self), n, 1, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1012 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1015 | static PyObject * |
| 1016 | BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) |
| 1017 | { |
| 1018 | return RBSlice((BufferObject *)(self), lo, hi, 1, |
| 1019 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1020 | } |
| 1021 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1022 | static PyObject * |
| 1023 | BufferSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1024 | { |
| 1025 | if (PyLong_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1026 | long _idx = PyLong_AsLong(idx); |
| 1027 | return BufferItem(self,_idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1028 | } else if (PySlice_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1029 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1030 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1031 | if (PySlice_GetIndicesEx((PySliceObject *)idx, |
| 1032 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, |
| 1033 | &start, &stop, |
| 1034 | &step, &slicelen) < 0) { |
| 1035 | return NULL; |
| 1036 | } |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1037 | return BufferSlice(self,start,stop); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1038 | } else { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1039 | PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
| 1040 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1044 | static Py_ssize_t |
| 1045 | BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) |
| 1046 | { |
| 1047 | if (PyLong_Check(idx)) { |
| 1048 | long n = PyLong_AsLong(idx); |
| 1049 | return RBAsItem((BufferObject *)(self), n, val, 1, |
| 1050 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1051 | NULL); |
| 1052 | } else if (PySlice_Check(idx)) { |
| 1053 | Py_ssize_t start, stop, step, slicelen; |
| 1054 | |
| 1055 | if (PySlice_GetIndicesEx((PySliceObject *)idx, |
| 1056 | (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, |
| 1057 | &start, &stop, |
| 1058 | &step, &slicelen) < 0) { |
| 1059 | return -1; |
| 1060 | } |
| 1061 | return RBAsSlice((BufferObject *)(self), start, stop, val, 1, |
| 1062 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
| 1063 | NULL); |
| 1064 | } else { |
| 1065 | PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | } |
| 1069 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1070 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1071 | (lenfunc) RangeLength, /* sq_length, len(x) */ |
| 1072 | (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ |
| 1073 | (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ |
| 1074 | (ssizeargfunc) RangeItem, /* sq_item, x[i] */ |
| 1075 | 0, /* was_sq_slice, x[i:j] */ |
| 1076 | (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ |
| 1077 | 0, /* sq_ass_slice, x[i:j]=v */ |
| 1078 | 0, /* sq_contains */ |
| 1079 | 0, /* sq_inplace_concat */ |
| 1080 | 0, /* sq_inplace_repeat */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1081 | }; |
| 1082 | |
| 1083 | PyMappingMethods RangeAsMapping = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1084 | /* mp_length */ (lenfunc)RangeLength, |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1085 | /* mp_subscript */ (binaryfunc)RangeSubscript, |
| 1086 | /* mp_ass_subscript */ (objobjargproc)0, |
| 1087 | }; |
| 1088 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1089 | /* Line range object - Implementation |
| 1090 | */ |
| 1091 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1092 | static void |
| 1093 | RangeDestructor(PyObject *self) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1094 | { |
| 1095 | Py_DECREF(((RangeObject *)(self))->buf); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1096 | Py_TYPE(self)->tp_free((PyObject*)self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1099 | static PyObject * |
| 1100 | RangeGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1101 | { |
| 1102 | char *name = ""; |
| 1103 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1104 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1105 | |
| 1106 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1107 | return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1108 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1109 | return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1110 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1111 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1114 | /****************/ |
| 1115 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1116 | static Py_ssize_t |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1117 | RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1118 | { |
| 1119 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1120 | ((RangeObject *)(self))->start, |
| 1121 | ((RangeObject *)(self))->end, |
| 1122 | &((RangeObject *)(self))->end); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1125 | static PyObject * |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1126 | RangeSubscript(PyObject *self, PyObject* idx) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1127 | { |
| 1128 | if (PyLong_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1129 | long _idx = PyLong_AsLong(idx); |
| 1130 | return RangeItem(self,_idx); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1131 | } else if (PySlice_Check(idx)) { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1132 | Py_ssize_t start, stop, step, slicelen; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1133 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1134 | if (PySlice_GetIndicesEx((PySliceObject *)idx, |
| 1135 | ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, |
| 1136 | &start, &stop, |
| 1137 | &step, &slicelen) < 0) { |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | return RangeSlice(self,start,stop+1); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1141 | } else { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1142 | PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); |
| 1143 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1147 | /* Buffer list object - Definitions |
| 1148 | */ |
| 1149 | |
| 1150 | typedef struct |
| 1151 | { |
| 1152 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1153 | } BufListObject; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1154 | |
| 1155 | static PySequenceMethods BufListAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1156 | (lenfunc) BufListLength, /* sq_length, len(x) */ |
| 1157 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1158 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1159 | (ssizeargfunc) BufListItem, /* sq_item, x[i] */ |
| 1160 | 0, /* was_sq_slice, x[i:j] */ |
| 1161 | (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */ |
| 1162 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1163 | 0, /* sq_contains */ |
| 1164 | 0, /* sq_inplace_concat */ |
| 1165 | 0, /* sq_inplace_repeat */ |
| 1166 | }; |
| 1167 | |
| 1168 | static PyTypeObject BufListType; |
| 1169 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1170 | /* Window object - Definitions |
| 1171 | */ |
| 1172 | |
| 1173 | static struct PyMethodDef WindowMethods[] = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1174 | /* name, function, calling, documentation */ |
| 1175 | { NULL, NULL, 0, NULL } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1176 | }; |
| 1177 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1178 | static PyTypeObject WindowType; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1179 | |
| 1180 | /* Window object - Implementation |
| 1181 | */ |
| 1182 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1183 | static PyObject * |
| 1184 | WindowNew(win_T *win) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1185 | { |
| 1186 | /* We need to handle deletion of windows underneath us. |
| 1187 | * If we add a "w_python3_ref" field to the win_T structure, |
| 1188 | * then we can get at it in win_free() in vim. We then |
| 1189 | * need to create only ONE Python object per window - if |
| 1190 | * we try to create a second, just INCREF the existing one |
| 1191 | * and return it. The (single) Python object referring to |
| 1192 | * the window is stored in "w_python3_ref". |
| 1193 | * On a win_free() we set the Python object's win_T* field |
| 1194 | * to an invalid value. We trap all uses of a window |
| 1195 | * object, and reject them if the win_T* field is invalid. |
| 1196 | */ |
| 1197 | |
| 1198 | WindowObject *self; |
| 1199 | |
| 1200 | if (win->w_python3_ref) |
| 1201 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1202 | self = win->w_python3_ref; |
| 1203 | Py_INCREF(self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1204 | } |
| 1205 | else |
| 1206 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1207 | self = PyObject_NEW(WindowObject, &WindowType); |
| 1208 | if (self == NULL) |
| 1209 | return NULL; |
| 1210 | self->win = win; |
| 1211 | win->w_python3_ref = self; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | return (PyObject *)(self); |
| 1215 | } |
| 1216 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1217 | static void |
| 1218 | WindowDestructor(PyObject *self) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1219 | { |
| 1220 | WindowObject *this = (WindowObject *)(self); |
| 1221 | |
| 1222 | if (this->win && this->win != INVALID_WINDOW_VALUE) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1223 | this->win->w_python3_ref = NULL; |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1224 | |
| 1225 | Py_TYPE(self)->tp_free((PyObject*)self); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1228 | static PyObject * |
| 1229 | WindowGetattro(PyObject *self, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1230 | { |
| 1231 | WindowObject *this = (WindowObject *)(self); |
| 1232 | |
| 1233 | char *name = ""; |
| 1234 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1235 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1236 | |
| 1237 | |
| 1238 | if (CheckWindow(this)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1239 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1240 | |
| 1241 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1242 | return (PyObject *)BufferNew(this->win->w_buffer); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1243 | else if (strcmp(name, "cursor") == 0) |
| 1244 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1245 | pos_T *pos = &this->win->w_cursor; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1246 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1247 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1248 | } |
| 1249 | else if (strcmp(name, "height") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1250 | return Py_BuildValue("l", (long)(this->win->w_height)); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1251 | #ifdef FEAT_VERTSPLIT |
| 1252 | else if (strcmp(name, "width") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1253 | return Py_BuildValue("l", (long)(W_WIDTH(this->win))); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1254 | #endif |
| 1255 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1256 | return Py_BuildValue("[sss]", "buffer", "cursor", "height"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1257 | else |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1258 | return PyObject_GenericGetAttr(self, nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1261 | static int |
| 1262 | WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1263 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1264 | char *name = ""; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1265 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1266 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1267 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1268 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1269 | return WindowSetattr(self, name, val); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /* Window list object - Definitions |
| 1273 | */ |
| 1274 | |
| 1275 | typedef struct |
| 1276 | { |
| 1277 | PyObject_HEAD |
| 1278 | } |
| 1279 | WinListObject; |
| 1280 | |
| 1281 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1282 | (lenfunc) WinListLength, /* sq_length, len(x) */ |
| 1283 | (binaryfunc) 0, /* sq_concat, x+y */ |
| 1284 | (ssizeargfunc) 0, /* sq_repeat, x*n */ |
| 1285 | (ssizeargfunc) WinListItem, /* sq_item, x[i] */ |
| 1286 | 0, /* sq_slice, x[i:j] */ |
| 1287 | (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ |
| 1288 | 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1289 | 0, /* sq_contains */ |
| 1290 | 0, /* sq_inplace_concat */ |
| 1291 | 0, /* sq_inplace_repeat */ |
| 1292 | }; |
| 1293 | |
| 1294 | static PyTypeObject WinListType; |
| 1295 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1296 | /* Current items object - Definitions |
| 1297 | */ |
| 1298 | |
| 1299 | typedef struct |
| 1300 | { |
| 1301 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1302 | } CurrentObject; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1303 | |
| 1304 | static PyTypeObject CurrentType; |
| 1305 | |
| 1306 | /* Current items object - Implementation |
| 1307 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1308 | static PyObject * |
| 1309 | CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1310 | { |
| 1311 | char *name = ""; |
| 1312 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1313 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1314 | |
| 1315 | if (strcmp(name, "buffer") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1316 | return (PyObject *)BufferNew(curbuf); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1317 | else if (strcmp(name, "window") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1318 | return (PyObject *)WindowNew(curwin); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1319 | else if (strcmp(name, "line") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1320 | return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1321 | else if (strcmp(name, "range") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1322 | return RangeNew(curbuf, RangeStart, RangeEnd); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1323 | else if (strcmp(name,"__members__") == 0) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1324 | return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1325 | else |
| 1326 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1327 | PyErr_SetString(PyExc_AttributeError, name); |
| 1328 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1332 | static int |
| 1333 | CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1334 | { |
| 1335 | char *name = ""; |
| 1336 | if (PyUnicode_Check(nameobj)) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1337 | name = _PyUnicode_AsString(nameobj); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1338 | |
| 1339 | if (strcmp(name, "line") == 0) |
| 1340 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1341 | if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL) |
| 1342 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1343 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1344 | return 0; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1345 | } |
| 1346 | else |
| 1347 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1348 | PyErr_SetString(PyExc_AttributeError, name); |
| 1349 | return -1; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /* External interface |
| 1354 | */ |
| 1355 | |
| 1356 | void |
| 1357 | python3_buffer_free(buf_T *buf) |
| 1358 | { |
| 1359 | if (buf->b_python3_ref != NULL) |
| 1360 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1361 | BufferObject *bp = buf->b_python3_ref; |
| 1362 | bp->buf = INVALID_BUFFER_VALUE; |
| 1363 | buf->b_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 1368 | void |
| 1369 | python3_window_free(win_T *win) |
| 1370 | { |
| 1371 | if (win->w_python3_ref != NULL) |
| 1372 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1373 | WindowObject *wp = win->w_python3_ref; |
| 1374 | wp->win = INVALID_WINDOW_VALUE; |
| 1375 | win->w_python3_ref = NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | #endif |
| 1379 | |
| 1380 | static BufListObject TheBufferList = |
| 1381 | { |
| 1382 | PyObject_HEAD_INIT(&BufListType) |
| 1383 | }; |
| 1384 | |
| 1385 | static WinListObject TheWindowList = |
| 1386 | { |
| 1387 | PyObject_HEAD_INIT(&WinListType) |
| 1388 | }; |
| 1389 | |
| 1390 | static CurrentObject TheCurrent = |
| 1391 | { |
| 1392 | PyObject_HEAD_INIT(&CurrentType) |
| 1393 | }; |
| 1394 | |
| 1395 | PyDoc_STRVAR(vim_module_doc,"vim python interface\n"); |
| 1396 | |
| 1397 | static struct PyModuleDef vimmodule; |
| 1398 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 1399 | #ifndef PROTO |
| 1400 | PyMODINIT_FUNC Py3Init_vim(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1401 | { |
| 1402 | PyObject *mod; |
| 1403 | /* The special value is removed from sys.path in Python3_Init(). */ |
| 1404 | static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; |
| 1405 | |
| 1406 | PyType_Ready(&BufferType); |
| 1407 | PyType_Ready(&RangeType); |
| 1408 | PyType_Ready(&WindowType); |
| 1409 | PyType_Ready(&BufListType); |
| 1410 | PyType_Ready(&WinListType); |
| 1411 | PyType_Ready(&CurrentType); |
| 1412 | |
| 1413 | /* Set sys.argv[] to avoid a crash in warn(). */ |
| 1414 | PySys_SetArgv(1, argv); |
| 1415 | |
| 1416 | mod = PyModule_Create(&vimmodule); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1417 | if (mod == NULL) |
| 1418 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1419 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1420 | VimError = PyErr_NewException("vim.error", NULL, NULL); |
| 1421 | Py_INCREF(VimError); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1422 | |
| 1423 | PyModule_AddObject(mod, "error", VimError); |
| 1424 | Py_INCREF((PyObject *)(void *)&TheBufferList); |
| 1425 | PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList); |
| 1426 | Py_INCREF((PyObject *)(void *)&TheCurrent); |
| 1427 | PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); |
| 1428 | Py_INCREF((PyObject *)(void *)&TheWindowList); |
| 1429 | PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList); |
| 1430 | |
| 1431 | if (PyErr_Occurred()) |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1432 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1433 | |
| 1434 | return mod; |
| 1435 | } |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 1436 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1437 | |
| 1438 | /************************************************************************* |
| 1439 | * 4. Utility functions for handling the interface between Vim and Python. |
| 1440 | */ |
| 1441 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1442 | /* Convert a Vim line into a Python string. |
| 1443 | * All internal newlines are replaced by null characters. |
| 1444 | * |
| 1445 | * On errors, the Python exception data is set, and NULL is returned. |
| 1446 | */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1447 | static PyObject * |
| 1448 | LineToString(const char *str) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1449 | { |
| 1450 | PyObject *result; |
| 1451 | Py_ssize_t len = strlen(str); |
| 1452 | char *tmp,*p; |
| 1453 | |
| 1454 | tmp = (char *)alloc((unsigned)(len+1)); |
| 1455 | p = tmp; |
| 1456 | if (p == NULL) |
| 1457 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1458 | PyErr_NoMemory(); |
| 1459 | return NULL; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | while (*str) |
| 1463 | { |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1464 | if (*str == '\n') |
| 1465 | *p = '\0'; |
| 1466 | else |
| 1467 | *p = *str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1468 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1469 | ++p; |
| 1470 | ++str; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1471 | } |
| 1472 | *p = '\0'; |
| 1473 | |
Bram Moolenaar | 3d64a31 | 2011-07-15 15:54:44 +0200 | [diff] [blame] | 1474 | result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER); |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1475 | |
| 1476 | vim_free(tmp); |
| 1477 | return result; |
| 1478 | } |
| 1479 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1480 | static void |
| 1481 | init_structs(void) |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1482 | { |
| 1483 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 1484 | OutputType.tp_name = "vim.message"; |
| 1485 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 1486 | OutputType.tp_getattro = OutputGetattro; |
| 1487 | OutputType.tp_setattro = OutputSetattro; |
| 1488 | OutputType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1489 | OutputType.tp_doc = "vim message object"; |
| 1490 | OutputType.tp_methods = OutputMethods; |
| 1491 | OutputType.tp_alloc = call_PyType_GenericAlloc; |
| 1492 | OutputType.tp_new = call_PyType_GenericNew; |
| 1493 | OutputType.tp_free = call_PyObject_Free; |
| 1494 | |
| 1495 | vim_memset(&BufferType, 0, sizeof(BufferType)); |
| 1496 | BufferType.tp_name = "vim.buffer"; |
| 1497 | BufferType.tp_basicsize = sizeof(BufferType); |
| 1498 | BufferType.tp_dealloc = BufferDestructor; |
| 1499 | BufferType.tp_repr = BufferRepr; |
| 1500 | BufferType.tp_as_sequence = &BufferAsSeq; |
| 1501 | BufferType.tp_as_mapping = &BufferAsMapping; |
| 1502 | BufferType.tp_getattro = BufferGetattro; |
| 1503 | BufferType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1504 | BufferType.tp_doc = "vim buffer object"; |
| 1505 | BufferType.tp_methods = BufferMethods; |
| 1506 | BufferType.tp_alloc = call_PyType_GenericAlloc; |
| 1507 | BufferType.tp_new = call_PyType_GenericNew; |
| 1508 | BufferType.tp_free = call_PyObject_Free; |
| 1509 | |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 1510 | vim_memset(&WindowType, 0, sizeof(WindowType)); |
| 1511 | WindowType.tp_name = "vim.window"; |
| 1512 | WindowType.tp_basicsize = sizeof(WindowObject); |
| 1513 | WindowType.tp_dealloc = WindowDestructor; |
| 1514 | WindowType.tp_repr = WindowRepr; |
| 1515 | WindowType.tp_getattro = WindowGetattro; |
| 1516 | WindowType.tp_setattro = WindowSetattro; |
| 1517 | WindowType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1518 | WindowType.tp_doc = "vim Window object"; |
| 1519 | WindowType.tp_methods = WindowMethods; |
| 1520 | WindowType.tp_alloc = call_PyType_GenericAlloc; |
| 1521 | WindowType.tp_new = call_PyType_GenericNew; |
| 1522 | WindowType.tp_free = call_PyObject_Free; |
| 1523 | |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1524 | vim_memset(&BufListType, 0, sizeof(BufListType)); |
| 1525 | BufListType.tp_name = "vim.bufferlist"; |
| 1526 | BufListType.tp_basicsize = sizeof(BufListObject); |
| 1527 | BufListType.tp_as_sequence = &BufListAsSeq; |
| 1528 | BufListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1529 | BufferType.tp_doc = "vim buffer list"; |
| 1530 | |
| 1531 | vim_memset(&WinListType, 0, sizeof(WinListType)); |
| 1532 | WinListType.tp_name = "vim.windowlist"; |
| 1533 | WinListType.tp_basicsize = sizeof(WinListType); |
| 1534 | WinListType.tp_as_sequence = &WinListAsSeq; |
| 1535 | WinListType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1536 | WinListType.tp_doc = "vim window list"; |
| 1537 | |
| 1538 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 1539 | RangeType.tp_name = "vim.range"; |
| 1540 | RangeType.tp_basicsize = sizeof(RangeObject); |
| 1541 | RangeType.tp_dealloc = RangeDestructor; |
| 1542 | RangeType.tp_repr = RangeRepr; |
| 1543 | RangeType.tp_as_sequence = &RangeAsSeq; |
| 1544 | RangeType.tp_as_mapping = &RangeAsMapping; |
| 1545 | RangeType.tp_getattro = RangeGetattro; |
| 1546 | RangeType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1547 | RangeType.tp_doc = "vim Range object"; |
| 1548 | RangeType.tp_methods = RangeMethods; |
| 1549 | RangeType.tp_alloc = call_PyType_GenericAlloc; |
| 1550 | RangeType.tp_new = call_PyType_GenericNew; |
| 1551 | RangeType.tp_free = call_PyObject_Free; |
| 1552 | |
| 1553 | vim_memset(&CurrentType, 0, sizeof(CurrentType)); |
| 1554 | CurrentType.tp_name = "vim.currentdata"; |
| 1555 | CurrentType.tp_basicsize = sizeof(CurrentObject); |
| 1556 | CurrentType.tp_getattro = CurrentGetattro; |
| 1557 | CurrentType.tp_setattro = CurrentSetattro; |
| 1558 | CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; |
| 1559 | CurrentType.tp_doc = "vim current object"; |
| 1560 | |
| 1561 | vim_memset(&vimmodule, 0, sizeof(vimmodule)); |
| 1562 | vimmodule.m_name = "vim"; |
| 1563 | vimmodule.m_doc = vim_module_doc; |
| 1564 | vimmodule.m_size = -1; |
| 1565 | vimmodule.m_methods = VimMethods; |
| 1566 | } |