blob: ebf402a4b0e35228414e82bc31f4c710f38be2b0 [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 Moolenaar3d64a312011-07-15 15:54:44 +020071/* The "surrogateescape" error handler is new in Python 3.1 */
72#if PY_VERSION_HEX >= 0x030100f0
73# define CODEC_ERROR_HANDLER "surrogateescape"
74#else
75# define CODEC_ERROR_HANDLER NULL
76#endif
77
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020078#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020079#define PyString_Check(obj) PyUnicode_Check(obj)
Bram Moolenaar3d64a312011-07-15 15:54:44 +020080#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +020081#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
82#define PyString_AsString(obj) PyBytes_AsString(obj)
83#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020084#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020085
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010086#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020087
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020088# ifndef WIN3264
89# include <dlfcn.h>
90# define FARPROC void*
91# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +010092# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020093# define load_dll(n) dlopen((n), RTLD_LAZY)
94# else
95# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
96# endif
97# define close_dll dlclose
98# define symbol_from_dll dlsym
99# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200100# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200101# define close_dll FreeLibrary
102# define symbol_from_dll GetProcAddress
103# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200104/*
105 * Wrapper defines
106 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200107# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200108# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200109# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200110# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200111# define PyMem_Free py3_PyMem_Free
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200112# define PyDict_SetItemString py3_PyDict_SetItemString
113# define PyErr_BadArgument py3_PyErr_BadArgument
114# define PyErr_Clear py3_PyErr_Clear
115# define PyErr_NoMemory py3_PyErr_NoMemory
116# define PyErr_Occurred py3_PyErr_Occurred
117# define PyErr_SetNone py3_PyErr_SetNone
118# define PyErr_SetString py3_PyErr_SetString
119# define PyEval_InitThreads py3_PyEval_InitThreads
120# define PyEval_RestoreThread py3_PyEval_RestoreThread
121# define PyEval_SaveThread py3_PyEval_SaveThread
122# define PyGILState_Ensure py3_PyGILState_Ensure
123# define PyGILState_Release py3_PyGILState_Release
124# define PyLong_AsLong py3_PyLong_AsLong
125# define PyLong_FromLong py3_PyLong_FromLong
126# define PyList_GetItem py3_PyList_GetItem
127# define PyList_Append py3_PyList_Append
128# define PyList_New py3_PyList_New
129# define PyList_SetItem py3_PyList_SetItem
130# define PyList_Size py3_PyList_Size
131# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
132# define PyImport_ImportModule py3_PyImport_ImportModule
133# define PyObject_Init py3__PyObject_Init
134# define PyDict_New py3_PyDict_New
135# define PyDict_GetItemString py3_PyDict_GetItemString
136# define PyModule_GetDict py3_PyModule_GetDict
137#undef PyRun_SimpleString
138# define PyRun_SimpleString py3_PyRun_SimpleString
139# define PySys_SetObject py3_PySys_SetObject
140# define PySys_SetArgv py3_PySys_SetArgv
141# define PyType_Type (*py3_PyType_Type)
142# define PyType_Ready py3_PyType_Ready
143#undef Py_BuildValue
144# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100145# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200146# define Py_Initialize py3_Py_Initialize
147# define Py_Finalize py3_Py_Finalize
148# define Py_IsInitialized py3_Py_IsInitialized
149# define _Py_NoneStruct (*py3__Py_NoneStruct)
150# define PyModule_AddObject py3_PyModule_AddObject
151# define PyImport_AppendInittab py3_PyImport_AppendInittab
152# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200153# undef PyUnicode_AsEncodedString
154# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
155# undef PyBytes_AsString
156# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200157# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
158# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200159# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200160# ifdef Py_DEBUG
161# define _Py_NegativeRefcount py3__Py_NegativeRefcount
162# define _Py_RefTotal (*py3__Py_RefTotal)
163# define _Py_Dealloc py3__Py_Dealloc
164# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
165# define _PyObject_DebugFree py3__PyObject_DebugFree
166# else
167# define PyObject_Malloc py3_PyObject_Malloc
168# define PyObject_Free py3_PyObject_Free
169# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200170# define PyType_GenericAlloc py3_PyType_GenericAlloc
171# define PyType_GenericNew py3_PyType_GenericNew
172# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200173# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200174# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200175# undef PyUnicode_Decode
176# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200177
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200178# ifdef Py_DEBUG
179# undef PyObject_NEW
180# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200181( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200182 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200183# endif
184
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200185/*
186 * Pointers for dynamic link
187 */
188static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100189static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200190static void (*py3_Py_Initialize)(void);
191static PyObject* (*py3_PyList_New)(Py_ssize_t size);
192static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
193static void (*py3_PyGILState_Release)(PyGILState_STATE);
194static int (*py3_PySys_SetObject)(char *, PyObject *);
195static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
196static Py_ssize_t (*py3_PyList_Size)(PyObject *);
197static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200198 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199static PyObject* (*py3_PyErr_NoMemory)(void);
200static void (*py3_Py_Finalize)(void);
201static void (*py3_PyErr_SetString)(PyObject *, const char *);
202static int (*py3_PyRun_SimpleString)(char *);
203static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
204static PyObject* (*py3_PyImport_ImportModule)(const char *);
205static int (*py3_PyErr_BadArgument)(void);
206static PyTypeObject* py3_PyType_Type;
207static PyObject* (*py3_PyErr_Occurred)(void);
208static PyObject* (*py3_PyModule_GetDict)(PyObject *);
209static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
210static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
211static PyObject* (*py3_PyLong_FromLong)(long);
212static PyObject* (*py3_PyDict_New)(void);
213static PyObject* (*py3_Py_BuildValue)(char *, ...);
214static int (*py3_PyType_Ready)(PyTypeObject *type);
215static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
216static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200217static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
218 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200219static long (*py3_PyLong_AsLong)(PyObject *);
220static void (*py3_PyErr_SetNone)(PyObject *);
221static void (*py3_PyEval_InitThreads)(void);
222static void(*py3_PyEval_RestoreThread)(PyThreadState *);
223static PyThreadState*(*py3_PyEval_SaveThread)(void);
224static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
225static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200226static int (*py3_PyMem_Free)(void *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227static int (*py3_Py_IsInitialized)(void);
228static void (*py3_PyErr_Clear)(void);
229static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
230static PyObject* py3__Py_NoneStruct;
231static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
232static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
233static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200234static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
235static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200236static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
237static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
238static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
239static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
240static PyTypeObject* py3_PySlice_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200241static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200242# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200243 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
244 static Py_ssize_t* py3__Py_RefTotal;
245 static void (*py3__Py_Dealloc)(PyObject *obj);
246 static void (*py3__PyObject_DebugFree)(void*);
247 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200248# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200249 static void (*py3_PyObject_Free)(void*);
250 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200251# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200252
253static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
254
255/* Imported exception objects */
256static PyObject *p3imp_PyExc_AttributeError;
257static PyObject *p3imp_PyExc_IndexError;
258static PyObject *p3imp_PyExc_KeyboardInterrupt;
259static PyObject *p3imp_PyExc_TypeError;
260static PyObject *p3imp_PyExc_ValueError;
261
262# define PyExc_AttributeError p3imp_PyExc_AttributeError
263# define PyExc_IndexError p3imp_PyExc_IndexError
264# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
265# define PyExc_TypeError p3imp_PyExc_TypeError
266# define PyExc_ValueError p3imp_PyExc_ValueError
267
268/*
269 * Table of name to function pointer of python.
270 */
271# define PYTHON_PROC FARPROC
272static struct
273{
274 char *name;
275 PYTHON_PROC *ptr;
276} py3_funcname_table[] =
277{
278 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100279 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200280 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
281 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200282 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200283 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
284 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
285 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
286 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
287 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
288 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
289 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
290 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
291 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
292 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
293 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
294 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
295 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
296 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
297 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
298 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
299 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
300 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
301 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
302 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
303 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
304 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
305 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
306 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
307 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
308 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
309 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
310 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
311 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
312 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200313 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
314 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
315 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
316 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
317 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
318 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
319 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200320 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200321 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
322 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
323 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
324 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
325 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200326 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200327# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
329 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
330 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
331 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
332 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200333# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200334 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
335 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200336# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200337 {"", NULL},
338};
339
340/*
341 * Free python.dll
342 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200343 static void
344end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200346 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200347 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200348 close_dll(hinstPy3);
349 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200350 }
351}
352
353/*
354 * Load library and get all pointers.
355 * Parameter 'libname' provides name of DLL.
356 * Return OK or FAIL.
357 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200358 static int
359py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200360{
361 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200362 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200363
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100364# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200365 /* Can't have Python and Python3 loaded at the same time.
366 * It cause a crash, because RTLD_GLOBAL is needed for
367 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200368 if (python_loaded())
369 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200370 if (verbose)
371 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200372 return FAIL;
373 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200374# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200375
376 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200377 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378 hinstPy3 = load_dll(libname);
379
380 if (!hinstPy3)
381 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200382 if (verbose)
383 EMSG2(_(e_loadlib), libname);
384 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200385 }
386
387 for (i = 0; py3_funcname_table[i].ptr; ++i)
388 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200389 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
390 py3_funcname_table[i].name)) == NULL)
391 {
392 close_dll(hinstPy3);
393 hinstPy3 = 0;
394 if (verbose)
395 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
396 return FAIL;
397 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398 }
399
Bram Moolenaar69154f22010-07-18 21:42:34 +0200400 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
401 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200402 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200403 ucs_decode = symbol_from_dll(hinstPy3,
404 "PyUnicodeUCS2_Decode");
405 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
406 "PyUnicodeUCS2_AsEncodedString");
407 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200409 ucs_from_string = symbol_from_dll(hinstPy3,
410 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200411 ucs_decode = symbol_from_dll(hinstPy3,
412 "PyUnicodeUCS4_Decode");
413 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
414 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200416 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200417 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200418 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200419 py3_PyUnicode_Decode = ucs_decode;
420 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421 }
422 else
423 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200424 close_dll(hinstPy3);
425 hinstPy3 = 0;
426 if (verbose)
427 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
428 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429 }
430
431 return OK;
432}
433
434/*
435 * If python is enabled (there is installed python on Windows system) return
436 * TRUE, else FALSE.
437 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200438 int
439python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200440{
441 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
442}
443
444/* Load the standard Python exceptions - don't import the symbols from the
445 * DLL, as this can cause errors (importing data symbols is not reliable).
446 */
447static void get_py3_exceptions __ARGS((void));
448
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200449 static void
450get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451{
452 PyObject *exmod = PyImport_ImportModule("builtins");
453 PyObject *exdict = PyModule_GetDict(exmod);
454 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
455 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
456 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
457 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
458 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
459 Py_XINCREF(p3imp_PyExc_AttributeError);
460 Py_XINCREF(p3imp_PyExc_IndexError);
461 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
462 Py_XINCREF(p3imp_PyExc_TypeError);
463 Py_XINCREF(p3imp_PyExc_ValueError);
464 Py_XDECREF(exmod);
465}
466#endif /* DYNAMIC_PYTHON3 */
467
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200468static PyObject *BufferNew (buf_T *);
469static PyObject *WindowNew(win_T *);
470static PyObject *LineToString(const char *);
471
472static PyTypeObject RangeType;
473
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200474/*
475 * Include the code shared with if_python.c
476 */
477#include "if_py_both.h"
478
479 static void
480call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481{
482#ifdef Py_DEBUG
483 _PyObject_DebugFree(p);
484#else
485 PyObject_Free(p);
486#endif
487}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200488
489 static PyObject *
490call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491{
492 return PyType_GenericNew(type,args,kwds);
493}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200494
495 static PyObject *
496call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497{
498 return PyType_GenericAlloc(type,nitems);
499}
500
501/******************************************************
502 * Internal function prototypes.
503 */
504
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200505static Py_ssize_t RangeStart;
506static Py_ssize_t RangeEnd;
507
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200508static int PythonIO_Init(void);
509static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200510PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200512/******************************************************
513 * 1. Python interpreter main program.
514 */
515
516static int py3initialised = 0;
517
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200518static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
519
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200520 void
521python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200522{
523 static int recurse = 0;
524
525 /* If a crash occurs while doing this, don't try again. */
526 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200527 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200528
529 ++recurse;
530
531#ifdef DYNAMIC_PYTHON3
532 if (hinstPy3)
533#endif
534 if (Py_IsInitialized())
535 {
536 // acquire lock before finalizing
537 pygilstate = PyGILState_Ensure();
538
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200539 PythonIO_Fini();
540 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 }
542
543#ifdef DYNAMIC_PYTHON3
544 end_dynamic_python3();
545#endif
546
547 --recurse;
548}
549
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200550#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
551 int
552python3_loaded()
553{
554 return (hinstPy3 != 0);
555}
556#endif
557
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200558 static int
559Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200560{
561 if (!py3initialised)
562 {
563#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200564 if (!python3_enabled(TRUE))
565 {
566 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
567 goto fail;
568 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200569#endif
570
571 init_structs();
572
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100573
574#ifdef PYTHON3_HOME
575 Py_SetPythonHome(PYTHON3_HOME);
576#endif
577
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200578#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200579 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200580#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200581 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200583 /* initialise threads, must be after Py_Initialize() */
584 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200585
586#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200587 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200588#endif
589
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200590 if (PythonIO_Init())
591 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200592
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200593 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200594
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200595 /* Remove the element from sys.path that was added because of our
596 * argv[0] value in Py3Init_vim(). Previously we used an empty
597 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200598 * the current directory in sys.path.
599 * Only after vim has been imported, the element does exist in
600 * sys.path.
601 */
602 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 +0200603
604 // lock is created and acquired in PyEval_InitThreads() and thread
605 // state is created in Py_Initialize()
606 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
607 // (python must have threads enabled!)
608 // so the following does both: unlock GIL and save thread state in TLS
609 // without deleting thread state
610 PyGILState_Release(pygilstate);
611
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200612 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200613 }
614
615 return 0;
616
617fail:
618 /* We call PythonIO_Flush() here to print any Python errors.
619 * This is OK, as it is possible to call this function even
620 * if PythonIO_Init() has not completed successfully (it will
621 * not do anything in this case).
622 */
623 PythonIO_Flush();
624 return -1;
625}
626
627/*
628 * External interface
629 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200630 static void
631DoPy3Command(exarg_T *eap, const char *cmd)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200632{
633#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200634 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200635#endif
636#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200637 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200639 PyObject *cmdstr;
640 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200641
642#if defined(MACOS) && !defined(MACOS_X_UNIX)
643 GetPort(&oldPort);
644 /* Check if the Python library is available */
645 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200646 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647#endif
648 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200649 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650
651 RangeStart = eap->line1;
652 RangeEnd = eap->line2;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200653 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200654
655#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
656 /* Python only works properly when the LC_NUMERIC locale is "C". */
657 saved_locale = setlocale(LC_NUMERIC, NULL);
658 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200659 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200660 else
661 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200662 /* Need to make a copy, value may change when setting new locale. */
663 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
664 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200665 }
666#endif
667
668 pygilstate = PyGILState_Ensure();
669
Bram Moolenaar19e60942011-06-19 00:27:51 +0200670 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
671 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200672 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
673 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
674 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200675 Py_XDECREF(cmdstr);
676 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
677 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200678
679 PyGILState_Release(pygilstate);
680
681#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
682 if (saved_locale != NULL)
683 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200684 (void)setlocale(LC_NUMERIC, saved_locale);
685 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200686 }
687#endif
688
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200689 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200690 PythonIO_Flush();
691#if defined(MACOS) && !defined(MACOS_X_UNIX)
692 SetPort(oldPort);
693#endif
694
695theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200696 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200697}
698
699/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200700 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200701 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200702 void
703ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200704{
705 char_u *script;
706
707 script = script_get(eap, eap->arg);
708 if (!eap->skip)
709 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 if (script == NULL)
711 DoPy3Command(eap, (char *)eap->arg);
712 else
713 DoPy3Command(eap, (char *)script);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200714 }
715 vim_free(script);
716}
717
718#define BUFFER_SIZE 2048
719
720/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200721 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200722 */
723 void
724ex_py3file(exarg_T *eap)
725{
726 static char buffer[BUFFER_SIZE];
727 const char *file;
728 char *p;
729 int i;
730
731 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
732 * stdio file pointer, but Vim and the Python DLL are compiled with
733 * different options under Windows, meaning that stdio pointers aren't
734 * compatible between the two. Yuk.
735 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200736 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
737 *
738 * Using bytes so that Python can detect the source encoding as it normally
739 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200740 *
741 * We need to escape any backslashes or single quotes in the file name, so that
742 * Python won't mangle the file name.
743 */
744
745 strcpy(buffer, "exec(compile(open('");
746 p = buffer + 19; /* size of "exec(compile(open('" */
747
748 for (i=0; i<2; ++i)
749 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200750 file = (char *)eap->arg;
751 while (*file && p < buffer + (BUFFER_SIZE - 3))
752 {
753 if (*file == '\\' || *file == '\'')
754 *p++ = '\\';
755 *p++ = *file++;
756 }
757 /* If we didn't finish the file name, we hit a buffer overflow */
758 if (*file != '\0')
759 return;
760 if (i==0)
761 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200762 strcpy(p,"','rb').read(),'");
763 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200764 }
765 else
766 {
767 strcpy(p,"','exec'))");
768 p += 10;
769 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770 }
771
772
773 /* Execute the file */
774 DoPy3Command(eap, buffer);
775}
776
777/******************************************************
778 * 2. Python output stream: writes output via [e]msg().
779 */
780
781/* Implementation functions
782 */
783
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200784 static PyObject *
785OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200786{
787 char *name = "";
788 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200789 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790
791 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793
794 return PyObject_GenericGetAttr(self, nameobj);
795}
796
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200797 static int
798OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200799{
800 char *name = "";
801 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200802 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200803
804 if (val == NULL) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200805 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
806 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200807 }
808
809 if (strcmp(name, "softspace") == 0)
810 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200811 if (!PyLong_Check(val)) {
812 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
813 return -1;
814 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200815
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200816 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
817 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200818 }
819
820 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
821 return -1;
822}
823
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200824/***************/
825
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200826 static int
827PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200828{
829 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200830 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200831}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200832
833 static void
834PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200835{
836 PySys_SetObject("stdout", NULL);
837 PySys_SetObject("stderr", NULL);
838}
839
840/******************************************************
841 * 3. Implementation of the Vim module for Python
842 */
843
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200844/* Window type - Implementation functions
845 * --------------------------------------
846 */
847
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
849
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200850/* Buffer type - Implementation functions
851 * --------------------------------------
852 */
853
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200854#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
855
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856static Py_ssize_t BufferLength(PyObject *);
857static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200858static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
859static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861
862/* Line range type - Implementation functions
863 * --------------------------------------
864 */
865
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
867
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200868static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200869static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200870static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200871
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200872/* Current objects type - Implementation functions
873 * -----------------------------------------------
874 */
875
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200876static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200877 (lenfunc) BufferLength, /* sq_length, len(x) */
878 (binaryfunc) 0, /* sq_concat, x+y */
879 (ssizeargfunc) 0, /* sq_repeat, x*n */
880 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
881 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +0200882 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200883 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200884 0, /* sq_contains */
885 0, /* sq_inplace_concat */
886 0, /* sq_inplace_repeat */
887};
888
889PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200890 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200891 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +0200892 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893};
894
895
896/* Buffer object - Definitions
897 */
898
899static PyTypeObject BufferType;
900
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200901 static PyObject *
902BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903{
904 /* We need to handle deletion of buffers underneath us.
905 * If we add a "b_python3_ref" field to the buf_T structure,
906 * then we can get at it in buf_freeall() in vim. We then
907 * need to create only ONE Python object per buffer - if
908 * we try to create a second, just INCREF the existing one
909 * and return it. The (single) Python object referring to
910 * the buffer is stored in "b_python3_ref".
911 * Question: what to do on a buf_freeall(). We'll probably
912 * have to either delete the Python object (DECREF it to
913 * zero - a bad idea, as it leaves dangling refs!) or
914 * set the buf_T * value to an invalid value (-1?), which
915 * means we need checks in all access functions... Bah.
916 */
917
918 BufferObject *self;
919
920 if (buf->b_python3_ref != NULL)
921 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200922 self = buf->b_python3_ref;
923 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200924 }
925 else
926 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200927 self = PyObject_NEW(BufferObject, &BufferType);
928 buf->b_python3_ref = self;
929 if (self == NULL)
930 return NULL;
931 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200932 }
933
934 return (PyObject *)(self);
935}
936
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200937 static void
938BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939{
940 BufferObject *this = (BufferObject *)(self);
941
942 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200943 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200944
945 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200946}
947
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200948 static PyObject *
949BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950{
951 BufferObject *this = (BufferObject *)(self);
952
953 char *name = "";
954 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200955 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200956
957 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200958 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200959
960 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200961 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200962 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200963 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200965 return Py_BuildValue("[ss]", "name", "number");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200967 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968}
969
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200970 static PyObject *
971BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200972{
973 static char repr[100];
974 BufferObject *this = (BufferObject *)(self);
975
976 if (this->buf == INVALID_BUFFER_VALUE)
977 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200978 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
979 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980 }
981 else
982 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200983 char *name = (char *)this->buf->b_fname;
984 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200986 if (name == NULL)
987 name = "";
988 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200990 if (len > 35)
991 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200992
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200993 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200995 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996 }
997}
998
999/******************/
1000
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001001 static Py_ssize_t
1002BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003{
1004 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001005 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001006
1007 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1008}
1009
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001010 static PyObject *
1011BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001012{
1013 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001014 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015}
1016
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001017 static PyObject *
1018BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1019{
1020 return RBSlice((BufferObject *)(self), lo, hi, 1,
1021 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1022}
1023
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001024 static PyObject *
1025BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026{
1027 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001028 long _idx = PyLong_AsLong(idx);
1029 return BufferItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001030 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001031 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001033 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1034 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1035 &start, &stop,
1036 &step, &slicelen) < 0) {
1037 return NULL;
1038 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001039 return BufferSlice(self, start, stop);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001040 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001041 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1042 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043 }
1044}
1045
Bram Moolenaar19e60942011-06-19 00:27:51 +02001046 static Py_ssize_t
1047BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1048{
1049 if (PyLong_Check(idx)) {
1050 long n = PyLong_AsLong(idx);
1051 return RBAsItem((BufferObject *)(self), n, val, 1,
1052 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1053 NULL);
1054 } else if (PySlice_Check(idx)) {
1055 Py_ssize_t start, stop, step, slicelen;
1056
1057 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1058 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1059 &start, &stop,
1060 &step, &slicelen) < 0) {
1061 return -1;
1062 }
1063 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1064 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1065 NULL);
1066 } else {
1067 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1068 return -1;
1069 }
1070}
1071
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001073 (lenfunc) RangeLength, /* sq_length, len(x) */
1074 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1075 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1076 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1077 0, /* was_sq_slice, x[i:j] */
1078 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1079 0, /* sq_ass_slice, x[i:j]=v */
1080 0, /* sq_contains */
1081 0, /* sq_inplace_concat */
1082 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083};
1084
1085PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001086 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001087 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001088 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089};
1090
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001091/* Line range object - Implementation
1092 */
1093
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001094 static void
1095RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001096{
1097 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001098 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001099}
1100
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101 static PyObject *
1102RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103{
1104 char *name = "";
1105 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001106 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107
1108 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001109 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001111 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001112 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001113 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001114}
1115
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001116/****************/
1117
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001118 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001119RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001120{
1121 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 ((RangeObject *)(self))->start,
1123 ((RangeObject *)(self))->end,
1124 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001125}
1126
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001127 static Py_ssize_t
1128RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1129{
1130 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1131 ((RangeObject *)(self))->start,
1132 ((RangeObject *)(self))->end,
1133 &((RangeObject *)(self))->end);
1134}
1135
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001136 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001137RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001138{
1139 if (PyLong_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001140 long _idx = PyLong_AsLong(idx);
1141 return RangeItem(self,_idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001142 } else if (PySlice_Check(idx)) {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001144
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001145 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1146 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1147 &start, &stop,
1148 &step, &slicelen) < 0) {
1149 return NULL;
1150 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001151 return RangeSlice(self, start, stop);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001152 } else {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001153 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1154 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155 }
1156}
1157
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001158 static Py_ssize_t
1159RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1160{
1161 if (PyLong_Check(idx)) {
1162 long n = PyLong_AsLong(idx);
1163 return RangeAsItem(self, n, val);
1164 } else if (PySlice_Check(idx)) {
1165 Py_ssize_t start, stop, step, slicelen;
1166
1167 if (PySlice_GetIndicesEx((PySliceObject *)idx,
1168 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1169 &start, &stop,
1170 &step, &slicelen) < 0) {
1171 return -1;
1172 }
1173 return RangeAsSlice(self, start, stop, val);
1174 } else {
1175 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1176 return -1;
1177 }
1178}
1179
1180
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001181/* Buffer list object - Definitions
1182 */
1183
1184typedef struct
1185{
1186 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001187} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188
1189static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001190 (lenfunc) BufListLength, /* sq_length, len(x) */
1191 (binaryfunc) 0, /* sq_concat, x+y */
1192 (ssizeargfunc) 0, /* sq_repeat, x*n */
1193 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1194 0, /* was_sq_slice, x[i:j] */
1195 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1196 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197 0, /* sq_contains */
1198 0, /* sq_inplace_concat */
1199 0, /* sq_inplace_repeat */
1200};
1201
1202static PyTypeObject BufListType;
1203
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204/* Window object - Definitions
1205 */
1206
1207static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 /* name, function, calling, documentation */
1209 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001210};
1211
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213
1214/* Window object - Implementation
1215 */
1216
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001217 static PyObject *
1218WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001219{
1220 /* We need to handle deletion of windows underneath us.
1221 * If we add a "w_python3_ref" field to the win_T structure,
1222 * then we can get at it in win_free() in vim. We then
1223 * need to create only ONE Python object per window - if
1224 * we try to create a second, just INCREF the existing one
1225 * and return it. The (single) Python object referring to
1226 * the window is stored in "w_python3_ref".
1227 * On a win_free() we set the Python object's win_T* field
1228 * to an invalid value. We trap all uses of a window
1229 * object, and reject them if the win_T* field is invalid.
1230 */
1231
1232 WindowObject *self;
1233
1234 if (win->w_python3_ref)
1235 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 self = win->w_python3_ref;
1237 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001238 }
1239 else
1240 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 self = PyObject_NEW(WindowObject, &WindowType);
1242 if (self == NULL)
1243 return NULL;
1244 self->win = win;
1245 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001246 }
1247
1248 return (PyObject *)(self);
1249}
1250
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001251 static void
1252WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253{
1254 WindowObject *this = (WindowObject *)(self);
1255
1256 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001258
1259 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260}
1261
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001262 static PyObject *
1263WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001264{
1265 WindowObject *this = (WindowObject *)(self);
1266
1267 char *name = "";
1268 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001269 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001270
1271
1272 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001273 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001274
1275 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001276 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001277 else if (strcmp(name, "cursor") == 0)
1278 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001279 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001280
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001282 }
1283 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001284 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285#ifdef FEAT_VERTSPLIT
1286 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001287 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001288#endif
1289 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001290 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001291 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001292 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001293}
1294
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001295 static int
1296WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001298 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001299
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001301 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001302
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001304}
1305
1306/* Window list object - Definitions
1307 */
1308
1309typedef struct
1310{
1311 PyObject_HEAD
1312}
1313WinListObject;
1314
1315static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 (lenfunc) WinListLength, /* sq_length, len(x) */
1317 (binaryfunc) 0, /* sq_concat, x+y */
1318 (ssizeargfunc) 0, /* sq_repeat, x*n */
1319 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1320 0, /* sq_slice, x[i:j] */
1321 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1322 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323 0, /* sq_contains */
1324 0, /* sq_inplace_concat */
1325 0, /* sq_inplace_repeat */
1326};
1327
1328static PyTypeObject WinListType;
1329
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001330/* Current items object - Definitions
1331 */
1332
1333typedef struct
1334{
1335 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001336} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001337
1338static PyTypeObject CurrentType;
1339
1340/* Current items object - Implementation
1341 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001342 static PyObject *
1343CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001344{
1345 char *name = "";
1346 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001347 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001348
1349 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001350 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001351 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001352 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001355 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001356 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001357 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001358 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001359 else
1360 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001361 PyErr_SetString(PyExc_AttributeError, name);
1362 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363 }
1364}
1365
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001366 static int
1367CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368{
1369 char *name = "";
1370 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001371 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001372
1373 if (strcmp(name, "line") == 0)
1374 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001375 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1376 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001377
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001378 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001379 }
1380 else
1381 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001382 PyErr_SetString(PyExc_AttributeError, name);
1383 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001384 }
1385}
1386
1387/* External interface
1388 */
1389
1390 void
1391python3_buffer_free(buf_T *buf)
1392{
1393 if (buf->b_python3_ref != NULL)
1394 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001395 BufferObject *bp = buf->b_python3_ref;
1396 bp->buf = INVALID_BUFFER_VALUE;
1397 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001398 }
1399}
1400
1401#if defined(FEAT_WINDOWS) || defined(PROTO)
1402 void
1403python3_window_free(win_T *win)
1404{
1405 if (win->w_python3_ref != NULL)
1406 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001407 WindowObject *wp = win->w_python3_ref;
1408 wp->win = INVALID_WINDOW_VALUE;
1409 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001410 }
1411}
1412#endif
1413
1414static BufListObject TheBufferList =
1415{
1416 PyObject_HEAD_INIT(&BufListType)
1417};
1418
1419static WinListObject TheWindowList =
1420{
1421 PyObject_HEAD_INIT(&WinListType)
1422};
1423
1424static CurrentObject TheCurrent =
1425{
1426 PyObject_HEAD_INIT(&CurrentType)
1427};
1428
1429PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1430
1431static struct PyModuleDef vimmodule;
1432
Bram Moolenaar69154f22010-07-18 21:42:34 +02001433#ifndef PROTO
1434PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435{
1436 PyObject *mod;
1437 /* The special value is removed from sys.path in Python3_Init(). */
1438 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1439
1440 PyType_Ready(&BufferType);
1441 PyType_Ready(&RangeType);
1442 PyType_Ready(&WindowType);
1443 PyType_Ready(&BufListType);
1444 PyType_Ready(&WinListType);
1445 PyType_Ready(&CurrentType);
1446
1447 /* Set sys.argv[] to avoid a crash in warn(). */
1448 PySys_SetArgv(1, argv);
1449
1450 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001451 if (mod == NULL)
1452 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001453
Bram Moolenaar19e60942011-06-19 00:27:51 +02001454 VimError = PyErr_NewException("vim.error", NULL, NULL);
1455 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456
1457 PyModule_AddObject(mod, "error", VimError);
1458 Py_INCREF((PyObject *)(void *)&TheBufferList);
1459 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1460 Py_INCREF((PyObject *)(void *)&TheCurrent);
1461 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1462 Py_INCREF((PyObject *)(void *)&TheWindowList);
1463 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1464
1465 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001466 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001467
1468 return mod;
1469}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001470#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471
1472/*************************************************************************
1473 * 4. Utility functions for handling the interface between Vim and Python.
1474 */
1475
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001476/* Convert a Vim line into a Python string.
1477 * All internal newlines are replaced by null characters.
1478 *
1479 * On errors, the Python exception data is set, and NULL is returned.
1480 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001481 static PyObject *
1482LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001483{
1484 PyObject *result;
1485 Py_ssize_t len = strlen(str);
1486 char *tmp,*p;
1487
1488 tmp = (char *)alloc((unsigned)(len+1));
1489 p = tmp;
1490 if (p == NULL)
1491 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001492 PyErr_NoMemory();
1493 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494 }
1495
1496 while (*str)
1497 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001498 if (*str == '\n')
1499 *p = '\0';
1500 else
1501 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001503 ++p;
1504 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505 }
1506 *p = '\0';
1507
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001508 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509
1510 vim_free(tmp);
1511 return result;
1512}
1513
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001514 static void
1515init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516{
1517 vim_memset(&OutputType, 0, sizeof(OutputType));
1518 OutputType.tp_name = "vim.message";
1519 OutputType.tp_basicsize = sizeof(OutputObject);
1520 OutputType.tp_getattro = OutputGetattro;
1521 OutputType.tp_setattro = OutputSetattro;
1522 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1523 OutputType.tp_doc = "vim message object";
1524 OutputType.tp_methods = OutputMethods;
1525 OutputType.tp_alloc = call_PyType_GenericAlloc;
1526 OutputType.tp_new = call_PyType_GenericNew;
1527 OutputType.tp_free = call_PyObject_Free;
1528
1529 vim_memset(&BufferType, 0, sizeof(BufferType));
1530 BufferType.tp_name = "vim.buffer";
1531 BufferType.tp_basicsize = sizeof(BufferType);
1532 BufferType.tp_dealloc = BufferDestructor;
1533 BufferType.tp_repr = BufferRepr;
1534 BufferType.tp_as_sequence = &BufferAsSeq;
1535 BufferType.tp_as_mapping = &BufferAsMapping;
1536 BufferType.tp_getattro = BufferGetattro;
1537 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1538 BufferType.tp_doc = "vim buffer object";
1539 BufferType.tp_methods = BufferMethods;
1540 BufferType.tp_alloc = call_PyType_GenericAlloc;
1541 BufferType.tp_new = call_PyType_GenericNew;
1542 BufferType.tp_free = call_PyObject_Free;
1543
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 vim_memset(&WindowType, 0, sizeof(WindowType));
1545 WindowType.tp_name = "vim.window";
1546 WindowType.tp_basicsize = sizeof(WindowObject);
1547 WindowType.tp_dealloc = WindowDestructor;
1548 WindowType.tp_repr = WindowRepr;
1549 WindowType.tp_getattro = WindowGetattro;
1550 WindowType.tp_setattro = WindowSetattro;
1551 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1552 WindowType.tp_doc = "vim Window object";
1553 WindowType.tp_methods = WindowMethods;
1554 WindowType.tp_alloc = call_PyType_GenericAlloc;
1555 WindowType.tp_new = call_PyType_GenericNew;
1556 WindowType.tp_free = call_PyObject_Free;
1557
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001558 vim_memset(&BufListType, 0, sizeof(BufListType));
1559 BufListType.tp_name = "vim.bufferlist";
1560 BufListType.tp_basicsize = sizeof(BufListObject);
1561 BufListType.tp_as_sequence = &BufListAsSeq;
1562 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1563 BufferType.tp_doc = "vim buffer list";
1564
1565 vim_memset(&WinListType, 0, sizeof(WinListType));
1566 WinListType.tp_name = "vim.windowlist";
1567 WinListType.tp_basicsize = sizeof(WinListType);
1568 WinListType.tp_as_sequence = &WinListAsSeq;
1569 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1570 WinListType.tp_doc = "vim window list";
1571
1572 vim_memset(&RangeType, 0, sizeof(RangeType));
1573 RangeType.tp_name = "vim.range";
1574 RangeType.tp_basicsize = sizeof(RangeObject);
1575 RangeType.tp_dealloc = RangeDestructor;
1576 RangeType.tp_repr = RangeRepr;
1577 RangeType.tp_as_sequence = &RangeAsSeq;
1578 RangeType.tp_as_mapping = &RangeAsMapping;
1579 RangeType.tp_getattro = RangeGetattro;
1580 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1581 RangeType.tp_doc = "vim Range object";
1582 RangeType.tp_methods = RangeMethods;
1583 RangeType.tp_alloc = call_PyType_GenericAlloc;
1584 RangeType.tp_new = call_PyType_GenericNew;
1585 RangeType.tp_free = call_PyObject_Free;
1586
1587 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1588 CurrentType.tp_name = "vim.currentdata";
1589 CurrentType.tp_basicsize = sizeof(CurrentObject);
1590 CurrentType.tp_getattro = CurrentGetattro;
1591 CurrentType.tp_setattro = CurrentSetattro;
1592 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1593 CurrentType.tp_doc = "vim current object";
1594
1595 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1596 vimmodule.m_name = "vim";
1597 vimmodule.m_doc = vim_module_doc;
1598 vimmodule.m_size = -1;
1599 vimmodule.m_methods = VimMethods;
1600}