Mercurial > vim
annotate src/if_python.c @ 3618:c052f3b79b99 v7.3.569
updated for version 7.3.569
Problem: Evaluating Vim expression in Python is insufficient.
Solution: Add vim.bindeval(). Also add pyeval() and py3eval(). (ZyX)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Fri, 29 Jun 2012 12:54:53 +0200 |
parents | e4f3fa1a474e |
children | 80ed6aa7b9eb |
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 |
1594 | 47 #define PY_SSIZE_T_CLEAN |
48 | |
7 | 49 #include <Python.h> |
50 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
51 # include "macglue.h" | |
52 # include <CodeFragments.h> | |
53 #endif | |
54 #undef main /* Defined in python.h - aargh */ | |
55 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ | |
56 | |
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
|
57 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
|
58 |
3618 | 59 #define PyBytes_FromString PyString_FromString |
60 | |
2894 | 61 /* No-op conversion functions, use with care! */ |
62 #define PyString_AsBytes(obj) (obj) | |
63 #define PyString_FreeBytes(obj) | |
64 | |
7 | 65 #if !defined(FEAT_PYTHON) && defined(PROTO) |
66 /* Use this to be able to generate prototypes without python being used. */ | |
1607 | 67 # define PyObject Py_ssize_t |
68 # define PyThreadState Py_ssize_t | |
69 # define PyTypeObject Py_ssize_t | |
70 struct PyMethodDef { Py_ssize_t a; }; | |
71 # define PySequenceMethods Py_ssize_t | |
7 | 72 #endif |
73 | |
1594 | 74 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 |
75 # define PyInt Py_ssize_t | |
76 # define PyInquiry lenfunc | |
77 # define PyIntArgFunc ssizeargfunc | |
78 # define PyIntIntArgFunc ssizessizeargfunc | |
79 # define PyIntObjArgProc ssizeobjargproc | |
80 # define PyIntIntObjArgProc ssizessizeobjargproc | |
1607 | 81 # define Py_ssize_t_fmt "n" |
1594 | 82 #else |
83 # define PyInt int | |
84 # define PyInquiry inquiry | |
85 # define PyIntArgFunc intargfunc | |
86 # define PyIntIntArgFunc intintargfunc | |
87 # define PyIntObjArgProc intobjargproc | |
88 # define PyIntIntObjArgProc intintobjargproc | |
1607 | 89 # define Py_ssize_t_fmt "i" |
1594 | 90 #endif |
91 | |
7 | 92 /* Parser flags */ |
93 #define single_input 256 | |
94 #define file_input 257 | |
95 #define eval_input 258 | |
96 | |
97 #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0 | |
98 /* Python 2.3: can invoke ":python" recursively. */ | |
99 # define PY_CAN_RECURSE | |
100 #endif | |
101 | |
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
|
102 # 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
|
103 # 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
|
104 # 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
|
105 # endif |
7 | 106 |
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
|
107 # 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
|
108 # 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
|
109 # 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
|
110 # define HINSTANCE void* |
2641 | 111 # 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
|
112 # 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
|
113 # 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
|
114 # 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
|
115 # 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
|
116 # 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
|
117 # 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
|
118 # else |
2612 | 119 # 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
|
120 # 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
|
121 # 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
|
122 # endif |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
123 |
1607 | 124 /* This makes if_python.c compile without warnings against Python 2.5 |
125 * 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
|
126 # undef PyRun_SimpleString |
3618 | 127 # 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
|
128 # 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
|
129 # 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
|
130 # 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
|
131 # 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
|
132 # undef Py_InitModule4_64 |
3618 | 133 # undef PyObject_CallMethod |
1607 | 134 |
7 | 135 /* |
136 * Wrapper defines | |
137 */ | |
138 # define PyArg_Parse dll_PyArg_Parse | |
139 # define PyArg_ParseTuple dll_PyArg_ParseTuple | |
2894 | 140 # define PyMem_Free dll_PyMem_Free |
3618 | 141 # define PyMem_Malloc dll_PyMem_Malloc |
7 | 142 # define PyDict_SetItemString dll_PyDict_SetItemString |
143 # define PyErr_BadArgument dll_PyErr_BadArgument | |
144 # define PyErr_Clear dll_PyErr_Clear | |
145 # define PyErr_NoMemory dll_PyErr_NoMemory | |
146 # define PyErr_Occurred dll_PyErr_Occurred | |
147 # define PyErr_SetNone dll_PyErr_SetNone | |
148 # define PyErr_SetString dll_PyErr_SetString | |
149 # define PyEval_InitThreads dll_PyEval_InitThreads | |
150 # define PyEval_RestoreThread dll_PyEval_RestoreThread | |
151 # define PyEval_SaveThread dll_PyEval_SaveThread | |
152 # ifdef PY_CAN_RECURSE | |
153 # define PyGILState_Ensure dll_PyGILState_Ensure | |
154 # define PyGILState_Release dll_PyGILState_Release | |
155 # endif | |
156 # define PyInt_AsLong dll_PyInt_AsLong | |
157 # define PyInt_FromLong dll_PyInt_FromLong | |
3618 | 158 # define PyLong_AsLong dll_PyLong_AsLong |
159 # define PyLong_FromLong dll_PyLong_FromLong | |
7 | 160 # define PyInt_Type (*dll_PyInt_Type) |
3618 | 161 # define PyLong_Type (*dll_PyLong_Type) |
7 | 162 # define PyList_GetItem dll_PyList_GetItem |
637 | 163 # define PyList_Append dll_PyList_Append |
7 | 164 # define PyList_New dll_PyList_New |
165 # define PyList_SetItem dll_PyList_SetItem | |
166 # define PyList_Size dll_PyList_Size | |
167 # define PyList_Type (*dll_PyList_Type) | |
3618 | 168 # define PySequence_Check dll_PySequence_Check |
169 # define PySequence_Size dll_PySequence_Size | |
170 # define PySequence_GetItem dll_PySequence_GetItem | |
171 # define PyTuple_Size dll_PyTuple_Size | |
172 # define PyTuple_GetItem dll_PyTuple_GetItem | |
173 # define PyTuple_Type (*dll_PyTuple_Type) | |
7 | 174 # define PyImport_ImportModule dll_PyImport_ImportModule |
637 | 175 # define PyDict_New dll_PyDict_New |
7 | 176 # define PyDict_GetItemString dll_PyDict_GetItemString |
3618 | 177 # define PyDict_Next dll_PyDict_Next |
178 # ifdef PyMapping_Items | |
179 # define PY_NO_MAPPING_ITEMS | |
180 # else | |
181 # define PyMapping_Items dll_PyMapping_Items | |
182 # endif | |
183 # define PyObject_CallMethod dll_PyObject_CallMethod | |
184 # define PyMapping_Check dll_PyMapping_Check | |
185 # define PyIter_Next dll_PyIter_Next | |
7 | 186 # define PyModule_GetDict dll_PyModule_GetDict |
187 # define PyRun_SimpleString dll_PyRun_SimpleString | |
3618 | 188 # define PyRun_String dll_PyRun_String |
7 | 189 # define PyString_AsString dll_PyString_AsString |
190 # define PyString_FromString dll_PyString_FromString | |
191 # define PyString_FromStringAndSize dll_PyString_FromStringAndSize | |
192 # define PyString_Size dll_PyString_Size | |
193 # define PyString_Type (*dll_PyString_Type) | |
3618 | 194 # define PyUnicode_Type (*dll_PyUnicode_Type) |
195 # define PyUnicodeUCS4_AsEncodedString (*dll_PyUnicodeUCS4_AsEncodedString) | |
196 # define PyFloat_AsDouble dll_PyFloat_AsDouble | |
197 # define PyFloat_FromDouble dll_PyFloat_FromDouble | |
198 # define PyFloat_Type (*dll_PyFloat_Type) | |
199 # define PyImport_AddModule (*dll_PyImport_AddModule) | |
7 | 200 # define PySys_SetObject dll_PySys_SetObject |
201 # define PySys_SetArgv dll_PySys_SetArgv | |
202 # define PyType_Type (*dll_PyType_Type) | |
2737 | 203 # define PyType_Ready (*dll_PyType_Ready) |
7 | 204 # define Py_BuildValue dll_Py_BuildValue |
205 # define Py_FindMethod dll_Py_FindMethod | |
206 # define Py_InitModule4 dll_Py_InitModule4 | |
2641 | 207 # define Py_SetPythonHome dll_Py_SetPythonHome |
7 | 208 # define Py_Initialize dll_Py_Initialize |
242 | 209 # define Py_Finalize dll_Py_Finalize |
210 # define Py_IsInitialized dll_Py_IsInitialized | |
7 | 211 # define _PyObject_New dll__PyObject_New |
3618 | 212 # define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented) |
7 | 213 # define _Py_NoneStruct (*dll__Py_NoneStruct) |
214 # define PyObject_Init dll__PyObject_Init | |
3618 | 215 # define PyObject_GetIter dll_PyObject_GetIter |
7 | 216 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 |
217 # define PyType_IsSubtype dll_PyType_IsSubtype | |
218 # endif | |
219 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
220 # define PyObject_Malloc dll_PyObject_Malloc | |
221 # define PyObject_Free dll_PyObject_Free | |
222 # endif | |
3618 | 223 # define PyCapsule_New dll_PyCapsule_New |
224 # define PyCapsule_GetPointer dll_PyCapsule_GetPointer | |
7 | 225 |
226 /* | |
227 * Pointers for dynamic link | |
228 */ | |
229 static int(*dll_PyArg_Parse)(PyObject *, char *, ...); | |
230 static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...); | |
2894 | 231 static int(*dll_PyMem_Free)(void *); |
3618 | 232 static void* (*dll_PyMem_Malloc)(size_t); |
7 | 233 static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); |
234 static int(*dll_PyErr_BadArgument)(void); | |
235 static void(*dll_PyErr_Clear)(void); | |
236 static PyObject*(*dll_PyErr_NoMemory)(void); | |
237 static PyObject*(*dll_PyErr_Occurred)(void); | |
238 static void(*dll_PyErr_SetNone)(PyObject *); | |
239 static void(*dll_PyErr_SetString)(PyObject *, const char *); | |
240 static void(*dll_PyEval_InitThreads)(void); | |
241 static void(*dll_PyEval_RestoreThread)(PyThreadState *); | |
242 static PyThreadState*(*dll_PyEval_SaveThread)(void); | |
243 # ifdef PY_CAN_RECURSE | |
244 static PyGILState_STATE (*dll_PyGILState_Ensure)(void); | |
245 static void (*dll_PyGILState_Release)(PyGILState_STATE); | |
3618 | 246 # endif |
7 | 247 static long(*dll_PyInt_AsLong)(PyObject *); |
248 static PyObject*(*dll_PyInt_FromLong)(long); | |
3618 | 249 static long(*dll_PyLong_AsLong)(PyObject *); |
250 static PyObject*(*dll_PyLong_FromLong)(long); | |
7 | 251 static PyTypeObject* dll_PyInt_Type; |
3618 | 252 static PyTypeObject* dll_PyLong_Type; |
1594 | 253 static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt); |
637 | 254 static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *); |
1594 | 255 static PyObject*(*dll_PyList_New)(PyInt size); |
256 static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *); | |
257 static PyInt(*dll_PyList_Size)(PyObject *); | |
7 | 258 static PyTypeObject* dll_PyList_Type; |
3618 | 259 static int (*dll_PySequence_Check)(PyObject *); |
260 static PyInt(*dll_PySequence_Size)(PyObject *); | |
261 static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt); | |
262 static PyInt(*dll_PyTuple_Size)(PyObject *); | |
263 static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt); | |
264 static PyTypeObject* dll_PyTuple_Type; | |
7 | 265 static PyObject*(*dll_PyImport_ImportModule)(const char *); |
637 | 266 static PyObject*(*dll_PyDict_New)(void); |
7 | 267 static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *); |
3618 | 268 static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); |
269 # ifndef PY_NO_MAPPING_ITEMS | |
270 static PyObject* (*dll_PyMapping_Items)(PyObject *); | |
271 # endif | |
272 static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *); | |
273 static int (*dll_PyMapping_Check)(PyObject *); | |
274 static PyObject* (*dll_PyIter_Next)(PyObject *); | |
7 | 275 static PyObject*(*dll_PyModule_GetDict)(PyObject *); |
276 static int(*dll_PyRun_SimpleString)(char *); | |
3618 | 277 static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *); |
7 | 278 static char*(*dll_PyString_AsString)(PyObject *); |
279 static PyObject*(*dll_PyString_FromString)(const char *); | |
1594 | 280 static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt); |
281 static PyInt(*dll_PyString_Size)(PyObject *); | |
7 | 282 static PyTypeObject* dll_PyString_Type; |
3618 | 283 static PyTypeObject* dll_PyUnicode_Type; |
284 static PyObject *(*PyUnicodeUCS4_AsEncodedString)(PyObject *, char *, char *); | |
285 static double(*dll_PyFloat_AsDouble)(PyObject *); | |
286 static PyObject*(*dll_PyFloat_FromDouble)(double); | |
287 static PyTypeObject* dll_PyFloat_Type; | |
7 | 288 static int(*dll_PySys_SetObject)(char *, PyObject *); |
289 static int(*dll_PySys_SetArgv)(int, char **); | |
290 static PyTypeObject* dll_PyType_Type; | |
2737 | 291 static int (*dll_PyType_Ready)(PyTypeObject *type); |
7 | 292 static PyObject*(*dll_Py_BuildValue)(char *, ...); |
293 static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *); | |
294 static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int); | |
3618 | 295 static PyObject*(*dll_PyImport_AddModule)(char *); |
2641 | 296 static void(*dll_Py_SetPythonHome)(char *home); |
7 | 297 static void(*dll_Py_Initialize)(void); |
242 | 298 static void(*dll_Py_Finalize)(void); |
299 static int(*dll_Py_IsInitialized)(void); | |
7 | 300 static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *); |
301 static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *); | |
3618 | 302 static PyObject* (*dll_PyObject_GetIter)(PyObject *); |
303 static iternextfunc dll__PyObject_NextNotImplemented; | |
7 | 304 static PyObject* dll__Py_NoneStruct; |
305 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 | |
306 static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); | |
307 # endif | |
308 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
309 static void* (*dll_PyObject_Malloc)(size_t); | |
310 static void (*dll_PyObject_Free)(void*); | |
311 # endif | |
3618 | 312 static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor); |
313 static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *); | |
7 | 314 |
315 static HINSTANCE hinstPython = 0; /* Instance of python.dll */ | |
316 | |
317 /* Imported exception objects */ | |
318 static PyObject *imp_PyExc_AttributeError; | |
319 static PyObject *imp_PyExc_IndexError; | |
320 static PyObject *imp_PyExc_KeyboardInterrupt; | |
321 static PyObject *imp_PyExc_TypeError; | |
322 static PyObject *imp_PyExc_ValueError; | |
323 | |
324 # define PyExc_AttributeError imp_PyExc_AttributeError | |
325 # define PyExc_IndexError imp_PyExc_IndexError | |
326 # define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt | |
327 # define PyExc_TypeError imp_PyExc_TypeError | |
328 # define PyExc_ValueError imp_PyExc_ValueError | |
329 | |
330 /* | |
331 * Table of name to function pointer of python. | |
332 */ | |
333 # define PYTHON_PROC FARPROC | |
334 static struct | |
335 { | |
336 char *name; | |
337 PYTHON_PROC *ptr; | |
338 } python_funcname_table[] = | |
339 { | |
340 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse}, | |
341 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple}, | |
2894 | 342 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free}, |
3618 | 343 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc}, |
7 | 344 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString}, |
345 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument}, | |
346 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear}, | |
347 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory}, | |
348 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred}, | |
349 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone}, | |
350 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString}, | |
351 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads}, | |
352 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread}, | |
353 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread}, | |
354 # ifdef PY_CAN_RECURSE | |
355 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure}, | |
356 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release}, | |
357 # endif | |
358 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong}, | |
359 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong}, | |
3618 | 360 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong}, |
361 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong}, | |
7 | 362 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type}, |
3618 | 363 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type}, |
7 | 364 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem}, |
637 | 365 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append}, |
7 | 366 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New}, |
367 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem}, | |
368 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size}, | |
369 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type}, | |
3618 | 370 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem}, |
371 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size}, | |
372 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check}, | |
373 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem}, | |
374 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size}, | |
375 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type}, | |
7 | 376 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule}, |
377 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString}, | |
3618 | 378 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next}, |
637 | 379 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New}, |
3618 | 380 # ifndef PY_NO_MAPPING_ITEMS |
381 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items}, | |
382 # endif | |
383 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod}, | |
384 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check}, | |
385 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next}, | |
7 | 386 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict}, |
387 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString}, | |
3618 | 388 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String}, |
7 | 389 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString}, |
390 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString}, | |
391 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize}, | |
392 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size}, | |
393 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type}, | |
3618 | 394 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type}, |
395 {"PyUnicodeUCS4_AsEncodedString", (PYTHON_PROC*)&dll_PyUnicodeUCS4_AsEncodedString}, | |
396 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type}, | |
397 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble}, | |
398 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble}, | |
399 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule}, | |
7 | 400 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject}, |
401 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv}, | |
402 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type}, | |
2737 | 403 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready}, |
7 | 404 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue}, |
405 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod}, | |
1607 | 406 # if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT |
407 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4}, | |
408 # else | |
7 | 409 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4}, |
1607 | 410 # endif |
2641 | 411 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome}, |
7 | 412 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize}, |
242 | 413 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize}, |
414 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized}, | |
7 | 415 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New}, |
416 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init}, | |
3618 | 417 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter}, |
418 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented}, | |
7 | 419 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct}, |
420 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000 | |
421 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype}, | |
422 # endif | |
423 # if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000 | |
424 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc}, | |
425 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free}, | |
426 # endif | |
3618 | 427 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New}, |
428 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer}, | |
7 | 429 {"", NULL}, |
430 }; | |
431 | |
432 /* | |
433 * Free python.dll | |
434 */ | |
435 static void | |
436 end_dynamic_python(void) | |
437 { | |
438 if (hinstPython) | |
439 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
440 close_dll(hinstPython); |
7 | 441 hinstPython = 0; |
442 } | |
443 } | |
444 | |
445 /* | |
446 * Load library and get all pointers. | |
447 * Parameter 'libname' provides name of DLL. | |
448 * Return OK or FAIL. | |
449 */ | |
450 static int | |
451 python_runtime_link_init(char *libname, int verbose) | |
452 { | |
453 int i; | |
454 | |
2641 | 455 #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
|
456 /* 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
|
457 * 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
|
458 * 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
|
459 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
|
460 { |
3032 | 461 if (verbose) |
462 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
|
463 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
|
464 } |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
465 #endif |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
466 |
7 | 467 if (hinstPython) |
468 return OK; | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
469 hinstPython = load_dll(libname); |
7 | 470 if (!hinstPython) |
471 { | |
472 if (verbose) | |
473 EMSG2(_(e_loadlib), libname); | |
474 return FAIL; | |
475 } | |
476 | |
477 for (i = 0; python_funcname_table[i].ptr; ++i) | |
478 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
479 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython, |
7 | 480 python_funcname_table[i].name)) == NULL) |
481 { | |
2329
ad2889f48843
Added support for Python 3. (Roland Puntaier)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
482 close_dll(hinstPython); |
7 | 483 hinstPython = 0; |
484 if (verbose) | |
485 EMSG2(_(e_loadfunc), python_funcname_table[i].name); | |
486 return FAIL; | |
487 } | |
488 } | |
489 return OK; | |
490 } | |
491 | |
492 /* | |
493 * If python is enabled (there is installed python on Windows system) return | |
494 * TRUE, else FALSE. | |
495 */ | |
496 int | |
1607 | 497 python_enabled(int verbose) |
7 | 498 { |
499 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK; | |
500 } | |
501 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
502 /* |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
503 * Load the standard Python exceptions - don't import the symbols from the |
7 | 504 * DLL, as this can cause errors (importing data symbols is not reliable). |
505 */ | |
506 static void | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
507 get_exceptions(void) |
7 | 508 { |
509 PyObject *exmod = PyImport_ImportModule("exceptions"); | |
510 PyObject *exdict = PyModule_GetDict(exmod); | |
511 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); | |
512 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); | |
513 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); | |
514 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); | |
515 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); | |
516 Py_XINCREF(imp_PyExc_AttributeError); | |
517 Py_XINCREF(imp_PyExc_IndexError); | |
518 Py_XINCREF(imp_PyExc_KeyboardInterrupt); | |
519 Py_XINCREF(imp_PyExc_TypeError); | |
520 Py_XINCREF(imp_PyExc_ValueError); | |
521 Py_XDECREF(exmod); | |
522 } | |
523 #endif /* DYNAMIC_PYTHON */ | |
524 | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
525 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
|
526 static PyObject *WindowNew(win_T *); |
3618 | 527 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
|
528 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
|
529 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
530 static PyTypeObject RangeType; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
531 |
3618 | 532 static int initialised = 0; |
533 #define PYINITIALISED initialised | |
534 | |
535 /* Add conversion from PyInt? */ | |
536 #define DICTKEY_GET(err) \ | |
537 if (!PyString_Check(keyObject)) \ | |
538 { \ | |
539 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \ | |
540 return err; \ | |
541 } \ | |
542 key = (char_u *) PyString_AsString(keyObject); | |
543 #define DICTKEY_UNREF | |
544 #define DICTKEY_DECL | |
545 | |
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
|
546 /* |
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
|
547 * 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
|
548 */ |
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
|
549 #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
|
550 |
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
|
551 |
7 | 552 /****************************************************** |
553 * Internal function prototypes. | |
554 */ | |
555 | |
1607 | 556 static PyInt RangeStart; |
557 static PyInt RangeEnd; | |
7 | 558 |
3618 | 559 static PyObject *globals; |
560 | |
7 | 561 static void PythonIO_Flush(void); |
562 static int PythonIO_Init(void); | |
563 static int PythonMod_Init(void); | |
564 | |
565 /* Utility functions for the vim/python interface | |
566 * ---------------------------------------------- | |
567 */ | |
568 | |
1607 | 569 static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *); |
7 | 570 |
571 | |
572 /****************************************************** | |
573 * 1. Python interpreter main program. | |
574 */ | |
575 | |
576 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ | |
577 typedef PyObject PyThreadState; | |
323 | 578 #endif |
579 | |
580 #ifdef PY_CAN_RECURSE | |
581 static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; | |
582 #else | |
36 | 583 static PyThreadState *saved_python_thread = NULL; |
323 | 584 #endif |
7 | 585 |
586 /* | |
587 * Suspend a thread of the Python interpreter, other threads are allowed to | |
588 * run. | |
589 */ | |
36 | 590 static void |
591 Python_SaveThread(void) | |
7 | 592 { |
323 | 593 #ifdef PY_CAN_RECURSE |
594 PyGILState_Release(pygilstate); | |
595 #else | |
7 | 596 saved_python_thread = PyEval_SaveThread(); |
323 | 597 #endif |
7 | 598 } |
599 | |
600 /* | |
601 * Restore a thread of the Python interpreter, waits for other threads to | |
602 * block. | |
603 */ | |
36 | 604 static void |
605 Python_RestoreThread(void) | |
7 | 606 { |
323 | 607 #ifdef PY_CAN_RECURSE |
608 pygilstate = PyGILState_Ensure(); | |
609 #else | |
7 | 610 PyEval_RestoreThread(saved_python_thread); |
611 saved_python_thread = NULL; | |
323 | 612 #endif |
7 | 613 } |
614 | |
615 void | |
616 python_end() | |
617 { | |
557 | 618 static int recurse = 0; |
619 | |
620 /* If a crash occurs while doing this, don't try again. */ | |
621 if (recurse != 0) | |
622 return; | |
623 | |
624 ++recurse; | |
625 | |
7 | 626 #ifdef DYNAMIC_PYTHON |
242 | 627 if (hinstPython && Py_IsInitialized()) |
323 | 628 { |
856 | 629 Python_RestoreThread(); /* enter python */ |
630 Py_Finalize(); | |
323 | 631 } |
7 | 632 end_dynamic_python(); |
242 | 633 #else |
634 if (Py_IsInitialized()) | |
323 | 635 { |
856 | 636 Python_RestoreThread(); /* enter python */ |
637 Py_Finalize(); | |
323 | 638 } |
7 | 639 #endif |
557 | 640 |
641 --recurse; | |
7 | 642 } |
643 | |
2384
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
644 #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
|
645 int |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
646 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
|
647 { |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
648 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
|
649 } |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
650 #endif |
aeea25941392
Temporary solution for crashing when using both :py and :py3: disallow both in
Bram Moolenaar <bram@vim.org>
parents:
2374
diff
changeset
|
651 |
7 | 652 static int |
653 Python_Init(void) | |
654 { | |
655 if (!initialised) | |
656 { | |
657 #ifdef DYNAMIC_PYTHON | |
658 if (!python_enabled(TRUE)) | |
659 { | |
660 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); | |
661 goto fail; | |
662 } | |
663 #endif | |
664 | |
2641 | 665 #ifdef PYTHON_HOME |
666 Py_SetPythonHome(PYTHON_HOME); | |
667 #endif | |
668 | |
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
|
669 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
|
670 |
7 | 671 #if !defined(MACOS) || defined(MACOS_X_UNIX) |
672 Py_Initialize(); | |
673 #else | |
674 PyMac_Initialize(); | |
675 #endif | |
676 /* initialise threads */ | |
677 PyEval_InitThreads(); | |
678 | |
679 #ifdef DYNAMIC_PYTHON | |
680 get_exceptions(); | |
681 #endif | |
682 | |
683 if (PythonIO_Init()) | |
684 goto fail; | |
685 | |
686 if (PythonMod_Init()) | |
687 goto fail; | |
688 | |
3618 | 689 globals = PyModule_GetDict(PyImport_AddModule("__main__")); |
690 | |
1748 | 691 /* Remove the element from sys.path that was added because of our |
692 * argv[0] value in PythonMod_Init(). Previously we used an empty | |
693 * string, but dependinding on the OS we then get an empty entry or | |
694 * the current directory in sys.path. */ | |
695 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); | |
696 | |
36 | 697 /* the first python thread is vim's, release the lock */ |
7 | 698 Python_SaveThread(); |
699 | |
700 initialised = 1; | |
701 } | |
702 | |
703 return 0; | |
704 | |
705 fail: | |
706 /* We call PythonIO_Flush() here to print any Python errors. | |
707 * This is OK, as it is possible to call this function even | |
708 * if PythonIO_Init() has not completed successfully (it will | |
709 * not do anything in this case). | |
710 */ | |
711 PythonIO_Flush(); | |
712 return -1; | |
713 } | |
714 | |
715 /* | |
716 * External interface | |
717 */ | |
718 static void | |
3618 | 719 DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv) |
7 | 720 { |
323 | 721 #ifndef PY_CAN_RECURSE |
7 | 722 static int recursive = 0; |
723 #endif | |
724 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
725 GrafPtr oldPort; | |
726 #endif | |
727 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
728 char *saved_locale; | |
729 #endif | |
730 | |
731 #ifndef PY_CAN_RECURSE | |
732 if (recursive) | |
733 { | |
734 EMSG(_("E659: Cannot invoke Python recursively")); | |
735 return; | |
736 } | |
737 ++recursive; | |
738 #endif | |
739 | |
740 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
741 GetPort(&oldPort); | |
742 /* Check if the Python library is available */ | |
743 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) | |
744 goto theend; | |
745 #endif | |
746 if (Python_Init()) | |
747 goto theend; | |
748 | |
3618 | 749 if (rettv == NULL) |
750 { | |
751 RangeStart = eap->line1; | |
752 RangeEnd = eap->line2; | |
753 } | |
754 else | |
755 { | |
756 RangeStart = (PyInt) curwin->w_cursor.lnum; | |
757 RangeEnd = RangeStart; | |
758 } | |
7 | 759 Python_Release_Vim(); /* leave vim */ |
760 | |
761 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
762 /* Python only works properly when the LC_NUMERIC locale is "C". */ | |
763 saved_locale = setlocale(LC_NUMERIC, NULL); | |
764 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) | |
765 saved_locale = NULL; | |
766 else | |
767 { | |
768 /* Need to make a copy, value may change when setting new locale. */ | |
769 saved_locale = (char *)vim_strsave((char_u *)saved_locale); | |
770 (void)setlocale(LC_NUMERIC, "C"); | |
771 } | |
772 #endif | |
773 | |
774 Python_RestoreThread(); /* enter python */ | |
775 | |
3618 | 776 if (rettv == NULL) |
777 PyRun_SimpleString((char *)(cmd)); | |
778 else | |
779 { | |
780 PyObject *r; | |
781 | |
782 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals); | |
783 if (r == NULL) | |
784 EMSG(_("E858: Eval did not return a valid python object")); | |
785 else | |
786 { | |
787 if (ConvertFromPyObject(r, rettv) == -1) | |
788 EMSG(_("E859: Failed to convert returned python object to vim value")); | |
789 Py_DECREF(r); | |
790 } | |
791 PyErr_Clear(); | |
792 } | |
7 | 793 |
794 Python_SaveThread(); /* leave python */ | |
795 | |
796 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) | |
797 if (saved_locale != NULL) | |
798 { | |
799 (void)setlocale(LC_NUMERIC, saved_locale); | |
800 vim_free(saved_locale); | |
801 } | |
802 #endif | |
803 | |
804 Python_Lock_Vim(); /* enter vim */ | |
805 PythonIO_Flush(); | |
806 #if defined(MACOS) && !defined(MACOS_X_UNIX) | |
807 SetPort(oldPort); | |
808 #endif | |
809 | |
810 theend: | |
811 #ifndef PY_CAN_RECURSE | |
812 --recursive; | |
813 #endif | |
3618 | 814 return; |
7 | 815 } |
816 | |
817 /* | |
818 * ":python" | |
819 */ | |
820 void | |
821 ex_python(exarg_T *eap) | |
822 { | |
823 char_u *script; | |
824 | |
825 script = script_get(eap, eap->arg); | |
826 if (!eap->skip) | |
827 { | |
828 if (script == NULL) | |
3618 | 829 DoPythonCommand(eap, (char *)eap->arg, NULL); |
7 | 830 else |
3618 | 831 DoPythonCommand(eap, (char *)script, NULL); |
7 | 832 } |
833 vim_free(script); | |
834 } | |
835 | |
836 #define BUFFER_SIZE 1024 | |
837 | |
838 /* | |
839 * ":pyfile" | |
840 */ | |
841 void | |
842 ex_pyfile(exarg_T *eap) | |
843 { | |
844 static char buffer[BUFFER_SIZE]; | |
845 const char *file = (char *)eap->arg; | |
846 char *p; | |
847 | |
848 /* Have to do it like this. PyRun_SimpleFile requires you to pass a | |
849 * stdio file pointer, but Vim and the Python DLL are compiled with | |
850 * different options under Windows, meaning that stdio pointers aren't | |
851 * compatible between the two. Yuk. | |
852 * | |
853 * Put the string "execfile('file')" into buffer. But, we need to | |
854 * escape any backslashes or single quotes in the file name, so that | |
855 * Python won't mangle the file name. | |
856 */ | |
857 strcpy(buffer, "execfile('"); | |
858 p = buffer + 10; /* size of "execfile('" */ | |
859 | |
860 while (*file && p < buffer + (BUFFER_SIZE - 3)) | |
861 { | |
862 if (*file == '\\' || *file == '\'') | |
863 *p++ = '\\'; | |
864 *p++ = *file++; | |
865 } | |
866 | |
867 /* If we didn't finish the file name, we hit a buffer overflow */ | |
868 if (*file != '\0') | |
869 return; | |
870 | |
871 /* Put in the terminating "')" and a null */ | |
872 *p++ = '\''; | |
873 *p++ = ')'; | |
874 *p++ = '\0'; | |
875 | |
876 /* Execute the file */ | |
3618 | 877 DoPythonCommand(eap, buffer, NULL); |
7 | 878 } |
879 | |
880 /****************************************************** | |
881 * 2. Python output stream: writes output via [e]msg(). | |
882 */ | |
883 | |
884 /* Implementation functions | |
885 */ | |
886 | |
887 static PyObject * | |
888 OutputGetattr(PyObject *self, char *name) | |
889 { | |
890 if (strcmp(name, "softspace") == 0) | |
891 return PyInt_FromLong(((OutputObject *)(self))->softspace); | |
892 | |
893 return Py_FindMethod(OutputMethods, self, name); | |
894 } | |
895 | |
896 static int | |
897 OutputSetattr(PyObject *self, char *name, PyObject *val) | |
898 { | |
3618 | 899 if (val == NULL) |
900 { | |
7 | 901 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); |
902 return -1; | |
903 } | |
904 | |
905 if (strcmp(name, "softspace") == 0) | |
906 { | |
3618 | 907 if (!PyInt_Check(val)) |
908 { | |
7 | 909 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); |
910 return -1; | |
911 } | |
912 | |
913 ((OutputObject *)(self))->softspace = PyInt_AsLong(val); | |
914 return 0; | |
915 } | |
916 | |
917 PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); | |
918 return -1; | |
919 } | |
920 | |
921 /***************/ | |
922 | |
923 static int | |
924 PythonIO_Init(void) | |
925 { | |
926 /* Fixups... */ | |
2735 | 927 PyType_Ready(&OutputType); |
7 | 928 |
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
|
929 return PythonIO_Init_io(); |
7 | 930 } |
931 | |
932 /****************************************************** | |
933 * 3. Implementation of the Vim module for Python | |
934 */ | |
935 | |
3618 | 936 static PyObject *ConvertToPyObject(typval_T *); |
937 static int ConvertFromPyObject(PyObject *, typval_T *); | |
938 | |
7 | 939 /* Window type - Implementation functions |
940 * -------------------------------------- | |
941 */ | |
942 | |
943 #define WindowType_Check(obj) ((obj)->ob_type == &WindowType) | |
944 | |
945 static void WindowDestructor(PyObject *); | |
946 static PyObject *WindowGetattr(PyObject *, char *); | |
947 | |
948 /* Buffer type - Implementation functions | |
949 * -------------------------------------- | |
950 */ | |
951 | |
952 #define BufferType_Check(obj) ((obj)->ob_type == &BufferType) | |
953 | |
954 static void BufferDestructor(PyObject *); | |
955 static PyObject *BufferGetattr(PyObject *, char *); | |
956 static PyObject *BufferRepr(PyObject *); | |
957 | |
1594 | 958 static PyInt BufferLength(PyObject *); |
959 static PyObject *BufferItem(PyObject *, PyInt); | |
960 static PyObject *BufferSlice(PyObject *, PyInt, PyInt); | |
961 static PyInt BufferAssItem(PyObject *, PyInt, PyObject *); | |
962 static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *); | |
7 | 963 |
964 /* Line range type - Implementation functions | |
965 * -------------------------------------- | |
966 */ | |
967 | |
968 #define RangeType_Check(obj) ((obj)->ob_type == &RangeType) | |
969 | |
1594 | 970 static PyInt RangeAssItem(PyObject *, PyInt, PyObject *); |
971 static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *); | |
7 | 972 |
973 /* Current objects type - Implementation functions | |
974 * ----------------------------------------------- | |
975 */ | |
976 | |
977 static PyObject *CurrentGetattr(PyObject *, char *); | |
978 static int CurrentSetattr(PyObject *, char *, PyObject *); | |
979 | |
980 static PySequenceMethods BufferAsSeq = { | |
1594 | 981 (PyInquiry) BufferLength, /* sq_length, len(x) */ |
7 | 982 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */ |
1594 | 983 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */ |
984 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */ | |
985 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */ | |
986 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */ | |
987 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */ | |
7 | 988 }; |
989 | |
990 static PyTypeObject BufferType = { | |
991 PyObject_HEAD_INIT(0) | |
992 0, | |
993 "buffer", | |
994 sizeof(BufferObject), | |
995 0, | |
996 | |
997 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */ | |
998 (printfunc) 0, /* tp_print, print x */ | |
999 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */ | |
1000 (setattrfunc) 0, /* tp_setattr, x.attr=v */ | |
1001 (cmpfunc) 0, /* tp_compare, x>y */ | |
1002 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */ | |
1003 | |
1004 0, /* as number */ | |
1005 &BufferAsSeq, /* as sequence */ | |
1006 0, /* as mapping */ | |
1007 | |
1008 (hashfunc) 0, /* tp_hash, dict(x) */ | |
1009 (ternaryfunc) 0, /* tp_call, x() */ | |
1010 (reprfunc) 0, /* tp_str, str(x) */ | |
1011 }; | |
1012 | |
1013 /* Buffer object - Implementation | |
1014 */ | |
1015 | |
1016 static PyObject * | |
1017 BufferNew(buf_T *buf) | |
1018 { | |
1019 /* We need to handle deletion of buffers underneath us. | |
502 | 1020 * If we add a "b_python_ref" field to the buf_T structure, |
7 | 1021 * then we can get at it in buf_freeall() in vim. We then |
1022 * need to create only ONE Python object per buffer - if | |
1023 * we try to create a second, just INCREF the existing one | |
1024 * and return it. The (single) Python object referring to | |
502 | 1025 * the buffer is stored in "b_python_ref". |
7 | 1026 * Question: what to do on a buf_freeall(). We'll probably |
1027 * have to either delete the Python object (DECREF it to | |
1028 * zero - a bad idea, as it leaves dangling refs!) or | |
1029 * set the buf_T * value to an invalid value (-1?), which | |
1030 * means we need checks in all access functions... Bah. | |
1031 */ | |
1032 | |
1033 BufferObject *self; | |
1034 | |
502 | 1035 if (buf->b_python_ref != NULL) |
7 | 1036 { |
502 | 1037 self = buf->b_python_ref; |
7 | 1038 Py_INCREF(self); |
1039 } | |
1040 else | |
1041 { | |
1042 self = PyObject_NEW(BufferObject, &BufferType); | |
1043 if (self == NULL) | |
1044 return NULL; | |
1045 self->buf = buf; | |
502 | 1046 buf->b_python_ref = self; |
7 | 1047 } |
1048 | |
1049 return (PyObject *)(self); | |
1050 } | |
1051 | |
1052 static void | |
1053 BufferDestructor(PyObject *self) | |
1054 { | |
1055 BufferObject *this = (BufferObject *)(self); | |
1056 | |
1057 if (this->buf && this->buf != INVALID_BUFFER_VALUE) | |
502 | 1058 this->buf->b_python_ref = NULL; |
7 | 1059 |
986 | 1060 Py_DECREF(self); |
7 | 1061 } |
1062 | |
1063 static PyObject * | |
1064 BufferGetattr(PyObject *self, char *name) | |
1065 { | |
1066 BufferObject *this = (BufferObject *)(self); | |
1067 | |
1068 if (CheckBuffer(this)) | |
1069 return NULL; | |
1070 | |
1071 if (strcmp(name, "name") == 0) | |
1607 | 1072 return Py_BuildValue("s", this->buf->b_ffname); |
7 | 1073 else if (strcmp(name, "number") == 0) |
1607 | 1074 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum); |
7 | 1075 else if (strcmp(name,"__members__") == 0) |
1076 return Py_BuildValue("[ss]", "name", "number"); | |
1077 else | |
1078 return Py_FindMethod(BufferMethods, self, name); | |
1079 } | |
1080 | |
1081 static PyObject * | |
1082 BufferRepr(PyObject *self) | |
1083 { | |
274 | 1084 static char repr[100]; |
7 | 1085 BufferObject *this = (BufferObject *)(self); |
1086 | |
1087 if (this->buf == INVALID_BUFFER_VALUE) | |
1088 { | |
1607 | 1089 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); |
7 | 1090 return PyString_FromString(repr); |
1091 } | |
1092 else | |
1093 { | |
1094 char *name = (char *)this->buf->b_fname; | |
1607 | 1095 PyInt len; |
7 | 1096 |
1097 if (name == NULL) | |
1098 name = ""; | |
1099 len = strlen(name); | |
1100 | |
1101 if (len > 35) | |
1102 name = name + (35 - len); | |
1103 | |
274 | 1104 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); |
7 | 1105 |
1106 return PyString_FromString(repr); | |
1107 } | |
1108 } | |
1109 | |
1110 /******************/ | |
1111 | |
1594 | 1112 static PyInt |
7 | 1113 BufferLength(PyObject *self) |
1114 { | |
1115 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */ | |
1116 if (CheckBuffer((BufferObject *)(self))) | |
1117 return -1; /* ??? */ | |
1118 | |
1119 return (((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
1120 } | |
1121 | |
1122 static PyObject * | |
1594 | 1123 BufferItem(PyObject *self, PyInt n) |
7 | 1124 { |
1125 return RBItem((BufferObject *)(self), n, 1, | |
1126 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
1127 } | |
1128 | |
1129 static PyObject * | |
1594 | 1130 BufferSlice(PyObject *self, PyInt lo, PyInt hi) |
7 | 1131 { |
1132 return RBSlice((BufferObject *)(self), lo, hi, 1, | |
1133 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count); | |
1134 } | |
1135 | |
1594 | 1136 static PyInt |
1137 BufferAssItem(PyObject *self, PyInt n, PyObject *val) | |
7 | 1138 { |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1139 return RBAsItem((BufferObject *)(self), n, val, 1, |
1607 | 1140 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
7 | 1141 NULL); |
1142 } | |
1143 | |
1594 | 1144 static PyInt |
1145 BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) | |
7 | 1146 { |
2894 | 1147 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, |
1607 | 1148 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, |
7 | 1149 NULL); |
1150 } | |
1151 | |
1152 static PySequenceMethods RangeAsSeq = { | |
1594 | 1153 (PyInquiry) RangeLength, /* sq_length, len(x) */ |
7 | 1154 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */ |
1594 | 1155 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */ |
1156 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */ | |
1157 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */ | |
1158 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */ | |
1159 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */ | |
7 | 1160 }; |
1161 | |
1162 /* Line range object - Implementation | |
1163 */ | |
1164 | |
1165 static void | |
1166 RangeDestructor(PyObject *self) | |
1167 { | |
1168 Py_DECREF(((RangeObject *)(self))->buf); | |
986 | 1169 Py_DECREF(self); |
7 | 1170 } |
1171 | |
1172 static PyObject * | |
1173 RangeGetattr(PyObject *self, char *name) | |
1174 { | |
1175 if (strcmp(name, "start") == 0) | |
1607 | 1176 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1); |
7 | 1177 else if (strcmp(name, "end") == 0) |
1607 | 1178 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1); |
7 | 1179 else |
1180 return Py_FindMethod(RangeMethods, self, name); | |
1181 } | |
1182 | |
1183 /****************/ | |
1184 | |
1594 | 1185 static PyInt |
1186 RangeAssItem(PyObject *self, PyInt n, PyObject *val) | |
7 | 1187 { |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1188 return RBAsItem(((RangeObject *)(self))->buf, n, val, |
7 | 1189 ((RangeObject *)(self))->start, |
1190 ((RangeObject *)(self))->end, | |
1191 &((RangeObject *)(self))->end); | |
1192 } | |
1193 | |
1594 | 1194 static PyInt |
1195 RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val) | |
7 | 1196 { |
2894 | 1197 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, |
7 | 1198 ((RangeObject *)(self))->start, |
1199 ((RangeObject *)(self))->end, | |
1200 &((RangeObject *)(self))->end); | |
1201 } | |
1202 | |
1203 /* Buffer list object - Definitions | |
1204 */ | |
1205 | |
1206 typedef struct | |
1207 { | |
1208 PyObject_HEAD | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1209 } BufListObject; |
7 | 1210 |
1211 static PySequenceMethods BufListAsSeq = { | |
1594 | 1212 (PyInquiry) BufListLength, /* sq_length, len(x) */ |
7 | 1213 (binaryfunc) 0, /* sq_concat, x+y */ |
1594 | 1214 (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
1215 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */ | |
1216 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ | |
1217 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ | |
1218 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ | |
7 | 1219 }; |
1220 | |
1221 static PyTypeObject BufListType = { | |
1222 PyObject_HEAD_INIT(0) | |
1223 0, | |
1224 "buffer list", | |
1225 sizeof(BufListObject), | |
1226 0, | |
1227 | |
1228 (destructor) 0, /* tp_dealloc, refcount==0 */ | |
1229 (printfunc) 0, /* tp_print, print x */ | |
1230 (getattrfunc) 0, /* tp_getattr, x.attr */ | |
1231 (setattrfunc) 0, /* tp_setattr, x.attr=v */ | |
1232 (cmpfunc) 0, /* tp_compare, x>y */ | |
1233 (reprfunc) 0, /* tp_repr, `x`, print x */ | |
1234 | |
1235 0, /* as number */ | |
1236 &BufListAsSeq, /* as sequence */ | |
1237 0, /* as mapping */ | |
1238 | |
1239 (hashfunc) 0, /* tp_hash, dict(x) */ | |
1240 (ternaryfunc) 0, /* tp_call, x() */ | |
1241 (reprfunc) 0, /* tp_str, str(x) */ | |
1242 }; | |
1243 | |
1244 /* Window object - Definitions | |
1245 */ | |
1246 | |
1247 static struct PyMethodDef WindowMethods[] = { | |
1248 /* name, function, calling, documentation */ | |
1249 { NULL, NULL, 0, NULL } | |
1250 }; | |
1251 | |
1252 static PyTypeObject WindowType = { | |
1253 PyObject_HEAD_INIT(0) | |
1254 0, | |
1255 "window", | |
1256 sizeof(WindowObject), | |
1257 0, | |
1258 | |
1259 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */ | |
1260 (printfunc) 0, /* tp_print, print x */ | |
1261 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */ | |
1262 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */ | |
1263 (cmpfunc) 0, /* tp_compare, x>y */ | |
1264 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */ | |
1265 | |
1266 0, /* as number */ | |
1267 0, /* as sequence */ | |
1268 0, /* as mapping */ | |
1269 | |
1270 (hashfunc) 0, /* tp_hash, dict(x) */ | |
1271 (ternaryfunc) 0, /* tp_call, x() */ | |
1272 (reprfunc) 0, /* tp_str, str(x) */ | |
1273 }; | |
1274 | |
1275 /* Window object - Implementation | |
1276 */ | |
1277 | |
1278 static PyObject * | |
1279 WindowNew(win_T *win) | |
1280 { | |
1281 /* We need to handle deletion of windows underneath us. | |
502 | 1282 * If we add a "w_python_ref" field to the win_T structure, |
7 | 1283 * then we can get at it in win_free() in vim. We then |
1284 * need to create only ONE Python object per window - if | |
1285 * we try to create a second, just INCREF the existing one | |
1286 * and return it. The (single) Python object referring to | |
502 | 1287 * the window is stored in "w_python_ref". |
7 | 1288 * On a win_free() we set the Python object's win_T* field |
1289 * to an invalid value. We trap all uses of a window | |
1290 * object, and reject them if the win_T* field is invalid. | |
1291 */ | |
1292 | |
1293 WindowObject *self; | |
1294 | |
502 | 1295 if (win->w_python_ref) |
7 | 1296 { |
502 | 1297 self = win->w_python_ref; |
7 | 1298 Py_INCREF(self); |
1299 } | |
1300 else | |
1301 { | |
1302 self = PyObject_NEW(WindowObject, &WindowType); | |
1303 if (self == NULL) | |
1304 return NULL; | |
1305 self->win = win; | |
502 | 1306 win->w_python_ref = self; |
7 | 1307 } |
1308 | |
1309 return (PyObject *)(self); | |
1310 } | |
1311 | |
1312 static void | |
1313 WindowDestructor(PyObject *self) | |
1314 { | |
1315 WindowObject *this = (WindowObject *)(self); | |
1316 | |
1317 if (this->win && this->win != INVALID_WINDOW_VALUE) | |
502 | 1318 this->win->w_python_ref = NULL; |
7 | 1319 |
986 | 1320 Py_DECREF(self); |
7 | 1321 } |
1322 | |
1323 static PyObject * | |
1324 WindowGetattr(PyObject *self, char *name) | |
1325 { | |
1326 WindowObject *this = (WindowObject *)(self); | |
1327 | |
1328 if (CheckWindow(this)) | |
1329 return NULL; | |
1330 | |
1331 if (strcmp(name, "buffer") == 0) | |
1332 return (PyObject *)BufferNew(this->win->w_buffer); | |
1333 else if (strcmp(name, "cursor") == 0) | |
1334 { | |
1335 pos_T *pos = &this->win->w_cursor; | |
1336 | |
1337 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); | |
1338 } | |
1339 else if (strcmp(name, "height") == 0) | |
1340 return Py_BuildValue("l", (long)(this->win->w_height)); | |
1341 #ifdef FEAT_VERTSPLIT | |
1342 else if (strcmp(name, "width") == 0) | |
1343 return Py_BuildValue("l", (long)(W_WIDTH(this->win))); | |
1344 #endif | |
1345 else if (strcmp(name,"__members__") == 0) | |
1346 return Py_BuildValue("[sss]", "buffer", "cursor", "height"); | |
1347 else | |
1348 return Py_FindMethod(WindowMethods, self, name); | |
1349 } | |
1350 | |
1351 /* Window list object - Definitions | |
1352 */ | |
1353 | |
1354 typedef struct | |
1355 { | |
1356 PyObject_HEAD | |
1357 } | |
1358 WinListObject; | |
1359 | |
1360 static PySequenceMethods WinListAsSeq = { | |
1594 | 1361 (PyInquiry) WinListLength, /* sq_length, len(x) */ |
7 | 1362 (binaryfunc) 0, /* sq_concat, x+y */ |
1594 | 1363 (PyIntArgFunc) 0, /* sq_repeat, x*n */ |
1364 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */ | |
1365 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */ | |
1366 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */ | |
1367 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */ | |
7 | 1368 }; |
1369 | |
1370 static PyTypeObject WinListType = { | |
1371 PyObject_HEAD_INIT(0) | |
1372 0, | |
1373 "window list", | |
1374 sizeof(WinListObject), | |
1375 0, | |
1376 | |
1377 (destructor) 0, /* tp_dealloc, refcount==0 */ | |
1378 (printfunc) 0, /* tp_print, print x */ | |
1379 (getattrfunc) 0, /* tp_getattr, x.attr */ | |
1380 (setattrfunc) 0, /* tp_setattr, x.attr=v */ | |
1381 (cmpfunc) 0, /* tp_compare, x>y */ | |
1382 (reprfunc) 0, /* tp_repr, `x`, print x */ | |
1383 | |
1384 0, /* as number */ | |
1385 &WinListAsSeq, /* as sequence */ | |
1386 0, /* as mapping */ | |
1387 | |
1388 (hashfunc) 0, /* tp_hash, dict(x) */ | |
1389 (ternaryfunc) 0, /* tp_call, x() */ | |
1390 (reprfunc) 0, /* tp_str, str(x) */ | |
1391 }; | |
1392 | |
1393 /* Current items object - Definitions | |
1394 */ | |
1395 | |
1396 typedef struct | |
1397 { | |
1398 PyObject_HEAD | |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1399 } CurrentObject; |
7 | 1400 |
1401 static PyTypeObject CurrentType = { | |
1402 PyObject_HEAD_INIT(0) | |
1403 0, | |
1404 "current data", | |
1405 sizeof(CurrentObject), | |
1406 0, | |
1407 | |
1408 (destructor) 0, /* tp_dealloc, refcount==0 */ | |
1409 (printfunc) 0, /* tp_print, print x */ | |
1410 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */ | |
1411 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */ | |
1412 (cmpfunc) 0, /* tp_compare, x>y */ | |
1413 (reprfunc) 0, /* tp_repr, `x`, print x */ | |
1414 | |
1415 0, /* as number */ | |
1416 0, /* as sequence */ | |
1417 0, /* as mapping */ | |
1418 | |
1419 (hashfunc) 0, /* tp_hash, dict(x) */ | |
1420 (ternaryfunc) 0, /* tp_call, x() */ | |
1421 (reprfunc) 0, /* tp_str, str(x) */ | |
1422 }; | |
1423 | |
1424 /* Current items object - Implementation | |
1425 */ | |
1426 static PyObject * | |
1887 | 1427 CurrentGetattr(PyObject *self UNUSED, char *name) |
7 | 1428 { |
1429 if (strcmp(name, "buffer") == 0) | |
1430 return (PyObject *)BufferNew(curbuf); | |
1431 else if (strcmp(name, "window") == 0) | |
1432 return (PyObject *)WindowNew(curwin); | |
1433 else if (strcmp(name, "line") == 0) | |
1607 | 1434 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum); |
7 | 1435 else if (strcmp(name, "range") == 0) |
1436 return RangeNew(curbuf, RangeStart, RangeEnd); | |
1437 else if (strcmp(name,"__members__") == 0) | |
1438 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); | |
1439 else | |
1440 { | |
1441 PyErr_SetString(PyExc_AttributeError, name); | |
1442 return NULL; | |
1443 } | |
1444 } | |
1445 | |
1446 static int | |
1887 | 1447 CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value) |
7 | 1448 { |
1449 if (strcmp(name, "line") == 0) | |
1450 { | |
1607 | 1451 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL) |
7 | 1452 return -1; |
1453 | |
1454 return 0; | |
1455 } | |
1456 else | |
1457 { | |
1458 PyErr_SetString(PyExc_AttributeError, name); | |
1459 return -1; | |
1460 } | |
1461 } | |
1462 | |
1463 /* External interface | |
1464 */ | |
1465 | |
1466 void | |
1467 python_buffer_free(buf_T *buf) | |
1468 { | |
502 | 1469 if (buf->b_python_ref != NULL) |
7 | 1470 { |
502 | 1471 BufferObject *bp = buf->b_python_ref; |
7 | 1472 bp->buf = INVALID_BUFFER_VALUE; |
502 | 1473 buf->b_python_ref = NULL; |
7 | 1474 } |
1475 } | |
1476 | |
1477 #if defined(FEAT_WINDOWS) || defined(PROTO) | |
1478 void | |
1479 python_window_free(win_T *win) | |
1480 { | |
502 | 1481 if (win->w_python_ref != NULL) |
7 | 1482 { |
502 | 1483 WindowObject *wp = win->w_python_ref; |
7 | 1484 wp->win = INVALID_WINDOW_VALUE; |
502 | 1485 win->w_python_ref = NULL; |
7 | 1486 } |
1487 } | |
1488 #endif | |
1489 | |
1490 static BufListObject TheBufferList = | |
1491 { | |
1492 PyObject_HEAD_INIT(&BufListType) | |
1493 }; | |
1494 | |
1495 static WinListObject TheWindowList = | |
1496 { | |
1497 PyObject_HEAD_INIT(&WinListType) | |
1498 }; | |
1499 | |
1500 static CurrentObject TheCurrent = | |
1501 { | |
1502 PyObject_HEAD_INIT(&CurrentType) | |
1503 }; | |
1504 | |
1505 static int | |
1506 PythonMod_Init(void) | |
1507 { | |
1508 PyObject *mod; | |
1509 PyObject *dict; | |
1748 | 1510 /* The special value is removed from sys.path in Python_Init(). */ |
1511 static char *(argv[2]) = {"/must>not&exist/foo", NULL}; | |
7 | 1512 |
1513 /* Fixups... */ | |
2735 | 1514 PyType_Ready(&BufferType); |
1515 PyType_Ready(&RangeType); | |
1516 PyType_Ready(&WindowType); | |
1517 PyType_Ready(&BufListType); | |
1518 PyType_Ready(&WinListType); | |
1519 PyType_Ready(&CurrentType); | |
7 | 1520 |
1521 /* Set sys.argv[] to avoid a crash in warn(). */ | |
1522 PySys_SetArgv(1, argv); | |
1523 | |
1607 | 1524 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION); |
7 | 1525 dict = PyModule_GetDict(mod); |
1526 | |
1527 VimError = Py_BuildValue("s", "vim.error"); | |
1528 | |
1529 PyDict_SetItemString(dict, "error", VimError); | |
135 | 1530 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList); |
1531 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent); | |
1532 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList); | |
7 | 1533 |
1534 if (PyErr_Occurred()) | |
1535 return -1; | |
1536 | |
1537 return 0; | |
1538 } | |
1539 | |
1540 /************************************************************************* | |
1541 * 4. Utility functions for handling the interface between Vim and Python. | |
1542 */ | |
1543 | |
1544 /* Convert a Vim line into a Python string. | |
1545 * All internal newlines are replaced by null characters. | |
1546 * | |
1547 * On errors, the Python exception data is set, and NULL is returned. | |
1548 */ | |
1549 static PyObject * | |
1550 LineToString(const char *str) | |
1551 { | |
1552 PyObject *result; | |
1594 | 1553 PyInt len = strlen(str); |
7 | 1554 char *p; |
1555 | |
1556 /* Allocate an Python string object, with uninitialised contents. We | |
1557 * must do it this way, so that we can modify the string in place | |
1558 * later. See the Python source, Objects/stringobject.c for details. | |
1559 */ | |
1560 result = PyString_FromStringAndSize(NULL, len); | |
1561 if (result == NULL) | |
1562 return NULL; | |
1563 | |
1564 p = PyString_AsString(result); | |
1565 | |
1566 while (*str) | |
1567 { | |
1568 if (*str == '\n') | |
1569 *p = '\0'; | |
1570 else | |
1571 *p = *str; | |
1572 | |
1573 ++p; | |
1574 ++str; | |
1575 } | |
1576 | |
1577 return result; | |
1578 } | |
1579 | |
3618 | 1580 static void DictionaryDestructor(PyObject *); |
1581 static PyObject *DictionaryGetattr(PyObject *, char*); | |
1582 | |
1583 static PyMappingMethods DictionaryAsMapping = { | |
1584 (PyInquiry) DictionaryLength, | |
1585 (binaryfunc) DictionaryItem, | |
1586 (objobjargproc) DictionaryAssItem, | |
1587 }; | |
1588 | |
1589 static PyTypeObject DictionaryType = { | |
1590 PyObject_HEAD_INIT(0) | |
1591 0, | |
1592 "vimdictionary", | |
1593 sizeof(DictionaryObject), | |
1594 0, | |
1595 | |
1596 (destructor) DictionaryDestructor, | |
1597 (printfunc) 0, | |
1598 (getattrfunc) DictionaryGetattr, | |
1599 (setattrfunc) 0, | |
1600 (cmpfunc) 0, | |
1601 (reprfunc) 0, | |
1602 | |
1603 0, /* as number */ | |
1604 0, /* as sequence */ | |
1605 &DictionaryAsMapping, /* as mapping */ | |
1606 | |
1607 (hashfunc) 0, | |
1608 (ternaryfunc) 0, | |
1609 (reprfunc) 0, | |
1610 }; | |
1611 | |
1612 static void | |
1613 DictionaryDestructor(PyObject *self) | |
1614 { | |
1615 DictionaryObject *this = ((DictionaryObject *) (self)); | |
1616 | |
1617 pyll_remove(&this->ref, &lastdict); | |
1618 dict_unref(this->dict); | |
1619 | |
1620 Py_DECREF(self); | |
1621 } | |
1622 | |
1623 static PyObject * | |
1624 DictionaryGetattr(PyObject *self, char *name) | |
1625 { | |
1626 return Py_FindMethod(DictionaryMethods, self, name); | |
1627 } | |
1628 | |
1629 static void ListDestructor(PyObject *); | |
1630 static PyObject *ListGetattr(PyObject *, char *); | |
1631 | |
1632 static PySequenceMethods ListAsSeq = { | |
1633 (PyInquiry) ListLength, | |
1634 (binaryfunc) 0, | |
1635 (PyIntArgFunc) 0, | |
1636 (PyIntArgFunc) ListItem, | |
1637 (PyIntIntArgFunc) ListSlice, | |
1638 (PyIntObjArgProc) ListAssItem, | |
1639 (PyIntIntObjArgProc) ListAssSlice, | |
1640 (objobjproc) 0, | |
1641 #if PY_MAJOR_VERSION >= 2 | |
1642 (binaryfunc) ListConcatInPlace, | |
1643 0, | |
1644 #endif | |
1645 }; | |
1646 | |
1647 static PyTypeObject ListType = { | |
1648 PyObject_HEAD_INIT(0) | |
1649 0, | |
1650 "vimlist", | |
1651 sizeof(ListObject), | |
1652 0, | |
1653 | |
1654 (destructor) ListDestructor, | |
1655 (printfunc) 0, | |
1656 (getattrfunc) ListGetattr, | |
1657 (setattrfunc) 0, | |
1658 (cmpfunc) 0, | |
1659 (reprfunc) 0, | |
1660 | |
1661 0, /* as number */ | |
1662 &ListAsSeq, /* as sequence */ | |
1663 0, /* as mapping */ | |
1664 | |
1665 (hashfunc) 0, | |
1666 (ternaryfunc) 0, | |
1667 (reprfunc) 0, | |
1668 }; | |
1669 | |
1670 static void | |
1671 ListDestructor(PyObject *self) | |
1672 { | |
1673 ListObject *this = ((ListObject *) (self)); | |
1674 | |
1675 pyll_remove(&this->ref, &lastlist); | |
1676 list_unref(this->list); | |
1677 | |
1678 Py_DECREF(self); | |
1679 } | |
1680 | |
1681 static PyObject * | |
1682 ListGetattr(PyObject *self, char *name) | |
1683 { | |
1684 return Py_FindMethod(ListMethods, self, name); | |
1685 } | |
1686 | |
1687 static void FunctionDestructor(PyObject *); | |
1688 static PyObject *FunctionGetattr(PyObject *, char *); | |
1689 | |
1690 static PyTypeObject FunctionType = { | |
1691 PyObject_HEAD_INIT(0) | |
1692 0, | |
1693 "vimfunction", | |
1694 sizeof(FunctionObject), | |
1695 0, | |
1696 | |
1697 (destructor) FunctionDestructor, | |
1698 (printfunc) 0, | |
1699 (getattrfunc) FunctionGetattr, | |
1700 (setattrfunc) 0, | |
1701 (cmpfunc) 0, | |
1702 (reprfunc) 0, | |
1703 | |
1704 0, /* as number */ | |
1705 0, /* as sequence */ | |
1706 0, /* as mapping */ | |
1707 | |
1708 (hashfunc) 0, | |
1709 (ternaryfunc) FunctionCall, | |
1710 (reprfunc) 0, | |
1711 }; | |
1712 | |
1713 static void | |
1714 FunctionDestructor(PyObject *self) | |
1715 { | |
1716 FunctionObject *this = (FunctionObject *) (self); | |
1717 | |
1718 func_unref(this->name); | |
1719 PyMem_Del(this->name); | |
1720 | |
1721 Py_DECREF(self); | |
1722 } | |
1723 | |
1724 static PyObject * | |
1725 FunctionGetattr(PyObject *self, char *name) | |
1726 { | |
1727 FunctionObject *this = (FunctionObject *)(self); | |
1728 | |
1729 if (strcmp(name, "name") == 0) | |
1730 return PyString_FromString((char *)(this->name)); | |
1731 else | |
1732 return Py_FindMethod(FunctionMethods, self, name); | |
1733 } | |
1734 | |
1735 void | |
1736 do_pyeval (char_u *str, typval_T *rettv) | |
1737 { | |
1738 DoPythonCommand(NULL, (char *) str, rettv); | |
1739 switch(rettv->v_type) | |
1740 { | |
1741 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; | |
1742 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; | |
1743 case VAR_FUNC: func_ref(rettv->vval.v_string); break; | |
1744 } | |
1745 } | |
7 | 1746 |
1747 /* Don't generate a prototype for the next function, it generates an error on | |
1748 * newer Python versions. */ | |
1749 #if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO) | |
1750 | |
1751 char * | |
1752 Py_GetProgramName(void) | |
1753 { | |
1754 return "vim"; | |
1755 } | |
1756 #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
|
1757 |
3618 | 1758 void |
1759 set_ref_in_python (int copyID) | |
1760 { | |
1761 set_ref_in_py(copyID); | |
1762 } | |
1763 | |
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
|
1764 static 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
|
1765 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
|
1766 { |
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
|
1767 vim_memset(&OutputType, 0, sizeof(OutputType)); |
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
|
1768 OutputType.tp_name = "message"; |
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
|
1769 OutputType.tp_basicsize = sizeof(OutputObject); |
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
|
1770 OutputType.tp_getattr = OutputGetattr; |
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
|
1771 OutputType.tp_setattr = OutputSetattr; |
2447
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1772 |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1773 vim_memset(&RangeType, 0, sizeof(RangeType)); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1774 RangeType.tp_name = "range"; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1775 RangeType.tp_basicsize = sizeof(RangeObject); |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1776 RangeType.tp_dealloc = RangeDestructor; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1777 RangeType.tp_getattr = RangeGetattr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1778 RangeType.tp_repr = RangeRepr; |
84d353762845
Move many more common Python items to if_py_both.c.
Bram Moolenaar <bram@vim.org>
parents:
2399
diff
changeset
|
1779 RangeType.tp_as_sequence = &RangeAsSeq; |
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
|
1780 } |