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