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