blob: 91d3f65c157b59629c670f7e6d85089830c99ad1 [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
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010025/* uncomment this if used with the debug version of python */
26/* #define Py_DEBUG */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020027
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 Moolenaar0c1f3f42011-02-25 15:18:50 +010077#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020078
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020079# ifndef WIN3264
80# include <dlfcn.h>
81# define FARPROC void*
82# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +010083# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_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
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100135# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200136# define Py_Initialize py3_Py_Initialize
137# define Py_Finalize py3_Py_Finalize
138# define Py_IsInitialized py3_Py_IsInitialized
139# define _Py_NoneStruct (*py3__Py_NoneStruct)
140# define PyModule_AddObject py3_PyModule_AddObject
141# define PyImport_AppendInittab py3_PyImport_AppendInittab
142# define _PyUnicode_AsString py3__PyUnicode_AsString
143# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
144# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200145# ifdef Py_DEBUG
146# define _Py_NegativeRefcount py3__Py_NegativeRefcount
147# define _Py_RefTotal (*py3__Py_RefTotal)
148# define _Py_Dealloc py3__Py_Dealloc
149# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
150# define _PyObject_DebugFree py3__PyObject_DebugFree
151# else
152# define PyObject_Malloc py3_PyObject_Malloc
153# define PyObject_Free py3_PyObject_Free
154# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200155# define PyType_GenericAlloc py3_PyType_GenericAlloc
156# define PyType_GenericNew py3_PyType_GenericNew
157# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200158# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200159# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200160# undef PyUnicode_FromStringAndSize
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize
162
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200163# ifdef Py_DEBUG
164# undef PyObject_NEW
165# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200166( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200167 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200168# endif
169
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200170/*
171 * Pointers for dynamic link
172 */
173static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100174static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200175static void (*py3_Py_Initialize)(void);
176static PyObject* (*py3_PyList_New)(Py_ssize_t size);
177static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
178static void (*py3_PyGILState_Release)(PyGILState_STATE);
179static int (*py3_PySys_SetObject)(char *, PyObject *);
180static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
181static Py_ssize_t (*py3_PyList_Size)(PyObject *);
182static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200183 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200184static PyObject* (*py3_PyErr_NoMemory)(void);
185static void (*py3_Py_Finalize)(void);
186static void (*py3_PyErr_SetString)(PyObject *, const char *);
187static int (*py3_PyRun_SimpleString)(char *);
188static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
189static PyObject* (*py3_PyImport_ImportModule)(const char *);
190static int (*py3_PyErr_BadArgument)(void);
191static PyTypeObject* py3_PyType_Type;
192static PyObject* (*py3_PyErr_Occurred)(void);
193static PyObject* (*py3_PyModule_GetDict)(PyObject *);
194static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
195static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
196static PyObject* (*py3_PyLong_FromLong)(long);
197static PyObject* (*py3_PyDict_New)(void);
198static PyObject* (*py3_Py_BuildValue)(char *, ...);
199static int (*py3_PyType_Ready)(PyTypeObject *type);
200static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
201static PyObject* (*py3_PyUnicode_FromString)(const char *u);
202static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size);
203static long (*py3_PyLong_AsLong)(PyObject *);
204static void (*py3_PyErr_SetNone)(PyObject *);
205static void (*py3_PyEval_InitThreads)(void);
206static void(*py3_PyEval_RestoreThread)(PyThreadState *);
207static PyThreadState*(*py3_PyEval_SaveThread)(void);
208static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
209static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
210static int (*py3_Py_IsInitialized)(void);
211static void (*py3_PyErr_Clear)(void);
212static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
213static PyObject* py3__Py_NoneStruct;
214static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
215static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
216static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
217static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
218static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
219static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
220static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
221static PyTypeObject* py3_PySlice_Type;
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200222# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200223 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
224 static Py_ssize_t* py3__Py_RefTotal;
225 static void (*py3__Py_Dealloc)(PyObject *obj);
226 static void (*py3__PyObject_DebugFree)(void*);
227 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200228# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229 static void (*py3_PyObject_Free)(void*);
230 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200231# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200232
233static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
234
235/* Imported exception objects */
236static PyObject *p3imp_PyExc_AttributeError;
237static PyObject *p3imp_PyExc_IndexError;
238static PyObject *p3imp_PyExc_KeyboardInterrupt;
239static PyObject *p3imp_PyExc_TypeError;
240static PyObject *p3imp_PyExc_ValueError;
241
242# define PyExc_AttributeError p3imp_PyExc_AttributeError
243# define PyExc_IndexError p3imp_PyExc_IndexError
244# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
245# define PyExc_TypeError p3imp_PyExc_TypeError
246# define PyExc_ValueError p3imp_PyExc_ValueError
247
248/*
249 * Table of name to function pointer of python.
250 */
251# define PYTHON_PROC FARPROC
252static struct
253{
254 char *name;
255 PYTHON_PROC *ptr;
256} py3_funcname_table[] =
257{
258 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100259 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200260 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
261 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
262 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
263 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
264 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
265 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
266 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
267 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
268 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
269 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
270 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
271 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
272 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
273 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
274 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
275 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
276 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
277 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
278 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
279 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
280 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
281 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
282 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
283 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
284 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
285 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
286 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
287 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
288 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
289 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
290 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
291 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
292 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
293 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
294 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
295 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
296 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
297 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
298 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
299 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
300 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
301 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
302 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
303 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
304 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200306 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
307 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
308 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
309 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
310 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200311# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200312 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
313 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200314# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200315 {"", NULL},
316};
317
318/*
319 * Free python.dll
320 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200321 static void
322end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200324 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200325 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200326 close_dll(hinstPy3);
327 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328 }
329}
330
331/*
332 * Load library and get all pointers.
333 * Parameter 'libname' provides name of DLL.
334 * Return OK or FAIL.
335 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200336 static int
337py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338{
339 int i;
Bram Moolenaar69154f22010-07-18 21:42:34 +0200340 void *ucs_from_string, *ucs_from_string_and_size;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100342# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200343 /* Can't have Python and Python3 loaded at the same time.
344 * It cause a crash, because RTLD_GLOBAL is needed for
345 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200346 if (python_loaded())
347 {
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200348 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200349 return FAIL;
350 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200351# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200352
353 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200354 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200355 hinstPy3 = load_dll(libname);
356
357 if (!hinstPy3)
358 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200359 if (verbose)
360 EMSG2(_(e_loadlib), libname);
361 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362 }
363
364 for (i = 0; py3_funcname_table[i].ptr; ++i)
365 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200366 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
367 py3_funcname_table[i].name)) == NULL)
368 {
369 close_dll(hinstPy3);
370 hinstPy3 = 0;
371 if (verbose)
372 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
373 return FAIL;
374 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200375 }
376
Bram Moolenaar69154f22010-07-18 21:42:34 +0200377 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
378 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200379 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
380 ucs_from_string_and_size = symbol_from_dll(hinstPy3,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200381 "PyUnicodeUCS2_FromStringAndSize");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200382 if (!ucs_from_string || !ucs_from_string_and_size)
383 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200384 ucs_from_string = symbol_from_dll(hinstPy3,
385 "PyUnicodeUCS4_FromString");
386 ucs_from_string_and_size = symbol_from_dll(hinstPy3,
387 "PyUnicodeUCS4_FromStringAndSize");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388 }
389 if (ucs_from_string && ucs_from_string_and_size)
390 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200391 py3_PyUnicode_FromString = ucs_from_string;
392 py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200393 }
394 else
395 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200396 close_dll(hinstPy3);
397 hinstPy3 = 0;
398 if (verbose)
399 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
400 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401 }
402
403 return OK;
404}
405
406/*
407 * If python is enabled (there is installed python on Windows system) return
408 * TRUE, else FALSE.
409 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200410 int
411python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200412{
413 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
414}
415
416/* Load the standard Python exceptions - don't import the symbols from the
417 * DLL, as this can cause errors (importing data symbols is not reliable).
418 */
419static void get_py3_exceptions __ARGS((void));
420
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200421 static void
422get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200423{
424 PyObject *exmod = PyImport_ImportModule("builtins");
425 PyObject *exdict = PyModule_GetDict(exmod);
426 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
427 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
428 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
429 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
430 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
431 Py_XINCREF(p3imp_PyExc_AttributeError);
432 Py_XINCREF(p3imp_PyExc_IndexError);
433 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
434 Py_XINCREF(p3imp_PyExc_TypeError);
435 Py_XINCREF(p3imp_PyExc_ValueError);
436 Py_XDECREF(exmod);
437}
438#endif /* DYNAMIC_PYTHON3 */
439
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200440static PyObject *BufferNew (buf_T *);
441static PyObject *WindowNew(win_T *);
442static PyObject *LineToString(const char *);
443
444static PyTypeObject RangeType;
445
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200446/*
447 * Include the code shared with if_python.c
448 */
449#include "if_py_both.h"
450
451 static void
452call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200453{
454#ifdef Py_DEBUG
455 _PyObject_DebugFree(p);
456#else
457 PyObject_Free(p);
458#endif
459}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200460
461 static PyObject *
462call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463{
464 return PyType_GenericNew(type,args,kwds);
465}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466
467 static PyObject *
468call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469{
470 return PyType_GenericAlloc(type,nitems);
471}
472
473/******************************************************
474 * Internal function prototypes.
475 */
476
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477static Py_ssize_t RangeStart;
478static Py_ssize_t RangeEnd;
479
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480static int PythonIO_Init(void);
481static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200482PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200483
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484/******************************************************
485 * 1. Python interpreter main program.
486 */
487
488static int py3initialised = 0;
489
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200490static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
491
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200492 void
493python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494{
495 static int recurse = 0;
496
497 /* If a crash occurs while doing this, don't try again. */
498 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200499 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200500
501 ++recurse;
502
503#ifdef DYNAMIC_PYTHON3
504 if (hinstPy3)
505#endif
506 if (Py_IsInitialized())
507 {
508 // acquire lock before finalizing
509 pygilstate = PyGILState_Ensure();
510
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 PythonIO_Fini();
512 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200513 }
514
515#ifdef DYNAMIC_PYTHON3
516 end_dynamic_python3();
517#endif
518
519 --recurse;
520}
521
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200522#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
523 int
524python3_loaded()
525{
526 return (hinstPy3 != 0);
527}
528#endif
529
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200530 static int
531Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532{
533 if (!py3initialised)
534 {
535#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200536 if (!python3_enabled(TRUE))
537 {
538 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
539 goto fail;
540 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541#endif
542
543 init_structs();
544
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100545
546#ifdef PYTHON3_HOME
547 Py_SetPythonHome(PYTHON3_HOME);
548#endif
549
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200550 /* initialise threads */
551 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200552
553#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200554 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200555#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200556 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200557#endif
558
559#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200560 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200561#endif
562
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200563 if (PythonIO_Init())
564 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200565
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200566 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200567
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200568 /* Remove the element from sys.path that was added because of our
569 * argv[0] value in Py3Init_vim(). Previously we used an empty
570 * string, but dependinding on the OS we then get an empty entry or
571 * the current directory in sys.path. */
572 PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200573
574 // lock is created and acquired in PyEval_InitThreads() and thread
575 // state is created in Py_Initialize()
576 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
577 // (python must have threads enabled!)
578 // so the following does both: unlock GIL and save thread state in TLS
579 // without deleting thread state
580 PyGILState_Release(pygilstate);
581
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200582 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200583 }
584
585 return 0;
586
587fail:
588 /* We call PythonIO_Flush() here to print any Python errors.
589 * This is OK, as it is possible to call this function even
590 * if PythonIO_Init() has not completed successfully (it will
591 * not do anything in this case).
592 */
593 PythonIO_Flush();
594 return -1;
595}
596
597/*
598 * External interface
599 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200600 static void
601DoPy3Command(exarg_T *eap, const char *cmd)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200602{
603#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200604 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605#endif
606#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200607 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200608#endif
609
610#if defined(MACOS) && !defined(MACOS_X_UNIX)
611 GetPort(&oldPort);
612 /* Check if the Python library is available */
613 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200614 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200615#endif
616 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200617 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200618
619 RangeStart = eap->line1;
620 RangeEnd = eap->line2;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200621 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200622
623#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
624 /* Python only works properly when the LC_NUMERIC locale is "C". */
625 saved_locale = setlocale(LC_NUMERIC, NULL);
626 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200627 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200628 else
629 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200630 /* Need to make a copy, value may change when setting new locale. */
631 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
632 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633 }
634#endif
635
636 pygilstate = PyGILState_Ensure();
637
638 PyRun_SimpleString((char *)(cmd));
639
640 PyGILState_Release(pygilstate);
641
642#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
643 if (saved_locale != NULL)
644 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200645 (void)setlocale(LC_NUMERIC, saved_locale);
646 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647 }
648#endif
649
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200650 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200651 PythonIO_Flush();
652#if defined(MACOS) && !defined(MACOS_X_UNIX)
653 SetPort(oldPort);
654#endif
655
656theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200657 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200658}
659
660/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200661 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200662 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200663 void
664ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200665{
666 char_u *script;
667
668 script = script_get(eap, eap->arg);
669 if (!eap->skip)
670 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200671 if (script == NULL)
672 DoPy3Command(eap, (char *)eap->arg);
673 else
674 DoPy3Command(eap, (char *)script);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200675 }
676 vim_free(script);
677}
678
679#define BUFFER_SIZE 2048
680
681/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200682 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200683 */
684 void
685ex_py3file(exarg_T *eap)
686{
687 static char buffer[BUFFER_SIZE];
688 const char *file;
689 char *p;
690 int i;
691
692 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
693 * stdio file pointer, but Vim and the Python DLL are compiled with
694 * different options under Windows, meaning that stdio pointers aren't
695 * compatible between the two. Yuk.
696 *
697 * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec'))
698 *
699 * We need to escape any backslashes or single quotes in the file name, so that
700 * Python won't mangle the file name.
701 */
702
703 strcpy(buffer, "exec(compile(open('");
704 p = buffer + 19; /* size of "exec(compile(open('" */
705
706 for (i=0; i<2; ++i)
707 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200708 file = (char *)eap->arg;
709 while (*file && p < buffer + (BUFFER_SIZE - 3))
710 {
711 if (*file == '\\' || *file == '\'')
712 *p++ = '\\';
713 *p++ = *file++;
714 }
715 /* If we didn't finish the file name, we hit a buffer overflow */
716 if (*file != '\0')
717 return;
718 if (i==0)
719 {
720 strcpy(p,"').read(),'");
721 p += 11;
722 }
723 else
724 {
725 strcpy(p,"','exec'))");
726 p += 10;
727 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200728 }
729
730
731 /* Execute the file */
732 DoPy3Command(eap, buffer);
733}
734
735/******************************************************
736 * 2. Python output stream: writes output via [e]msg().
737 */
738
739/* Implementation functions
740 */
741
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200742 static PyObject *
743OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200744{
745 char *name = "";
746 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200747 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200748
749 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200750 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200751
752 return PyObject_GenericGetAttr(self, nameobj);
753}
754
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200755 static int
756OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200757{
758 char *name = "";
759 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200760 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200761
762 if (val == NULL) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200763 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
764 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200765 }
766
767 if (strcmp(name, "softspace") == 0)
768 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200769 if (!PyLong_Check(val)) {
770 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
771 return -1;
772 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200773
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200774 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
775 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200776 }
777
778 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
779 return -1;
780}
781
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200782/***************/
783
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200784 static int
785PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200786{
787 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200788 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200789}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200790
791 static void
792PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793{
794 PySys_SetObject("stdout", NULL);
795 PySys_SetObject("stderr", NULL);
796}
797
798/******************************************************
799 * 3. Implementation of the Vim module for Python
800 */
801
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200802/* Window type - Implementation functions
803 * --------------------------------------
804 */
805
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
807
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200808/* Buffer type - Implementation functions
809 * --------------------------------------
810 */
811
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200812#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
813
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200814static Py_ssize_t BufferLength(PyObject *);
815static PyObject *BufferItem(PyObject *, Py_ssize_t);
816static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *);
817static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
818
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819
820/* Line range type - Implementation functions
821 * --------------------------------------
822 */
823
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200824#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
825
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200826static PyObject* RangeSubscript(PyObject *self, PyObject* idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200827static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
828
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200829/* Current objects type - Implementation functions
830 * -----------------------------------------------
831 */
832
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200833static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200834 (lenfunc) BufferLength, /* sq_length, len(x) */
835 (binaryfunc) 0, /* sq_concat, x+y */
836 (ssizeargfunc) 0, /* sq_repeat, x*n */
837 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
838 0, /* was_sq_slice, x[i:j] */
839 (ssizeobjargproc) BufferAsItem, /* sq_ass_item, x[i]=v */
840 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841 0, /* sq_contains */
842 0, /* sq_inplace_concat */
843 0, /* sq_inplace_repeat */
844};
845
846PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200847 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848 /* mp_subscript */ (binaryfunc)BufferSubscript,
849 /* mp_ass_subscript */ (objobjargproc)0,
850};
851
852
853/* Buffer object - Definitions
854 */
855
856static PyTypeObject BufferType;
857
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200858 static PyObject *
859BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860{
861 /* We need to handle deletion of buffers underneath us.
862 * If we add a "b_python3_ref" field to the buf_T structure,
863 * then we can get at it in buf_freeall() in vim. We then
864 * need to create only ONE Python object per buffer - if
865 * we try to create a second, just INCREF the existing one
866 * and return it. The (single) Python object referring to
867 * the buffer is stored in "b_python3_ref".
868 * Question: what to do on a buf_freeall(). We'll probably
869 * have to either delete the Python object (DECREF it to
870 * zero - a bad idea, as it leaves dangling refs!) or
871 * set the buf_T * value to an invalid value (-1?), which
872 * means we need checks in all access functions... Bah.
873 */
874
875 BufferObject *self;
876
877 if (buf->b_python3_ref != NULL)
878 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200879 self = buf->b_python3_ref;
880 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881 }
882 else
883 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200884 self = PyObject_NEW(BufferObject, &BufferType);
885 buf->b_python3_ref = self;
886 if (self == NULL)
887 return NULL;
888 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200889 }
890
891 return (PyObject *)(self);
892}
893
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200894 static void
895BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200896{
897 BufferObject *this = (BufferObject *)(self);
898
899 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200900 this->buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200901}
902
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200903 static PyObject *
904BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200905{
906 BufferObject *this = (BufferObject *)(self);
907
908 char *name = "";
909 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200910 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200911
912 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200913 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200914
915 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200916 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200918 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200919 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 return Py_BuildValue("[ss]", "name", "number");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200922 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200923}
924
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200925 static PyObject *
926BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200927{
928 static char repr[100];
929 BufferObject *this = (BufferObject *)(self);
930
931 if (this->buf == INVALID_BUFFER_VALUE)
932 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200933 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
934 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935 }
936 else
937 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200938 char *name = (char *)this->buf->b_fname;
939 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200940
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200941 if (name == NULL)
942 name = "";
943 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200944
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200945 if (len > 35)
946 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200947
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200948 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200949
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200950 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200951 }
952}
953
954/******************/
955
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200956 static Py_ssize_t
957BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200958{
959 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200960 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200961
962 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
963}
964
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200965 static PyObject *
966BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200967{
968 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200969 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970}
971
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200972 static PyObject *
973BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
974{
975 return RBSlice((BufferObject *)(self), lo, hi, 1,
976 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
977}
978
979 static Py_ssize_t
980BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981{
982 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200983 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
984 NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985}
986
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200988 static PyObject *
989BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200990{
991 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200992 long _idx = PyLong_AsLong(idx);
993 return BufferItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200995 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200997 if (PySlice_GetIndicesEx((PySliceObject *)idx,
998 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
999 &start, &stop,
1000 &step, &slicelen) < 0) {
1001 return NULL;
1002 }
1003 return BufferSlice(self,start,stop+1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001005 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1006 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001007 }
1008}
1009
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001011 (lenfunc) RangeLength, /* sq_length, len(x) */
1012 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1013 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1014 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1015 0, /* was_sq_slice, x[i:j] */
1016 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1017 0, /* sq_ass_slice, x[i:j]=v */
1018 0, /* sq_contains */
1019 0, /* sq_inplace_concat */
1020 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021};
1022
1023PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001024 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001025 /* mp_subscript */ (binaryfunc)RangeSubscript,
1026 /* mp_ass_subscript */ (objobjargproc)0,
1027};
1028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001029/* Line range object - Implementation
1030 */
1031
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001032 static void
1033RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001034{
1035 Py_DECREF(((RangeObject *)(self))->buf);
1036}
1037
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001038 static PyObject *
1039RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001040{
1041 char *name = "";
1042 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001043 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001044
1045 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001046 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001047 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001048 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001050 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001051}
1052
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001053/****************/
1054
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001055 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001056RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001057{
1058 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001059 ((RangeObject *)(self))->start,
1060 ((RangeObject *)(self))->end,
1061 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001062}
1063
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001064 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001065RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001066{
1067 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001068 long _idx = PyLong_AsLong(idx);
1069 return RangeItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001070 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001071 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001073 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1074 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1075 &start, &stop,
1076 &step, &slicelen) < 0) {
1077 return NULL;
1078 }
1079 return RangeSlice(self,start,stop+1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001080 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001081 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1082 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083 }
1084}
1085
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086/* Buffer list object - Definitions
1087 */
1088
1089typedef struct
1090{
1091 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001092} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001093
1094static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001095 (lenfunc) BufListLength, /* sq_length, len(x) */
1096 (binaryfunc) 0, /* sq_concat, x+y */
1097 (ssizeargfunc) 0, /* sq_repeat, x*n */
1098 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1099 0, /* was_sq_slice, x[i:j] */
1100 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1101 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001102 0, /* sq_contains */
1103 0, /* sq_inplace_concat */
1104 0, /* sq_inplace_repeat */
1105};
1106
1107static PyTypeObject BufListType;
1108
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109/* Window object - Definitions
1110 */
1111
1112static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001113 /* name, function, calling, documentation */
1114 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115};
1116
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001117static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118
1119/* Window object - Implementation
1120 */
1121
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001122 static PyObject *
1123WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124{
1125 /* We need to handle deletion of windows underneath us.
1126 * If we add a "w_python3_ref" field to the win_T structure,
1127 * then we can get at it in win_free() in vim. We then
1128 * need to create only ONE Python object per window - if
1129 * we try to create a second, just INCREF the existing one
1130 * and return it. The (single) Python object referring to
1131 * the window is stored in "w_python3_ref".
1132 * On a win_free() we set the Python object's win_T* field
1133 * to an invalid value. We trap all uses of a window
1134 * object, and reject them if the win_T* field is invalid.
1135 */
1136
1137 WindowObject *self;
1138
1139 if (win->w_python3_ref)
1140 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001141 self = win->w_python3_ref;
1142 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001143 }
1144 else
1145 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001146 self = PyObject_NEW(WindowObject, &WindowType);
1147 if (self == NULL)
1148 return NULL;
1149 self->win = win;
1150 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151 }
1152
1153 return (PyObject *)(self);
1154}
1155
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001156 static void
1157WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001158{
1159 WindowObject *this = (WindowObject *)(self);
1160
1161 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001162 this->win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001163}
1164
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001165 static PyObject *
1166WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001167{
1168 WindowObject *this = (WindowObject *)(self);
1169
1170 char *name = "";
1171 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001172 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001173
1174
1175 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177
1178 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001179 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001180 else if (strcmp(name, "cursor") == 0)
1181 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001182 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001183
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001184 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001185 }
1186 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001187 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188#ifdef FEAT_VERTSPLIT
1189 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001190 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191#endif
1192 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001193 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001194 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001195 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196}
1197
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001198 static int
1199WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001200{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001202
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001204 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001205
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001206 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207}
1208
1209/* Window list object - Definitions
1210 */
1211
1212typedef struct
1213{
1214 PyObject_HEAD
1215}
1216WinListObject;
1217
1218static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001219 (lenfunc) WinListLength, /* sq_length, len(x) */
1220 (binaryfunc) 0, /* sq_concat, x+y */
1221 (ssizeargfunc) 0, /* sq_repeat, x*n */
1222 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1223 0, /* sq_slice, x[i:j] */
1224 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1225 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001226 0, /* sq_contains */
1227 0, /* sq_inplace_concat */
1228 0, /* sq_inplace_repeat */
1229};
1230
1231static PyTypeObject WinListType;
1232
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001233/* Current items object - Definitions
1234 */
1235
1236typedef struct
1237{
1238 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001239} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001240
1241static PyTypeObject CurrentType;
1242
1243/* Current items object - Implementation
1244 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001245 static PyObject *
1246CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247{
1248 char *name = "";
1249 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001250 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251
1252 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001254 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262 else
1263 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001264 PyErr_SetString(PyExc_AttributeError, name);
1265 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001266 }
1267}
1268
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001269 static int
1270CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001271{
1272 char *name = "";
1273 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001274 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001275
1276 if (strcmp(name, "line") == 0)
1277 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1279 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001280
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001282 }
1283 else
1284 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001285 PyErr_SetString(PyExc_AttributeError, name);
1286 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287 }
1288}
1289
1290/* External interface
1291 */
1292
1293 void
1294python3_buffer_free(buf_T *buf)
1295{
1296 if (buf->b_python3_ref != NULL)
1297 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 BufferObject *bp = buf->b_python3_ref;
1299 bp->buf = INVALID_BUFFER_VALUE;
1300 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001301 }
1302}
1303
1304#if defined(FEAT_WINDOWS) || defined(PROTO)
1305 void
1306python3_window_free(win_T *win)
1307{
1308 if (win->w_python3_ref != NULL)
1309 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001310 WindowObject *wp = win->w_python3_ref;
1311 wp->win = INVALID_WINDOW_VALUE;
1312 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313 }
1314}
1315#endif
1316
1317static BufListObject TheBufferList =
1318{
1319 PyObject_HEAD_INIT(&BufListType)
1320};
1321
1322static WinListObject TheWindowList =
1323{
1324 PyObject_HEAD_INIT(&WinListType)
1325};
1326
1327static CurrentObject TheCurrent =
1328{
1329 PyObject_HEAD_INIT(&CurrentType)
1330};
1331
1332PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1333
1334static struct PyModuleDef vimmodule;
1335
Bram Moolenaar69154f22010-07-18 21:42:34 +02001336#ifndef PROTO
1337PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001338{
1339 PyObject *mod;
1340 /* The special value is removed from sys.path in Python3_Init(). */
1341 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1342
1343 PyType_Ready(&BufferType);
1344 PyType_Ready(&RangeType);
1345 PyType_Ready(&WindowType);
1346 PyType_Ready(&BufListType);
1347 PyType_Ready(&WinListType);
1348 PyType_Ready(&CurrentType);
1349
1350 /* Set sys.argv[] to avoid a crash in warn(). */
1351 PySys_SetArgv(1, argv);
1352
1353 mod = PyModule_Create(&vimmodule);
1354
1355 VimError = Py_BuildValue("s", "vim.error");
1356
1357 PyModule_AddObject(mod, "error", VimError);
1358 Py_INCREF((PyObject *)(void *)&TheBufferList);
1359 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1360 Py_INCREF((PyObject *)(void *)&TheCurrent);
1361 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1362 Py_INCREF((PyObject *)(void *)&TheWindowList);
1363 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1364
1365 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001367
1368 return mod;
1369}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001370#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001371
1372/*************************************************************************
1373 * 4. Utility functions for handling the interface between Vim and Python.
1374 */
1375
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001376/* Convert a Vim line into a Python string.
1377 * All internal newlines are replaced by null characters.
1378 *
1379 * On errors, the Python exception data is set, and NULL is returned.
1380 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001381 static PyObject *
1382LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001383{
1384 PyObject *result;
1385 Py_ssize_t len = strlen(str);
1386 char *tmp,*p;
1387
1388 tmp = (char *)alloc((unsigned)(len+1));
1389 p = tmp;
1390 if (p == NULL)
1391 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001392 PyErr_NoMemory();
1393 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394 }
1395
1396 while (*str)
1397 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001398 if (*str == '\n')
1399 *p = '\0';
1400 else
1401 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001402
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001403 ++p;
1404 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001405 }
1406 *p = '\0';
1407
1408 result = PyUnicode_FromStringAndSize(tmp, len);
1409
1410 vim_free(tmp);
1411 return result;
1412}
1413
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001414 static void
1415init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001416{
1417 vim_memset(&OutputType, 0, sizeof(OutputType));
1418 OutputType.tp_name = "vim.message";
1419 OutputType.tp_basicsize = sizeof(OutputObject);
1420 OutputType.tp_getattro = OutputGetattro;
1421 OutputType.tp_setattro = OutputSetattro;
1422 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1423 OutputType.tp_doc = "vim message object";
1424 OutputType.tp_methods = OutputMethods;
1425 OutputType.tp_alloc = call_PyType_GenericAlloc;
1426 OutputType.tp_new = call_PyType_GenericNew;
1427 OutputType.tp_free = call_PyObject_Free;
1428
1429 vim_memset(&BufferType, 0, sizeof(BufferType));
1430 BufferType.tp_name = "vim.buffer";
1431 BufferType.tp_basicsize = sizeof(BufferType);
1432 BufferType.tp_dealloc = BufferDestructor;
1433 BufferType.tp_repr = BufferRepr;
1434 BufferType.tp_as_sequence = &BufferAsSeq;
1435 BufferType.tp_as_mapping = &BufferAsMapping;
1436 BufferType.tp_getattro = BufferGetattro;
1437 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1438 BufferType.tp_doc = "vim buffer object";
1439 BufferType.tp_methods = BufferMethods;
1440 BufferType.tp_alloc = call_PyType_GenericAlloc;
1441 BufferType.tp_new = call_PyType_GenericNew;
1442 BufferType.tp_free = call_PyObject_Free;
1443
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001444 vim_memset(&WindowType, 0, sizeof(WindowType));
1445 WindowType.tp_name = "vim.window";
1446 WindowType.tp_basicsize = sizeof(WindowObject);
1447 WindowType.tp_dealloc = WindowDestructor;
1448 WindowType.tp_repr = WindowRepr;
1449 WindowType.tp_getattro = WindowGetattro;
1450 WindowType.tp_setattro = WindowSetattro;
1451 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1452 WindowType.tp_doc = "vim Window object";
1453 WindowType.tp_methods = WindowMethods;
1454 WindowType.tp_alloc = call_PyType_GenericAlloc;
1455 WindowType.tp_new = call_PyType_GenericNew;
1456 WindowType.tp_free = call_PyObject_Free;
1457
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458 vim_memset(&BufListType, 0, sizeof(BufListType));
1459 BufListType.tp_name = "vim.bufferlist";
1460 BufListType.tp_basicsize = sizeof(BufListObject);
1461 BufListType.tp_as_sequence = &BufListAsSeq;
1462 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1463 BufferType.tp_doc = "vim buffer list";
1464
1465 vim_memset(&WinListType, 0, sizeof(WinListType));
1466 WinListType.tp_name = "vim.windowlist";
1467 WinListType.tp_basicsize = sizeof(WinListType);
1468 WinListType.tp_as_sequence = &WinListAsSeq;
1469 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1470 WinListType.tp_doc = "vim window list";
1471
1472 vim_memset(&RangeType, 0, sizeof(RangeType));
1473 RangeType.tp_name = "vim.range";
1474 RangeType.tp_basicsize = sizeof(RangeObject);
1475 RangeType.tp_dealloc = RangeDestructor;
1476 RangeType.tp_repr = RangeRepr;
1477 RangeType.tp_as_sequence = &RangeAsSeq;
1478 RangeType.tp_as_mapping = &RangeAsMapping;
1479 RangeType.tp_getattro = RangeGetattro;
1480 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1481 RangeType.tp_doc = "vim Range object";
1482 RangeType.tp_methods = RangeMethods;
1483 RangeType.tp_alloc = call_PyType_GenericAlloc;
1484 RangeType.tp_new = call_PyType_GenericNew;
1485 RangeType.tp_free = call_PyObject_Free;
1486
1487 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1488 CurrentType.tp_name = "vim.currentdata";
1489 CurrentType.tp_basicsize = sizeof(CurrentObject);
1490 CurrentType.tp_getattro = CurrentGetattro;
1491 CurrentType.tp_setattro = CurrentSetattro;
1492 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1493 CurrentType.tp_doc = "vim current object";
1494
1495 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1496 vimmodule.m_name = "vim";
1497 vimmodule.m_doc = vim_module_doc;
1498 vimmodule.m_size = -1;
1499 vimmodule.m_methods = VimMethods;
1500}