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