Mercurial > vim
annotate src/if_python.c @ 4500:47e6dec5ce3c v7.3.998
updated for version 7.3.998
Problem: Python: garbage collection issues.
Solution: Fix the GC issues: Use proper DESTRUCTOR_FINISH: avoids negative
refcounts, use PyObject_GC_* for objects with tp_traverse and
tp_clear, add RangeTraverse and RangeClear, use Py_XDECREF in some
places. (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Tue, 21 May 2013 20:51:59 +0200 |
parents | 6d517f6e5f0b |
children | ce94a870b59b |
rev | line source |
---|---|
3618 | 1 /* vi:set ts=8 sts=4 sw=4 noet: |
7 | 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 | |
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 | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2100
diff
changeset
|
29 #if defined(_WIN32) && defined(HAVE_FCNTL_H) |
7 | 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 | |
1991 | 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 | |
7 | 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 | |
3806 | 55 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
56 # define PY_SSIZE_T_CLEAN | |
57 #endif | |
58 | |
3618 | 59 #define PyBytes_FromString PyString_FromString |
4321 | 60 #define PyBytes_Check PyString_Check |
3618 | 61 |
2894 | 62 /* No-op conversion functions, use with care! */ |
63 #define PyString_AsBytes(obj) (obj) | |
64 #define PyString_FreeBytes(obj) | |
65 | |
7 | 66 #if !defined(FEAT_PYTHON) && defined(PROTO) |
67 /* Use this to be able to generate prototypes without python being used. */ | |
1607 | 68 # define PyObject Py_ssize_t |
69 # define PyThreadState Py_ssize_t | |
70 # define PyTypeObject Py_ssize_t | |
71 struct PyMethodDef { Py_ssize_t a; }; | |
72 # define PySequenceMethods Py_ssize_t | |
7 | 73 #endif |
74 | |
3638 | 75 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 |
76 # define PY_USE_CAPSULE | |
77 #endif | |
78 | |
1594 | 79 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
80 # define PyInt Py_ssize_t | |
81 # define PyInquiry lenfunc | |
82 # define PyIntArgFunc ssizeargfunc | |
83 # define PyIntIntArgFunc ssizessizeargfunc | |
84 # define PyIntObjArgProc ssizeobjargproc | |
85 # define PyIntIntObjArgProc ssizessizeobjargproc | |
1607 | 86 # define Py_ssize_t_fmt "n" |
1594 | 87 #else |
88 # define PyInt int | |
4319 | 89 # define lenfunc inquiry |
1594 | 90 # define PyInquiry inquiry |
91 # define PyIntArgFunc intargfunc | |
92 # define PyIntIntArgFunc intintargfunc | |
93 # define PyIntObjArgProc intobjargproc | |
94 # define PyIntIntObjArgProc intintobjargproc | |
1607 | 95 # define Py_ssize_t_fmt "i" |
1594 | 96 #endif |
97 | |
7 | 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 | |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
108 # if defined(DYNAMIC_PYTHON) || defined(PROTO) |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
109 # ifndef DYNAMIC_PYTHON |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
110 # define HINSTANCE long_u /* for generating prototypes */ |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
111 # endif |
7 | 112 |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
113 # ifndef WIN3264 |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
114 # include <dlfcn.h> |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
115 # define FARPROC void* |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
116 # define HINSTANCE void* |
2641 | 117 # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
118 # define load_dll(n) dlopen((n), RTLD_LAZY) |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
119 # else |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
120 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
121 # endif |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
122 # define close_dll dlclose |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
123 # define symbol_from_dll dlsym |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
124 # else |
2612 | 125 # define load_dll vimLoadLib |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
126 # define close_dll FreeLibrary |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
127 # define symbol_from_dll GetProcAddress |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
128 # endif |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
129 |
1607 | 130 /* This makes if_python.c compile without warnings against Python 2.5 |
131 * on Win32 and Win64. */ | |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
132 # undef PyRun_SimpleString |
3618 | 133 # undef PyRun_String |
2528
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
134 # undef PyArg_Parse |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
135 # undef PyArg_ParseTuple |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
136 # undef Py_BuildValue |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
137 # undef Py_InitModule4 |
8bc2e8390c11
When building with both Python 2 and Python 3 don't use RTLD_GLOBAL, so that
Bram Moolenaar <bram@vim.org>
parents:
2447
diff
changeset
|
138 # undef Py_InitModule4_64 |
3618 | 139 # undef PyObject_CallMethod |
1607 | 140 |
7 | 141 /* |
142 * Wrapper defines | |
143 */ | |
144 # define PyArg_Parse dll_PyArg_Parse | |
145 # define PyArg_ParseTuple dll_PyArg_ParseTuple | |
2894 | 146 # define PyMem_Free dll_PyMem_Free |
3618 | 147 # define PyMem_Malloc dll_PyMem_Malloc |
7 | 148 # define PyDict_SetItemString dll_PyDict_SetItemString |
149 # define PyErr_BadArgument dll_PyErr_BadArgument | |
4409 | 150 # define PyErr_NewException dll_PyErr_NewException |
7 | 151 # define PyErr_Clear dll_PyErr_Clear |
4145 | 152 # define PyErr_PrintEx dll_PyErr_PrintEx |
7 | 153 # define PyErr_NoMemory dll_PyErr_NoMemory |
154 # define PyErr_Occurred dll_PyErr_Occurred | |
155 # define PyErr_SetNone dll_PyErr_SetNone | |
156 # define PyErr_SetString dll_PyErr_SetString | |
4403 | 157 # define PyErr_SetObject dll_PyErr_SetObject |
7 | 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 | |
3618 | 167 # define PyLong_AsLong dll_PyLong_AsLong |
168 # define PyLong_FromLong dll_PyLong_FromLong | |
3828 | 169 # define PyBool_Type (*dll_PyBool_Type) |
7 | 170 # define PyInt_Type (*dll_PyInt_Type) |
3618 | 171 # define PyLong_Type (*dll_PyLong_Type) |
7 | 172 # define PyList_GetItem dll_PyList_GetItem |
637 | 173 # define PyList_Append dll_PyList_Append |
7 | 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) | |
3618 | 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) | |
7 | 184 # define PyImport_ImportModule dll_PyImport_ImportModule |
637 | 185 # define PyDict_New dll_PyDict_New |
7 | 186 # define PyDict_GetItemString dll_PyDict_GetItemString |
3618 | 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 | |
7 | 196 # define PyModule_GetDict dll_PyModule_GetDict |
197 # define PyRun_SimpleString dll_PyRun_SimpleString | |
3618 | 198 # define PyRun_String dll_PyRun_String |
4435 | 199 # define PyObject_GetAttrString dll_PyObject_GetAttrString |
200 # define PyObject_SetAttrString dll_PyObject_SetAttrString | |
201 # define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs | |
7 | 202 # define PyString_AsString dll_PyString_AsString |
3798 | 203 # define PyString_AsStringAndSize dll_PyString_AsStringAndSize |
7 | 204 # define PyString_FromString dll_PyString_FromString |
205 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize | |
206 # define PyString_Size dll_PyString_Size | |
207 # define PyString_Type (*dll_PyString_Type) | |
3618 | 208 # define PyUnicode_Type (*dll_PyUnicode_Type) |
3642 | 209 # undef PyUnicode_AsEncodedString |
210 # define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString | |
3618 | 211 # define PyFloat_AsDouble dll_PyFloat_AsDouble |
212 # define PyFloat_FromDouble dll_PyFloat_FromDouble | |
213 # define PyFloat_Type (*dll_PyFloat_Type) | |
214 # define PyImport_AddModule (*dll_PyImport_AddModule) | |
7 | 215 # define PySys_SetObject dll_PySys_SetObject |
216 # define PySys_SetArgv dll_PySys_SetArgv | |
217 # define PyType_Type (*dll_PyType_Type) | |
2737 | 218 # define PyType_Ready (*dll_PyType_Ready) |
7 | 219 # define Py_BuildValue dll_Py_BuildValue |
220 # define Py_FindMethod dll_Py_FindMethod | |
221 # define Py_InitModule4 dll_Py_InitModule4 | |
2641 | 222 # define Py_SetPythonHome dll_Py_SetPythonHome |
7 | 223 # define Py_Initialize dll_Py_Initialize |
242 | 224 # define Py_Finalize dll_Py_Finalize |
225 # define Py_IsInitialized dll_Py_IsInitialized | |
7 | 226 # define _PyObject_New dll__PyObject_New |
4500 | 227 # define _PyObject_GC_New dll__PyObject_GC_New |
228 # define PyObject_GC_Del dll_PyObject_GC_Del | |
229 # define PyObject_GC_UnTrack dll_PyObject_GC_UnTrack | |
3646 | 230 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 |
231 # define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented) | |
232 # endif | |
7 | 233 # define _Py_NoneStruct (*dll__Py_NoneStruct) |
3828 | 234 # define _Py_ZeroStruct (*dll__Py_ZeroStruct) |
235 # define _Py_TrueStruct (*dll__Py_TrueStruct) | |
7 | 236 # define PyObject_Init dll__PyObject_Init |
3618 | 237 # define PyObject_GetIter dll_PyObject_GetIter |
4399 | 238 # define PyObject_IsTrue dll_PyObject_IsTrue |
7 | 239 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
240 # define PyType_IsSubtype dll_PyType_IsSubtype | |
241 # endif | |
242 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
243 # define PyObject_Malloc dll_PyObject_Malloc | |
244 # define PyObject_Free dll_PyObject_Free | |
245 # endif | |
3638 | 246 # ifdef PY_USE_CAPSULE |
247 # define PyCapsule_New dll_PyCapsule_New | |
248 # define PyCapsule_GetPointer dll_PyCapsule_GetPointer | |
249 # else | |
250 # define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr | |
251 # define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr | |
252 # endif | |
7 | 253 |
254 /* | |
255 * Pointers for dynamic link | |
256 */ | |
257 static int(*dll_PyArg_Parse)(PyObject *, char *, ...); | |
258 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...); | |
2894 | 259 static int(*dll_PyMem_Free)(void *); |
3618 | 260 static void* (*dll_PyMem_Malloc)(size_t); |
7 | 261 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
262 static int(*dll_PyErr_BadArgument)(void); | |
4409 | 263 static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *); |
7 | 264 static void(*dll_PyErr_Clear)(void); |
4145 | 265 static void(*dll_PyErr_PrintEx)(int); |
7 | 266 static PyObject*(*dll_PyErr_NoMemory)(void); |
267 static PyObject*(*dll_PyErr_Occurred)(void); | |
268 static void(*dll_PyErr_SetNone)(PyObject *); | |
269 static void(*dll_PyErr_SetString)(PyObject *, const char *); | |
4403 | 270 static void(*dll_PyErr_SetObject)(PyObject *, PyObject *); |
7 | 271 static void(*dll_PyEval_InitThreads)(void); |
272 static void(*dll_PyEval_RestoreThread)(PyThreadState *); | |
273 static PyThreadState*(*dll_PyEval_SaveThread)(void); | |
274 # ifdef PY_CAN_RECURSE | |
275 static PyGILState_STATE (*dll_PyGILState_Ensure)(void); | |
276 static void (*dll_PyGILState_Release)(PyGILState_STATE); | |
3618 | 277 # endif |
7 | 278 static long(*dll_PyInt_AsLong)(PyObject *); |
279 static PyObject*(*dll_PyInt_FromLong)(long); | |
3618 | 280 static long(*dll_PyLong_AsLong)(PyObject *); |
281 static PyObject*(*dll_PyLong_FromLong)(long); | |
3828 | 282 static PyTypeObject* dll_PyBool_Type; |
7 | 283 static PyTypeObject* dll_PyInt_Type; |
3618 | 284 static PyTypeObject* dll_PyLong_Type; |
1594 | 285 static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt); |
637 | 286 static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *); |
1594 | 287 static PyObject*(*dll_PyList_New)(PyInt size); |
288 static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *); | |
289 static PyInt(*dll_PyList_Size)(PyObject *); | |
7 | 290 static PyTypeObject* dll_PyList_Type; |
3618 | 291 static int (*dll_PySequence_Check)(PyObject *); |
292 static PyInt(*dll_PySequence_Size)(PyObject *); | |
293 static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt); | |
294 static PyInt(*dll_PyTuple_Size)(PyObject *); | |
295 static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt); | |
296 static PyTypeObject* dll_PyTuple_Type; | |
7 | 297 static PyObject*(*dll_PyImport_ImportModule)(const char *); |
637 | 298 static PyObject*(*dll_PyDict_New)(void); |
7 | 299 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *); |
3618 | 300 static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); |
301 # ifndef PY_NO_MAPPING_ITEMS | |
302 static PyObject* (*dll_PyMapping_Items)(PyObject *); | |
303 # endif | |
304 static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *); | |
305 static int (*dll_PyMapping_Check)(PyObject *); | |
306 static PyObject* (*dll_PyIter_Next)(PyObject *); | |
7 | 307 static PyObject*(*dll_PyModule_GetDict)(PyObject *); |
308 static int(*dll_PyRun_SimpleString)(char *); | |
3618 | 309 static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *); |
4435 | 310 static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *); |
311 static PyObject* (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *); | |
312 static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...); | |
7 | 313 static char*(*dll_PyString_AsString)(PyObject *); |
3798 | 314 static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *); |
7 | 315 static PyObject*(*dll_PyString_FromString)(const char *); |
1594 | 316 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt); |
317 static PyInt(*dll_PyString_Size)(PyObject *); | |
7 | 318 static PyTypeObject* dll_PyString_Type; |
3618 | 319 static PyTypeObject* dll_PyUnicode_Type; |
3642 | 320 static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *); |
3618 | 321 static double(*dll_PyFloat_AsDouble)(PyObject *); |
322 static PyObject*(*dll_PyFloat_FromDouble)(double); | |
323 static PyTypeObject* dll_PyFloat_Type; | |
7 | 324 static int(*dll_PySys_SetObject)(char *, PyObject *); |
325 static int(*dll_PySys_SetArgv)(int, char **); | |
326 static PyTypeObject* dll_PyType_Type; | |
2737 | 327 static int (*dll_PyType_Ready)(PyTypeObject *type); |
7 | 328 static PyObject*(*dll_Py_BuildValue)(char *, ...); |
329 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *); | |
330 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int); | |
3618 | 331 static PyObject*(*dll_PyImport_AddModule)(char *); |
2641 | 332 static void(*dll_Py_SetPythonHome)(char *home); |
7 | 333 static void(*dll_Py_Initialize)(void); |
242 | 334 static void(*dll_Py_Finalize)(void); |
335 static int(*dll_Py_IsInitialized)(void); | |
7 | 336 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *); |
4500 | 337 static PyObject*(*dll__PyObject_GC_New)(PyTypeObject *); |
338 static void(*dll_PyObject_GC_Del)(void *); | |
339 static void(*dll_PyObject_GC_UnTrack)(void *); | |
7 | 340 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *); |
3618 | 341 static PyObject* (*dll_PyObject_GetIter)(PyObject *); |
4399 | 342 static int (*dll_PyObject_IsTrue)(PyObject *); |
3646 | 343 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 |
3618 | 344 static iternextfunc dll__PyObject_NextNotImplemented; |
3646 | 345 # endif |
7 | 346 static PyObject* dll__Py_NoneStruct; |
3828 | 347 static PyObject* _Py_ZeroStruct; |
348 static PyObject* dll__Py_TrueStruct; | |
7 | 349 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
350 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); | |
351 # endif | |
352 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
353 static void* (*dll_PyObject_Malloc)(size_t); | |
354 static void (*dll_PyObject_Free)(void*); | |
355 # endif | |
3638 | 356 # ifdef PY_USE_CAPSULE |
3618 | 357 static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor); |
358 static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *); | |
3638 | 359 # else |
3648 | 360 static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *)); |
361 static void* (*dll_PyCObject_AsVoidPtr)(PyObject *); | |
3638 | 362 # endif |
7 | 363 |
364 static HINSTANCE hinstPython = 0; /* Instance of python.dll */ | |
365 | |
366 /* Imported exception objects */ | |
367 static PyObject *imp_PyExc_AttributeError; | |
368 static PyObject *imp_PyExc_IndexError; | |
4315 | 369 static PyObject *imp_PyExc_KeyError; |
7 | 370 static PyObject *imp_PyExc_KeyboardInterrupt; |
371 static PyObject *imp_PyExc_TypeError; | |
372 static PyObject *imp_PyExc_ValueError; | |
4405 | 373 static PyObject *imp_PyExc_RuntimeError; |
7 | 374 |
375 # define PyExc_AttributeError imp_PyExc_AttributeError | |
376 # define PyExc_IndexError imp_PyExc_IndexError | |
4315 | 377 # define PyExc_KeyError imp_PyExc_KeyError |
7 | 378 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt |
379 # define PyExc_TypeError imp_PyExc_TypeError | |
380 # define PyExc_ValueError imp_PyExc_ValueError | |
4405 | 381 # define PyExc_RuntimeError imp_PyExc_RuntimeError |
7 | 382 |
383 /* | |
384 * Table of name to function pointer of python. | |
385 */ | |
386 # define PYTHON_PROC FARPROC | |
387 static struct | |
388 { | |
389 char *name; | |
390 PYTHON_PROC *ptr; | |
391 } python_funcname_table[] = | |
392 { | |
3806 | 393 #ifndef PY_SSIZE_T_CLEAN |
7 | 394 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse}, |
395 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple}, | |
3806 | 396 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue}, |
397 #else | |
398 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse}, | |
399 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple}, | |
400 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue}, | |
401 #endif | |
2894 | 402 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free}, |
3618 | 403 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc}, |
7 | 404 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString}, |
405 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument}, | |
4409 | 406 {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException}, |
7 | 407 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear}, |
4145 | 408 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx}, |
7 | 409 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory}, |
410 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred}, | |
411 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone}, | |
412 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString}, | |
4403 | 413 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject}, |
7 | 414 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads}, |
415 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread}, | |
416 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread}, | |
417 # ifdef PY_CAN_RECURSE | |
418 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure}, | |
419 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release}, | |
420 # endif | |
421 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong}, | |
422 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong}, | |
3618 | 423 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong}, |
424 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong}, | |
3828 | 425 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type}, |
7 | 426 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type}, |
3618 | 427 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type}, |
7 | 428 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem}, |
637 | 429 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append}, |
7 | 430 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New}, |
431 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem}, | |
432 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size}, | |
433 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type}, | |
3618 | 434 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem}, |
435 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size}, | |
436 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check}, | |
437 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem}, | |
438 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size}, | |
439 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type}, | |
7 | 440 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule}, |
441 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString}, | |
3618 | 442 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next}, |
637 | 443 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New}, |
3618 | 444 # ifndef PY_NO_MAPPING_ITEMS |
445 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items}, | |
446 # endif | |
447 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod}, | |
448 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check}, | |
449 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next}, | |
7 | 450 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict}, |
451 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString}, | |
3618 | 452 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String}, |
4435 | 453 {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString}, |
454 {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString}, | |
455 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs}, | |
7 | 456 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString}, |
3798 | 457 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize}, |
7 | 458 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString}, |
459 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize}, | |
460 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size}, | |
461 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type}, | |
3618 | 462 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type}, |
463 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type}, | |
464 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble}, | |
465 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble}, | |
466 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule}, | |
7 | 467 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject}, |
468 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv}, | |
469 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type}, | |
2737 | 470 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready}, |
7 | 471 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod}, |
3638 | 472 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \ |
473 && SIZEOF_SIZE_T != SIZEOF_INT | |
1607 | 474 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4}, |
475 # else | |
7 | 476 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4}, |
1607 | 477 # endif |
2641 | 478 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome}, |
7 | 479 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize}, |
242 | 480 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize}, |
481 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized}, | |
7 | 482 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New}, |
4500 | 483 {"_PyObject_GC_New", (PYTHON_PROC*)&dll__PyObject_GC_New}, |
484 {"PyObject_GC_Del", (PYTHON_PROC*)&dll_PyObject_GC_Del}, | |
485 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&dll_PyObject_GC_UnTrack}, | |
7 | 486 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init}, |
3618 | 487 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter}, |
4399 | 488 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue}, |
3646 | 489 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 |
3618 | 490 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented}, |
3646 | 491 # endif |
7 | 492 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct}, |
3828 | 493 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct}, |
494 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct}, | |
7 | 495 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
496 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype}, | |
497 # endif | |
498 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
499 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc}, | |
500 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free}, | |
501 # endif | |
3638 | 502 # ifdef PY_USE_CAPSULE |
3618 | 503 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New}, |
504 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer}, | |
3638 | 505 # else |
506 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr}, | |
507 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr}, | |
508 # endif | |
7 | 509 {"", NULL}, |
510 }; | |
511 | |
512 /* | |
513 * Free python.dll | |
514 */ | |
515 static void | |
516 end_dynamic_python(void) | |
517 { | |
518 if (hinstPython) | |
519 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
520 close_dll(hinstPython); |
7 | 521 hinstPython = 0; |
522 } | |
523 } | |
524 | |
525 /* | |
526 * Load library and get all pointers. | |
527 * Parameter 'libname' provides name of DLL. | |
528 * Return OK or FAIL. | |
529 */ | |
530 static int | |
531 python_runtime_link_init(char *libname, int verbose) | |
532 { | |
533 int i; | |
3642 | 534 void *ucs_as_encoded_string; |
7 | 535 |
2641 | 536 #if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3) |
2554
7abef60aca22
Add a configure check for RTLD_GLOBAL. (James Vega, Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2528
diff
changeset
|
537 /* Can't have Python and Python3 loaded at the same time. |
7abef60aca22
Add a configure check for RTLD_GLOBAL. (James Vega, Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2528
diff
changeset
|
538 * It cause a crash, because RTLD_GLOBAL is needed for |
7abef60aca22
Add a configure check for RTLD_GLOBAL. (James Vega, Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2528
diff
changeset
|
539 * standard C extension libraries of one or both python versions. */ |
2384
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
540 if (python3_loaded()) |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
541 { |
3032 | 542 if (verbose) |
543 EMSG(_("E836: This Vim cannot execute :python after using :py3")); | |
2384
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
544 return FAIL; |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
545 } |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
546 #endif |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
547 |
7 | 548 if (hinstPython) |
549 return OK; | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
550 hinstPython = load_dll(libname); |
7 | 551 if (!hinstPython) |
552 { | |
553 if (verbose) | |
554 EMSG2(_(e_loadlib), libname); | |
555 return FAIL; | |
556 } | |
557 | |
558 for (i = 0; python_funcname_table[i].ptr; ++i) | |
559 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
560 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, |
7 | 561 python_funcname_table[i].name)) == NULL) |
562 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
563 close_dll(hinstPython); |
7 | 564 hinstPython = 0; |
565 if (verbose) | |
566 EMSG2(_(e_loadfunc), python_funcname_table[i].name); | |
567 return FAIL; | |
568 } | |
569 } | |
3642 | 570 |
571 /* Load unicode functions separately as only the ucs2 or the ucs4 functions | |
572 * will be present in the library. */ | |
573 ucs_as_encoded_string = symbol_from_dll(hinstPython, | |
574 "PyUnicodeUCS2_AsEncodedString"); | |
575 if (ucs_as_encoded_string == NULL) | |
576 ucs_as_encoded_string = symbol_from_dll(hinstPython, | |
577 "PyUnicodeUCS4_AsEncodedString"); | |
578 if (ucs_as_encoded_string != NULL) | |
579 py_PyUnicode_AsEncodedString = ucs_as_encoded_string; | |
580 else | |
581 { | |
582 close_dll(hinstPython); | |
583 hinstPython = 0; | |
584 if (verbose) | |
585 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); | |
586 return FAIL; | |
587 } | |
588 | |
7 | 589 return OK; |
590 } | |
591 | |
592 /* | |
593 * If python is enabled (there is installed python on Windows system) return | |
594 * TRUE, else FALSE. | |
595 */ | |
596 int | |
1607 | 597 python_enabled(int verbose) |
7 | 598 { |
599 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK; | |
600 } | |
601 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
602 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
603 * Load the standard Python exceptions - don't import the symbols from the |
7 | 604 * DLL, as this can cause errors (importing data symbols is not reliable). |
605 */ | |
606 static void | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
607 get_exceptions(void) |
7 | 608 { |
609 PyObject *exmod = PyImport_ImportModule("exceptions"); | |
610 PyObject *exdict = PyModule_GetDict(exmod); | |
611 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); | |
612 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); | |
4315 | 613 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError"); |
7 | 614 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); |
615 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); | |
616 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); | |
4405 | 617 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError"); |
7 | 618 Py_XINCREF(imp_PyExc_AttributeError); |
619 Py_XINCREF(imp_PyExc_IndexError); | |
4315 | 620 Py_XINCREF(imp_PyExc_KeyError); |
7 | 621 Py_XINCREF(imp_PyExc_KeyboardInterrupt); |
622 Py_XINCREF(imp_PyExc_TypeError); | |
623 Py_XINCREF(imp_PyExc_ValueError); | |
4405 | 624 Py_XINCREF(imp_PyExc_RuntimeError); |
7 | 625 Py_XDECREF(exmod); |
626 } | |
627 #endif /* DYNAMIC_PYTHON */ | |
628 | |
3618 | 629 static int initialised = 0; |
630 #define PYINITIALISED initialised | |
631 | |
632 #define DICTKEY_GET(err) \ | |
633 if (!PyString_Check(keyObject)) \ | |
634 { \ | |
635 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \ | |
636 return err; \ | |
637 } \ | |
3798 | 638 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \ |
639 return err; | |
640 | |
3618 | 641 #define DICTKEY_UNREF |
642 #define DICTKEY_DECL | |
643 | |
4500 | 644 #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self); |
4319 | 645 |
4377 | 646 #define WIN_PYTHON_REF(win) win->w_python_ref |
647 #define BUF_PYTHON_REF(buf) buf->b_python_ref | |
4401 | 648 #define TAB_PYTHON_REF(tab) tab->tp_python_ref |
4377 | 649 |
4319 | 650 static PyObject *OutputGetattr(PyObject *, char *); |
651 static PyObject *BufferGetattr(PyObject *, char *); | |
652 static PyObject *WindowGetattr(PyObject *, char *); | |
4401 | 653 static PyObject *TabPageGetattr(PyObject *, char *); |
4319 | 654 static PyObject *RangeGetattr(PyObject *, char *); |
655 static PyObject *DictionaryGetattr(PyObject *, char*); | |
656 static PyObject *ListGetattr(PyObject *, char *); | |
657 static PyObject *FunctionGetattr(PyObject *, char *); | |
658 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
659 /* |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
660 * Include the code shared with if_python3.c |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
661 */ |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
662 #include "if_py_both.h" |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
663 |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
664 |
7 | 665 /****************************************************** |
666 * Internal function prototypes. | |
667 */ | |
668 | |
669 static int PythonMod_Init(void); | |
670 | |
671 | |
672 /****************************************************** | |
673 * 1. Python interpreter main program. | |
674 */ | |
675 | |
676 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ | |
677 typedef PyObject PyThreadState; | |
323 | 678 #endif |
679 | |
4415 | 680 #ifndef PY_CAN_RECURSE |
36 | 681 static PyThreadState *saved_python_thread = NULL; |
7 | 682 |
683 /* | |
684 * Suspend a thread of the Python interpreter, other threads are allowed to | |
685 * run. | |
686 */ | |
36 | 687 static void |
688 Python_SaveThread(void) | |
7 | 689 { |
690 saved_python_thread = PyEval_SaveThread(); | |
691 } | |
692 | |
693 /* | |
694 * Restore a thread of the Python interpreter, waits for other threads to | |
695 * block. | |
696 */ | |
36 | 697 static void |
698 Python_RestoreThread(void) | |
7 | 699 { |
700 PyEval_RestoreThread(saved_python_thread); | |
701 saved_python_thread = NULL; | |
4415 | 702 } |
323 | 703 #endif |
7 | 704 |
705 void | |
706 python_end() | |
707 { | |
557 | 708 static int recurse = 0; |
709 | |
710 /* If a crash occurs while doing this, don't try again. */ | |
711 if (recurse != 0) | |
712 return; | |
713 | |
714 ++recurse; | |
715 | |
7 | 716 #ifdef DYNAMIC_PYTHON |
242 | 717 if (hinstPython && Py_IsInitialized()) |
323 | 718 { |
4415 | 719 # ifdef PY_CAN_RECURSE |
720 PyGILState_Ensure(); | |
721 # else | |
856 | 722 Python_RestoreThread(); /* enter python */ |
4415 | 723 # endif |
856 | 724 Py_Finalize(); |
323 | 725 } |
7 | 726 end_dynamic_python(); |
242 | 727 #else |
728 if (Py_IsInitialized()) | |
323 | 729 { |
4415 | 730 # ifdef PY_CAN_RECURSE |
731 PyGILState_Ensure(); | |
732 # else | |
856 | 733 Python_RestoreThread(); /* enter python */ |
4415 | 734 # endif |
856 | 735 Py_Finalize(); |
323 | 736 } |
7 | 737 #endif |
557 | 738 |
739 --recurse; | |
7 | 740 } |
741 | |
2384
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
742 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO) |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
743 int |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
744 python_loaded() |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
745 { |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
746 return (hinstPython != 0); |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
747 } |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
748 #endif |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
749 |
7 | 750 static int |
751 Python_Init(void) | |
752 { | |
753 if (!initialised) | |
754 { | |
755 #ifdef DYNAMIC_PYTHON | |
756 if (!python_enabled(TRUE)) | |
757 { | |
758 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); | |
759 goto fail; | |
760 } | |
761 #endif | |
762 | |
2641 | 763 #ifdef PYTHON_HOME |
764 Py_SetPythonHome(PYTHON_HOME); | |
765 #endif | |
766 | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
767 init_structs(); |
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
768 |
7 | 769 #if !defined(MACOS) || defined(MACOS_X_UNIX) |
770 Py_Initialize(); | |
771 #else | |
772 PyMac_Initialize(); | |
773 #endif | |
4063 | 774 /* Initialise threads, and below save the state using |
4109 | 775 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread |
4063 | 776 * specific state (such as the system trace hook), will be lost |
777 * between invocations of Python code. */ | |
7 | 778 PyEval_InitThreads(); |
779 #ifdef DYNAMIC_PYTHON | |
780 get_exceptions(); | |
781 #endif | |
782 | |
4494 | 783 if (PythonIO_Init_io()) |
7 | 784 goto fail; |
785 | |
786 if (PythonMod_Init()) | |
787 goto fail; | |
788 | |
3618 | 789 globals = PyModule_GetDict(PyImport_AddModule("__main__")); |
790 | |
1748 | 791 /* Remove the element from sys.path that was added because of our |
792 * argv[0] value in PythonMod_Init(). Previously we used an empty | |
4352 | 793 * string, but depending on the OS we then get an empty entry or |
1748 | 794 * the current directory in sys.path. */ |
795 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); | |
796 | |
4109 | 797 /* lock is created and acquired in PyEval_InitThreads() and thread |
798 * state is created in Py_Initialize() | |
799 * there _PyGILState_NoteThreadState() also sets gilcounter to 1 | |
800 * (python must have threads enabled!) | |
801 * so the following does both: unlock GIL and save thread state in TLS | |
802 * without deleting thread state | |
803 */ | |
4399 | 804 #ifndef PY_CAN_RECURSE |
805 saved_python_thread = | |
806 #endif | |
807 PyEval_SaveThread(); | |
7 | 808 |
809 initialised = 1; | |
810 } | |
811 | |
812 return 0; | |
813 | |
814 fail: | |
815 /* We call PythonIO_Flush() here to print any Python errors. | |
816 * This is OK, as it is possible to call this function even | |
4494 | 817 * if PythonIO_Init_io() has not completed successfully (it will |
7 | 818 * not do anything in this case). |
819 */ | |
820 PythonIO_Flush(); | |
821 return -1; | |
822 } | |
823 | |
824 /* | |
825 * External interface | |
826 */ | |
827 static void | |
4486 | 828 DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) |
7 | 829 { |
323 | 830 #ifndef PY_CAN_RECURSE |
7 | 831 static int recursive = 0; |
832 #endif | |
833 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
834 GrafPtr oldPort; | |
835 #endif | |
836 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
837 char *saved_locale; | |
838 #endif | |
4415 | 839 #ifdef PY_CAN_RECURSE |
840 PyGILState_STATE pygilstate; | |
841 #endif | |
7 | 842 |
843 #ifndef PY_CAN_RECURSE | |
844 if (recursive) | |
845 { | |
846 EMSG(_("E659: Cannot invoke Python recursively")); | |
847 return; | |
848 } | |
849 ++recursive; | |
850 #endif | |
851 | |
852 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
853 GetPort(&oldPort); | |
854 /* Check if the Python library is available */ | |
855 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) | |
856 goto theend; | |
857 #endif | |
858 if (Python_Init()) | |
859 goto theend; | |
860 | |
4486 | 861 init_range(arg); |
862 | |
7 | 863 Python_Release_Vim(); /* leave vim */ |
864 | |
865 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
866 /* Python only works properly when the LC_NUMERIC locale is "C". */ | |
867 saved_locale = setlocale(LC_NUMERIC, NULL); | |
868 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) | |
869 saved_locale = NULL; | |
870 else | |
871 { | |
872 /* Need to make a copy, value may change when setting new locale. */ | |
873 saved_locale = (char *)vim_strsave((char_u *)saved_locale); | |
874 (void)setlocale(LC_NUMERIC, "C"); | |
875 } | |
876 #endif | |
877 | |
4415 | 878 #ifdef PY_CAN_RECURSE |
879 pygilstate = PyGILState_Ensure(); | |
880 #else | |
7 | 881 Python_RestoreThread(); /* enter python */ |
4415 | 882 #endif |
7 | 883 |
4486 | 884 run((char *) cmd, arg, &pygilstate); |
7 | 885 |
4415 | 886 #ifdef PY_CAN_RECURSE |
887 PyGILState_Release(pygilstate); | |
888 #else | |
7 | 889 Python_SaveThread(); /* leave python */ |
4415 | 890 #endif |
7 | 891 |
892 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
893 if (saved_locale != NULL) | |
894 { | |
895 (void)setlocale(LC_NUMERIC, saved_locale); | |
896 vim_free(saved_locale); | |
897 } | |
898 #endif | |
899 | |
900 Python_Lock_Vim(); /* enter vim */ | |
901 PythonIO_Flush(); | |
902 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
903 SetPort(oldPort); | |
904 #endif | |
905 | |
906 theend: | |
907 #ifndef PY_CAN_RECURSE | |
908 --recursive; | |
909 #endif | |
3618 | 910 return; |
7 | 911 } |
912 | |
913 /* | |
914 * ":python" | |
915 */ | |
916 void | |
917 ex_python(exarg_T *eap) | |
918 { | |
919 char_u *script; | |
920 | |
921 script = script_get(eap, eap->arg); | |
922 if (!eap->skip) | |
923 { | |
4486 | 924 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script, |
925 (rangeinitializer) init_range_cmd, | |
926 (runner) run_cmd, | |
927 (void *) eap); | |
7 | 928 } |
929 vim_free(script); | |
930 } | |
931 | |
932 #define BUFFER_SIZE 1024 | |
933 | |
934 /* | |
935 * ":pyfile" | |
936 */ | |
937 void | |
938 ex_pyfile(exarg_T *eap) | |
939 { | |
940 static char buffer[BUFFER_SIZE]; | |
941 const char *file = (char *)eap->arg; | |
942 char *p; | |
943 | |
944 /* Have to do it like this. PyRun_SimpleFile requires you to pass a | |
945 * stdio file pointer, but Vim and the Python DLL are compiled with | |
946 * different options under Windows, meaning that stdio pointers aren't | |
947 * compatible between the two. Yuk. | |
948 * | |
949 * Put the string "execfile('file')" into buffer. But, we need to | |
950 * escape any backslashes or single quotes in the file name, so that | |
951 * Python won't mangle the file name. | |
952 */ | |
953 strcpy(buffer, "execfile('"); | |
954 p = buffer + 10; /* size of "execfile('" */ | |
955 | |
956 while (*file && p < buffer + (BUFFER_SIZE - 3)) | |
957 { | |
958 if (*file == '\\' || *file == '\'') | |
959 *p++ = '\\'; | |
960 *p++ = *file++; | |
961 } | |
962 | |
963 /* If we didn't finish the file name, we hit a buffer overflow */ | |
964 if (*file != '\0') | |
965 return; | |
966 | |
967 /* Put in the terminating "')" and a null */ | |
968 *p++ = '\''; | |
969 *p++ = ')'; | |
970 *p++ = '\0'; | |
971 | |
972 /* Execute the file */ | |
4486 | 973 DoPyCommand(buffer, |
974 (rangeinitializer) init_range_cmd, | |
975 (runner) run_cmd, | |
976 (void *) eap); | |
7 | 977 } |
978 | |
4435 | 979 void |
980 ex_pydo(exarg_T *eap) | |
981 { | |
4486 | 982 DoPyCommand((char *)eap->arg, |
983 (rangeinitializer) init_range_cmd, | |
984 (runner)run_do, | |
985 (void *)eap); | |
4435 | 986 } |
987 | |
7 | 988 /****************************************************** |
989 * 2. Python output stream: writes output via [e]msg(). | |
990 */ | |
991 | |
992 /* Implementation functions | |
993 */ | |
994 | |
995 static PyObject * | |
996 OutputGetattr(PyObject *self, char *name) | |
997 { | |
998 if (strcmp(name, "softspace") == 0) | |
999 return PyInt_FromLong(((OutputObject *)(self))->softspace); | |
1000 | |
1001 return Py_FindMethod(OutputMethods, self, name); | |
1002 } | |
1003 | |
1004 /****************************************************** | |
1005 * 3. Implementation of the Vim module for Python | |
1006 */ | |
1007 | |
1008 /* Window type - Implementation functions | |
1009 * -------------------------------------- | |
1010 */ | |
1011 | |
1012 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType) | |
1013 | |
1014 /* Buffer type - Implementation functions | |
1015 * -------------------------------------- | |
1016 */ | |
1017 | |
1018 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType) | |
1019 | |
1594 | 1020 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *); |
1021 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *); | |
7 | 1022 |
1023 /* Line range type - Implementation functions | |
1024 * -------------------------------------- | |
1025 */ | |
1026 | |
1027 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType) | |
1028 | |
1594 | 1029 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *); |
1030 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *); | |
7 | 1031 |
1032 /* Current objects type - Implementation functions | |
1033 * ----------------------------------------------- | |
1034 */ | |
1035 | |
1036 static PySequenceMethods BufferAsSeq = { | |
1594 | 1037 (PyInquiry) BufferLength, /* sq_length, len(x) */ |
3796 | 1038 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */ |
1039 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */ | |
1594 | 1040 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */ |
1041 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */ | |
1042 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */ | |
4319 | 1043 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */ |
1044 (objobjproc) 0, | |
1045 #if PY_MAJOR_VERSION >= 2 | |
1046 (binaryfunc) 0, | |
7 | 1047 0, |
4319 | 1048 #endif |
7 | 1049 }; |
1050 | |
1051 /* Buffer object - Implementation | |
1052 */ | |
1053 | |
1054 static PyObject * | |
1055 BufferGetattr(PyObject *self, char *name) | |
1056 { | |
4319 | 1057 PyObject *r; |
7 | 1058 |
4319 | 1059 if (CheckBuffer((BufferObject *)(self))) |
7 | 1060 return NULL; |
1061 | |
4319 | 1062 r = BufferAttr((BufferObject *)(self), name); |
1063 if (r || PyErr_Occurred()) | |
1064 return r; | |
7 | 1065 else |
1066 return Py_FindMethod(BufferMethods, self, name); | |
1067 } | |
1068 | |
1069 /******************/ | |
1070 | |
1594 | 1071 static PyInt |
1072 BufferAssItem(PyObject *self, PyInt n, PyObject *val) | |
7 | 1073 { |
4387 | 1074 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL); |
7 | 1075 } |
1076 | |
1594 | 1077 static PyInt |
1078 BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) | |
7 | 1079 { |
4387 | 1080 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL); |
7 | 1081 } |
1082 | |
1083 static PySequenceMethods RangeAsSeq = { | |
4319 | 1084 (PyInquiry) RangeLength, /* sq_length, len(x) */ |
1085 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */ | |
1086 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */ | |
1087 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */ | |
1088 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */ | |
1089 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */ | |
1090 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */ | |
1091 (objobjproc) 0, | |
1092 #if PY_MAJOR_VERSION >= 2 | |
1093 (binaryfunc) 0, | |
1094 0, | |
1095 #endif | |
7 | 1096 }; |
1097 | |
1098 /* Line range object - Implementation | |
1099 */ | |
1100 | |
1101 static PyObject * | |
1102 RangeGetattr(PyObject *self, char *name) | |
1103 { | |
1104 if (strcmp(name, "start") == 0) | |
1607 | 1105 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1); |
7 | 1106 else if (strcmp(name, "end") == 0) |
1607 | 1107 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1); |
7 | 1108 else |
1109 return Py_FindMethod(RangeMethods, self, name); | |
1110 } | |
1111 | |
1112 /****************/ | |
1113 | |
1594 | 1114 static PyInt |
1115 RangeAssItem(PyObject *self, PyInt n, PyObject *val) | |
7 | 1116 { |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1117 return RBAsItem(((RangeObject *)(self))->buf, n, val, |
7 | 1118 ((RangeObject *)(self))->start, |
1119 ((RangeObject *)(self))->end, | |
1120 &((RangeObject *)(self))->end); | |
1121 } | |
1122 | |
1594 | 1123 static PyInt |
1124 RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) | |
7 | 1125 { |
2894 | 1126 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
7 | 1127 ((RangeObject *)(self))->start, |
1128 ((RangeObject *)(self))->end, | |
1129 &((RangeObject *)(self))->end); | |
1130 } | |
1131 | |
4401 | 1132 /* TabPage object - Implementation |
1133 */ | |
1134 | |
1135 static PyObject * | |
1136 TabPageGetattr(PyObject *self, char *name) | |
1137 { | |
1138 PyObject *r; | |
1139 | |
1140 if (CheckTabPage((TabPageObject *)(self))) | |
1141 return NULL; | |
1142 | |
1143 r = TabPageAttr((TabPageObject *)(self), name); | |
1144 if (r || PyErr_Occurred()) | |
1145 return r; | |
1146 else | |
1147 return Py_FindMethod(TabPageMethods, self, name); | |
1148 } | |
1149 | |
7 | 1150 /* Window object - Implementation |
1151 */ | |
1152 | |
1153 static PyObject * | |
1154 WindowGetattr(PyObject *self, char *name) | |
1155 { | |
4319 | 1156 PyObject *r; |
7 | 1157 |
4319 | 1158 if (CheckWindow((WindowObject *)(self))) |
7 | 1159 return NULL; |
1160 | |
4319 | 1161 r = WindowAttr((WindowObject *)(self), name); |
1162 if (r || PyErr_Occurred()) | |
1163 return r; | |
7 | 1164 else |
1165 return Py_FindMethod(WindowMethods, self, name); | |
1166 } | |
1167 | |
4401 | 1168 /* Tab page list object - Definitions |
1169 */ | |
1170 | |
1171 static PySequenceMethods TabListAsSeq = { | |
1172 (PyInquiry) TabListLength, /* sq_length, len(x) */ | |
1173 (binaryfunc) 0, /* sq_concat, x+y */ | |
1174 (PyIntArgFunc) 0, /* sq_repeat, x*n */ | |
1175 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */ | |
1176 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ | |
1177 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ | |
1178 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ | |
1179 (objobjproc) 0, | |
1180 #if PY_MAJOR_VERSION >= 2 | |
1181 (binaryfunc) 0, | |
1182 0, | |
1183 #endif | |
1184 }; | |
1185 | |
7 | 1186 /* Window list object - Definitions |
1187 */ | |
1188 | |
1189 static PySequenceMethods WinListAsSeq = { | |
1594 | 1190 (PyInquiry) WinListLength, /* sq_length, len(x) */ |
7 | 1191 (binaryfunc) 0, /* sq_concat, x+y */ |
1594 | 1192 (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
1193 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */ | |
1194 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ | |
1195 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ | |
4319 | 1196 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ |
1197 (objobjproc) 0, | |
1198 #if PY_MAJOR_VERSION >= 2 | |
1199 (binaryfunc) 0, | |
7 | 1200 0, |
4319 | 1201 #endif |
7 | 1202 }; |
1203 | |
1204 /* External interface | |
1205 */ | |
1206 | |
1207 void | |
1208 python_buffer_free(buf_T *buf) | |
1209 { | |
4377 | 1210 if (BUF_PYTHON_REF(buf) != NULL) |
7 | 1211 { |
4377 | 1212 BufferObject *bp = BUF_PYTHON_REF(buf); |
7 | 1213 bp->buf = INVALID_BUFFER_VALUE; |
4377 | 1214 BUF_PYTHON_REF(buf) = NULL; |
7 | 1215 } |
1216 } | |
1217 | |
1218 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
1219 void | |
1220 python_window_free(win_T *win) | |
1221 { | |
4377 | 1222 if (WIN_PYTHON_REF(win) != NULL) |
7 | 1223 { |
4377 | 1224 WindowObject *wp = WIN_PYTHON_REF(win); |
7 | 1225 wp->win = INVALID_WINDOW_VALUE; |
4377 | 1226 WIN_PYTHON_REF(win) = NULL; |
7 | 1227 } |
1228 } | |
4401 | 1229 |
1230 void | |
1231 python_tabpage_free(tabpage_T *tab) | |
1232 { | |
1233 if (TAB_PYTHON_REF(tab) != NULL) | |
1234 { | |
1235 TabPageObject *tp = TAB_PYTHON_REF(tab); | |
1236 tp->tab = INVALID_TABPAGE_VALUE; | |
1237 TAB_PYTHON_REF(tab) = NULL; | |
1238 } | |
1239 } | |
7 | 1240 #endif |
1241 | |
4494 | 1242 static int |
1243 add_object(PyObject *dict, const char *name, PyObject *object) | |
7 | 1244 { |
4494 | 1245 if (PyDict_SetItemString(dict, (char *) name, object)) |
1246 return -1; | |
1247 Py_DECREF(object); | |
1248 return 0; | |
1249 } | |
4401 | 1250 |
7 | 1251 static int |
1252 PythonMod_Init(void) | |
1253 { | |
1254 PyObject *mod; | |
1255 PyObject *dict; | |
4494 | 1256 |
1748 | 1257 /* The special value is removed from sys.path in Python_Init(). */ |
1258 static char *(argv[2]) = {"/must>not&exist/foo", NULL}; | |
7 | 1259 |
4494 | 1260 if (init_types()) |
1261 return -1; | |
7 | 1262 |
1263 /* Set sys.argv[] to avoid a crash in warn(). */ | |
1264 PySys_SetArgv(1, argv); | |
1265 | |
1607 | 1266 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION); |
7 | 1267 dict = PyModule_GetDict(mod); |
1268 | |
4494 | 1269 return populate_module(dict, add_object); |
7 | 1270 } |
1271 | |
1272 /************************************************************************* | |
1273 * 4. Utility functions for handling the interface between Vim and Python. | |
1274 */ | |
1275 | |
1276 /* Convert a Vim line into a Python string. | |
1277 * All internal newlines are replaced by null characters. | |
1278 * | |
1279 * On errors, the Python exception data is set, and NULL is returned. | |
1280 */ | |
1281 static PyObject * | |
1282 LineToString(const char *str) | |
1283 { | |
1284 PyObject *result; | |
1594 | 1285 PyInt len = strlen(str); |
7 | 1286 char *p; |
1287 | |
1288 /* Allocate an Python string object, with uninitialised contents. We | |
1289 * must do it this way, so that we can modify the string in place | |
1290 * later. See the Python source, Objects/stringobject.c for details. | |
1291 */ | |
1292 result = PyString_FromStringAndSize(NULL, len); | |
1293 if (result == NULL) | |
1294 return NULL; | |
1295 | |
1296 p = PyString_AsString(result); | |
1297 | |
1298 while (*str) | |
1299 { | |
1300 if (*str == '\n') | |
1301 *p = '\0'; | |
1302 else | |
1303 *p = *str; | |
1304 | |
1305 ++p; | |
1306 ++str; | |
1307 } | |
1308 | |
1309 return result; | |
1310 } | |
1311 | |
3618 | 1312 static PyObject * |
1313 DictionaryGetattr(PyObject *self, char *name) | |
1314 { | |
3828 | 1315 DictionaryObject *this = ((DictionaryObject *) (self)); |
1316 | |
1317 if (strcmp(name, "locked") == 0) | |
1318 return PyInt_FromLong(this->dict->dv_lock); | |
1319 else if (strcmp(name, "scope") == 0) | |
1320 return PyInt_FromLong(this->dict->dv_scope); | |
1321 | |
3618 | 1322 return Py_FindMethod(DictionaryMethods, self, name); |
1323 } | |
1324 | |
1325 static PySequenceMethods ListAsSeq = { | |
1326 (PyInquiry) ListLength, | |
1327 (binaryfunc) 0, | |
1328 (PyIntArgFunc) 0, | |
1329 (PyIntArgFunc) ListItem, | |
1330 (PyIntIntArgFunc) ListSlice, | |
1331 (PyIntObjArgProc) ListAssItem, | |
1332 (PyIntIntObjArgProc) ListAssSlice, | |
1333 (objobjproc) 0, | |
1334 #if PY_MAJOR_VERSION >= 2 | |
1335 (binaryfunc) ListConcatInPlace, | |
1336 0, | |
1337 #endif | |
1338 }; | |
1339 | |
1340 static PyObject * | |
1341 ListGetattr(PyObject *self, char *name) | |
1342 { | |
3828 | 1343 if (strcmp(name, "locked") == 0) |
1344 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock); | |
1345 | |
3618 | 1346 return Py_FindMethod(ListMethods, self, name); |
1347 } | |
1348 | |
1349 static PyObject * | |
1350 FunctionGetattr(PyObject *self, char *name) | |
1351 { | |
1352 FunctionObject *this = (FunctionObject *)(self); | |
1353 | |
1354 if (strcmp(name, "name") == 0) | |
1355 return PyString_FromString((char *)(this->name)); | |
1356 else | |
1357 return Py_FindMethod(FunctionMethods, self, name); | |
1358 } | |
1359 | |
1360 void | |
1361 do_pyeval (char_u *str, typval_T *rettv) | |
1362 { | |
4486 | 1363 DoPyCommand((char *) str, |
1364 (rangeinitializer) init_range_eval, | |
1365 (runner) run_eval, | |
1366 (void *) rettv); | |
3618 | 1367 switch(rettv->v_type) |
1368 { | |
1369 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; | |
1370 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; | |
1371 case VAR_FUNC: func_ref(rettv->vval.v_string); break; | |
3796 | 1372 case VAR_UNKNOWN: |
1373 rettv->v_type = VAR_NUMBER; | |
1374 rettv->vval.v_number = 0; | |
1375 break; | |
3618 | 1376 } |
1377 } | |
7 | 1378 |
1379 /* Don't generate a prototype for the next function, it generates an error on | |
1380 * newer Python versions. */ | |
1381 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO) | |
1382 | |
1383 char * | |
1384 Py_GetProgramName(void) | |
1385 { | |
1386 return "vim"; | |
1387 } | |
1388 #endif /* Python 1.4 */ | |
2399
76f0c4918f5c
Move some common code from if_python.c and if_python3.c to if_py_both.h.
Bram Moolenaar <bram@vim.org>
parents:
2384
diff
changeset
|
1389 |
3618 | 1390 void |
1391 set_ref_in_python (int copyID) | |
1392 { | |
1393 set_ref_in_py(copyID); | |
1394 } |