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