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