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