Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | #include "vim.h" |
| 21 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #include <limits.h> |
| 23 | |
| 24 | /* Python.h defines _POSIX_THREADS itself (if needed) */ |
| 25 | #ifdef _POSIX_THREADS |
| 26 | # undef _POSIX_THREADS |
| 27 | #endif |
| 28 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 29 | #if defined(_WIN32) && defined(HAVE_FCNTL_H) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | # undef HAVE_FCNTL_H |
| 31 | #endif |
| 32 | |
| 33 | #ifdef _DEBUG |
| 34 | # undef _DEBUG |
| 35 | #endif |
| 36 | |
| 37 | #ifdef HAVE_STDARG_H |
| 38 | # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ |
| 39 | #endif |
Bram Moolenaar | be2c9ae | 2009-11-11 14:06:59 +0000 | [diff] [blame] | 40 | #ifdef _POSIX_C_SOURCE |
| 41 | # undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */ |
| 42 | #endif |
| 43 | #ifdef _XOPEN_SOURCE |
| 44 | # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ |
| 45 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 47 | #define PY_SSIZE_T_CLEAN |
| 48 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | #include <Python.h> |
| 50 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 51 | # include "macglue.h" |
| 52 | # include <CodeFragments.h> |
| 53 | #endif |
| 54 | #undef main /* Defined in python.h - aargh */ |
| 55 | #undef HAVE_FCNTL_H /* Clash with os_win32.h */ |
| 56 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 57 | static void init_structs(void); |
| 58 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 59 | #define PyBytes_FromString PyString_FromString |
| 60 | |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 61 | /* No-op conversion functions, use with care! */ |
| 62 | #define PyString_AsBytes(obj) (obj) |
| 63 | #define PyString_FreeBytes(obj) |
| 64 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | #if !defined(FEAT_PYTHON) && defined(PROTO) |
| 66 | /* Use this to be able to generate prototypes without python being used. */ |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 67 | # define PyObject Py_ssize_t |
| 68 | # define PyThreadState Py_ssize_t |
| 69 | # define PyTypeObject Py_ssize_t |
| 70 | struct PyMethodDef { Py_ssize_t a; }; |
| 71 | # define PySequenceMethods Py_ssize_t |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 74 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 |
| 75 | # define PY_USE_CAPSULE |
| 76 | #endif |
| 77 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 78 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
| 79 | # define PyInt Py_ssize_t |
| 80 | # define PyInquiry lenfunc |
| 81 | # define PyIntArgFunc ssizeargfunc |
| 82 | # define PyIntIntArgFunc ssizessizeargfunc |
| 83 | # define PyIntObjArgProc ssizeobjargproc |
| 84 | # define PyIntIntObjArgProc ssizessizeobjargproc |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 85 | # define Py_ssize_t_fmt "n" |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 86 | #else |
| 87 | # define PyInt int |
| 88 | # define PyInquiry inquiry |
| 89 | # define PyIntArgFunc intargfunc |
| 90 | # define PyIntIntArgFunc intintargfunc |
| 91 | # define PyIntObjArgProc intobjargproc |
| 92 | # define PyIntIntObjArgProc intintobjargproc |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 93 | # define Py_ssize_t_fmt "i" |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | /* Parser flags */ |
| 97 | #define single_input 256 |
| 98 | #define file_input 257 |
| 99 | #define eval_input 258 |
| 100 | |
| 101 | #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0 |
| 102 | /* Python 2.3: can invoke ":python" recursively. */ |
| 103 | # define PY_CAN_RECURSE |
| 104 | #endif |
| 105 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 106 | # if defined(DYNAMIC_PYTHON) || defined(PROTO) |
| 107 | # ifndef DYNAMIC_PYTHON |
| 108 | # define HINSTANCE long_u /* for generating prototypes */ |
| 109 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 111 | # ifndef WIN3264 |
| 112 | # include <dlfcn.h> |
| 113 | # define FARPROC void* |
| 114 | # define HINSTANCE void* |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 115 | # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 116 | # define load_dll(n) dlopen((n), RTLD_LAZY) |
| 117 | # else |
| 118 | # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
| 119 | # endif |
| 120 | # define close_dll dlclose |
| 121 | # define symbol_from_dll dlsym |
| 122 | # else |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 123 | # define load_dll vimLoadLib |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 124 | # define close_dll FreeLibrary |
| 125 | # define symbol_from_dll GetProcAddress |
| 126 | # endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 127 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 128 | /* This makes if_python.c compile without warnings against Python 2.5 |
| 129 | * on Win32 and Win64. */ |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 130 | # undef PyRun_SimpleString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 131 | # undef PyRun_String |
Bram Moolenaar | b61f95c | 2010-08-09 22:06:13 +0200 | [diff] [blame] | 132 | # undef PyArg_Parse |
| 133 | # undef PyArg_ParseTuple |
| 134 | # undef Py_BuildValue |
| 135 | # undef Py_InitModule4 |
| 136 | # undef Py_InitModule4_64 |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 137 | # undef PyObject_CallMethod |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 138 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | /* |
| 140 | * Wrapper defines |
| 141 | */ |
| 142 | # define PyArg_Parse dll_PyArg_Parse |
| 143 | # define PyArg_ParseTuple dll_PyArg_ParseTuple |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 144 | # define PyMem_Free dll_PyMem_Free |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 145 | # define PyMem_Malloc dll_PyMem_Malloc |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 146 | # define PyDict_SetItemString dll_PyDict_SetItemString |
| 147 | # define PyErr_BadArgument dll_PyErr_BadArgument |
| 148 | # define PyErr_Clear dll_PyErr_Clear |
| 149 | # define PyErr_NoMemory dll_PyErr_NoMemory |
| 150 | # define PyErr_Occurred dll_PyErr_Occurred |
| 151 | # define PyErr_SetNone dll_PyErr_SetNone |
| 152 | # define PyErr_SetString dll_PyErr_SetString |
| 153 | # define PyEval_InitThreads dll_PyEval_InitThreads |
| 154 | # define PyEval_RestoreThread dll_PyEval_RestoreThread |
| 155 | # define PyEval_SaveThread dll_PyEval_SaveThread |
| 156 | # ifdef PY_CAN_RECURSE |
| 157 | # define PyGILState_Ensure dll_PyGILState_Ensure |
| 158 | # define PyGILState_Release dll_PyGILState_Release |
| 159 | # endif |
| 160 | # define PyInt_AsLong dll_PyInt_AsLong |
| 161 | # define PyInt_FromLong dll_PyInt_FromLong |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 162 | # define PyLong_AsLong dll_PyLong_AsLong |
| 163 | # define PyLong_FromLong dll_PyLong_FromLong |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | # define PyInt_Type (*dll_PyInt_Type) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 165 | # define PyLong_Type (*dll_PyLong_Type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | # define PyList_GetItem dll_PyList_GetItem |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 167 | # define PyList_Append dll_PyList_Append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | # define PyList_New dll_PyList_New |
| 169 | # define PyList_SetItem dll_PyList_SetItem |
| 170 | # define PyList_Size dll_PyList_Size |
| 171 | # define PyList_Type (*dll_PyList_Type) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 172 | # define PySequence_Check dll_PySequence_Check |
| 173 | # define PySequence_Size dll_PySequence_Size |
| 174 | # define PySequence_GetItem dll_PySequence_GetItem |
| 175 | # define PyTuple_Size dll_PyTuple_Size |
| 176 | # define PyTuple_GetItem dll_PyTuple_GetItem |
| 177 | # define PyTuple_Type (*dll_PyTuple_Type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | # define PyImport_ImportModule dll_PyImport_ImportModule |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 179 | # define PyDict_New dll_PyDict_New |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | # define PyDict_GetItemString dll_PyDict_GetItemString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 181 | # define PyDict_Next dll_PyDict_Next |
| 182 | # ifdef PyMapping_Items |
| 183 | # define PY_NO_MAPPING_ITEMS |
| 184 | # else |
| 185 | # define PyMapping_Items dll_PyMapping_Items |
| 186 | # endif |
| 187 | # define PyObject_CallMethod dll_PyObject_CallMethod |
| 188 | # define PyMapping_Check dll_PyMapping_Check |
| 189 | # define PyIter_Next dll_PyIter_Next |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | # define PyModule_GetDict dll_PyModule_GetDict |
| 191 | # define PyRun_SimpleString dll_PyRun_SimpleString |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 192 | # define PyRun_String dll_PyRun_String |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | # define PyString_AsString dll_PyString_AsString |
| 194 | # define PyString_FromString dll_PyString_FromString |
| 195 | # define PyString_FromStringAndSize dll_PyString_FromStringAndSize |
| 196 | # define PyString_Size dll_PyString_Size |
| 197 | # define PyString_Type (*dll_PyString_Type) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 198 | # define PyUnicode_Type (*dll_PyUnicode_Type) |
| 199 | # define PyUnicodeUCS4_AsEncodedString (*dll_PyUnicodeUCS4_AsEncodedString) |
| 200 | # define PyFloat_AsDouble dll_PyFloat_AsDouble |
| 201 | # define PyFloat_FromDouble dll_PyFloat_FromDouble |
| 202 | # define PyFloat_Type (*dll_PyFloat_Type) |
| 203 | # define PyImport_AddModule (*dll_PyImport_AddModule) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | # define PySys_SetObject dll_PySys_SetObject |
| 205 | # define PySys_SetArgv dll_PySys_SetArgv |
| 206 | # define PyType_Type (*dll_PyType_Type) |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 207 | # define PyType_Ready (*dll_PyType_Ready) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 208 | # define Py_BuildValue dll_Py_BuildValue |
| 209 | # define Py_FindMethod dll_Py_FindMethod |
| 210 | # define Py_InitModule4 dll_Py_InitModule4 |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 211 | # define Py_SetPythonHome dll_Py_SetPythonHome |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | # define Py_Initialize dll_Py_Initialize |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 213 | # define Py_Finalize dll_Py_Finalize |
| 214 | # define Py_IsInitialized dll_Py_IsInitialized |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 215 | # define _PyObject_New dll__PyObject_New |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 216 | # define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | # define _Py_NoneStruct (*dll__Py_NoneStruct) |
| 218 | # define PyObject_Init dll__PyObject_Init |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 219 | # define PyObject_GetIter dll_PyObject_GetIter |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 221 | # define PyType_IsSubtype dll_PyType_IsSubtype |
| 222 | # endif |
| 223 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 224 | # define PyObject_Malloc dll_PyObject_Malloc |
| 225 | # define PyObject_Free dll_PyObject_Free |
| 226 | # endif |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 227 | # ifdef PY_USE_CAPSULE |
| 228 | # define PyCapsule_New dll_PyCapsule_New |
| 229 | # define PyCapsule_GetPointer dll_PyCapsule_GetPointer |
| 230 | # else |
| 231 | # define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr |
| 232 | # define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr |
| 233 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | * Pointers for dynamic link |
| 237 | */ |
| 238 | static int(*dll_PyArg_Parse)(PyObject *, char *, ...); |
| 239 | static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...); |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 240 | static int(*dll_PyMem_Free)(void *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 241 | static void* (*dll_PyMem_Malloc)(size_t); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
| 243 | static int(*dll_PyErr_BadArgument)(void); |
| 244 | static void(*dll_PyErr_Clear)(void); |
| 245 | static PyObject*(*dll_PyErr_NoMemory)(void); |
| 246 | static PyObject*(*dll_PyErr_Occurred)(void); |
| 247 | static void(*dll_PyErr_SetNone)(PyObject *); |
| 248 | static void(*dll_PyErr_SetString)(PyObject *, const char *); |
| 249 | static void(*dll_PyEval_InitThreads)(void); |
| 250 | static void(*dll_PyEval_RestoreThread)(PyThreadState *); |
| 251 | static PyThreadState*(*dll_PyEval_SaveThread)(void); |
| 252 | # ifdef PY_CAN_RECURSE |
| 253 | static PyGILState_STATE (*dll_PyGILState_Ensure)(void); |
| 254 | static void (*dll_PyGILState_Release)(PyGILState_STATE); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 255 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | static long(*dll_PyInt_AsLong)(PyObject *); |
| 257 | static PyObject*(*dll_PyInt_FromLong)(long); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 258 | static long(*dll_PyLong_AsLong)(PyObject *); |
| 259 | static PyObject*(*dll_PyLong_FromLong)(long); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 260 | static PyTypeObject* dll_PyInt_Type; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 261 | static PyTypeObject* dll_PyLong_Type; |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 262 | static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt); |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 263 | static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *); |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 264 | static PyObject*(*dll_PyList_New)(PyInt size); |
| 265 | static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *); |
| 266 | static PyInt(*dll_PyList_Size)(PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 267 | static PyTypeObject* dll_PyList_Type; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 268 | static int (*dll_PySequence_Check)(PyObject *); |
| 269 | static PyInt(*dll_PySequence_Size)(PyObject *); |
| 270 | static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt); |
| 271 | static PyInt(*dll_PyTuple_Size)(PyObject *); |
| 272 | static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt); |
| 273 | static PyTypeObject* dll_PyTuple_Type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | static PyObject*(*dll_PyImport_ImportModule)(const char *); |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 275 | static PyObject*(*dll_PyDict_New)(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 276 | static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 277 | static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); |
| 278 | # ifndef PY_NO_MAPPING_ITEMS |
| 279 | static PyObject* (*dll_PyMapping_Items)(PyObject *); |
| 280 | # endif |
| 281 | static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *); |
| 282 | static int (*dll_PyMapping_Check)(PyObject *); |
| 283 | static PyObject* (*dll_PyIter_Next)(PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 284 | static PyObject*(*dll_PyModule_GetDict)(PyObject *); |
| 285 | static int(*dll_PyRun_SimpleString)(char *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 286 | static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 287 | static char*(*dll_PyString_AsString)(PyObject *); |
| 288 | static PyObject*(*dll_PyString_FromString)(const char *); |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 289 | static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt); |
| 290 | static PyInt(*dll_PyString_Size)(PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 291 | static PyTypeObject* dll_PyString_Type; |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 292 | static PyTypeObject* dll_PyUnicode_Type; |
| 293 | static PyObject *(*PyUnicodeUCS4_AsEncodedString)(PyObject *, char *, char *); |
| 294 | static double(*dll_PyFloat_AsDouble)(PyObject *); |
| 295 | static PyObject*(*dll_PyFloat_FromDouble)(double); |
| 296 | static PyTypeObject* dll_PyFloat_Type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 297 | static int(*dll_PySys_SetObject)(char *, PyObject *); |
| 298 | static int(*dll_PySys_SetArgv)(int, char **); |
| 299 | static PyTypeObject* dll_PyType_Type; |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 300 | static int (*dll_PyType_Ready)(PyTypeObject *type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 301 | static PyObject*(*dll_Py_BuildValue)(char *, ...); |
| 302 | static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *); |
| 303 | static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 304 | static PyObject*(*dll_PyImport_AddModule)(char *); |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 305 | static void(*dll_Py_SetPythonHome)(char *home); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 306 | static void(*dll_Py_Initialize)(void); |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 307 | static void(*dll_Py_Finalize)(void); |
| 308 | static int(*dll_Py_IsInitialized)(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *); |
| 310 | static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 311 | static PyObject* (*dll_PyObject_GetIter)(PyObject *); |
| 312 | static iternextfunc dll__PyObject_NextNotImplemented; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 313 | static PyObject* dll__Py_NoneStruct; |
| 314 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 315 | static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); |
| 316 | # endif |
| 317 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 318 | static void* (*dll_PyObject_Malloc)(size_t); |
| 319 | static void (*dll_PyObject_Free)(void*); |
| 320 | # endif |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 321 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 322 | static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor); |
| 323 | static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *); |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 324 | # else |
| 325 | static PyCObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *)); |
| 326 | static void* (*dll_PyCObject_AsVoidPtr)(PyCObject *); |
| 327 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | |
| 329 | static HINSTANCE hinstPython = 0; /* Instance of python.dll */ |
| 330 | |
| 331 | /* Imported exception objects */ |
| 332 | static PyObject *imp_PyExc_AttributeError; |
| 333 | static PyObject *imp_PyExc_IndexError; |
| 334 | static PyObject *imp_PyExc_KeyboardInterrupt; |
| 335 | static PyObject *imp_PyExc_TypeError; |
| 336 | static PyObject *imp_PyExc_ValueError; |
| 337 | |
| 338 | # define PyExc_AttributeError imp_PyExc_AttributeError |
| 339 | # define PyExc_IndexError imp_PyExc_IndexError |
| 340 | # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt |
| 341 | # define PyExc_TypeError imp_PyExc_TypeError |
| 342 | # define PyExc_ValueError imp_PyExc_ValueError |
| 343 | |
| 344 | /* |
| 345 | * Table of name to function pointer of python. |
| 346 | */ |
| 347 | # define PYTHON_PROC FARPROC |
| 348 | static struct |
| 349 | { |
| 350 | char *name; |
| 351 | PYTHON_PROC *ptr; |
| 352 | } python_funcname_table[] = |
| 353 | { |
| 354 | {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse}, |
| 355 | {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple}, |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 356 | {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 357 | {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString}, |
| 359 | {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument}, |
| 360 | {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear}, |
| 361 | {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory}, |
| 362 | {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred}, |
| 363 | {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone}, |
| 364 | {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString}, |
| 365 | {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads}, |
| 366 | {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread}, |
| 367 | {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread}, |
| 368 | # ifdef PY_CAN_RECURSE |
| 369 | {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure}, |
| 370 | {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release}, |
| 371 | # endif |
| 372 | {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong}, |
| 373 | {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 374 | {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong}, |
| 375 | {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 376 | {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 377 | {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 378 | {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem}, |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 379 | {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 380 | {"PyList_New", (PYTHON_PROC*)&dll_PyList_New}, |
| 381 | {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem}, |
| 382 | {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size}, |
| 383 | {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 384 | {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem}, |
| 385 | {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size}, |
| 386 | {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check}, |
| 387 | {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem}, |
| 388 | {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size}, |
| 389 | {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 390 | {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule}, |
| 391 | {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 392 | {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next}, |
Bram Moolenaar | 0ac9379 | 2006-01-21 22:16:51 +0000 | [diff] [blame] | 393 | {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 394 | # ifndef PY_NO_MAPPING_ITEMS |
| 395 | {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items}, |
| 396 | # endif |
| 397 | {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod}, |
| 398 | {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check}, |
| 399 | {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 400 | {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict}, |
| 401 | {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 402 | {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 403 | {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString}, |
| 404 | {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString}, |
| 405 | {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize}, |
| 406 | {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size}, |
| 407 | {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 408 | {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type}, |
| 409 | {"PyUnicodeUCS4_AsEncodedString", (PYTHON_PROC*)&dll_PyUnicodeUCS4_AsEncodedString}, |
| 410 | {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type}, |
| 411 | {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble}, |
| 412 | {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble}, |
| 413 | {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject}, |
| 415 | {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv}, |
| 416 | {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type}, |
Bram Moolenaar | 30fec7b | 2011-03-26 18:32:05 +0100 | [diff] [blame] | 417 | {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue}, |
| 419 | {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod}, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 420 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \ |
| 421 | && SIZEOF_SIZE_T != SIZEOF_INT |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 422 | {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4}, |
| 423 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 424 | {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4}, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 425 | # endif |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 426 | {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 427 | {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize}, |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 428 | {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize}, |
| 429 | {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New}, |
| 431 | {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init}, |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 432 | {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter}, |
| 433 | {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 434 | {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct}, |
| 435 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
| 436 | {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype}, |
| 437 | # endif |
| 438 | # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 |
| 439 | {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc}, |
| 440 | {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free}, |
| 441 | # endif |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 442 | # ifdef PY_USE_CAPSULE |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 443 | {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New}, |
| 444 | {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer}, |
Bram Moolenaar | 2afa323 | 2012-06-29 16:28:28 +0200 | [diff] [blame] | 445 | # else |
| 446 | {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr}, |
| 447 | {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr}, |
| 448 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 449 | {"", NULL}, |
| 450 | }; |
| 451 | |
| 452 | /* |
| 453 | * Free python.dll |
| 454 | */ |
| 455 | static void |
| 456 | end_dynamic_python(void) |
| 457 | { |
| 458 | if (hinstPython) |
| 459 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 460 | close_dll(hinstPython); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 461 | hinstPython = 0; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | * Load library and get all pointers. |
| 467 | * Parameter 'libname' provides name of DLL. |
| 468 | * Return OK or FAIL. |
| 469 | */ |
| 470 | static int |
| 471 | python_runtime_link_init(char *libname, int verbose) |
| 472 | { |
| 473 | int i; |
| 474 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 475 | #if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) |
Bram Moolenaar | b744b2f | 2010-08-13 16:22:57 +0200 | [diff] [blame] | 476 | /* Can't have Python and Python3 loaded at the same time. |
| 477 | * It cause a crash, because RTLD_GLOBAL is needed for |
| 478 | * standard C extension libraries of one or both python versions. */ |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 479 | if (python3_loaded()) |
| 480 | { |
Bram Moolenaar | 9dc93ae | 2011-08-28 16:00:19 +0200 | [diff] [blame] | 481 | if (verbose) |
| 482 | EMSG(_("E836: This Vim cannot execute :python after using :py3")); |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 483 | return FAIL; |
| 484 | } |
| 485 | #endif |
| 486 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | if (hinstPython) |
| 488 | return OK; |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 489 | hinstPython = load_dll(libname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | if (!hinstPython) |
| 491 | { |
| 492 | if (verbose) |
| 493 | EMSG2(_(e_loadlib), libname); |
| 494 | return FAIL; |
| 495 | } |
| 496 | |
| 497 | for (i = 0; python_funcname_table[i].ptr; ++i) |
| 498 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 499 | if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 500 | python_funcname_table[i].name)) == NULL) |
| 501 | { |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 502 | close_dll(hinstPython); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | hinstPython = 0; |
| 504 | if (verbose) |
| 505 | EMSG2(_(e_loadfunc), python_funcname_table[i].name); |
| 506 | return FAIL; |
| 507 | } |
| 508 | } |
| 509 | return OK; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * If python is enabled (there is installed python on Windows system) return |
| 514 | * TRUE, else FALSE. |
| 515 | */ |
| 516 | int |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 517 | python_enabled(int verbose) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | { |
| 519 | return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK; |
| 520 | } |
| 521 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 522 | /* |
| 523 | * Load the standard Python exceptions - don't import the symbols from the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | * DLL, as this can cause errors (importing data symbols is not reliable). |
| 525 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | static void |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 527 | get_exceptions(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 528 | { |
| 529 | PyObject *exmod = PyImport_ImportModule("exceptions"); |
| 530 | PyObject *exdict = PyModule_GetDict(exmod); |
| 531 | imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); |
| 532 | imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); |
| 533 | imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
| 534 | imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); |
| 535 | imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); |
| 536 | Py_XINCREF(imp_PyExc_AttributeError); |
| 537 | Py_XINCREF(imp_PyExc_IndexError); |
| 538 | Py_XINCREF(imp_PyExc_KeyboardInterrupt); |
| 539 | Py_XINCREF(imp_PyExc_TypeError); |
| 540 | Py_XINCREF(imp_PyExc_ValueError); |
| 541 | Py_XDECREF(exmod); |
| 542 | } |
| 543 | #endif /* DYNAMIC_PYTHON */ |
| 544 | |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 545 | static PyObject *BufferNew (buf_T *); |
| 546 | static PyObject *WindowNew(win_T *); |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 547 | static PyObject *DictionaryNew(dict_T *); |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 548 | static PyObject *LineToString(const char *); |
| 549 | |
| 550 | static PyTypeObject RangeType; |
| 551 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 552 | static int initialised = 0; |
| 553 | #define PYINITIALISED initialised |
| 554 | |
| 555 | /* Add conversion from PyInt? */ |
| 556 | #define DICTKEY_GET(err) \ |
| 557 | if (!PyString_Check(keyObject)) \ |
| 558 | { \ |
| 559 | PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \ |
| 560 | return err; \ |
| 561 | } \ |
| 562 | key = (char_u *) PyString_AsString(keyObject); |
| 563 | #define DICTKEY_UNREF |
| 564 | #define DICTKEY_DECL |
| 565 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 566 | /* |
| 567 | * Include the code shared with if_python3.c |
| 568 | */ |
| 569 | #include "if_py_both.h" |
| 570 | |
| 571 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 572 | /****************************************************** |
| 573 | * Internal function prototypes. |
| 574 | */ |
| 575 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 576 | static PyInt RangeStart; |
| 577 | static PyInt RangeEnd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 578 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 579 | static PyObject *globals; |
| 580 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 581 | static void PythonIO_Flush(void); |
| 582 | static int PythonIO_Init(void); |
| 583 | static int PythonMod_Init(void); |
| 584 | |
| 585 | /* Utility functions for the vim/python interface |
| 586 | * ---------------------------------------------- |
| 587 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 588 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 589 | static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 591 | |
| 592 | /****************************************************** |
| 593 | * 1. Python interpreter main program. |
| 594 | */ |
| 595 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ |
| 597 | typedef PyObject PyThreadState; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 598 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 599 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 600 | #ifdef PY_CAN_RECURSE |
| 601 | static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; |
| 602 | #else |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 603 | static PyThreadState *saved_python_thread = NULL; |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 604 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Suspend a thread of the Python interpreter, other threads are allowed to |
| 608 | * run. |
| 609 | */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 610 | static void |
| 611 | Python_SaveThread(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 612 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 613 | #ifdef PY_CAN_RECURSE |
| 614 | PyGILState_Release(pygilstate); |
| 615 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | saved_python_thread = PyEval_SaveThread(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 617 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Restore a thread of the Python interpreter, waits for other threads to |
| 622 | * block. |
| 623 | */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 624 | static void |
| 625 | Python_RestoreThread(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 627 | #ifdef PY_CAN_RECURSE |
| 628 | pygilstate = PyGILState_Ensure(); |
| 629 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 630 | PyEval_RestoreThread(saved_python_thread); |
| 631 | saved_python_thread = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 632 | #endif |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 633 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | void |
| 636 | python_end() |
| 637 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 638 | static int recurse = 0; |
| 639 | |
| 640 | /* If a crash occurs while doing this, don't try again. */ |
| 641 | if (recurse != 0) |
| 642 | return; |
| 643 | |
| 644 | ++recurse; |
| 645 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | #ifdef DYNAMIC_PYTHON |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 647 | if (hinstPython && Py_IsInitialized()) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 648 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 649 | Python_RestoreThread(); /* enter python */ |
| 650 | Py_Finalize(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 651 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | end_dynamic_python(); |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 653 | #else |
| 654 | if (Py_IsInitialized()) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 655 | { |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 656 | Python_RestoreThread(); /* enter python */ |
| 657 | Py_Finalize(); |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 658 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | #endif |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 660 | |
| 661 | --recurse; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Bram Moolenaar | 4c3a326 | 2010-07-24 15:42:14 +0200 | [diff] [blame] | 664 | #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO) |
| 665 | int |
| 666 | python_loaded() |
| 667 | { |
| 668 | return (hinstPython != 0); |
| 669 | } |
| 670 | #endif |
| 671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 672 | static int |
| 673 | Python_Init(void) |
| 674 | { |
| 675 | if (!initialised) |
| 676 | { |
| 677 | #ifdef DYNAMIC_PYTHON |
| 678 | if (!python_enabled(TRUE)) |
| 679 | { |
| 680 | EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); |
| 681 | goto fail; |
| 682 | } |
| 683 | #endif |
| 684 | |
Bram Moolenaar | 644d37b | 2010-11-16 19:26:02 +0100 | [diff] [blame] | 685 | #ifdef PYTHON_HOME |
| 686 | Py_SetPythonHome(PYTHON_HOME); |
| 687 | #endif |
| 688 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 689 | init_structs(); |
| 690 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | #if !defined(MACOS) || defined(MACOS_X_UNIX) |
| 692 | Py_Initialize(); |
| 693 | #else |
| 694 | PyMac_Initialize(); |
| 695 | #endif |
| 696 | /* initialise threads */ |
| 697 | PyEval_InitThreads(); |
| 698 | |
| 699 | #ifdef DYNAMIC_PYTHON |
| 700 | get_exceptions(); |
| 701 | #endif |
| 702 | |
| 703 | if (PythonIO_Init()) |
| 704 | goto fail; |
| 705 | |
| 706 | if (PythonMod_Init()) |
| 707 | goto fail; |
| 708 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 709 | globals = PyModule_GetDict(PyImport_AddModule("__main__")); |
| 710 | |
Bram Moolenaar | 9774ecc | 2008-11-20 10:04:53 +0000 | [diff] [blame] | 711 | /* Remove the element from sys.path that was added because of our |
| 712 | * argv[0] value in PythonMod_Init(). Previously we used an empty |
| 713 | * string, but dependinding on the OS we then get an empty entry or |
| 714 | * the current directory in sys.path. */ |
| 715 | PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); |
| 716 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 717 | /* the first python thread is vim's, release the lock */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 718 | Python_SaveThread(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | |
| 720 | initialised = 1; |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | |
| 725 | fail: |
| 726 | /* We call PythonIO_Flush() here to print any Python errors. |
| 727 | * This is OK, as it is possible to call this function even |
| 728 | * if PythonIO_Init() has not completed successfully (it will |
| 729 | * not do anything in this case). |
| 730 | */ |
| 731 | PythonIO_Flush(); |
| 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * External interface |
| 737 | */ |
| 738 | static void |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 739 | DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 741 | #ifndef PY_CAN_RECURSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | static int recursive = 0; |
| 743 | #endif |
| 744 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 745 | GrafPtr oldPort; |
| 746 | #endif |
| 747 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 748 | char *saved_locale; |
| 749 | #endif |
| 750 | |
| 751 | #ifndef PY_CAN_RECURSE |
| 752 | if (recursive) |
| 753 | { |
| 754 | EMSG(_("E659: Cannot invoke Python recursively")); |
| 755 | return; |
| 756 | } |
| 757 | ++recursive; |
| 758 | #endif |
| 759 | |
| 760 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 761 | GetPort(&oldPort); |
| 762 | /* Check if the Python library is available */ |
| 763 | if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) |
| 764 | goto theend; |
| 765 | #endif |
| 766 | if (Python_Init()) |
| 767 | goto theend; |
| 768 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 769 | if (rettv == NULL) |
| 770 | { |
| 771 | RangeStart = eap->line1; |
| 772 | RangeEnd = eap->line2; |
| 773 | } |
| 774 | else |
| 775 | { |
| 776 | RangeStart = (PyInt) curwin->w_cursor.lnum; |
| 777 | RangeEnd = RangeStart; |
| 778 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 779 | Python_Release_Vim(); /* leave vim */ |
| 780 | |
| 781 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 782 | /* Python only works properly when the LC_NUMERIC locale is "C". */ |
| 783 | saved_locale = setlocale(LC_NUMERIC, NULL); |
| 784 | if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) |
| 785 | saved_locale = NULL; |
| 786 | else |
| 787 | { |
| 788 | /* Need to make a copy, value may change when setting new locale. */ |
| 789 | saved_locale = (char *)vim_strsave((char_u *)saved_locale); |
| 790 | (void)setlocale(LC_NUMERIC, "C"); |
| 791 | } |
| 792 | #endif |
| 793 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | Python_RestoreThread(); /* enter python */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 795 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 796 | if (rettv == NULL) |
| 797 | PyRun_SimpleString((char *)(cmd)); |
| 798 | else |
| 799 | { |
| 800 | PyObject *r; |
| 801 | |
| 802 | r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals); |
| 803 | if (r == NULL) |
| 804 | EMSG(_("E858: Eval did not return a valid python object")); |
| 805 | else |
| 806 | { |
| 807 | if (ConvertFromPyObject(r, rettv) == -1) |
| 808 | EMSG(_("E859: Failed to convert returned python object to vim value")); |
| 809 | Py_DECREF(r); |
| 810 | } |
| 811 | PyErr_Clear(); |
| 812 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 813 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | Python_SaveThread(); /* leave python */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | |
| 816 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 817 | if (saved_locale != NULL) |
| 818 | { |
| 819 | (void)setlocale(LC_NUMERIC, saved_locale); |
| 820 | vim_free(saved_locale); |
| 821 | } |
| 822 | #endif |
| 823 | |
| 824 | Python_Lock_Vim(); /* enter vim */ |
| 825 | PythonIO_Flush(); |
| 826 | #if defined(MACOS) && !defined(MACOS_X_UNIX) |
| 827 | SetPort(oldPort); |
| 828 | #endif |
| 829 | |
| 830 | theend: |
| 831 | #ifndef PY_CAN_RECURSE |
| 832 | --recursive; |
| 833 | #endif |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 834 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | * ":python" |
| 839 | */ |
| 840 | void |
| 841 | ex_python(exarg_T *eap) |
| 842 | { |
| 843 | char_u *script; |
| 844 | |
| 845 | script = script_get(eap, eap->arg); |
| 846 | if (!eap->skip) |
| 847 | { |
| 848 | if (script == NULL) |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 849 | DoPythonCommand(eap, (char *)eap->arg, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | else |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 851 | DoPythonCommand(eap, (char *)script, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | } |
| 853 | vim_free(script); |
| 854 | } |
| 855 | |
| 856 | #define BUFFER_SIZE 1024 |
| 857 | |
| 858 | /* |
| 859 | * ":pyfile" |
| 860 | */ |
| 861 | void |
| 862 | ex_pyfile(exarg_T *eap) |
| 863 | { |
| 864 | static char buffer[BUFFER_SIZE]; |
| 865 | const char *file = (char *)eap->arg; |
| 866 | char *p; |
| 867 | |
| 868 | /* Have to do it like this. PyRun_SimpleFile requires you to pass a |
| 869 | * stdio file pointer, but Vim and the Python DLL are compiled with |
| 870 | * different options under Windows, meaning that stdio pointers aren't |
| 871 | * compatible between the two. Yuk. |
| 872 | * |
| 873 | * Put the string "execfile('file')" into buffer. But, we need to |
| 874 | * escape any backslashes or single quotes in the file name, so that |
| 875 | * Python won't mangle the file name. |
| 876 | */ |
| 877 | strcpy(buffer, "execfile('"); |
| 878 | p = buffer + 10; /* size of "execfile('" */ |
| 879 | |
| 880 | while (*file && p < buffer + (BUFFER_SIZE - 3)) |
| 881 | { |
| 882 | if (*file == '\\' || *file == '\'') |
| 883 | *p++ = '\\'; |
| 884 | *p++ = *file++; |
| 885 | } |
| 886 | |
| 887 | /* If we didn't finish the file name, we hit a buffer overflow */ |
| 888 | if (*file != '\0') |
| 889 | return; |
| 890 | |
| 891 | /* Put in the terminating "')" and a null */ |
| 892 | *p++ = '\''; |
| 893 | *p++ = ')'; |
| 894 | *p++ = '\0'; |
| 895 | |
| 896 | /* Execute the file */ |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 897 | DoPythonCommand(eap, buffer, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /****************************************************** |
| 901 | * 2. Python output stream: writes output via [e]msg(). |
| 902 | */ |
| 903 | |
| 904 | /* Implementation functions |
| 905 | */ |
| 906 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | static PyObject * |
| 908 | OutputGetattr(PyObject *self, char *name) |
| 909 | { |
| 910 | if (strcmp(name, "softspace") == 0) |
| 911 | return PyInt_FromLong(((OutputObject *)(self))->softspace); |
| 912 | |
| 913 | return Py_FindMethod(OutputMethods, self, name); |
| 914 | } |
| 915 | |
| 916 | static int |
| 917 | OutputSetattr(PyObject *self, char *name, PyObject *val) |
| 918 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 919 | if (val == NULL) |
| 920 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
| 922 | return -1; |
| 923 | } |
| 924 | |
| 925 | if (strcmp(name, "softspace") == 0) |
| 926 | { |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 927 | if (!PyInt_Check(val)) |
| 928 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
| 930 | return -1; |
| 931 | } |
| 932 | |
| 933 | ((OutputObject *)(self))->softspace = PyInt_AsLong(val); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); |
| 938 | return -1; |
| 939 | } |
| 940 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | /***************/ |
| 942 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 943 | static int |
| 944 | PythonIO_Init(void) |
| 945 | { |
| 946 | /* Fixups... */ |
Bram Moolenaar | 21377c8 | 2011-03-26 13:56:48 +0100 | [diff] [blame] | 947 | PyType_Ready(&OutputType); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 949 | return PythonIO_Init_io(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /****************************************************** |
| 953 | * 3. Implementation of the Vim module for Python |
| 954 | */ |
| 955 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 956 | static PyObject *ConvertToPyObject(typval_T *); |
| 957 | static int ConvertFromPyObject(PyObject *, typval_T *); |
| 958 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | /* Window type - Implementation functions |
| 960 | * -------------------------------------- |
| 961 | */ |
| 962 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 963 | #define WindowType_Check(obj) ((obj)->ob_type == &WindowType) |
| 964 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | static void WindowDestructor(PyObject *); |
| 966 | static PyObject *WindowGetattr(PyObject *, char *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | |
| 968 | /* Buffer type - Implementation functions |
| 969 | * -------------------------------------- |
| 970 | */ |
| 971 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | #define BufferType_Check(obj) ((obj)->ob_type == &BufferType) |
| 973 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | static void BufferDestructor(PyObject *); |
| 975 | static PyObject *BufferGetattr(PyObject *, char *); |
| 976 | static PyObject *BufferRepr(PyObject *); |
| 977 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 978 | static PyInt BufferLength(PyObject *); |
| 979 | static PyObject *BufferItem(PyObject *, PyInt); |
| 980 | static PyObject *BufferSlice(PyObject *, PyInt, PyInt); |
| 981 | static PyInt BufferAssItem(PyObject *, PyInt, PyObject *); |
| 982 | static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 984 | /* Line range type - Implementation functions |
| 985 | * -------------------------------------- |
| 986 | */ |
| 987 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | #define RangeType_Check(obj) ((obj)->ob_type == &RangeType) |
| 989 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 990 | static PyInt RangeAssItem(PyObject *, PyInt, PyObject *); |
| 991 | static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 993 | /* Current objects type - Implementation functions |
| 994 | * ----------------------------------------------- |
| 995 | */ |
| 996 | |
| 997 | static PyObject *CurrentGetattr(PyObject *, char *); |
| 998 | static int CurrentSetattr(PyObject *, char *, PyObject *); |
| 999 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1000 | static PySequenceMethods BufferAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1001 | (PyInquiry) BufferLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1003 | (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */ |
| 1004 | (PyIntArgFunc) BufferItem, /* sq_item, x[i] */ |
| 1005 | (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */ |
| 1006 | (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */ |
| 1007 | (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1008 | }; |
| 1009 | |
| 1010 | static PyTypeObject BufferType = { |
| 1011 | PyObject_HEAD_INIT(0) |
| 1012 | 0, |
| 1013 | "buffer", |
| 1014 | sizeof(BufferObject), |
| 1015 | 0, |
| 1016 | |
| 1017 | (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */ |
| 1018 | (printfunc) 0, /* tp_print, print x */ |
| 1019 | (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */ |
| 1020 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 1021 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1022 | (reprfunc) BufferRepr, /* tp_repr, `x`, print x */ |
| 1023 | |
| 1024 | 0, /* as number */ |
| 1025 | &BufferAsSeq, /* as sequence */ |
| 1026 | 0, /* as mapping */ |
| 1027 | |
| 1028 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1029 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1030 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1031 | }; |
| 1032 | |
| 1033 | /* Buffer object - Implementation |
| 1034 | */ |
| 1035 | |
| 1036 | static PyObject * |
| 1037 | BufferNew(buf_T *buf) |
| 1038 | { |
| 1039 | /* We need to handle deletion of buffers underneath us. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1040 | * If we add a "b_python_ref" field to the buf_T structure, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1041 | * then we can get at it in buf_freeall() in vim. We then |
| 1042 | * need to create only ONE Python object per buffer - if |
| 1043 | * we try to create a second, just INCREF the existing one |
| 1044 | * and return it. The (single) Python object referring to |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1045 | * the buffer is stored in "b_python_ref". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1046 | * Question: what to do on a buf_freeall(). We'll probably |
| 1047 | * have to either delete the Python object (DECREF it to |
| 1048 | * zero - a bad idea, as it leaves dangling refs!) or |
| 1049 | * set the buf_T * value to an invalid value (-1?), which |
| 1050 | * means we need checks in all access functions... Bah. |
| 1051 | */ |
| 1052 | |
| 1053 | BufferObject *self; |
| 1054 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1055 | if (buf->b_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1057 | self = buf->b_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1058 | Py_INCREF(self); |
| 1059 | } |
| 1060 | else |
| 1061 | { |
| 1062 | self = PyObject_NEW(BufferObject, &BufferType); |
| 1063 | if (self == NULL) |
| 1064 | return NULL; |
| 1065 | self->buf = buf; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1066 | buf->b_python_ref = self; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | return (PyObject *)(self); |
| 1070 | } |
| 1071 | |
| 1072 | static void |
| 1073 | BufferDestructor(PyObject *self) |
| 1074 | { |
| 1075 | BufferObject *this = (BufferObject *)(self); |
| 1076 | |
| 1077 | if (this->buf && this->buf != INVALID_BUFFER_VALUE) |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1078 | this->buf->b_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 1080 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | static PyObject * |
| 1084 | BufferGetattr(PyObject *self, char *name) |
| 1085 | { |
| 1086 | BufferObject *this = (BufferObject *)(self); |
| 1087 | |
| 1088 | if (CheckBuffer(this)) |
| 1089 | return NULL; |
| 1090 | |
| 1091 | if (strcmp(name, "name") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1092 | return Py_BuildValue("s", this->buf->b_ffname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | else if (strcmp(name, "number") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1094 | return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1095 | else if (strcmp(name,"__members__") == 0) |
| 1096 | return Py_BuildValue("[ss]", "name", "number"); |
| 1097 | else |
| 1098 | return Py_FindMethod(BufferMethods, self, name); |
| 1099 | } |
| 1100 | |
| 1101 | static PyObject * |
| 1102 | BufferRepr(PyObject *self) |
| 1103 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1104 | static char repr[100]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | BufferObject *this = (BufferObject *)(self); |
| 1106 | |
| 1107 | if (this->buf == INVALID_BUFFER_VALUE) |
| 1108 | { |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1109 | vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | return PyString_FromString(repr); |
| 1111 | } |
| 1112 | else |
| 1113 | { |
| 1114 | char *name = (char *)this->buf->b_fname; |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1115 | PyInt len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | |
| 1117 | if (name == NULL) |
| 1118 | name = ""; |
| 1119 | len = strlen(name); |
| 1120 | |
| 1121 | if (len > 35) |
| 1122 | name = name + (35 - len); |
| 1123 | |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 1124 | vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1125 | |
| 1126 | return PyString_FromString(repr); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /******************/ |
| 1131 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1132 | static PyInt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | BufferLength(PyObject *self) |
| 1134 | { |
| 1135 | /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ |
| 1136 | if (CheckBuffer((BufferObject *)(self))) |
| 1137 | return -1; /* ??? */ |
| 1138 | |
| 1139 | return (((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1140 | } |
| 1141 | |
| 1142 | static PyObject * |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1143 | BufferItem(PyObject *self, PyInt n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1144 | { |
| 1145 | return RBItem((BufferObject *)(self), n, 1, |
| 1146 | (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1147 | } |
| 1148 | |
| 1149 | static PyObject * |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1150 | BufferSlice(PyObject *self, PyInt lo, PyInt hi) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | { |
| 1152 | return RBSlice((BufferObject *)(self), lo, hi, 1, |
| 1153 | (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); |
| 1154 | } |
| 1155 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1156 | static PyInt |
| 1157 | BufferAssItem(PyObject *self, PyInt n, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1159 | return RBAsItem((BufferObject *)(self), n, val, 1, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1160 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | NULL); |
| 1162 | } |
| 1163 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1164 | static PyInt |
| 1165 | BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1167 | return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1168 | (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1169 | NULL); |
| 1170 | } |
| 1171 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1172 | static PySequenceMethods RangeAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1173 | (PyInquiry) RangeLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1174 | (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1175 | (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */ |
| 1176 | (PyIntArgFunc) RangeItem, /* sq_item, x[i] */ |
| 1177 | (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */ |
| 1178 | (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */ |
| 1179 | (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1182 | /* Line range object - Implementation |
| 1183 | */ |
| 1184 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | static void |
| 1186 | RangeDestructor(PyObject *self) |
| 1187 | { |
| 1188 | Py_DECREF(((RangeObject *)(self))->buf); |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 1189 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | static PyObject * |
| 1193 | RangeGetattr(PyObject *self, char *name) |
| 1194 | { |
| 1195 | if (strcmp(name, "start") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1196 | return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1197 | else if (strcmp(name, "end") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1198 | return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1199 | else |
| 1200 | return Py_FindMethod(RangeMethods, self, name); |
| 1201 | } |
| 1202 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1203 | /****************/ |
| 1204 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1205 | static PyInt |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1206 | RangeAssItem(PyObject *self, PyInt n, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | { |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1208 | return RBAsItem(((RangeObject *)(self))->buf, n, val, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | ((RangeObject *)(self))->start, |
| 1210 | ((RangeObject *)(self))->end, |
| 1211 | &((RangeObject *)(self))->end); |
| 1212 | } |
| 1213 | |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1214 | static PyInt |
| 1215 | RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1216 | { |
Bram Moolenaar | 19e6094 | 2011-06-19 00:27:51 +0200 | [diff] [blame] | 1217 | return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | ((RangeObject *)(self))->start, |
| 1219 | ((RangeObject *)(self))->end, |
| 1220 | &((RangeObject *)(self))->end); |
| 1221 | } |
| 1222 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | /* Buffer list object - Definitions |
| 1224 | */ |
| 1225 | |
| 1226 | typedef struct |
| 1227 | { |
| 1228 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1229 | } BufListObject; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | |
| 1231 | static PySequenceMethods BufListAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1232 | (PyInquiry) BufListLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | (binaryfunc) 0, /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1234 | (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
| 1235 | (PyIntArgFunc) BufListItem, /* sq_item, x[i] */ |
| 1236 | (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ |
| 1237 | (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ |
| 1238 | (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | }; |
| 1240 | |
| 1241 | static PyTypeObject BufListType = { |
| 1242 | PyObject_HEAD_INIT(0) |
| 1243 | 0, |
| 1244 | "buffer list", |
| 1245 | sizeof(BufListObject), |
| 1246 | 0, |
| 1247 | |
| 1248 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1249 | (printfunc) 0, /* tp_print, print x */ |
| 1250 | (getattrfunc) 0, /* tp_getattr, x.attr */ |
| 1251 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 1252 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1253 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1254 | |
| 1255 | 0, /* as number */ |
| 1256 | &BufListAsSeq, /* as sequence */ |
| 1257 | 0, /* as mapping */ |
| 1258 | |
| 1259 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1260 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1261 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1262 | }; |
| 1263 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | /* Window object - Definitions |
| 1265 | */ |
| 1266 | |
| 1267 | static struct PyMethodDef WindowMethods[] = { |
| 1268 | /* name, function, calling, documentation */ |
| 1269 | { NULL, NULL, 0, NULL } |
| 1270 | }; |
| 1271 | |
| 1272 | static PyTypeObject WindowType = { |
| 1273 | PyObject_HEAD_INIT(0) |
| 1274 | 0, |
| 1275 | "window", |
| 1276 | sizeof(WindowObject), |
| 1277 | 0, |
| 1278 | |
| 1279 | (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */ |
| 1280 | (printfunc) 0, /* tp_print, print x */ |
| 1281 | (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */ |
| 1282 | (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */ |
| 1283 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1284 | (reprfunc) WindowRepr, /* tp_repr, `x`, print x */ |
| 1285 | |
| 1286 | 0, /* as number */ |
| 1287 | 0, /* as sequence */ |
| 1288 | 0, /* as mapping */ |
| 1289 | |
| 1290 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1291 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1292 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1293 | }; |
| 1294 | |
| 1295 | /* Window object - Implementation |
| 1296 | */ |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | WindowNew(win_T *win) |
| 1300 | { |
| 1301 | /* We need to handle deletion of windows underneath us. |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1302 | * If we add a "w_python_ref" field to the win_T structure, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | * then we can get at it in win_free() in vim. We then |
| 1304 | * need to create only ONE Python object per window - if |
| 1305 | * we try to create a second, just INCREF the existing one |
| 1306 | * and return it. The (single) Python object referring to |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1307 | * the window is stored in "w_python_ref". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | * On a win_free() we set the Python object's win_T* field |
| 1309 | * to an invalid value. We trap all uses of a window |
| 1310 | * object, and reject them if the win_T* field is invalid. |
| 1311 | */ |
| 1312 | |
| 1313 | WindowObject *self; |
| 1314 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1315 | if (win->w_python_ref) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1316 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1317 | self = win->w_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1318 | Py_INCREF(self); |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | self = PyObject_NEW(WindowObject, &WindowType); |
| 1323 | if (self == NULL) |
| 1324 | return NULL; |
| 1325 | self->win = win; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1326 | win->w_python_ref = self; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | return (PyObject *)(self); |
| 1330 | } |
| 1331 | |
| 1332 | static void |
| 1333 | WindowDestructor(PyObject *self) |
| 1334 | { |
| 1335 | WindowObject *this = (WindowObject *)(self); |
| 1336 | |
| 1337 | if (this->win && this->win != INVALID_WINDOW_VALUE) |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1338 | this->win->w_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1339 | |
Bram Moolenaar | 658ada6 | 2006-10-03 13:02:36 +0000 | [diff] [blame] | 1340 | Py_DECREF(self); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1343 | static PyObject * |
| 1344 | WindowGetattr(PyObject *self, char *name) |
| 1345 | { |
| 1346 | WindowObject *this = (WindowObject *)(self); |
| 1347 | |
| 1348 | if (CheckWindow(this)) |
| 1349 | return NULL; |
| 1350 | |
| 1351 | if (strcmp(name, "buffer") == 0) |
| 1352 | return (PyObject *)BufferNew(this->win->w_buffer); |
| 1353 | else if (strcmp(name, "cursor") == 0) |
| 1354 | { |
| 1355 | pos_T *pos = &this->win->w_cursor; |
| 1356 | |
| 1357 | return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); |
| 1358 | } |
| 1359 | else if (strcmp(name, "height") == 0) |
| 1360 | return Py_BuildValue("l", (long)(this->win->w_height)); |
| 1361 | #ifdef FEAT_VERTSPLIT |
| 1362 | else if (strcmp(name, "width") == 0) |
| 1363 | return Py_BuildValue("l", (long)(W_WIDTH(this->win))); |
| 1364 | #endif |
| 1365 | else if (strcmp(name,"__members__") == 0) |
| 1366 | return Py_BuildValue("[sss]", "buffer", "cursor", "height"); |
| 1367 | else |
| 1368 | return Py_FindMethod(WindowMethods, self, name); |
| 1369 | } |
| 1370 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | /* Window list object - Definitions |
| 1372 | */ |
| 1373 | |
| 1374 | typedef struct |
| 1375 | { |
| 1376 | PyObject_HEAD |
| 1377 | } |
| 1378 | WinListObject; |
| 1379 | |
| 1380 | static PySequenceMethods WinListAsSeq = { |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1381 | (PyInquiry) WinListLength, /* sq_length, len(x) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | (binaryfunc) 0, /* sq_concat, x+y */ |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1383 | (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
| 1384 | (PyIntArgFunc) WinListItem, /* sq_item, x[i] */ |
| 1385 | (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ |
| 1386 | (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ |
| 1387 | (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | }; |
| 1389 | |
| 1390 | static PyTypeObject WinListType = { |
| 1391 | PyObject_HEAD_INIT(0) |
| 1392 | 0, |
| 1393 | "window list", |
| 1394 | sizeof(WinListObject), |
| 1395 | 0, |
| 1396 | |
| 1397 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1398 | (printfunc) 0, /* tp_print, print x */ |
| 1399 | (getattrfunc) 0, /* tp_getattr, x.attr */ |
| 1400 | (setattrfunc) 0, /* tp_setattr, x.attr=v */ |
| 1401 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1402 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1403 | |
| 1404 | 0, /* as number */ |
| 1405 | &WinListAsSeq, /* as sequence */ |
| 1406 | 0, /* as mapping */ |
| 1407 | |
| 1408 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1409 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1410 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1411 | }; |
| 1412 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | /* Current items object - Definitions |
| 1414 | */ |
| 1415 | |
| 1416 | typedef struct |
| 1417 | { |
| 1418 | PyObject_HEAD |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1419 | } CurrentObject; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1420 | |
| 1421 | static PyTypeObject CurrentType = { |
| 1422 | PyObject_HEAD_INIT(0) |
| 1423 | 0, |
| 1424 | "current data", |
| 1425 | sizeof(CurrentObject), |
| 1426 | 0, |
| 1427 | |
| 1428 | (destructor) 0, /* tp_dealloc, refcount==0 */ |
| 1429 | (printfunc) 0, /* tp_print, print x */ |
| 1430 | (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */ |
| 1431 | (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */ |
| 1432 | (cmpfunc) 0, /* tp_compare, x>y */ |
| 1433 | (reprfunc) 0, /* tp_repr, `x`, print x */ |
| 1434 | |
| 1435 | 0, /* as number */ |
| 1436 | 0, /* as sequence */ |
| 1437 | 0, /* as mapping */ |
| 1438 | |
| 1439 | (hashfunc) 0, /* tp_hash, dict(x) */ |
| 1440 | (ternaryfunc) 0, /* tp_call, x() */ |
| 1441 | (reprfunc) 0, /* tp_str, str(x) */ |
| 1442 | }; |
| 1443 | |
| 1444 | /* Current items object - Implementation |
| 1445 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1446 | static PyObject * |
Bram Moolenaar | 4bdbbf7 | 2009-05-21 21:27:43 +0000 | [diff] [blame] | 1447 | CurrentGetattr(PyObject *self UNUSED, char *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | { |
| 1449 | if (strcmp(name, "buffer") == 0) |
| 1450 | return (PyObject *)BufferNew(curbuf); |
| 1451 | else if (strcmp(name, "window") == 0) |
| 1452 | return (PyObject *)WindowNew(curwin); |
| 1453 | else if (strcmp(name, "line") == 0) |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1454 | return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1455 | else if (strcmp(name, "range") == 0) |
| 1456 | return RangeNew(curbuf, RangeStart, RangeEnd); |
| 1457 | else if (strcmp(name,"__members__") == 0) |
| 1458 | return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); |
| 1459 | else |
| 1460 | { |
| 1461 | PyErr_SetString(PyExc_AttributeError, name); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | } |
| 1465 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | static int |
Bram Moolenaar | 4bdbbf7 | 2009-05-21 21:27:43 +0000 | [diff] [blame] | 1467 | CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1468 | { |
| 1469 | if (strcmp(name, "line") == 0) |
| 1470 | { |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1471 | if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1472 | return -1; |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | else |
| 1477 | { |
| 1478 | PyErr_SetString(PyExc_AttributeError, name); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* External interface |
| 1484 | */ |
| 1485 | |
| 1486 | void |
| 1487 | python_buffer_free(buf_T *buf) |
| 1488 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1489 | if (buf->b_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1490 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1491 | BufferObject *bp = buf->b_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | bp->buf = INVALID_BUFFER_VALUE; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1493 | buf->b_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 1498 | void |
| 1499 | python_window_free(win_T *win) |
| 1500 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1501 | if (win->w_python_ref != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1502 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1503 | WindowObject *wp = win->w_python_ref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1504 | wp->win = INVALID_WINDOW_VALUE; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1505 | win->w_python_ref = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | #endif |
| 1509 | |
| 1510 | static BufListObject TheBufferList = |
| 1511 | { |
| 1512 | PyObject_HEAD_INIT(&BufListType) |
| 1513 | }; |
| 1514 | |
| 1515 | static WinListObject TheWindowList = |
| 1516 | { |
| 1517 | PyObject_HEAD_INIT(&WinListType) |
| 1518 | }; |
| 1519 | |
| 1520 | static CurrentObject TheCurrent = |
| 1521 | { |
| 1522 | PyObject_HEAD_INIT(&CurrentType) |
| 1523 | }; |
| 1524 | |
| 1525 | static int |
| 1526 | PythonMod_Init(void) |
| 1527 | { |
| 1528 | PyObject *mod; |
| 1529 | PyObject *dict; |
Bram Moolenaar | 9774ecc | 2008-11-20 10:04:53 +0000 | [diff] [blame] | 1530 | /* The special value is removed from sys.path in Python_Init(). */ |
| 1531 | static char *(argv[2]) = {"/must>not&exist/foo", NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1532 | |
| 1533 | /* Fixups... */ |
Bram Moolenaar | 21377c8 | 2011-03-26 13:56:48 +0100 | [diff] [blame] | 1534 | PyType_Ready(&BufferType); |
| 1535 | PyType_Ready(&RangeType); |
| 1536 | PyType_Ready(&WindowType); |
| 1537 | PyType_Ready(&BufListType); |
| 1538 | PyType_Ready(&WinListType); |
| 1539 | PyType_Ready(&CurrentType); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | |
| 1541 | /* Set sys.argv[] to avoid a crash in warn(). */ |
| 1542 | PySys_SetArgv(1, argv); |
| 1543 | |
Bram Moolenaar | e7cb9cf | 2008-06-20 14:32:41 +0000 | [diff] [blame] | 1544 | mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1545 | dict = PyModule_GetDict(mod); |
| 1546 | |
| 1547 | VimError = Py_BuildValue("s", "vim.error"); |
| 1548 | |
| 1549 | PyDict_SetItemString(dict, "error", VimError); |
Bram Moolenaar | 7df2d66 | 2005-01-25 22:18:08 +0000 | [diff] [blame] | 1550 | PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList); |
| 1551 | PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent); |
| 1552 | PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (PyErr_Occurred()) |
| 1555 | return -1; |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | /************************************************************************* |
| 1561 | * 4. Utility functions for handling the interface between Vim and Python. |
| 1562 | */ |
| 1563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1564 | /* Convert a Vim line into a Python string. |
| 1565 | * All internal newlines are replaced by null characters. |
| 1566 | * |
| 1567 | * On errors, the Python exception data is set, and NULL is returned. |
| 1568 | */ |
| 1569 | static PyObject * |
| 1570 | LineToString(const char *str) |
| 1571 | { |
| 1572 | PyObject *result; |
Bram Moolenaar | 2c45e94 | 2008-06-04 11:35:26 +0000 | [diff] [blame] | 1573 | PyInt len = strlen(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1574 | char *p; |
| 1575 | |
| 1576 | /* Allocate an Python string object, with uninitialised contents. We |
| 1577 | * must do it this way, so that we can modify the string in place |
| 1578 | * later. See the Python source, Objects/stringobject.c for details. |
| 1579 | */ |
| 1580 | result = PyString_FromStringAndSize(NULL, len); |
| 1581 | if (result == NULL) |
| 1582 | return NULL; |
| 1583 | |
| 1584 | p = PyString_AsString(result); |
| 1585 | |
| 1586 | while (*str) |
| 1587 | { |
| 1588 | if (*str == '\n') |
| 1589 | *p = '\0'; |
| 1590 | else |
| 1591 | *p = *str; |
| 1592 | |
| 1593 | ++p; |
| 1594 | ++str; |
| 1595 | } |
| 1596 | |
| 1597 | return result; |
| 1598 | } |
| 1599 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1600 | static void DictionaryDestructor(PyObject *); |
| 1601 | static PyObject *DictionaryGetattr(PyObject *, char*); |
| 1602 | |
| 1603 | static PyMappingMethods DictionaryAsMapping = { |
| 1604 | (PyInquiry) DictionaryLength, |
| 1605 | (binaryfunc) DictionaryItem, |
| 1606 | (objobjargproc) DictionaryAssItem, |
| 1607 | }; |
| 1608 | |
| 1609 | static PyTypeObject DictionaryType = { |
| 1610 | PyObject_HEAD_INIT(0) |
| 1611 | 0, |
| 1612 | "vimdictionary", |
| 1613 | sizeof(DictionaryObject), |
| 1614 | 0, |
| 1615 | |
| 1616 | (destructor) DictionaryDestructor, |
| 1617 | (printfunc) 0, |
| 1618 | (getattrfunc) DictionaryGetattr, |
| 1619 | (setattrfunc) 0, |
| 1620 | (cmpfunc) 0, |
| 1621 | (reprfunc) 0, |
| 1622 | |
| 1623 | 0, /* as number */ |
| 1624 | 0, /* as sequence */ |
| 1625 | &DictionaryAsMapping, /* as mapping */ |
| 1626 | |
| 1627 | (hashfunc) 0, |
| 1628 | (ternaryfunc) 0, |
| 1629 | (reprfunc) 0, |
| 1630 | }; |
| 1631 | |
| 1632 | static void |
| 1633 | DictionaryDestructor(PyObject *self) |
| 1634 | { |
| 1635 | DictionaryObject *this = ((DictionaryObject *) (self)); |
| 1636 | |
| 1637 | pyll_remove(&this->ref, &lastdict); |
| 1638 | dict_unref(this->dict); |
| 1639 | |
| 1640 | Py_DECREF(self); |
| 1641 | } |
| 1642 | |
| 1643 | static PyObject * |
| 1644 | DictionaryGetattr(PyObject *self, char *name) |
| 1645 | { |
| 1646 | return Py_FindMethod(DictionaryMethods, self, name); |
| 1647 | } |
| 1648 | |
| 1649 | static void ListDestructor(PyObject *); |
| 1650 | static PyObject *ListGetattr(PyObject *, char *); |
| 1651 | |
| 1652 | static PySequenceMethods ListAsSeq = { |
| 1653 | (PyInquiry) ListLength, |
| 1654 | (binaryfunc) 0, |
| 1655 | (PyIntArgFunc) 0, |
| 1656 | (PyIntArgFunc) ListItem, |
| 1657 | (PyIntIntArgFunc) ListSlice, |
| 1658 | (PyIntObjArgProc) ListAssItem, |
| 1659 | (PyIntIntObjArgProc) ListAssSlice, |
| 1660 | (objobjproc) 0, |
| 1661 | #if PY_MAJOR_VERSION >= 2 |
| 1662 | (binaryfunc) ListConcatInPlace, |
| 1663 | 0, |
| 1664 | #endif |
| 1665 | }; |
| 1666 | |
| 1667 | static PyTypeObject ListType = { |
| 1668 | PyObject_HEAD_INIT(0) |
| 1669 | 0, |
| 1670 | "vimlist", |
| 1671 | sizeof(ListObject), |
| 1672 | 0, |
| 1673 | |
| 1674 | (destructor) ListDestructor, |
| 1675 | (printfunc) 0, |
| 1676 | (getattrfunc) ListGetattr, |
| 1677 | (setattrfunc) 0, |
| 1678 | (cmpfunc) 0, |
| 1679 | (reprfunc) 0, |
| 1680 | |
| 1681 | 0, /* as number */ |
| 1682 | &ListAsSeq, /* as sequence */ |
| 1683 | 0, /* as mapping */ |
| 1684 | |
| 1685 | (hashfunc) 0, |
| 1686 | (ternaryfunc) 0, |
| 1687 | (reprfunc) 0, |
| 1688 | }; |
| 1689 | |
| 1690 | static void |
| 1691 | ListDestructor(PyObject *self) |
| 1692 | { |
| 1693 | ListObject *this = ((ListObject *) (self)); |
| 1694 | |
| 1695 | pyll_remove(&this->ref, &lastlist); |
| 1696 | list_unref(this->list); |
| 1697 | |
| 1698 | Py_DECREF(self); |
| 1699 | } |
| 1700 | |
| 1701 | static PyObject * |
| 1702 | ListGetattr(PyObject *self, char *name) |
| 1703 | { |
| 1704 | return Py_FindMethod(ListMethods, self, name); |
| 1705 | } |
| 1706 | |
| 1707 | static void FunctionDestructor(PyObject *); |
| 1708 | static PyObject *FunctionGetattr(PyObject *, char *); |
| 1709 | |
| 1710 | static PyTypeObject FunctionType = { |
| 1711 | PyObject_HEAD_INIT(0) |
| 1712 | 0, |
| 1713 | "vimfunction", |
| 1714 | sizeof(FunctionObject), |
| 1715 | 0, |
| 1716 | |
| 1717 | (destructor) FunctionDestructor, |
| 1718 | (printfunc) 0, |
| 1719 | (getattrfunc) FunctionGetattr, |
| 1720 | (setattrfunc) 0, |
| 1721 | (cmpfunc) 0, |
| 1722 | (reprfunc) 0, |
| 1723 | |
| 1724 | 0, /* as number */ |
| 1725 | 0, /* as sequence */ |
| 1726 | 0, /* as mapping */ |
| 1727 | |
| 1728 | (hashfunc) 0, |
| 1729 | (ternaryfunc) FunctionCall, |
| 1730 | (reprfunc) 0, |
| 1731 | }; |
| 1732 | |
| 1733 | static void |
| 1734 | FunctionDestructor(PyObject *self) |
| 1735 | { |
| 1736 | FunctionObject *this = (FunctionObject *) (self); |
| 1737 | |
| 1738 | func_unref(this->name); |
| 1739 | PyMem_Del(this->name); |
| 1740 | |
| 1741 | Py_DECREF(self); |
| 1742 | } |
| 1743 | |
| 1744 | static PyObject * |
| 1745 | FunctionGetattr(PyObject *self, char *name) |
| 1746 | { |
| 1747 | FunctionObject *this = (FunctionObject *)(self); |
| 1748 | |
| 1749 | if (strcmp(name, "name") == 0) |
| 1750 | return PyString_FromString((char *)(this->name)); |
| 1751 | else |
| 1752 | return Py_FindMethod(FunctionMethods, self, name); |
| 1753 | } |
| 1754 | |
| 1755 | void |
| 1756 | do_pyeval (char_u *str, typval_T *rettv) |
| 1757 | { |
| 1758 | DoPythonCommand(NULL, (char *) str, rettv); |
| 1759 | switch(rettv->v_type) |
| 1760 | { |
| 1761 | case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; |
| 1762 | case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; |
| 1763 | case VAR_FUNC: func_ref(rettv->vval.v_string); break; |
| 1764 | } |
| 1765 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1766 | |
| 1767 | /* Don't generate a prototype for the next function, it generates an error on |
| 1768 | * newer Python versions. */ |
| 1769 | #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO) |
| 1770 | |
| 1771 | char * |
| 1772 | Py_GetProgramName(void) |
| 1773 | { |
| 1774 | return "vim"; |
| 1775 | } |
| 1776 | #endif /* Python 1.4 */ |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1777 | |
Bram Moolenaar | db91395 | 2012-06-29 12:54:53 +0200 | [diff] [blame] | 1778 | void |
| 1779 | set_ref_in_python (int copyID) |
| 1780 | { |
| 1781 | set_ref_in_py(copyID); |
| 1782 | } |
| 1783 | |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1784 | static void |
| 1785 | init_structs(void) |
| 1786 | { |
| 1787 | vim_memset(&OutputType, 0, sizeof(OutputType)); |
| 1788 | OutputType.tp_name = "message"; |
| 1789 | OutputType.tp_basicsize = sizeof(OutputObject); |
| 1790 | OutputType.tp_getattr = OutputGetattr; |
| 1791 | OutputType.tp_setattr = OutputSetattr; |
Bram Moolenaar | ca8a4df | 2010-07-31 19:54:14 +0200 | [diff] [blame] | 1792 | |
| 1793 | vim_memset(&RangeType, 0, sizeof(RangeType)); |
| 1794 | RangeType.tp_name = "range"; |
| 1795 | RangeType.tp_basicsize = sizeof(RangeObject); |
| 1796 | RangeType.tp_dealloc = RangeDestructor; |
| 1797 | RangeType.tp_getattr = RangeGetattr; |
| 1798 | RangeType.tp_repr = RangeRepr; |
| 1799 | RangeType.tp_as_sequence = &RangeAsSeq; |
Bram Moolenaar | 170bf1a | 2010-07-24 23:51:45 +0200 | [diff] [blame] | 1800 | } |