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