blob: 5f02a468bdd2b8f6afa219a0ae522a1999a0e957 [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)
Bram Moolenaar19e60942011-06-19 00:27:51 +020073#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)p_enc, NULL);
74#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
75#define PyString_AsString(obj) PyBytes_AsString(obj)
76#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020077#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020078
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010079#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020080
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020081# ifndef WIN3264
82# include <dlfcn.h>
83# define FARPROC void*
84# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +010085# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020086# define load_dll(n) dlopen((n), RTLD_LAZY)
87# else
88# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
89# endif
90# define close_dll dlclose
91# define symbol_from_dll dlsym
92# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +020093# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020094# define close_dll FreeLibrary
95# define symbol_from_dll GetProcAddress
96# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020097/*
98 * Wrapper defines
99 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200100# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200101# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200102# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200103# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200104# define PyMem_Free py3_PyMem_Free
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200105# define PyDict_SetItemString py3_PyDict_SetItemString
106# define PyErr_BadArgument py3_PyErr_BadArgument
107# define PyErr_Clear py3_PyErr_Clear
108# define PyErr_NoMemory py3_PyErr_NoMemory
109# define PyErr_Occurred py3_PyErr_Occurred
110# define PyErr_SetNone py3_PyErr_SetNone
111# define PyErr_SetString py3_PyErr_SetString
112# define PyEval_InitThreads py3_PyEval_InitThreads
113# define PyEval_RestoreThread py3_PyEval_RestoreThread
114# define PyEval_SaveThread py3_PyEval_SaveThread
115# define PyGILState_Ensure py3_PyGILState_Ensure
116# define PyGILState_Release py3_PyGILState_Release
117# define PyLong_AsLong py3_PyLong_AsLong
118# define PyLong_FromLong py3_PyLong_FromLong
119# define PyList_GetItem py3_PyList_GetItem
120# define PyList_Append py3_PyList_Append
121# define PyList_New py3_PyList_New
122# define PyList_SetItem py3_PyList_SetItem
123# define PyList_Size py3_PyList_Size
124# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
125# define PyImport_ImportModule py3_PyImport_ImportModule
126# define PyObject_Init py3__PyObject_Init
127# define PyDict_New py3_PyDict_New
128# define PyDict_GetItemString py3_PyDict_GetItemString
129# define PyModule_GetDict py3_PyModule_GetDict
130#undef PyRun_SimpleString
131# define PyRun_SimpleString py3_PyRun_SimpleString
132# define PySys_SetObject py3_PySys_SetObject
133# define PySys_SetArgv py3_PySys_SetArgv
134# define PyType_Type (*py3_PyType_Type)
135# define PyType_Ready py3_PyType_Ready
136#undef Py_BuildValue
137# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100138# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200139# define Py_Initialize py3_Py_Initialize
140# define Py_Finalize py3_Py_Finalize
141# define Py_IsInitialized py3_Py_IsInitialized
142# define _Py_NoneStruct (*py3__Py_NoneStruct)
143# define PyModule_AddObject py3_PyModule_AddObject
144# define PyImport_AppendInittab py3_PyImport_AppendInittab
145# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200146# undef PyUnicode_AsEncodedString
147# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
148# undef PyBytes_AsString
149# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
151# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200152# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200153# ifdef Py_DEBUG
154# define _Py_NegativeRefcount py3__Py_NegativeRefcount
155# define _Py_RefTotal (*py3__Py_RefTotal)
156# define _Py_Dealloc py3__Py_Dealloc
157# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
158# define _PyObject_DebugFree py3__PyObject_DebugFree
159# else
160# define PyObject_Malloc py3_PyObject_Malloc
161# define PyObject_Free py3_PyObject_Free
162# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define PyType_GenericAlloc py3_PyType_GenericAlloc
164# define PyType_GenericNew py3_PyType_GenericNew
165# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200166# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200167# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200168# undef PyUnicode_Decode
169# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200170
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200171# ifdef Py_DEBUG
172# undef PyObject_NEW
173# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200174( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200175 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200176# endif
177
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200178/*
179 * Pointers for dynamic link
180 */
181static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100182static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200183static void (*py3_Py_Initialize)(void);
184static PyObject* (*py3_PyList_New)(Py_ssize_t size);
185static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
186static void (*py3_PyGILState_Release)(PyGILState_STATE);
187static int (*py3_PySys_SetObject)(char *, PyObject *);
188static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
189static Py_ssize_t (*py3_PyList_Size)(PyObject *);
190static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200191 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192static PyObject* (*py3_PyErr_NoMemory)(void);
193static void (*py3_Py_Finalize)(void);
194static void (*py3_PyErr_SetString)(PyObject *, const char *);
195static int (*py3_PyRun_SimpleString)(char *);
196static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
197static PyObject* (*py3_PyImport_ImportModule)(const char *);
198static int (*py3_PyErr_BadArgument)(void);
199static PyTypeObject* py3_PyType_Type;
200static PyObject* (*py3_PyErr_Occurred)(void);
201static PyObject* (*py3_PyModule_GetDict)(PyObject *);
202static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
203static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
204static PyObject* (*py3_PyLong_FromLong)(long);
205static PyObject* (*py3_PyDict_New)(void);
206static PyObject* (*py3_Py_BuildValue)(char *, ...);
207static int (*py3_PyType_Ready)(PyTypeObject *type);
208static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
209static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200210static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
211 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200212static long (*py3_PyLong_AsLong)(PyObject *);
213static void (*py3_PyErr_SetNone)(PyObject *);
214static void (*py3_PyEval_InitThreads)(void);
215static void(*py3_PyEval_RestoreThread)(PyThreadState *);
216static PyThreadState*(*py3_PyEval_SaveThread)(void);
217static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
218static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200219static int (*py3_PyMem_Free)(void *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200220static int (*py3_Py_IsInitialized)(void);
221static void (*py3_PyErr_Clear)(void);
222static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
223static PyObject* py3__Py_NoneStruct;
224static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
225static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
226static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200227static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
228static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
230static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
231static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
232static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
233static PyTypeObject* py3_PySlice_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200234static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200235# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200236 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
237 static Py_ssize_t* py3__Py_RefTotal;
238 static void (*py3__Py_Dealloc)(PyObject *obj);
239 static void (*py3__PyObject_DebugFree)(void*);
240 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200241# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200242 static void (*py3_PyObject_Free)(void*);
243 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200244# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200245
246static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
247
248/* Imported exception objects */
249static PyObject *p3imp_PyExc_AttributeError;
250static PyObject *p3imp_PyExc_IndexError;
251static PyObject *p3imp_PyExc_KeyboardInterrupt;
252static PyObject *p3imp_PyExc_TypeError;
253static PyObject *p3imp_PyExc_ValueError;
254
255# define PyExc_AttributeError p3imp_PyExc_AttributeError
256# define PyExc_IndexError p3imp_PyExc_IndexError
257# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
258# define PyExc_TypeError p3imp_PyExc_TypeError
259# define PyExc_ValueError p3imp_PyExc_ValueError
260
261/*
262 * Table of name to function pointer of python.
263 */
264# define PYTHON_PROC FARPROC
265static struct
266{
267 char *name;
268 PYTHON_PROC *ptr;
269} py3_funcname_table[] =
270{
271 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100272 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
274 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200275 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200276 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
277 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
278 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
279 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
280 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
281 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
282 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
283 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
284 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
285 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
286 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
287 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
288 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
289 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
290 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
291 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
292 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
293 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
294 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
295 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
296 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
297 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
298 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
299 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
300 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
301 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
302 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
303 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
304 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
305 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200306 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
307 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
308 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
309 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
310 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
311 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
312 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200313 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200314 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
315 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
316 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
317 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
318 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200319 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200320# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200321 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
322 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
323 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
324 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
325 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200326# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200327 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
328 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200329# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200330 {"", NULL},
331};
332
333/*
334 * Free python.dll
335 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200336 static void
337end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200339 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200340 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200341 close_dll(hinstPy3);
342 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200343 }
344}
345
346/*
347 * Load library and get all pointers.
348 * Parameter 'libname' provides name of DLL.
349 * Return OK or FAIL.
350 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200351 static int
352py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200353{
354 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200355 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100357# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200358 /* Can't have Python and Python3 loaded at the same time.
359 * It cause a crash, because RTLD_GLOBAL is needed for
360 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200361 if (python_loaded())
362 {
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200363 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200364 return FAIL;
365 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200366# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200367
368 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200369 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370 hinstPy3 = load_dll(libname);
371
372 if (!hinstPy3)
373 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200374 if (verbose)
375 EMSG2(_(e_loadlib), libname);
376 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200377 }
378
379 for (i = 0; py3_funcname_table[i].ptr; ++i)
380 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200381 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
382 py3_funcname_table[i].name)) == NULL)
383 {
384 close_dll(hinstPy3);
385 hinstPy3 = 0;
386 if (verbose)
387 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
388 return FAIL;
389 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390 }
391
Bram Moolenaar69154f22010-07-18 21:42:34 +0200392 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
393 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200394 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200395 ucs_decode = symbol_from_dll(hinstPy3,
396 "PyUnicodeUCS2_Decode");
397 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
398 "PyUnicodeUCS2_AsEncodedString");
399 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200400 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200401 ucs_from_string = symbol_from_dll(hinstPy3,
402 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200403 ucs_decode = symbol_from_dll(hinstPy3,
404 "PyUnicodeUCS4_Decode");
405 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
406 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200408 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200409 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200410 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200411 py3_PyUnicode_Decode = ucs_decode;
412 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413 }
414 else
415 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200416 close_dll(hinstPy3);
417 hinstPy3 = 0;
418 if (verbose)
419 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
420 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421 }
422
423 return OK;
424}
425
426/*
427 * If python is enabled (there is installed python on Windows system) return
428 * TRUE, else FALSE.
429 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200430 int
431python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200432{
433 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
434}
435
436/* Load the standard Python exceptions - don't import the symbols from the
437 * DLL, as this can cause errors (importing data symbols is not reliable).
438 */
439static void get_py3_exceptions __ARGS((void));
440
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200441 static void
442get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200443{
444 PyObject *exmod = PyImport_ImportModule("builtins");
445 PyObject *exdict = PyModule_GetDict(exmod);
446 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
447 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
448 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
449 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
450 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
451 Py_XINCREF(p3imp_PyExc_AttributeError);
452 Py_XINCREF(p3imp_PyExc_IndexError);
453 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
454 Py_XINCREF(p3imp_PyExc_TypeError);
455 Py_XINCREF(p3imp_PyExc_ValueError);
456 Py_XDECREF(exmod);
457}
458#endif /* DYNAMIC_PYTHON3 */
459
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200460static PyObject *BufferNew (buf_T *);
461static PyObject *WindowNew(win_T *);
462static PyObject *LineToString(const char *);
463
464static PyTypeObject RangeType;
465
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200466/*
467 * Include the code shared with if_python.c
468 */
469#include "if_py_both.h"
470
471 static void
472call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473{
474#ifdef Py_DEBUG
475 _PyObject_DebugFree(p);
476#else
477 PyObject_Free(p);
478#endif
479}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200480
481 static PyObject *
482call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200483{
484 return PyType_GenericNew(type,args,kwds);
485}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200486
487 static PyObject *
488call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200489{
490 return PyType_GenericAlloc(type,nitems);
491}
492
493/******************************************************
494 * Internal function prototypes.
495 */
496
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497static Py_ssize_t RangeStart;
498static Py_ssize_t RangeEnd;
499
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200500static int PythonIO_Init(void);
501static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200502PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200504/******************************************************
505 * 1. Python interpreter main program.
506 */
507
508static int py3initialised = 0;
509
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200510static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
511
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200512 void
513python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200514{
515 static int recurse = 0;
516
517 /* If a crash occurs while doing this, don't try again. */
518 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200519 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520
521 ++recurse;
522
523#ifdef DYNAMIC_PYTHON3
524 if (hinstPy3)
525#endif
526 if (Py_IsInitialized())
527 {
528 // acquire lock before finalizing
529 pygilstate = PyGILState_Ensure();
530
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200531 PythonIO_Fini();
532 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200533 }
534
535#ifdef DYNAMIC_PYTHON3
536 end_dynamic_python3();
537#endif
538
539 --recurse;
540}
541
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200542#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
543 int
544python3_loaded()
545{
546 return (hinstPy3 != 0);
547}
548#endif
549
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200550 static int
551Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200552{
553 if (!py3initialised)
554 {
555#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200556 if (!python3_enabled(TRUE))
557 {
558 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
559 goto fail;
560 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200561#endif
562
563 init_structs();
564
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100565
566#ifdef PYTHON3_HOME
567 Py_SetPythonHome(PYTHON3_HOME);
568#endif
569
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200571 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200572#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200573 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200575 /* initialise threads, must be after Py_Initialize() */
576 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200577
578#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200579 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200580#endif
581
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200582 if (PythonIO_Init())
583 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200584
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200585 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200586
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 /* Remove the element from sys.path that was added because of our
588 * argv[0] value in Py3Init_vim(). Previously we used an empty
589 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200590 * the current directory in sys.path.
591 * Only after vim has been imported, the element does exist in
592 * sys.path.
593 */
594 PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200595
596 // lock is created and acquired in PyEval_InitThreads() and thread
597 // state is created in Py_Initialize()
598 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
599 // (python must have threads enabled!)
600 // so the following does both: unlock GIL and save thread state in TLS
601 // without deleting thread state
602 PyGILState_Release(pygilstate);
603
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200604 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605 }
606
607 return 0;
608
609fail:
610 /* We call PythonIO_Flush() here to print any Python errors.
611 * This is OK, as it is possible to call this function even
612 * if PythonIO_Init() has not completed successfully (it will
613 * not do anything in this case).
614 */
615 PythonIO_Flush();
616 return -1;
617}
618
619/*
620 * External interface
621 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200622 static void
623DoPy3Command(exarg_T *eap, const char *cmd)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624{
625#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200626 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200627#endif
628#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200629 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200631 PyObject *cmdstr;
632 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633
634#if defined(MACOS) && !defined(MACOS_X_UNIX)
635 GetPort(&oldPort);
636 /* Check if the Python library is available */
637 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200638 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200639#endif
640 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200641 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642
643 RangeStart = eap->line1;
644 RangeEnd = eap->line2;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200645 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200646
647#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
648 /* Python only works properly when the LC_NUMERIC locale is "C". */
649 saved_locale = setlocale(LC_NUMERIC, NULL);
650 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200651 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200652 else
653 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200654 /* Need to make a copy, value may change when setting new locale. */
655 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
656 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657 }
658#endif
659
660 pygilstate = PyGILState_Ensure();
661
Bram Moolenaar19e60942011-06-19 00:27:51 +0200662 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
663 * SyntaxError (unicode error). */
664 cmdstr = PyUnicode_Decode(cmd, strlen(cmd), (char *)p_enc, NULL);
665 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", NULL);
666 Py_XDECREF(cmdstr);
667 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
668 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200669
670 PyGILState_Release(pygilstate);
671
672#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
673 if (saved_locale != NULL)
674 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200675 (void)setlocale(LC_NUMERIC, saved_locale);
676 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200677 }
678#endif
679
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200680 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200681 PythonIO_Flush();
682#if defined(MACOS) && !defined(MACOS_X_UNIX)
683 SetPort(oldPort);
684#endif
685
686theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200687 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200688}
689
690/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200691 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200692 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200693 void
694ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200695{
696 char_u *script;
697
698 script = script_get(eap, eap->arg);
699 if (!eap->skip)
700 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200701 if (script == NULL)
702 DoPy3Command(eap, (char *)eap->arg);
703 else
704 DoPy3Command(eap, (char *)script);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200705 }
706 vim_free(script);
707}
708
709#define BUFFER_SIZE 2048
710
711/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200712 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200713 */
714 void
715ex_py3file(exarg_T *eap)
716{
717 static char buffer[BUFFER_SIZE];
718 const char *file;
719 char *p;
720 int i;
721
722 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
723 * stdio file pointer, but Vim and the Python DLL are compiled with
724 * different options under Windows, meaning that stdio pointers aren't
725 * compatible between the two. Yuk.
726 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200727 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
728 *
729 * Using bytes so that Python can detect the source encoding as it normally
730 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200731 *
732 * We need to escape any backslashes or single quotes in the file name, so that
733 * Python won't mangle the file name.
734 */
735
736 strcpy(buffer, "exec(compile(open('");
737 p = buffer + 19; /* size of "exec(compile(open('" */
738
739 for (i=0; i<2; ++i)
740 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200741 file = (char *)eap->arg;
742 while (*file && p < buffer + (BUFFER_SIZE - 3))
743 {
744 if (*file == '\\' || *file == '\'')
745 *p++ = '\\';
746 *p++ = *file++;
747 }
748 /* If we didn't finish the file name, we hit a buffer overflow */
749 if (*file != '\0')
750 return;
751 if (i==0)
752 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200753 strcpy(p,"','rb').read(),'");
754 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200755 }
756 else
757 {
758 strcpy(p,"','exec'))");
759 p += 10;
760 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200761 }
762
763
764 /* Execute the file */
765 DoPy3Command(eap, buffer);
766}
767
768/******************************************************
769 * 2. Python output stream: writes output via [e]msg().
770 */
771
772/* Implementation functions
773 */
774
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200775 static PyObject *
776OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200777{
778 char *name = "";
779 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200780 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200781
782 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200783 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200784
785 return PyObject_GenericGetAttr(self, nameobj);
786}
787
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200788 static int
789OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790{
791 char *name = "";
792 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200793 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200794
795 if (val == NULL) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200796 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
797 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200798 }
799
800 if (strcmp(name, "softspace") == 0)
801 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200802 if (!PyLong_Check(val)) {
803 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
804 return -1;
805 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200807 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
808 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809 }
810
811 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
812 return -1;
813}
814
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200815/***************/
816
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200817 static int
818PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819{
820 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200821 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200822}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200823
824 static void
825PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200826{
827 PySys_SetObject("stdout", NULL);
828 PySys_SetObject("stderr", NULL);
829}
830
831/******************************************************
832 * 3. Implementation of the Vim module for Python
833 */
834
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200835/* Window type - Implementation functions
836 * --------------------------------------
837 */
838
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
840
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841/* Buffer type - Implementation functions
842 * --------------------------------------
843 */
844
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200845#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
846
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200847static Py_ssize_t BufferLength(PyObject *);
848static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200849static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200850static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200851
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852
853/* Line range type - Implementation functions
854 * --------------------------------------
855 */
856
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200857#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
858
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200859static PyObject* RangeSubscript(PyObject *self, PyObject* idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
861
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200862/* Current objects type - Implementation functions
863 * -----------------------------------------------
864 */
865
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200867 (lenfunc) BufferLength, /* sq_length, len(x) */
868 (binaryfunc) 0, /* sq_concat, x+y */
869 (ssizeargfunc) 0, /* sq_repeat, x*n */
870 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
871 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +0200872 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200873 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874 0, /* sq_contains */
875 0, /* sq_inplace_concat */
876 0, /* sq_inplace_repeat */
877};
878
879PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200880 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200882 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200883};
884
885
886/* Buffer object - Definitions
887 */
888
889static PyTypeObject BufferType;
890
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200891 static PyObject *
892BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893{
894 /* We need to handle deletion of buffers underneath us.
895 * If we add a "b_python3_ref" field to the buf_T structure,
896 * then we can get at it in buf_freeall() in vim. We then
897 * need to create only ONE Python object per buffer - if
898 * we try to create a second, just INCREF the existing one
899 * and return it. The (single) Python object referring to
900 * the buffer is stored in "b_python3_ref".
901 * Question: what to do on a buf_freeall(). We'll probably
902 * have to either delete the Python object (DECREF it to
903 * zero - a bad idea, as it leaves dangling refs!) or
904 * set the buf_T * value to an invalid value (-1?), which
905 * means we need checks in all access functions... Bah.
906 */
907
908 BufferObject *self;
909
910 if (buf->b_python3_ref != NULL)
911 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200912 self = buf->b_python3_ref;
913 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200914 }
915 else
916 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200917 self = PyObject_NEW(BufferObject, &BufferType);
918 buf->b_python3_ref = self;
919 if (self == NULL)
920 return NULL;
921 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200922 }
923
924 return (PyObject *)(self);
925}
926
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200927 static void
928BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200929{
930 BufferObject *this = (BufferObject *)(self);
931
932 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200933 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200934
935 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200936}
937
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200938 static PyObject *
939BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200940{
941 BufferObject *this = (BufferObject *)(self);
942
943 char *name = "";
944 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200945 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200946
947 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200948 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200949
950 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200951 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200952 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200953 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200954 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200955 return Py_BuildValue("[ss]", "name", "number");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200956 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200957 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200958}
959
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200960 static PyObject *
961BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962{
963 static char repr[100];
964 BufferObject *this = (BufferObject *)(self);
965
966 if (this->buf == INVALID_BUFFER_VALUE)
967 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200968 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
969 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970 }
971 else
972 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200973 char *name = (char *)this->buf->b_fname;
974 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200975
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200976 if (name == NULL)
977 name = "";
978 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200980 if (len > 35)
981 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200983 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200984
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200985 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986 }
987}
988
989/******************/
990
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200991 static Py_ssize_t
992BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993{
994 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200995 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996
997 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
998}
999
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001000 static PyObject *
1001BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002{
1003 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001004 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005}
1006
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001007 static PyObject *
1008BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1009{
1010 return RBSlice((BufferObject *)(self), lo, hi, 1,
1011 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1012}
1013
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001014 static PyObject *
1015BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016{
1017 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001018 long _idx = PyLong_AsLong(idx);
1019 return BufferItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001021 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001022
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001023 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1024 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1025 &start, &stop,
1026 &step, &slicelen) < 0) {
1027 return NULL;
1028 }
Bram Moolenaar19e60942011-06-19 00:27:51 +02001029 return BufferSlice(self,start,stop);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001030 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001031 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1032 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001033 }
1034}
1035
Bram Moolenaar19e60942011-06-19 00:27:51 +02001036 static Py_ssize_t
1037BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1038{
1039 if (PyLong_Check(idx)) {
1040 long n = PyLong_AsLong(idx);
1041 return RBAsItem((BufferObject *)(self), n, val, 1,
1042 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1043 NULL);
1044 } else if (PySlice_Check(idx)) {
1045 Py_ssize_t start, stop, step, slicelen;
1046
1047 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1048 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1049 &start, &stop,
1050 &step, &slicelen) < 0) {
1051 return -1;
1052 }
1053 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1054 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1055 NULL);
1056 } else {
1057 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1058 return -1;
1059 }
1060}
1061
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001062static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001063 (lenfunc) RangeLength, /* sq_length, len(x) */
1064 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1065 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1066 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1067 0, /* was_sq_slice, x[i:j] */
1068 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1069 0, /* sq_ass_slice, x[i:j]=v */
1070 0, /* sq_contains */
1071 0, /* sq_inplace_concat */
1072 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001073};
1074
1075PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001076 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001077 /* mp_subscript */ (binaryfunc)RangeSubscript,
1078 /* mp_ass_subscript */ (objobjargproc)0,
1079};
1080
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001081/* Line range object - Implementation
1082 */
1083
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001084 static void
1085RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086{
1087 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001088 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089}
1090
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001091 static PyObject *
1092RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001093{
1094 char *name = "";
1095 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001096 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097
1098 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001099 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001100 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001101 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001102 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001103 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001104}
1105
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001106/****************/
1107
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001108 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001109RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110{
1111 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001112 ((RangeObject *)(self))->start,
1113 ((RangeObject *)(self))->end,
1114 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115}
1116
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001117 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001118RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001119{
1120 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001121 long _idx = PyLong_AsLong(idx);
1122 return RangeItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001125
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001126 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1127 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1128 &start, &stop,
1129 &step, &slicelen) < 0) {
1130 return NULL;
1131 }
1132 return RangeSlice(self,start,stop+1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001133 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001134 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1135 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001136 }
1137}
1138
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001139/* Buffer list object - Definitions
1140 */
1141
1142typedef struct
1143{
1144 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001145} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001146
1147static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001148 (lenfunc) BufListLength, /* sq_length, len(x) */
1149 (binaryfunc) 0, /* sq_concat, x+y */
1150 (ssizeargfunc) 0, /* sq_repeat, x*n */
1151 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1152 0, /* was_sq_slice, x[i:j] */
1153 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1154 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155 0, /* sq_contains */
1156 0, /* sq_inplace_concat */
1157 0, /* sq_inplace_repeat */
1158};
1159
1160static PyTypeObject BufListType;
1161
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001162/* Window object - Definitions
1163 */
1164
1165static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001166 /* name, function, calling, documentation */
1167 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001168};
1169
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001170static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001171
1172/* Window object - Implementation
1173 */
1174
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001175 static PyObject *
1176WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177{
1178 /* We need to handle deletion of windows underneath us.
1179 * If we add a "w_python3_ref" field to the win_T structure,
1180 * then we can get at it in win_free() in vim. We then
1181 * need to create only ONE Python object per window - if
1182 * we try to create a second, just INCREF the existing one
1183 * and return it. The (single) Python object referring to
1184 * the window is stored in "w_python3_ref".
1185 * On a win_free() we set the Python object's win_T* field
1186 * to an invalid value. We trap all uses of a window
1187 * object, and reject them if the win_T* field is invalid.
1188 */
1189
1190 WindowObject *self;
1191
1192 if (win->w_python3_ref)
1193 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001194 self = win->w_python3_ref;
1195 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196 }
1197 else
1198 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001199 self = PyObject_NEW(WindowObject, &WindowType);
1200 if (self == NULL)
1201 return NULL;
1202 self->win = win;
1203 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204 }
1205
1206 return (PyObject *)(self);
1207}
1208
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001209 static void
1210WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211{
1212 WindowObject *this = (WindowObject *)(self);
1213
1214 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001215 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001216
1217 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001218}
1219
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001220 static PyObject *
1221WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222{
1223 WindowObject *this = (WindowObject *)(self);
1224
1225 char *name = "";
1226 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001227 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001228
1229
1230 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001231 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001232
1233 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235 else if (strcmp(name, "cursor") == 0)
1236 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001237 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001238
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001240 }
1241 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001242 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243#ifdef FEAT_VERTSPLIT
1244 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246#endif
1247 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001250 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251}
1252
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001253 static int
1254WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001257
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001261 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262}
1263
1264/* Window list object - Definitions
1265 */
1266
1267typedef struct
1268{
1269 PyObject_HEAD
1270}
1271WinListObject;
1272
1273static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001274 (lenfunc) WinListLength, /* sq_length, len(x) */
1275 (binaryfunc) 0, /* sq_concat, x+y */
1276 (ssizeargfunc) 0, /* sq_repeat, x*n */
1277 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1278 0, /* sq_slice, x[i:j] */
1279 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1280 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001281 0, /* sq_contains */
1282 0, /* sq_inplace_concat */
1283 0, /* sq_inplace_repeat */
1284};
1285
1286static PyTypeObject WinListType;
1287
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001288/* Current items object - Definitions
1289 */
1290
1291typedef struct
1292{
1293 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001294} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001295
1296static PyTypeObject CurrentType;
1297
1298/* Current items object - Implementation
1299 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001300 static PyObject *
1301CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001302{
1303 char *name = "";
1304 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001305 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001306
1307 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001308 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001309 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001310 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001311 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317 else
1318 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001319 PyErr_SetString(PyExc_AttributeError, name);
1320 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001321 }
1322}
1323
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001324 static int
1325CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001326{
1327 char *name = "";
1328 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001329 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001330
1331 if (strcmp(name, "line") == 0)
1332 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001333 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1334 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001335
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001336 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001337 }
1338 else
1339 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001340 PyErr_SetString(PyExc_AttributeError, name);
1341 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001342 }
1343}
1344
1345/* External interface
1346 */
1347
1348 void
1349python3_buffer_free(buf_T *buf)
1350{
1351 if (buf->b_python3_ref != NULL)
1352 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001353 BufferObject *bp = buf->b_python3_ref;
1354 bp->buf = INVALID_BUFFER_VALUE;
1355 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356 }
1357}
1358
1359#if defined(FEAT_WINDOWS) || defined(PROTO)
1360 void
1361python3_window_free(win_T *win)
1362{
1363 if (win->w_python3_ref != NULL)
1364 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001365 WindowObject *wp = win->w_python3_ref;
1366 wp->win = INVALID_WINDOW_VALUE;
1367 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368 }
1369}
1370#endif
1371
1372static BufListObject TheBufferList =
1373{
1374 PyObject_HEAD_INIT(&BufListType)
1375};
1376
1377static WinListObject TheWindowList =
1378{
1379 PyObject_HEAD_INIT(&WinListType)
1380};
1381
1382static CurrentObject TheCurrent =
1383{
1384 PyObject_HEAD_INIT(&CurrentType)
1385};
1386
1387PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1388
1389static struct PyModuleDef vimmodule;
1390
Bram Moolenaar69154f22010-07-18 21:42:34 +02001391#ifndef PROTO
1392PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001393{
1394 PyObject *mod;
1395 /* The special value is removed from sys.path in Python3_Init(). */
1396 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1397
1398 PyType_Ready(&BufferType);
1399 PyType_Ready(&RangeType);
1400 PyType_Ready(&WindowType);
1401 PyType_Ready(&BufListType);
1402 PyType_Ready(&WinListType);
1403 PyType_Ready(&CurrentType);
1404
1405 /* Set sys.argv[] to avoid a crash in warn(). */
1406 PySys_SetArgv(1, argv);
1407
1408 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001409 if (mod == NULL)
1410 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001411
Bram Moolenaar19e60942011-06-19 00:27:51 +02001412 VimError = PyErr_NewException("vim.error", NULL, NULL);
1413 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001414
1415 PyModule_AddObject(mod, "error", VimError);
1416 Py_INCREF((PyObject *)(void *)&TheBufferList);
1417 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1418 Py_INCREF((PyObject *)(void *)&TheCurrent);
1419 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1420 Py_INCREF((PyObject *)(void *)&TheWindowList);
1421 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1422
1423 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001424 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
1426 return mod;
1427}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001428#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001429
1430/*************************************************************************
1431 * 4. Utility functions for handling the interface between Vim and Python.
1432 */
1433
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434/* Convert a Vim line into a Python string.
1435 * All internal newlines are replaced by null characters.
1436 *
1437 * On errors, the Python exception data is set, and NULL is returned.
1438 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001439 static PyObject *
1440LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001441{
1442 PyObject *result;
1443 Py_ssize_t len = strlen(str);
1444 char *tmp,*p;
1445
1446 tmp = (char *)alloc((unsigned)(len+1));
1447 p = tmp;
1448 if (p == NULL)
1449 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001450 PyErr_NoMemory();
1451 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001452 }
1453
1454 while (*str)
1455 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456 if (*str == '\n')
1457 *p = '\0';
1458 else
1459 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 ++p;
1462 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001463 }
1464 *p = '\0';
1465
Bram Moolenaar19e60942011-06-19 00:27:51 +02001466 result = PyUnicode_Decode(tmp, len, (char *)p_enc, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467
1468 vim_free(tmp);
1469 return result;
1470}
1471
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001472 static void
1473init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474{
1475 vim_memset(&OutputType, 0, sizeof(OutputType));
1476 OutputType.tp_name = "vim.message";
1477 OutputType.tp_basicsize = sizeof(OutputObject);
1478 OutputType.tp_getattro = OutputGetattro;
1479 OutputType.tp_setattro = OutputSetattro;
1480 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1481 OutputType.tp_doc = "vim message object";
1482 OutputType.tp_methods = OutputMethods;
1483 OutputType.tp_alloc = call_PyType_GenericAlloc;
1484 OutputType.tp_new = call_PyType_GenericNew;
1485 OutputType.tp_free = call_PyObject_Free;
1486
1487 vim_memset(&BufferType, 0, sizeof(BufferType));
1488 BufferType.tp_name = "vim.buffer";
1489 BufferType.tp_basicsize = sizeof(BufferType);
1490 BufferType.tp_dealloc = BufferDestructor;
1491 BufferType.tp_repr = BufferRepr;
1492 BufferType.tp_as_sequence = &BufferAsSeq;
1493 BufferType.tp_as_mapping = &BufferAsMapping;
1494 BufferType.tp_getattro = BufferGetattro;
1495 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1496 BufferType.tp_doc = "vim buffer object";
1497 BufferType.tp_methods = BufferMethods;
1498 BufferType.tp_alloc = call_PyType_GenericAlloc;
1499 BufferType.tp_new = call_PyType_GenericNew;
1500 BufferType.tp_free = call_PyObject_Free;
1501
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001502 vim_memset(&WindowType, 0, sizeof(WindowType));
1503 WindowType.tp_name = "vim.window";
1504 WindowType.tp_basicsize = sizeof(WindowObject);
1505 WindowType.tp_dealloc = WindowDestructor;
1506 WindowType.tp_repr = WindowRepr;
1507 WindowType.tp_getattro = WindowGetattro;
1508 WindowType.tp_setattro = WindowSetattro;
1509 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1510 WindowType.tp_doc = "vim Window object";
1511 WindowType.tp_methods = WindowMethods;
1512 WindowType.tp_alloc = call_PyType_GenericAlloc;
1513 WindowType.tp_new = call_PyType_GenericNew;
1514 WindowType.tp_free = call_PyObject_Free;
1515
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516 vim_memset(&BufListType, 0, sizeof(BufListType));
1517 BufListType.tp_name = "vim.bufferlist";
1518 BufListType.tp_basicsize = sizeof(BufListObject);
1519 BufListType.tp_as_sequence = &BufListAsSeq;
1520 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1521 BufferType.tp_doc = "vim buffer list";
1522
1523 vim_memset(&WinListType, 0, sizeof(WinListType));
1524 WinListType.tp_name = "vim.windowlist";
1525 WinListType.tp_basicsize = sizeof(WinListType);
1526 WinListType.tp_as_sequence = &WinListAsSeq;
1527 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1528 WinListType.tp_doc = "vim window list";
1529
1530 vim_memset(&RangeType, 0, sizeof(RangeType));
1531 RangeType.tp_name = "vim.range";
1532 RangeType.tp_basicsize = sizeof(RangeObject);
1533 RangeType.tp_dealloc = RangeDestructor;
1534 RangeType.tp_repr = RangeRepr;
1535 RangeType.tp_as_sequence = &RangeAsSeq;
1536 RangeType.tp_as_mapping = &RangeAsMapping;
1537 RangeType.tp_getattro = RangeGetattro;
1538 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1539 RangeType.tp_doc = "vim Range object";
1540 RangeType.tp_methods = RangeMethods;
1541 RangeType.tp_alloc = call_PyType_GenericAlloc;
1542 RangeType.tp_new = call_PyType_GenericNew;
1543 RangeType.tp_free = call_PyObject_Free;
1544
1545 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1546 CurrentType.tp_name = "vim.currentdata";
1547 CurrentType.tp_basicsize = sizeof(CurrentObject);
1548 CurrentType.tp_getattro = CurrentGetattro;
1549 CurrentType.tp_setattro = CurrentSetattro;
1550 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1551 CurrentType.tp_doc = "vim current object";
1552
1553 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1554 vimmodule.m_name = "vim";
1555 vimmodule.m_doc = vim_module_doc;
1556 vimmodule.m_size = -1;
1557 vimmodule.m_methods = VimMethods;
1558}