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