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