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