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