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