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