blob: e56cdbdabad062c2356d3a4f8f5b23a81df75404 [file] [log] [blame]
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001/* 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/*
21 * Roland Puntaier 2009/sept/16:
22 * Adaptations to support both python3.x and python2.x
23 */
24
25// uncomment this if used with the debug version of python
26// #define Py_DEBUG
27
28#include "vim.h"
29
30#include <limits.h>
31
32/* Python.h defines _POSIX_THREADS itself (if needed) */
33#ifdef _POSIX_THREADS
34# undef _POSIX_THREADS
35#endif
36
Bram Moolenaard68554d2010-07-25 13:43:20 +020037#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020038# undef HAVE_FCNTL_H
39#endif
40
41#ifdef _DEBUG
42# undef _DEBUG
43#endif
44
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020045#define PY_SSIZE_T_CLEAN
46
47#ifdef F_BLANK
48# undef F_BLANK
49#endif
50
Bram Moolenaar6df6f472010-07-18 18:04:50 +020051#ifdef HAVE_STDARG_H
52# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
53#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020054#ifdef _POSIX_C_SOURCE /* defined in feature.h */
55# undef _POSIX_C_SOURCE
56#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020057#ifdef _XOPEN_SOURCE
58# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
59#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020060
61#include <Python.h>
62#if defined(MACOS) && !defined(MACOS_X_UNIX)
63# include "macglue.h"
64# include <CodeFragments.h>
65#endif
66#undef main /* Defined in python.h - aargh */
67#undef HAVE_FCNTL_H /* Clash with os_win32.h */
68
69static void init_structs(void);
70
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020071#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020072#define PyString_Check(obj) PyUnicode_Check(obj)
73#define PyString_AsString(obj) _PyUnicode_AsString(obj)
74#define PyString_Size(obj) PyUnicode_GET_SIZE(obj)
75#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020076
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020077#if defined(DYNAMIC_PYTHON3)
78
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020079# ifndef WIN3264
80# include <dlfcn.h>
81# define FARPROC void*
82# define HINSTANCE void*
Bram Moolenaarb744b2f2010-08-13 16:22:57 +020083# ifdef PY_NO_RTLD_GLOBAL
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020084# define load_dll(n) dlopen((n), RTLD_LAZY)
85# else
86# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
87# endif
88# define close_dll dlclose
89# define symbol_from_dll dlsym
90# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020091# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020092# define close_dll FreeLibrary
93# define symbol_from_dll GetProcAddress
94# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020095/*
96 * Wrapper defines
97 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020098# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020099# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200100# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200101# define PyArg_ParseTuple py3_PyArg_ParseTuple
102# define PyDict_SetItemString py3_PyDict_SetItemString
103# define PyErr_BadArgument py3_PyErr_BadArgument
104# define PyErr_Clear py3_PyErr_Clear
105# define PyErr_NoMemory py3_PyErr_NoMemory
106# define PyErr_Occurred py3_PyErr_Occurred
107# define PyErr_SetNone py3_PyErr_SetNone
108# define PyErr_SetString py3_PyErr_SetString
109# define PyEval_InitThreads py3_PyEval_InitThreads
110# define PyEval_RestoreThread py3_PyEval_RestoreThread
111# define PyEval_SaveThread py3_PyEval_SaveThread
112# define PyGILState_Ensure py3_PyGILState_Ensure
113# define PyGILState_Release py3_PyGILState_Release
114# define PyLong_AsLong py3_PyLong_AsLong
115# define PyLong_FromLong py3_PyLong_FromLong
116# define PyList_GetItem py3_PyList_GetItem
117# define PyList_Append py3_PyList_Append
118# define PyList_New py3_PyList_New
119# define PyList_SetItem py3_PyList_SetItem
120# define PyList_Size py3_PyList_Size
121# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
122# define PyImport_ImportModule py3_PyImport_ImportModule
123# define PyObject_Init py3__PyObject_Init
124# define PyDict_New py3_PyDict_New
125# define PyDict_GetItemString py3_PyDict_GetItemString
126# define PyModule_GetDict py3_PyModule_GetDict
127#undef PyRun_SimpleString
128# define PyRun_SimpleString py3_PyRun_SimpleString
129# define PySys_SetObject py3_PySys_SetObject
130# define PySys_SetArgv py3_PySys_SetArgv
131# define PyType_Type (*py3_PyType_Type)
132# define PyType_Ready py3_PyType_Ready
133#undef Py_BuildValue
134# define Py_BuildValue py3_Py_BuildValue
135# define Py_Initialize py3_Py_Initialize
136# define Py_Finalize py3_Py_Finalize
137# define Py_IsInitialized py3_Py_IsInitialized
138# define _Py_NoneStruct (*py3__Py_NoneStruct)
139# define PyModule_AddObject py3_PyModule_AddObject
140# define PyImport_AppendInittab py3_PyImport_AppendInittab
141# define _PyUnicode_AsString py3__PyUnicode_AsString
142# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
143# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200144# ifdef Py_DEBUG
145# define _Py_NegativeRefcount py3__Py_NegativeRefcount
146# define _Py_RefTotal (*py3__Py_RefTotal)
147# define _Py_Dealloc py3__Py_Dealloc
148# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
149# define _PyObject_DebugFree py3__PyObject_DebugFree
150# else
151# define PyObject_Malloc py3_PyObject_Malloc
152# define PyObject_Free py3_PyObject_Free
153# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200154# define PyType_GenericAlloc py3_PyType_GenericAlloc
155# define PyType_GenericNew py3_PyType_GenericNew
156# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200157# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200158# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200159# undef PyUnicode_FromStringAndSize
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200160# define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize
161
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200162# ifdef Py_DEBUG
163# undef PyObject_NEW
164# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200166 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200167# endif
168
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200169/*
170 * Pointers for dynamic link
171 */
172static int (*py3_PySys_SetArgv)(int, wchar_t **);
173static void (*py3_Py_Initialize)(void);
174static PyObject* (*py3_PyList_New)(Py_ssize_t size);
175static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
176static void (*py3_PyGILState_Release)(PyGILState_STATE);
177static int (*py3_PySys_SetObject)(char *, PyObject *);
178static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
179static Py_ssize_t (*py3_PyList_Size)(PyObject *);
180static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200181 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200182static PyObject* (*py3_PyErr_NoMemory)(void);
183static void (*py3_Py_Finalize)(void);
184static void (*py3_PyErr_SetString)(PyObject *, const char *);
185static int (*py3_PyRun_SimpleString)(char *);
186static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
187static PyObject* (*py3_PyImport_ImportModule)(const char *);
188static int (*py3_PyErr_BadArgument)(void);
189static PyTypeObject* py3_PyType_Type;
190static PyObject* (*py3_PyErr_Occurred)(void);
191static PyObject* (*py3_PyModule_GetDict)(PyObject *);
192static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
193static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
194static PyObject* (*py3_PyLong_FromLong)(long);
195static PyObject* (*py3_PyDict_New)(void);
196static PyObject* (*py3_Py_BuildValue)(char *, ...);
197static int (*py3_PyType_Ready)(PyTypeObject *type);
198static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
199static PyObject* (*py3_PyUnicode_FromString)(const char *u);
200static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size);
201static long (*py3_PyLong_AsLong)(PyObject *);
202static void (*py3_PyErr_SetNone)(PyObject *);
203static void (*py3_PyEval_InitThreads)(void);
204static void(*py3_PyEval_RestoreThread)(PyThreadState *);
205static PyThreadState*(*py3_PyEval_SaveThread)(void);
206static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
207static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
208static int (*py3_Py_IsInitialized)(void);
209static void (*py3_PyErr_Clear)(void);
210static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
211static PyObject* py3__Py_NoneStruct;
212static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
213static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
214static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
215static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
216static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
217static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
218static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
219static PyTypeObject* py3_PySlice_Type;
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200220# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200221 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
222 static Py_ssize_t* py3__Py_RefTotal;
223 static void (*py3__Py_Dealloc)(PyObject *obj);
224 static void (*py3__PyObject_DebugFree)(void*);
225 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200226# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227 static void (*py3_PyObject_Free)(void*);
228 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200229# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200230
231static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
232
233/* Imported exception objects */
234static PyObject *p3imp_PyExc_AttributeError;
235static PyObject *p3imp_PyExc_IndexError;
236static PyObject *p3imp_PyExc_KeyboardInterrupt;
237static PyObject *p3imp_PyExc_TypeError;
238static PyObject *p3imp_PyExc_ValueError;
239
240# define PyExc_AttributeError p3imp_PyExc_AttributeError
241# define PyExc_IndexError p3imp_PyExc_IndexError
242# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
243# define PyExc_TypeError p3imp_PyExc_TypeError
244# define PyExc_ValueError p3imp_PyExc_ValueError
245
246/*
247 * Table of name to function pointer of python.
248 */
249# define PYTHON_PROC FARPROC
250static struct
251{
252 char *name;
253 PYTHON_PROC *ptr;
254} py3_funcname_table[] =
255{
256 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
257 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
258 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
259 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
260 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
261 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
262 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
263 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
264 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
265 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
266 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
267 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
268 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
269 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
270 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
271 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
272 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
273 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
274 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
275 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
276 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
277 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
278 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
279 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
280 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
281 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
282 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
283 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
284 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
285 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
286 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
287 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
288 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
289 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
290 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
291 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
292 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
293 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
294 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
295 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
296 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
297 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
298 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
299 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
300 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
301 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200302# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200303 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
304 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
305 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
306 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
307 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200308# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200309 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
310 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200311# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200312 {"", NULL},
313};
314
315/*
316 * Free python.dll
317 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200318 static void
319end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200320{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200321 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200322 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200323 close_dll(hinstPy3);
324 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200325 }
326}
327
328/*
329 * Load library and get all pointers.
330 * Parameter 'libname' provides name of DLL.
331 * Return OK or FAIL.
332 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200333 static int
334py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200335{
336 int i;
Bram Moolenaar69154f22010-07-18 21:42:34 +0200337 void *ucs_from_string, *ucs_from_string_and_size;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200339# if !defined(PY_NO_RTLD_GLOBAL) && defined(UNIX) && defined(FEAT_PYTHON)
340 /* Can't have Python and Python3 loaded at the same time.
341 * It cause a crash, because RTLD_GLOBAL is needed for
342 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200343 if (python_loaded())
344 {
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200345 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200346 return FAIL;
347 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200348# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200349
350 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200351 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200352 hinstPy3 = load_dll(libname);
353
354 if (!hinstPy3)
355 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200356 if (verbose)
357 EMSG2(_(e_loadlib), libname);
358 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359 }
360
361 for (i = 0; py3_funcname_table[i].ptr; ++i)
362 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200363 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
364 py3_funcname_table[i].name)) == NULL)
365 {
366 close_dll(hinstPy3);
367 hinstPy3 = 0;
368 if (verbose)
369 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
370 return FAIL;
371 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372 }
373
Bram Moolenaar69154f22010-07-18 21:42:34 +0200374 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
375 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200376 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
377 ucs_from_string_and_size = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200378 "PyUnicodeUCS2_FromStringAndSize");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200379 if (!ucs_from_string || !ucs_from_string_and_size)
380 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200381 ucs_from_string = symbol_from_dll(hinstPy3,
382 "PyUnicodeUCS4_FromString");
383 ucs_from_string_and_size = symbol_from_dll(hinstPy3,
384 "PyUnicodeUCS4_FromStringAndSize");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200385 }
386 if (ucs_from_string && ucs_from_string_and_size)
387 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200388 py3_PyUnicode_FromString = ucs_from_string;
389 py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390 }
391 else
392 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200393 close_dll(hinstPy3);
394 hinstPy3 = 0;
395 if (verbose)
396 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
397 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398 }
399
400 return OK;
401}
402
403/*
404 * If python is enabled (there is installed python on Windows system) return
405 * TRUE, else FALSE.
406 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200407 int
408python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200409{
410 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
411}
412
413/* Load the standard Python exceptions - don't import the symbols from the
414 * DLL, as this can cause errors (importing data symbols is not reliable).
415 */
416static void get_py3_exceptions __ARGS((void));
417
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200418 static void
419get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200420{
421 PyObject *exmod = PyImport_ImportModule("builtins");
422 PyObject *exdict = PyModule_GetDict(exmod);
423 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
424 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
425 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
426 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
427 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
428 Py_XINCREF(p3imp_PyExc_AttributeError);
429 Py_XINCREF(p3imp_PyExc_IndexError);
430 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
431 Py_XINCREF(p3imp_PyExc_TypeError);
432 Py_XINCREF(p3imp_PyExc_ValueError);
433 Py_XDECREF(exmod);
434}
435#endif /* DYNAMIC_PYTHON3 */
436
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200437static PyObject *BufferNew (buf_T *);
438static PyObject *WindowNew(win_T *);
439static PyObject *LineToString(const char *);
440
441static PyTypeObject RangeType;
442
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200443/*
444 * Include the code shared with if_python.c
445 */
446#include "if_py_both.h"
447
448 static void
449call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200450{
451#ifdef Py_DEBUG
452 _PyObject_DebugFree(p);
453#else
454 PyObject_Free(p);
455#endif
456}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200457
458 static PyObject *
459call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200460{
461 return PyType_GenericNew(type,args,kwds);
462}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200463
464 static PyObject *
465call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466{
467 return PyType_GenericAlloc(type,nitems);
468}
469
470/******************************************************
471 * Internal function prototypes.
472 */
473
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200474static Py_ssize_t RangeStart;
475static Py_ssize_t RangeEnd;
476
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477static int PythonIO_Init(void);
478static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200479PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481/******************************************************
482 * 1. Python interpreter main program.
483 */
484
485static int py3initialised = 0;
486
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200487static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
488
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200489 void
490python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491{
492 static int recurse = 0;
493
494 /* If a crash occurs while doing this, don't try again. */
495 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200496 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497
498 ++recurse;
499
500#ifdef DYNAMIC_PYTHON3
501 if (hinstPy3)
502#endif
503 if (Py_IsInitialized())
504 {
505 // acquire lock before finalizing
506 pygilstate = PyGILState_Ensure();
507
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200508 PythonIO_Fini();
509 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200510 }
511
512#ifdef DYNAMIC_PYTHON3
513 end_dynamic_python3();
514#endif
515
516 --recurse;
517}
518
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200519#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
520 int
521python3_loaded()
522{
523 return (hinstPy3 != 0);
524}
525#endif
526
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200527 static int
528Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200529{
530 if (!py3initialised)
531 {
532#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200533 if (!python3_enabled(TRUE))
534 {
535 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
536 goto fail;
537 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200538#endif
539
540 init_structs();
541
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200542 /* initialise threads */
543 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200544
545#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200546 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200547#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200548 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200549#endif
550
551#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200552 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200553#endif
554
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200555 if (PythonIO_Init())
556 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200557
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200558 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200559
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200560 /* Remove the element from sys.path that was added because of our
561 * argv[0] value in Py3Init_vim(). Previously we used an empty
562 * string, but dependinding on the OS we then get an empty entry or
563 * the current directory in sys.path. */
564 PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200565
566 // lock is created and acquired in PyEval_InitThreads() and thread
567 // state is created in Py_Initialize()
568 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
569 // (python must have threads enabled!)
570 // so the following does both: unlock GIL and save thread state in TLS
571 // without deleting thread state
572 PyGILState_Release(pygilstate);
573
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200574 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200575 }
576
577 return 0;
578
579fail:
580 /* We call PythonIO_Flush() here to print any Python errors.
581 * This is OK, as it is possible to call this function even
582 * if PythonIO_Init() has not completed successfully (it will
583 * not do anything in this case).
584 */
585 PythonIO_Flush();
586 return -1;
587}
588
589/*
590 * External interface
591 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200592 static void
593DoPy3Command(exarg_T *eap, const char *cmd)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200594{
595#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200596 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200597#endif
598#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200599 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200600#endif
601
602#if defined(MACOS) && !defined(MACOS_X_UNIX)
603 GetPort(&oldPort);
604 /* Check if the Python library is available */
605 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200606 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200607#endif
608 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200609 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610
611 RangeStart = eap->line1;
612 RangeEnd = eap->line2;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200613 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614
615#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
616 /* Python only works properly when the LC_NUMERIC locale is "C". */
617 saved_locale = setlocale(LC_NUMERIC, NULL);
618 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200619 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200620 else
621 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200622 /* Need to make a copy, value may change when setting new locale. */
623 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
624 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200625 }
626#endif
627
628 pygilstate = PyGILState_Ensure();
629
630 PyRun_SimpleString((char *)(cmd));
631
632 PyGILState_Release(pygilstate);
633
634#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
635 if (saved_locale != NULL)
636 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200637 (void)setlocale(LC_NUMERIC, saved_locale);
638 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200639 }
640#endif
641
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200642 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200643 PythonIO_Flush();
644#if defined(MACOS) && !defined(MACOS_X_UNIX)
645 SetPort(oldPort);
646#endif
647
648theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200649 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650}
651
652/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200653 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200654 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200655 void
656ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657{
658 char_u *script;
659
660 script = script_get(eap, eap->arg);
661 if (!eap->skip)
662 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200663 if (script == NULL)
664 DoPy3Command(eap, (char *)eap->arg);
665 else
666 DoPy3Command(eap, (char *)script);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200667 }
668 vim_free(script);
669}
670
671#define BUFFER_SIZE 2048
672
673/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200674 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200675 */
676 void
677ex_py3file(exarg_T *eap)
678{
679 static char buffer[BUFFER_SIZE];
680 const char *file;
681 char *p;
682 int i;
683
684 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
685 * stdio file pointer, but Vim and the Python DLL are compiled with
686 * different options under Windows, meaning that stdio pointers aren't
687 * compatible between the two. Yuk.
688 *
689 * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec'))
690 *
691 * We need to escape any backslashes or single quotes in the file name, so that
692 * Python won't mangle the file name.
693 */
694
695 strcpy(buffer, "exec(compile(open('");
696 p = buffer + 19; /* size of "exec(compile(open('" */
697
698 for (i=0; i<2; ++i)
699 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200700 file = (char *)eap->arg;
701 while (*file && p < buffer + (BUFFER_SIZE - 3))
702 {
703 if (*file == '\\' || *file == '\'')
704 *p++ = '\\';
705 *p++ = *file++;
706 }
707 /* If we didn't finish the file name, we hit a buffer overflow */
708 if (*file != '\0')
709 return;
710 if (i==0)
711 {
712 strcpy(p,"').read(),'");
713 p += 11;
714 }
715 else
716 {
717 strcpy(p,"','exec'))");
718 p += 10;
719 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200720 }
721
722
723 /* Execute the file */
724 DoPy3Command(eap, buffer);
725}
726
727/******************************************************
728 * 2. Python output stream: writes output via [e]msg().
729 */
730
731/* Implementation functions
732 */
733
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200734 static PyObject *
735OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736{
737 char *name = "";
738 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200739 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200740
741 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200742 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200743
744 return PyObject_GenericGetAttr(self, nameobj);
745}
746
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200747 static int
748OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200749{
750 char *name = "";
751 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200752 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753
754 if (val == NULL) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200755 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
756 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200757 }
758
759 if (strcmp(name, "softspace") == 0)
760 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200761 if (!PyLong_Check(val)) {
762 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
763 return -1;
764 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200765
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200766 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
767 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200768 }
769
770 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
771 return -1;
772}
773
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200774/***************/
775
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200776 static int
777PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778{
779 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200780 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200781}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200782
783 static void
784PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200785{
786 PySys_SetObject("stdout", NULL);
787 PySys_SetObject("stderr", NULL);
788}
789
790/******************************************************
791 * 3. Implementation of the Vim module for Python
792 */
793
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200794/* Window type - Implementation functions
795 * --------------------------------------
796 */
797
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200798#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
799
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200800/* Buffer type - Implementation functions
801 * --------------------------------------
802 */
803
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200804#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
805
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806static Py_ssize_t BufferLength(PyObject *);
807static PyObject *BufferItem(PyObject *, Py_ssize_t);
808static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *);
809static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
810
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200811
812/* Line range type - Implementation functions
813 * --------------------------------------
814 */
815
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200816#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
817
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200818static PyObject* RangeSubscript(PyObject *self, PyObject* idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
820
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200821/* Current objects type - Implementation functions
822 * -----------------------------------------------
823 */
824
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200825static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200826 (lenfunc) BufferLength, /* sq_length, len(x) */
827 (binaryfunc) 0, /* sq_concat, x+y */
828 (ssizeargfunc) 0, /* sq_repeat, x*n */
829 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
830 0, /* was_sq_slice, x[i:j] */
831 (ssizeobjargproc) BufferAsItem, /* sq_ass_item, x[i]=v */
832 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200833 0, /* sq_contains */
834 0, /* sq_inplace_concat */
835 0, /* sq_inplace_repeat */
836};
837
838PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200839 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200840 /* mp_subscript */ (binaryfunc)BufferSubscript,
841 /* mp_ass_subscript */ (objobjargproc)0,
842};
843
844
845/* Buffer object - Definitions
846 */
847
848static PyTypeObject BufferType;
849
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200850 static PyObject *
851BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852{
853 /* We need to handle deletion of buffers underneath us.
854 * If we add a "b_python3_ref" field to the buf_T structure,
855 * then we can get at it in buf_freeall() in vim. We then
856 * need to create only ONE Python object per buffer - if
857 * we try to create a second, just INCREF the existing one
858 * and return it. The (single) Python object referring to
859 * the buffer is stored in "b_python3_ref".
860 * Question: what to do on a buf_freeall(). We'll probably
861 * have to either delete the Python object (DECREF it to
862 * zero - a bad idea, as it leaves dangling refs!) or
863 * set the buf_T * value to an invalid value (-1?), which
864 * means we need checks in all access functions... Bah.
865 */
866
867 BufferObject *self;
868
869 if (buf->b_python3_ref != NULL)
870 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200871 self = buf->b_python3_ref;
872 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200873 }
874 else
875 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200876 self = PyObject_NEW(BufferObject, &BufferType);
877 buf->b_python3_ref = self;
878 if (self == NULL)
879 return NULL;
880 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881 }
882
883 return (PyObject *)(self);
884}
885
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200886 static void
887BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200888{
889 BufferObject *this = (BufferObject *)(self);
890
891 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200892 this->buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893}
894
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200895 static PyObject *
896BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200897{
898 BufferObject *this = (BufferObject *)(self);
899
900 char *name = "";
901 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200902 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903
904 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200905 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200906
907 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200908 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200909 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200910 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200911 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200912 return Py_BuildValue("[ss]", "name", "number");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200914 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200915}
916
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200917 static PyObject *
918BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200919{
920 static char repr[100];
921 BufferObject *this = (BufferObject *)(self);
922
923 if (this->buf == INVALID_BUFFER_VALUE)
924 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200925 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
926 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200927 }
928 else
929 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200930 char *name = (char *)this->buf->b_fname;
931 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200932
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200933 if (name == NULL)
934 name = "";
935 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200936
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200937 if (len > 35)
938 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200940 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200941
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200942 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200943 }
944}
945
946/******************/
947
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200948 static Py_ssize_t
949BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950{
951 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200952 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200953
954 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
955}
956
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200957 static PyObject *
958BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200959{
960 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200961 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962}
963
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200964 static PyObject *
965BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
966{
967 return RBSlice((BufferObject *)(self), lo, hi, 1,
968 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
969}
970
971 static Py_ssize_t
972BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200973{
974 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200975 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
976 NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200977}
978
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200980 static PyObject *
981BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982{
983 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200984 long _idx = PyLong_AsLong(idx);
985 return BufferItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200987 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200989 if (PySlice_GetIndicesEx((PySliceObject *)idx,
990 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
991 &start, &stop,
992 &step, &slicelen) < 0) {
993 return NULL;
994 }
995 return BufferSlice(self,start,stop+1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200997 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
998 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999 }
1000}
1001
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001003 (lenfunc) RangeLength, /* sq_length, len(x) */
1004 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1005 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1006 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1007 0, /* was_sq_slice, x[i:j] */
1008 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1009 0, /* sq_ass_slice, x[i:j]=v */
1010 0, /* sq_contains */
1011 0, /* sq_inplace_concat */
1012 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001013};
1014
1015PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001016 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017 /* mp_subscript */ (binaryfunc)RangeSubscript,
1018 /* mp_ass_subscript */ (objobjargproc)0,
1019};
1020
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021/* Line range object - Implementation
1022 */
1023
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001024 static void
1025RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026{
1027 Py_DECREF(((RangeObject *)(self))->buf);
1028}
1029
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001030 static PyObject *
1031RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032{
1033 char *name = "";
1034 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001035 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001036
1037 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001038 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001040 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001041 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001042 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043}
1044
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001045/****************/
1046
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001047 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001048RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049{
1050 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001051 ((RangeObject *)(self))->start,
1052 ((RangeObject *)(self))->end,
1053 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054}
1055
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001056 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001057RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001058{
1059 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001060 long _idx = PyLong_AsLong(idx);
1061 return RangeItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001062 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001063 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001064
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001065 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1066 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1067 &start, &stop,
1068 &step, &slicelen) < 0) {
1069 return NULL;
1070 }
1071 return RangeSlice(self,start,stop+1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001073 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1074 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001075 }
1076}
1077
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001078/* Buffer list object - Definitions
1079 */
1080
1081typedef struct
1082{
1083 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001084} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001085
1086static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001087 (lenfunc) BufListLength, /* sq_length, len(x) */
1088 (binaryfunc) 0, /* sq_concat, x+y */
1089 (ssizeargfunc) 0, /* sq_repeat, x*n */
1090 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1091 0, /* was_sq_slice, x[i:j] */
1092 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1093 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001094 0, /* sq_contains */
1095 0, /* sq_inplace_concat */
1096 0, /* sq_inplace_repeat */
1097};
1098
1099static PyTypeObject BufListType;
1100
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001101/* Window object - Definitions
1102 */
1103
1104static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001105 /* name, function, calling, documentation */
1106 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107};
1108
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001109static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110
1111/* Window object - Implementation
1112 */
1113
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001114 static PyObject *
1115WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001116{
1117 /* We need to handle deletion of windows underneath us.
1118 * If we add a "w_python3_ref" field to the win_T structure,
1119 * then we can get at it in win_free() in vim. We then
1120 * need to create only ONE Python object per window - if
1121 * we try to create a second, just INCREF the existing one
1122 * and return it. The (single) Python object referring to
1123 * the window is stored in "w_python3_ref".
1124 * On a win_free() we set the Python object's win_T* field
1125 * to an invalid value. We trap all uses of a window
1126 * object, and reject them if the win_T* field is invalid.
1127 */
1128
1129 WindowObject *self;
1130
1131 if (win->w_python3_ref)
1132 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 self = win->w_python3_ref;
1134 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135 }
1136 else
1137 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001138 self = PyObject_NEW(WindowObject, &WindowType);
1139 if (self == NULL)
1140 return NULL;
1141 self->win = win;
1142 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001143 }
1144
1145 return (PyObject *)(self);
1146}
1147
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001148 static void
1149WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001150{
1151 WindowObject *this = (WindowObject *)(self);
1152
1153 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001154 this->win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155}
1156
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001157 static PyObject *
1158WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001159{
1160 WindowObject *this = (WindowObject *)(self);
1161
1162 char *name = "";
1163 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001164 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001165
1166
1167 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001168 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169
1170 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172 else if (strcmp(name, "cursor") == 0)
1173 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001174 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001175
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177 }
1178 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001179 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001180#ifdef FEAT_VERTSPLIT
1181 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001182 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001183#endif
1184 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001185 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188}
1189
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001190 static int
1191WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001192{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001193 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001194
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001195 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001196 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001198 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001199}
1200
1201/* Window list object - Definitions
1202 */
1203
1204typedef struct
1205{
1206 PyObject_HEAD
1207}
1208WinListObject;
1209
1210static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001211 (lenfunc) WinListLength, /* sq_length, len(x) */
1212 (binaryfunc) 0, /* sq_concat, x+y */
1213 (ssizeargfunc) 0, /* sq_repeat, x*n */
1214 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1215 0, /* sq_slice, x[i:j] */
1216 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1217 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001218 0, /* sq_contains */
1219 0, /* sq_inplace_concat */
1220 0, /* sq_inplace_repeat */
1221};
1222
1223static PyTypeObject WinListType;
1224
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001225/* Current items object - Definitions
1226 */
1227
1228typedef struct
1229{
1230 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001231} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001232
1233static PyTypeObject CurrentType;
1234
1235/* Current items object - Implementation
1236 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001237 static PyObject *
1238CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239{
1240 char *name = "";
1241 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001242 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243
1244 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001247 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001248 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001249 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001250 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001254 else
1255 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001256 PyErr_SetString(PyExc_AttributeError, name);
1257 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 }
1259}
1260
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001261 static int
1262CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001263{
1264 char *name = "";
1265 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001266 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001267
1268 if (strcmp(name, "line") == 0)
1269 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001270 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1271 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001272
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001273 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001274 }
1275 else
1276 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001277 PyErr_SetString(PyExc_AttributeError, name);
1278 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001279 }
1280}
1281
1282/* External interface
1283 */
1284
1285 void
1286python3_buffer_free(buf_T *buf)
1287{
1288 if (buf->b_python3_ref != NULL)
1289 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 BufferObject *bp = buf->b_python3_ref;
1291 bp->buf = INVALID_BUFFER_VALUE;
1292 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001293 }
1294}
1295
1296#if defined(FEAT_WINDOWS) || defined(PROTO)
1297 void
1298python3_window_free(win_T *win)
1299{
1300 if (win->w_python3_ref != NULL)
1301 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001302 WindowObject *wp = win->w_python3_ref;
1303 wp->win = INVALID_WINDOW_VALUE;
1304 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001305 }
1306}
1307#endif
1308
1309static BufListObject TheBufferList =
1310{
1311 PyObject_HEAD_INIT(&BufListType)
1312};
1313
1314static WinListObject TheWindowList =
1315{
1316 PyObject_HEAD_INIT(&WinListType)
1317};
1318
1319static CurrentObject TheCurrent =
1320{
1321 PyObject_HEAD_INIT(&CurrentType)
1322};
1323
1324PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1325
1326static struct PyModuleDef vimmodule;
1327
Bram Moolenaar69154f22010-07-18 21:42:34 +02001328#ifndef PROTO
1329PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001330{
1331 PyObject *mod;
1332 /* The special value is removed from sys.path in Python3_Init(). */
1333 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1334
1335 PyType_Ready(&BufferType);
1336 PyType_Ready(&RangeType);
1337 PyType_Ready(&WindowType);
1338 PyType_Ready(&BufListType);
1339 PyType_Ready(&WinListType);
1340 PyType_Ready(&CurrentType);
1341
1342 /* Set sys.argv[] to avoid a crash in warn(). */
1343 PySys_SetArgv(1, argv);
1344
1345 mod = PyModule_Create(&vimmodule);
1346
1347 VimError = Py_BuildValue("s", "vim.error");
1348
1349 PyModule_AddObject(mod, "error", VimError);
1350 Py_INCREF((PyObject *)(void *)&TheBufferList);
1351 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1352 Py_INCREF((PyObject *)(void *)&TheCurrent);
1353 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1354 Py_INCREF((PyObject *)(void *)&TheWindowList);
1355 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1356
1357 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001358 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001359
1360 return mod;
1361}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001362#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363
1364/*************************************************************************
1365 * 4. Utility functions for handling the interface between Vim and Python.
1366 */
1367
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368/* Convert a Vim line into a Python string.
1369 * All internal newlines are replaced by null characters.
1370 *
1371 * On errors, the Python exception data is set, and NULL is returned.
1372 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001373 static PyObject *
1374LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001375{
1376 PyObject *result;
1377 Py_ssize_t len = strlen(str);
1378 char *tmp,*p;
1379
1380 tmp = (char *)alloc((unsigned)(len+1));
1381 p = tmp;
1382 if (p == NULL)
1383 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001384 PyErr_NoMemory();
1385 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001386 }
1387
1388 while (*str)
1389 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001390 if (*str == '\n')
1391 *p = '\0';
1392 else
1393 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 ++p;
1396 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001397 }
1398 *p = '\0';
1399
1400 result = PyUnicode_FromStringAndSize(tmp, len);
1401
1402 vim_free(tmp);
1403 return result;
1404}
1405
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001406 static void
1407init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408{
1409 vim_memset(&OutputType, 0, sizeof(OutputType));
1410 OutputType.tp_name = "vim.message";
1411 OutputType.tp_basicsize = sizeof(OutputObject);
1412 OutputType.tp_getattro = OutputGetattro;
1413 OutputType.tp_setattro = OutputSetattro;
1414 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1415 OutputType.tp_doc = "vim message object";
1416 OutputType.tp_methods = OutputMethods;
1417 OutputType.tp_alloc = call_PyType_GenericAlloc;
1418 OutputType.tp_new = call_PyType_GenericNew;
1419 OutputType.tp_free = call_PyObject_Free;
1420
1421 vim_memset(&BufferType, 0, sizeof(BufferType));
1422 BufferType.tp_name = "vim.buffer";
1423 BufferType.tp_basicsize = sizeof(BufferType);
1424 BufferType.tp_dealloc = BufferDestructor;
1425 BufferType.tp_repr = BufferRepr;
1426 BufferType.tp_as_sequence = &BufferAsSeq;
1427 BufferType.tp_as_mapping = &BufferAsMapping;
1428 BufferType.tp_getattro = BufferGetattro;
1429 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1430 BufferType.tp_doc = "vim buffer object";
1431 BufferType.tp_methods = BufferMethods;
1432 BufferType.tp_alloc = call_PyType_GenericAlloc;
1433 BufferType.tp_new = call_PyType_GenericNew;
1434 BufferType.tp_free = call_PyObject_Free;
1435
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001436 vim_memset(&WindowType, 0, sizeof(WindowType));
1437 WindowType.tp_name = "vim.window";
1438 WindowType.tp_basicsize = sizeof(WindowObject);
1439 WindowType.tp_dealloc = WindowDestructor;
1440 WindowType.tp_repr = WindowRepr;
1441 WindowType.tp_getattro = WindowGetattro;
1442 WindowType.tp_setattro = WindowSetattro;
1443 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1444 WindowType.tp_doc = "vim Window object";
1445 WindowType.tp_methods = WindowMethods;
1446 WindowType.tp_alloc = call_PyType_GenericAlloc;
1447 WindowType.tp_new = call_PyType_GenericNew;
1448 WindowType.tp_free = call_PyObject_Free;
1449
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001450 vim_memset(&BufListType, 0, sizeof(BufListType));
1451 BufListType.tp_name = "vim.bufferlist";
1452 BufListType.tp_basicsize = sizeof(BufListObject);
1453 BufListType.tp_as_sequence = &BufListAsSeq;
1454 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1455 BufferType.tp_doc = "vim buffer list";
1456
1457 vim_memset(&WinListType, 0, sizeof(WinListType));
1458 WinListType.tp_name = "vim.windowlist";
1459 WinListType.tp_basicsize = sizeof(WinListType);
1460 WinListType.tp_as_sequence = &WinListAsSeq;
1461 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1462 WinListType.tp_doc = "vim window list";
1463
1464 vim_memset(&RangeType, 0, sizeof(RangeType));
1465 RangeType.tp_name = "vim.range";
1466 RangeType.tp_basicsize = sizeof(RangeObject);
1467 RangeType.tp_dealloc = RangeDestructor;
1468 RangeType.tp_repr = RangeRepr;
1469 RangeType.tp_as_sequence = &RangeAsSeq;
1470 RangeType.tp_as_mapping = &RangeAsMapping;
1471 RangeType.tp_getattro = RangeGetattro;
1472 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1473 RangeType.tp_doc = "vim Range object";
1474 RangeType.tp_methods = RangeMethods;
1475 RangeType.tp_alloc = call_PyType_GenericAlloc;
1476 RangeType.tp_new = call_PyType_GenericNew;
1477 RangeType.tp_free = call_PyObject_Free;
1478
1479 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1480 CurrentType.tp_name = "vim.currentdata";
1481 CurrentType.tp_basicsize = sizeof(CurrentObject);
1482 CurrentType.tp_getattro = CurrentGetattro;
1483 CurrentType.tp_setattro = CurrentSetattro;
1484 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1485 CurrentType.tp_doc = "vim current object";
1486
1487 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1488 vimmodule.m_name = "vim";
1489 vimmodule.m_doc = vim_module_doc;
1490 vimmodule.m_size = -1;
1491 vimmodule.m_methods = VimMethods;
1492}