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