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