blob: 36ae8eb77855fcb79c78fab35334e329810e3626 [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 Moolenaar2afa3232012-06-29 16:28:28 +020078/* Python 3 does not support CObjects, always use Capsules */
79#define PY_USE_CAPSULE
80
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020081#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020082#define PyString_Check(obj) PyUnicode_Check(obj)
Bram Moolenaardb913952012-06-29 12:54:53 +020083#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
Bram Moolenaar19e60942011-06-19 00:27:51 +020084#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
85#define PyString_AsString(obj) PyBytes_AsString(obj)
86#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020087#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020088
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010089#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020090
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020091# ifndef WIN3264
92# include <dlfcn.h>
93# define FARPROC void*
94# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +010095# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020096# define load_dll(n) dlopen((n), RTLD_LAZY)
97# else
98# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
99# endif
100# define close_dll dlclose
101# define symbol_from_dll dlsym
102# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200103# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200104# define close_dll FreeLibrary
105# define symbol_from_dll GetProcAddress
106# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200107/*
108 * Wrapper defines
109 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200111# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200112# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200113# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200114# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200115# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200116# define PyDict_SetItemString py3_PyDict_SetItemString
117# define PyErr_BadArgument py3_PyErr_BadArgument
118# define PyErr_Clear py3_PyErr_Clear
119# define PyErr_NoMemory py3_PyErr_NoMemory
120# define PyErr_Occurred py3_PyErr_Occurred
121# define PyErr_SetNone py3_PyErr_SetNone
122# define PyErr_SetString py3_PyErr_SetString
123# define PyEval_InitThreads py3_PyEval_InitThreads
124# define PyEval_RestoreThread py3_PyEval_RestoreThread
125# define PyEval_SaveThread py3_PyEval_SaveThread
126# define PyGILState_Ensure py3_PyGILState_Ensure
127# define PyGILState_Release py3_PyGILState_Release
128# define PyLong_AsLong py3_PyLong_AsLong
129# define PyLong_FromLong py3_PyLong_FromLong
130# define PyList_GetItem py3_PyList_GetItem
131# define PyList_Append py3_PyList_Append
132# define PyList_New py3_PyList_New
133# define PyList_SetItem py3_PyList_SetItem
134# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200135# define PySequence_Check py3_PySequence_Check
136# define PySequence_Size py3_PySequence_Size
137# define PySequence_GetItem py3_PySequence_GetItem
138# define PyTuple_Size py3_PyTuple_Size
139# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200140# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
141# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200142# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200143# define PyObject_Init py3__PyObject_Init
144# define PyDict_New py3_PyDict_New
145# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200146# define PyDict_Next py3_PyDict_Next
147# define PyMapping_Check py3_PyMapping_Check
148# define PyMapping_Items py3_PyMapping_Items
149# define PyIter_Next py3_PyIter_Next
150# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200151# define PyModule_GetDict py3_PyModule_GetDict
152#undef PyRun_SimpleString
153# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200154#undef PyRun_String
155# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200156# define PySys_SetObject py3_PySys_SetObject
157# define PySys_SetArgv py3_PySys_SetArgv
158# define PyType_Type (*py3_PyType_Type)
159# define PyType_Ready py3_PyType_Ready
160#undef Py_BuildValue
161# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100162# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define Py_Initialize py3_Py_Initialize
164# define Py_Finalize py3_Py_Finalize
165# define Py_IsInitialized py3_Py_IsInitialized
166# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200167# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200168# define PyModule_AddObject py3_PyModule_AddObject
169# define PyImport_AppendInittab py3_PyImport_AppendInittab
170# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200171# undef PyUnicode_AsEncodedString
172# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
173# undef PyBytes_AsString
174# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200175# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200176# undef PyBytes_FromString
177# define PyBytes_FromString py3_PyBytes_FromString
178# define PyFloat_FromDouble py3_PyFloat_FromDouble
179# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200180# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
181# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200182# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200183# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200184# ifdef Py_DEBUG
185# define _Py_NegativeRefcount py3__Py_NegativeRefcount
186# define _Py_RefTotal (*py3__Py_RefTotal)
187# define _Py_Dealloc py3__Py_Dealloc
188# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
189# define _PyObject_DebugFree py3__PyObject_DebugFree
190# else
191# define PyObject_Malloc py3_PyObject_Malloc
192# define PyObject_Free py3_PyObject_Free
193# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define PyType_GenericAlloc py3_PyType_GenericAlloc
195# define PyType_GenericNew py3_PyType_GenericNew
196# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200197# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200198# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200199# undef PyUnicode_Decode
200# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200201# define PyType_IsSubtype py3_PyType_IsSubtype
202# define PyCapsule_New py3_PyCapsule_New
203# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200205# ifdef Py_DEBUG
206# undef PyObject_NEW
207# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200209 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200210# endif
211
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200212/*
213 * Pointers for dynamic link
214 */
215static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100216static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200217static void (*py3_Py_Initialize)(void);
218static PyObject* (*py3_PyList_New)(Py_ssize_t size);
219static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
220static void (*py3_PyGILState_Release)(PyGILState_STATE);
221static int (*py3_PySys_SetObject)(char *, PyObject *);
222static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
223static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200224static int (*py3_PySequence_Check)(PyObject *);
225static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
226static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
227static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
228static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
229static int (*py3_PyMapping_Check)(PyObject *);
230static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200231static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200232 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200233static PyObject* (*py3_PyErr_NoMemory)(void);
234static void (*py3_Py_Finalize)(void);
235static void (*py3_PyErr_SetString)(PyObject *, const char *);
236static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200237static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200238static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
239static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200240static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241static int (*py3_PyErr_BadArgument)(void);
242static PyTypeObject* py3_PyType_Type;
243static PyObject* (*py3_PyErr_Occurred)(void);
244static PyObject* (*py3_PyModule_GetDict)(PyObject *);
245static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
246static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200247static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200248static PyObject* (*py3_PyLong_FromLong)(long);
249static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200250static PyObject* (*py3_PyIter_Next)(PyObject *);
251static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200252static PyObject* (*py3_Py_BuildValue)(char *, ...);
253static int (*py3_PyType_Ready)(PyTypeObject *type);
254static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
255static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200256static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
257 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258static long (*py3_PyLong_AsLong)(PyObject *);
259static void (*py3_PyErr_SetNone)(PyObject *);
260static void (*py3_PyEval_InitThreads)(void);
261static void(*py3_PyEval_RestoreThread)(PyThreadState *);
262static PyThreadState*(*py3_PyEval_SaveThread)(void);
263static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
264static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200265static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200266static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200267static int (*py3_Py_IsInitialized)(void);
268static void (*py3_PyErr_Clear)(void);
269static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200270static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271static PyObject* py3__Py_NoneStruct;
272static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
273static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
274static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200275static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
276static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200277static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200278static PyObject* (*py3_PyBytes_FromString)(char *str);
279static PyObject* (*py3_PyFloat_FromDouble)(double num);
280static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200281static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
282static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
283static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
284static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
285static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200286static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200287static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200288static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
289static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200290# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200291 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
292 static Py_ssize_t* py3__Py_RefTotal;
293 static void (*py3__Py_Dealloc)(PyObject *obj);
294 static void (*py3__PyObject_DebugFree)(void*);
295 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200296# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297 static void (*py3_PyObject_Free)(void*);
298 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200299# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200300static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200301
302static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
303
304/* Imported exception objects */
305static PyObject *p3imp_PyExc_AttributeError;
306static PyObject *p3imp_PyExc_IndexError;
307static PyObject *p3imp_PyExc_KeyboardInterrupt;
308static PyObject *p3imp_PyExc_TypeError;
309static PyObject *p3imp_PyExc_ValueError;
310
311# define PyExc_AttributeError p3imp_PyExc_AttributeError
312# define PyExc_IndexError p3imp_PyExc_IndexError
313# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
314# define PyExc_TypeError p3imp_PyExc_TypeError
315# define PyExc_ValueError p3imp_PyExc_ValueError
316
317/*
318 * Table of name to function pointer of python.
319 */
320# define PYTHON_PROC FARPROC
321static struct
322{
323 char *name;
324 PYTHON_PROC *ptr;
325} py3_funcname_table[] =
326{
327 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100328 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200329 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
330 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200331 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200332 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200333 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
334 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
335 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
336 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
337 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
338 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200339 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
340 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
341 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
342 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
343 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200344 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
345 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
346 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
347 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
348 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200349 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200350 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
351 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200352 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200353 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
354 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
355 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
356 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
357 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
358 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200359 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
360 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
361 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
362 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
363 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
365 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
366 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
367 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
368 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
369 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
370 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
371 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
372 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
373 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
374 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200375 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200376 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200377 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
378 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
379 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
380 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
381 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
382 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200383 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200384 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200385 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
386 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
387 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
389 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
390 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
391 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
392 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200393 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200394 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200395# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200396 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
397 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
398 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
399 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
400 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200401# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200402 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
403 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200404# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200405 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
406 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
407 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408 {"", NULL},
409};
410
411/*
412 * Free python.dll
413 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200414 static void
415end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200417 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200418 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200419 close_dll(hinstPy3);
420 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421 }
422}
423
424/*
425 * Load library and get all pointers.
426 * Parameter 'libname' provides name of DLL.
427 * Return OK or FAIL.
428 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200429 static int
430py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200431{
432 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200433 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200434
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100435# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200436 /* Can't have Python and Python3 loaded at the same time.
437 * It cause a crash, because RTLD_GLOBAL is needed for
438 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200439 if (python_loaded())
440 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200441 if (verbose)
442 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200443 return FAIL;
444 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200445# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200446
447 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200448 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200449 hinstPy3 = load_dll(libname);
450
451 if (!hinstPy3)
452 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200453 if (verbose)
454 EMSG2(_(e_loadlib), libname);
455 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200456 }
457
458 for (i = 0; py3_funcname_table[i].ptr; ++i)
459 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200460 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
461 py3_funcname_table[i].name)) == NULL)
462 {
463 close_dll(hinstPy3);
464 hinstPy3 = 0;
465 if (verbose)
466 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
467 return FAIL;
468 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469 }
470
Bram Moolenaar69154f22010-07-18 21:42:34 +0200471 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
472 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200474 ucs_decode = symbol_from_dll(hinstPy3,
475 "PyUnicodeUCS2_Decode");
476 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
477 "PyUnicodeUCS2_AsEncodedString");
478 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200480 ucs_from_string = symbol_from_dll(hinstPy3,
481 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200482 ucs_decode = symbol_from_dll(hinstPy3,
483 "PyUnicodeUCS4_Decode");
484 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
485 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200486 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200487 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200488 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200489 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200490 py3_PyUnicode_Decode = ucs_decode;
491 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200492 }
493 else
494 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200495 close_dll(hinstPy3);
496 hinstPy3 = 0;
497 if (verbose)
498 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
499 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200500 }
501
502 return OK;
503}
504
505/*
506 * If python is enabled (there is installed python on Windows system) return
507 * TRUE, else FALSE.
508 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200509 int
510python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511{
512 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
513}
514
515/* Load the standard Python exceptions - don't import the symbols from the
516 * DLL, as this can cause errors (importing data symbols is not reliable).
517 */
518static void get_py3_exceptions __ARGS((void));
519
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200520 static void
521get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200522{
523 PyObject *exmod = PyImport_ImportModule("builtins");
524 PyObject *exdict = PyModule_GetDict(exmod);
525 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
526 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
527 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
528 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
529 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
530 Py_XINCREF(p3imp_PyExc_AttributeError);
531 Py_XINCREF(p3imp_PyExc_IndexError);
532 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
533 Py_XINCREF(p3imp_PyExc_TypeError);
534 Py_XINCREF(p3imp_PyExc_ValueError);
535 Py_XDECREF(exmod);
536}
537#endif /* DYNAMIC_PYTHON3 */
538
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200539static PyObject *BufferNew (buf_T *);
540static PyObject *WindowNew(win_T *);
541static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100542static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200543
544static PyTypeObject RangeType;
545
Bram Moolenaardb913952012-06-29 12:54:53 +0200546static int py3initialised = 0;
547
548#define PYINITIALISED py3initialised
549
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200550#define DICTKEY_DECL PyObject *bytes = NULL;
551
Bram Moolenaardb913952012-06-29 12:54:53 +0200552#define DICTKEY_GET(err) \
553 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200554 { \
555 if (PyBytes_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
556 return err; \
557 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200558 else if (PyUnicode_Check(keyObject)) \
559 { \
560 bytes = PyString_AsBytes(keyObject); \
561 if (bytes == NULL) \
562 return err; \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200563 if (PyBytes_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200564 return err; \
565 } \
566 else \
567 { \
568 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
569 return err; \
570 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200571
Bram Moolenaardb913952012-06-29 12:54:53 +0200572#define DICTKEY_UNREF \
573 if (bytes != NULL) \
574 Py_XDECREF(bytes);
575
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200576/*
577 * Include the code shared with if_python.c
578 */
579#include "if_py_both.h"
580
Bram Moolenaardb913952012-06-29 12:54:53 +0200581#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
582
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200583 static void
584call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200585{
586#ifdef Py_DEBUG
587 _PyObject_DebugFree(p);
588#else
589 PyObject_Free(p);
590#endif
591}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200592
593 static PyObject *
594call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200595{
596 return PyType_GenericNew(type,args,kwds);
597}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200598
599 static PyObject *
600call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200601{
602 return PyType_GenericAlloc(type,nitems);
603}
604
605/******************************************************
606 * Internal function prototypes.
607 */
608
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200609static Py_ssize_t RangeStart;
610static Py_ssize_t RangeEnd;
611
Bram Moolenaardb913952012-06-29 12:54:53 +0200612static PyObject *globals;
613
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614static int PythonIO_Init(void);
615static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200616PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200617
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200618/******************************************************
619 * 1. Python interpreter main program.
620 */
621
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200622static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
623
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200624 void
625python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200626{
627 static int recurse = 0;
628
629 /* If a crash occurs while doing this, don't try again. */
630 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200631 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200632
633 ++recurse;
634
635#ifdef DYNAMIC_PYTHON3
636 if (hinstPy3)
637#endif
638 if (Py_IsInitialized())
639 {
640 // acquire lock before finalizing
641 pygilstate = PyGILState_Ensure();
642
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200643 PythonIO_Fini();
644 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200645 }
646
647#ifdef DYNAMIC_PYTHON3
648 end_dynamic_python3();
649#endif
650
651 --recurse;
652}
653
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200654#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
655 int
656python3_loaded()
657{
658 return (hinstPy3 != 0);
659}
660#endif
661
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200662 static int
663Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200664{
665 if (!py3initialised)
666 {
667#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200668 if (!python3_enabled(TRUE))
669 {
670 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
671 goto fail;
672 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200673#endif
674
675 init_structs();
676
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100677
678#ifdef PYTHON3_HOME
679 Py_SetPythonHome(PYTHON3_HOME);
680#endif
681
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200682#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200683 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200684#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200685 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200686#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200687 /* initialise threads, must be after Py_Initialize() */
688 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689
690#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200691 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200692#endif
693
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200694 if (PythonIO_Init())
695 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200696
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200697 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200698
Bram Moolenaardb913952012-06-29 12:54:53 +0200699 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
700
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200701 /* Remove the element from sys.path that was added because of our
702 * argv[0] value in Py3Init_vim(). Previously we used an empty
703 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200704 * the current directory in sys.path.
705 * Only after vim has been imported, the element does exist in
706 * sys.path.
707 */
708 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 +0200709
710 // lock is created and acquired in PyEval_InitThreads() and thread
711 // state is created in Py_Initialize()
712 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
713 // (python must have threads enabled!)
714 // so the following does both: unlock GIL and save thread state in TLS
715 // without deleting thread state
716 PyGILState_Release(pygilstate);
717
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200718 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200719 }
720
721 return 0;
722
723fail:
724 /* We call PythonIO_Flush() here to print any Python errors.
725 * This is OK, as it is possible to call this function even
726 * if PythonIO_Init() has not completed successfully (it will
727 * not do anything in this case).
728 */
729 PythonIO_Flush();
730 return -1;
731}
732
733/*
734 * External interface
735 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200736 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200737DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200738{
739#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200740 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200741#endif
742#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200743 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200744#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200745 PyObject *cmdstr;
746 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200747
748#if defined(MACOS) && !defined(MACOS_X_UNIX)
749 GetPort(&oldPort);
750 /* Check if the Python library is available */
751 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200752 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200753#endif
754 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200755 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200756
Bram Moolenaardb913952012-06-29 12:54:53 +0200757 if (rettv == NULL)
758 {
759 RangeStart = eap->line1;
760 RangeEnd = eap->line2;
761 }
762 else
763 {
764 RangeStart = (PyInt) curwin->w_cursor.lnum;
765 RangeEnd = RangeStart;
766 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200767 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200768
769#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
770 /* Python only works properly when the LC_NUMERIC locale is "C". */
771 saved_locale = setlocale(LC_NUMERIC, NULL);
772 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200774 else
775 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200776 /* Need to make a copy, value may change when setting new locale. */
777 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
778 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200779 }
780#endif
781
782 pygilstate = PyGILState_Ensure();
783
Bram Moolenaar19e60942011-06-19 00:27:51 +0200784 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
785 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200786 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
787 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
788 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200789 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200790 if (rettv == NULL)
791 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
792 else
793 {
794 PyObject *r;
795
796 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
797 globals, globals);
798 if (r == NULL)
799 EMSG(_("E860: Eval did not return a valid python 3 object"));
800 else
801 {
802 if (ConvertFromPyObject(r, rettv) == -1)
803 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
804 Py_DECREF(r);
805 }
806 PyErr_Clear();
807 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200808 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809
810 PyGILState_Release(pygilstate);
811
812#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
813 if (saved_locale != NULL)
814 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200815 (void)setlocale(LC_NUMERIC, saved_locale);
816 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200817 }
818#endif
819
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200820 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200821 PythonIO_Flush();
822#if defined(MACOS) && !defined(MACOS_X_UNIX)
823 SetPort(oldPort);
824#endif
825
826theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200827 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200828}
829
830/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200831 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200832 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200833 void
834ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200835{
836 char_u *script;
837
838 script = script_get(eap, eap->arg);
839 if (!eap->skip)
840 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200841 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200842 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200843 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200844 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200845 }
846 vim_free(script);
847}
848
849#define BUFFER_SIZE 2048
850
851/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200852 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853 */
854 void
855ex_py3file(exarg_T *eap)
856{
857 static char buffer[BUFFER_SIZE];
858 const char *file;
859 char *p;
860 int i;
861
862 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
863 * stdio file pointer, but Vim and the Python DLL are compiled with
864 * different options under Windows, meaning that stdio pointers aren't
865 * compatible between the two. Yuk.
866 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200867 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
868 *
869 * Using bytes so that Python can detect the source encoding as it normally
870 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200871 *
872 * We need to escape any backslashes or single quotes in the file name, so that
873 * Python won't mangle the file name.
874 */
875
876 strcpy(buffer, "exec(compile(open('");
877 p = buffer + 19; /* size of "exec(compile(open('" */
878
879 for (i=0; i<2; ++i)
880 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200881 file = (char *)eap->arg;
882 while (*file && p < buffer + (BUFFER_SIZE - 3))
883 {
884 if (*file == '\\' || *file == '\'')
885 *p++ = '\\';
886 *p++ = *file++;
887 }
888 /* If we didn't finish the file name, we hit a buffer overflow */
889 if (*file != '\0')
890 return;
891 if (i==0)
892 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200893 strcpy(p,"','rb').read(),'");
894 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200895 }
896 else
897 {
898 strcpy(p,"','exec'))");
899 p += 10;
900 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200901 }
902
903
904 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200905 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200906}
907
908/******************************************************
909 * 2. Python output stream: writes output via [e]msg().
910 */
911
912/* Implementation functions
913 */
914
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200915 static PyObject *
916OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917{
918 char *name = "";
919 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921
922 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200923 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200924
925 return PyObject_GenericGetAttr(self, nameobj);
926}
927
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200928 static int
929OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930{
931 char *name = "";
932 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200933 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200934
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 if (val == NULL)
936 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200937 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
938 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939 }
940
941 if (strcmp(name, "softspace") == 0)
942 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 if (!PyLong_Check(val))
944 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200945 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
946 return -1;
947 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200948
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200949 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
950 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200951 }
952
953 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
954 return -1;
955}
956
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957/***************/
958
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200959 static int
960PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200961{
962 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200963 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200964}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200965
966 static void
967PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968{
969 PySys_SetObject("stdout", NULL);
970 PySys_SetObject("stderr", NULL);
971}
972
973/******************************************************
974 * 3. Implementation of the Vim module for Python
975 */
976
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200977/* Window type - Implementation functions
978 * --------------------------------------
979 */
980
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
982
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983/* Buffer type - Implementation functions
984 * --------------------------------------
985 */
986
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
988
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989static Py_ssize_t BufferLength(PyObject *);
990static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200991static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
992static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200993
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994
995/* Line range type - Implementation functions
996 * --------------------------------------
997 */
998
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1000
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001001static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001003static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005/* Current objects type - Implementation functions
1006 * -----------------------------------------------
1007 */
1008
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001009static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001010 (lenfunc) BufferLength, /* sq_length, len(x) */
1011 (binaryfunc) 0, /* sq_concat, x+y */
1012 (ssizeargfunc) 0, /* sq_repeat, x*n */
1013 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1014 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001015 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001016 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017 0, /* sq_contains */
1018 0, /* sq_inplace_concat */
1019 0, /* sq_inplace_repeat */
1020};
1021
1022PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001023 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001024 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001025 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026};
1027
1028
1029/* Buffer object - Definitions
1030 */
1031
1032static PyTypeObject BufferType;
1033
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001034 static PyObject *
1035BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001036{
1037 /* We need to handle deletion of buffers underneath us.
1038 * If we add a "b_python3_ref" field to the buf_T structure,
1039 * then we can get at it in buf_freeall() in vim. We then
1040 * need to create only ONE Python object per buffer - if
1041 * we try to create a second, just INCREF the existing one
1042 * and return it. The (single) Python object referring to
1043 * the buffer is stored in "b_python3_ref".
1044 * Question: what to do on a buf_freeall(). We'll probably
1045 * have to either delete the Python object (DECREF it to
1046 * zero - a bad idea, as it leaves dangling refs!) or
1047 * set the buf_T * value to an invalid value (-1?), which
1048 * means we need checks in all access functions... Bah.
1049 */
1050
1051 BufferObject *self;
1052
1053 if (buf->b_python3_ref != NULL)
1054 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001055 self = buf->b_python3_ref;
1056 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001057 }
1058 else
1059 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001060 self = PyObject_NEW(BufferObject, &BufferType);
1061 buf->b_python3_ref = self;
1062 if (self == NULL)
1063 return NULL;
1064 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001065 }
1066
1067 return (PyObject *)(self);
1068}
1069
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001070 static void
1071BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072{
1073 BufferObject *this = (BufferObject *)(self);
1074
1075 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001076 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001077
1078 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001079}
1080
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001081 static PyObject *
1082BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083{
1084 BufferObject *this = (BufferObject *)(self);
1085
1086 char *name = "";
1087 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001088 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089
1090 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001091 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092
1093 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001094 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001095 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001096 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001098 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001099}
1100
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001102BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1103{
1104 return Py_BuildValue("[sssss]", "name", "number",
1105 "append", "mark", "range");
1106}
1107
1108 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001109BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110{
1111 static char repr[100];
1112 BufferObject *this = (BufferObject *)(self);
1113
1114 if (this->buf == INVALID_BUFFER_VALUE)
1115 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001116 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1117 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118 }
1119 else
1120 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001121 char *name = (char *)this->buf->b_fname;
1122 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 if (name == NULL)
1125 name = "";
1126 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001127
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001128 if (len > 35)
1129 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001130
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001131 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001132
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001134 }
1135}
1136
1137/******************/
1138
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001139 static Py_ssize_t
1140BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001141{
1142 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001144
1145 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1146}
1147
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001148 static PyObject *
1149BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001150{
1151 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001152 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001153}
1154
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001155 static PyObject *
1156BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1157{
1158 return RBSlice((BufferObject *)(self), lo, hi, 1,
1159 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1160}
1161
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001162 static PyObject *
1163BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164{
Bram Moolenaardb913952012-06-29 12:54:53 +02001165 if (PyLong_Check(idx))
1166 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001167 long _idx = PyLong_AsLong(idx);
1168 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001169 } else if (PySlice_Check(idx))
1170 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001171 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001173 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001174 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1175 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001176 &step, &slicelen) < 0)
1177 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 return NULL;
1179 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001180 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001181 }
1182 else
1183 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001184 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1185 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186 }
1187}
1188
Bram Moolenaar19e60942011-06-19 00:27:51 +02001189 static Py_ssize_t
1190BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1191{
Bram Moolenaardb913952012-06-29 12:54:53 +02001192 if (PyLong_Check(idx))
1193 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001194 long n = PyLong_AsLong(idx);
1195 return RBAsItem((BufferObject *)(self), n, val, 1,
1196 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1197 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001198 } else if (PySlice_Check(idx))
1199 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001200 Py_ssize_t start, stop, step, slicelen;
1201
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001202 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001203 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1204 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001205 &step, &slicelen) < 0)
1206 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001207 return -1;
1208 }
1209 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1210 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1211 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001212 }
1213 else
1214 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001215 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1216 return -1;
1217 }
1218}
1219
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001220static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001221 (lenfunc) RangeLength, /* sq_length, len(x) */
1222 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1223 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1224 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1225 0, /* was_sq_slice, x[i:j] */
1226 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1227 0, /* sq_ass_slice, x[i:j]=v */
1228 0, /* sq_contains */
1229 0, /* sq_inplace_concat */
1230 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231};
1232
1233PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001236 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001237};
1238
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239/* Line range object - Implementation
1240 */
1241
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001242 static void
1243RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244{
1245 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001246 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247}
1248
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001249 static PyObject *
1250RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251{
1252 char *name = "";
1253 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001254 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255
1256 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001257 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001258 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001261 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262}
1263
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001264/****************/
1265
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001266 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001267RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001268{
1269 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001270 ((RangeObject *)(self))->start,
1271 ((RangeObject *)(self))->end,
1272 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001273}
1274
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001275 static Py_ssize_t
1276RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1277{
1278 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1279 ((RangeObject *)(self))->start,
1280 ((RangeObject *)(self))->end,
1281 &((RangeObject *)(self))->end);
1282}
1283
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001284 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001285RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001286{
Bram Moolenaardb913952012-06-29 12:54:53 +02001287 if (PyLong_Check(idx))
1288 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001289 long _idx = PyLong_AsLong(idx);
1290 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001291 } else if (PySlice_Check(idx))
1292 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001293 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001294
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001295 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001296 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1297 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001298 &step, &slicelen) < 0)
1299 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001300 return NULL;
1301 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001302 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001303 }
1304 else
1305 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001306 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1307 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308 }
1309}
1310
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001311 static Py_ssize_t
1312RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1313{
Bram Moolenaardb913952012-06-29 12:54:53 +02001314 if (PyLong_Check(idx))
1315 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001316 long n = PyLong_AsLong(idx);
1317 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001318 } else if (PySlice_Check(idx))
1319 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001320 Py_ssize_t start, stop, step, slicelen;
1321
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001322 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001323 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1324 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001325 &step, &slicelen) < 0)
1326 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001327 return -1;
1328 }
1329 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001330 }
1331 else
1332 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001333 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1334 return -1;
1335 }
1336}
1337
1338
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001339/* Buffer list object - Definitions
1340 */
1341
1342typedef struct
1343{
1344 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001345} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001346
1347static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 (lenfunc) BufListLength, /* sq_length, len(x) */
1349 (binaryfunc) 0, /* sq_concat, x+y */
1350 (ssizeargfunc) 0, /* sq_repeat, x*n */
1351 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1352 0, /* was_sq_slice, x[i:j] */
1353 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1354 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001355 0, /* sq_contains */
1356 0, /* sq_inplace_concat */
1357 0, /* sq_inplace_repeat */
1358};
1359
1360static PyTypeObject BufListType;
1361
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001362/* Window object - Definitions
1363 */
1364
1365static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001366 /* name, function, calling, documentation */
1367 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368};
1369
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001371
1372/* Window object - Implementation
1373 */
1374
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001375 static PyObject *
1376WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001377{
1378 /* We need to handle deletion of windows underneath us.
1379 * If we add a "w_python3_ref" field to the win_T structure,
1380 * then we can get at it in win_free() in vim. We then
1381 * need to create only ONE Python object per window - if
1382 * we try to create a second, just INCREF the existing one
1383 * and return it. The (single) Python object referring to
1384 * the window is stored in "w_python3_ref".
1385 * On a win_free() we set the Python object's win_T* field
1386 * to an invalid value. We trap all uses of a window
1387 * object, and reject them if the win_T* field is invalid.
1388 */
1389
1390 WindowObject *self;
1391
1392 if (win->w_python3_ref)
1393 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001394 self = win->w_python3_ref;
1395 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001396 }
1397 else
1398 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001399 self = PyObject_NEW(WindowObject, &WindowType);
1400 if (self == NULL)
1401 return NULL;
1402 self->win = win;
1403 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001404 }
1405
1406 return (PyObject *)(self);
1407}
1408
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001409 static void
1410WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001411{
1412 WindowObject *this = (WindowObject *)(self);
1413
1414 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001415 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001416
1417 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418}
1419
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001420 static PyObject *
1421WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001422{
1423 WindowObject *this = (WindowObject *)(self);
1424
1425 char *name = "";
1426 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001427 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001428
1429
1430 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432
1433 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435 else if (strcmp(name, "cursor") == 0)
1436 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001439 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440 }
1441 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001442 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443#ifdef FEAT_VERTSPLIT
1444 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001445 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001446#endif
1447 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001448 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001450 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001451}
1452
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001453 static int
1454WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001455{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001456 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001457
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001461 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001462}
1463
1464/* Window list object - Definitions
1465 */
1466
1467typedef struct
1468{
1469 PyObject_HEAD
1470}
1471WinListObject;
1472
1473static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001474 (lenfunc) WinListLength, /* sq_length, len(x) */
1475 (binaryfunc) 0, /* sq_concat, x+y */
1476 (ssizeargfunc) 0, /* sq_repeat, x*n */
1477 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1478 0, /* sq_slice, x[i:j] */
1479 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1480 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001481 0, /* sq_contains */
1482 0, /* sq_inplace_concat */
1483 0, /* sq_inplace_repeat */
1484};
1485
1486static PyTypeObject WinListType;
1487
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001488/* Current items object - Definitions
1489 */
1490
1491typedef struct
1492{
1493 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001494} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001495
1496static PyTypeObject CurrentType;
1497
1498/* Current items object - Implementation
1499 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001500 static PyObject *
1501CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502{
1503 char *name = "";
1504 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001505 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506
1507 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001512 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001513 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001515 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001516 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001517 else
1518 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001519 PyErr_SetString(PyExc_AttributeError, name);
1520 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521 }
1522}
1523
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001524 static int
1525CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001526{
1527 char *name = "";
1528 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001529 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001530
1531 if (strcmp(name, "line") == 0)
1532 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1534 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001536 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001537 }
1538 else
1539 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001540 PyErr_SetString(PyExc_AttributeError, name);
1541 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001542 }
1543}
1544
Bram Moolenaardb913952012-06-29 12:54:53 +02001545/* Dictionary object - Definitions
1546 */
1547
1548static PyInt DictionaryLength(PyObject *);
1549
1550static PyMappingMethods DictionaryAsMapping = {
1551 /* mp_length */ (lenfunc) DictionaryLength,
1552 /* mp_subscript */ (binaryfunc) DictionaryItem,
1553 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1554};
1555
1556static PyTypeObject DictionaryType;
1557
1558 static void
1559DictionaryDestructor(PyObject *self)
1560{
1561 DictionaryObject *this = (DictionaryObject *)(self);
1562
1563 pyll_remove(&this->ref, &lastdict);
1564 dict_unref(this->dict);
1565
1566 Py_TYPE(self)->tp_free((PyObject*)self);
1567}
1568
1569/* List object - Definitions
1570 */
1571
1572static PyInt ListLength(PyObject *);
1573static PyObject *ListItem(PyObject *, Py_ssize_t);
1574
1575static PySequenceMethods ListAsSeq = {
1576 (lenfunc) ListLength, /* sq_length, len(x) */
1577 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1578 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1579 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1580 (void *) 0, /* was_sq_slice, x[i:j] */
1581 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1582 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1583 0, /* sq_contains */
1584 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1585 0, /* sq_inplace_repeat */
1586};
1587
1588static PyObject *ListSubscript(PyObject *, PyObject *);
1589static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1590
1591static PyMappingMethods ListAsMapping = {
1592 /* mp_length */ (lenfunc) ListLength,
1593 /* mp_subscript */ (binaryfunc) ListSubscript,
1594 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1595};
1596
1597static PyTypeObject ListType;
1598
1599 static PyObject *
1600ListSubscript(PyObject *self, PyObject* idxObject)
1601{
1602 if (PyLong_Check(idxObject))
1603 {
1604 long idx = PyLong_AsLong(idxObject);
1605 return ListItem(self, idx);
1606 }
1607 else if (PySlice_Check(idxObject))
1608 {
1609 Py_ssize_t start, stop, step, slicelen;
1610
1611 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1612 &step, &slicelen) < 0)
1613 return NULL;
1614 return ListSlice(self, start, stop);
1615 }
1616 else
1617 {
1618 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1619 return NULL;
1620 }
1621}
1622
1623 static Py_ssize_t
1624ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1625{
1626 if (PyLong_Check(idxObject))
1627 {
1628 long idx = PyLong_AsLong(idxObject);
1629 return ListAssItem(self, idx, obj);
1630 }
1631 else if (PySlice_Check(idxObject))
1632 {
1633 Py_ssize_t start, stop, step, slicelen;
1634
1635 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1636 &step, &slicelen) < 0)
1637 return -1;
1638 return ListAssSlice(self, start, stop, obj);
1639 }
1640 else
1641 {
1642 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1643 return -1;
1644 }
1645}
1646
1647 static void
1648ListDestructor(PyObject *self)
1649{
1650 ListObject *this = (ListObject *)(self);
1651
1652 pyll_remove(&this->ref, &lastlist);
1653 list_unref(this->list);
1654
1655 Py_TYPE(self)->tp_free((PyObject*)self);
1656}
1657
1658/* Function object - Definitions
1659 */
1660
1661 static void
1662FunctionDestructor(PyObject *self)
1663{
1664 FunctionObject *this = (FunctionObject *) (self);
1665
1666 func_unref(this->name);
1667 PyMem_Del(this->name);
1668
1669 Py_TYPE(self)->tp_free((PyObject*)self);
1670}
1671
1672 static PyObject *
1673FunctionGetattro(PyObject *self, PyObject *nameobj)
1674{
1675 FunctionObject *this = (FunctionObject *)(self);
1676 char *name = "";
1677 if (PyUnicode_Check(nameobj))
1678 name = _PyUnicode_AsString(nameobj);
1679
1680 if (strcmp(name, "name") == 0)
1681 return PyUnicode_FromString((char *)(this->name));
1682
1683 return PyObject_GenericGetAttr(self, nameobj);
1684}
1685
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001686/* External interface
1687 */
1688
1689 void
1690python3_buffer_free(buf_T *buf)
1691{
1692 if (buf->b_python3_ref != NULL)
1693 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001694 BufferObject *bp = buf->b_python3_ref;
1695 bp->buf = INVALID_BUFFER_VALUE;
1696 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001697 }
1698}
1699
1700#if defined(FEAT_WINDOWS) || defined(PROTO)
1701 void
1702python3_window_free(win_T *win)
1703{
1704 if (win->w_python3_ref != NULL)
1705 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001706 WindowObject *wp = win->w_python3_ref;
1707 wp->win = INVALID_WINDOW_VALUE;
1708 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001709 }
1710}
1711#endif
1712
1713static BufListObject TheBufferList =
1714{
1715 PyObject_HEAD_INIT(&BufListType)
1716};
1717
1718static WinListObject TheWindowList =
1719{
1720 PyObject_HEAD_INIT(&WinListType)
1721};
1722
1723static CurrentObject TheCurrent =
1724{
1725 PyObject_HEAD_INIT(&CurrentType)
1726};
1727
1728PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1729
1730static struct PyModuleDef vimmodule;
1731
Bram Moolenaar69154f22010-07-18 21:42:34 +02001732#ifndef PROTO
1733PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001734{
1735 PyObject *mod;
1736 /* The special value is removed from sys.path in Python3_Init(). */
1737 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1738
1739 PyType_Ready(&BufferType);
1740 PyType_Ready(&RangeType);
1741 PyType_Ready(&WindowType);
1742 PyType_Ready(&BufListType);
1743 PyType_Ready(&WinListType);
1744 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001745 PyType_Ready(&DictionaryType);
1746 PyType_Ready(&ListType);
1747 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001748
1749 /* Set sys.argv[] to avoid a crash in warn(). */
1750 PySys_SetArgv(1, argv);
1751
1752 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001753 if (mod == NULL)
1754 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001755
Bram Moolenaar19e60942011-06-19 00:27:51 +02001756 VimError = PyErr_NewException("vim.error", NULL, NULL);
1757 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001758
1759 PyModule_AddObject(mod, "error", VimError);
1760 Py_INCREF((PyObject *)(void *)&TheBufferList);
1761 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1762 Py_INCREF((PyObject *)(void *)&TheCurrent);
1763 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1764 Py_INCREF((PyObject *)(void *)&TheWindowList);
1765 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1766
1767 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001768 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001769
1770 return mod;
1771}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001772#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001773
1774/*************************************************************************
1775 * 4. Utility functions for handling the interface between Vim and Python.
1776 */
1777
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001778/* Convert a Vim line into a Python string.
1779 * All internal newlines are replaced by null characters.
1780 *
1781 * On errors, the Python exception data is set, and NULL is returned.
1782 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001783 static PyObject *
1784LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001785{
1786 PyObject *result;
1787 Py_ssize_t len = strlen(str);
1788 char *tmp,*p;
1789
1790 tmp = (char *)alloc((unsigned)(len+1));
1791 p = tmp;
1792 if (p == NULL)
1793 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001794 PyErr_NoMemory();
1795 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001796 }
1797
1798 while (*str)
1799 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001800 if (*str == '\n')
1801 *p = '\0';
1802 else
1803 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001804
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001805 ++p;
1806 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001807 }
1808 *p = '\0';
1809
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001810 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001811
1812 vim_free(tmp);
1813 return result;
1814}
1815
Bram Moolenaardb913952012-06-29 12:54:53 +02001816 void
1817do_py3eval (char_u *str, typval_T *rettv)
1818{
1819 DoPy3Command(NULL, (char *) str, rettv);
1820 switch(rettv->v_type)
1821 {
1822 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1823 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1824 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001825 case VAR_UNKNOWN:
1826 rettv->v_type = VAR_NUMBER;
1827 rettv->vval.v_number = 0;
1828 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001829 }
1830}
1831
1832 void
1833set_ref_in_python3 (int copyID)
1834{
1835 set_ref_in_py(copyID);
1836}
1837
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001838 static void
1839init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001840{
1841 vim_memset(&OutputType, 0, sizeof(OutputType));
1842 OutputType.tp_name = "vim.message";
1843 OutputType.tp_basicsize = sizeof(OutputObject);
1844 OutputType.tp_getattro = OutputGetattro;
1845 OutputType.tp_setattro = OutputSetattro;
1846 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1847 OutputType.tp_doc = "vim message object";
1848 OutputType.tp_methods = OutputMethods;
1849 OutputType.tp_alloc = call_PyType_GenericAlloc;
1850 OutputType.tp_new = call_PyType_GenericNew;
1851 OutputType.tp_free = call_PyObject_Free;
1852
1853 vim_memset(&BufferType, 0, sizeof(BufferType));
1854 BufferType.tp_name = "vim.buffer";
1855 BufferType.tp_basicsize = sizeof(BufferType);
1856 BufferType.tp_dealloc = BufferDestructor;
1857 BufferType.tp_repr = BufferRepr;
1858 BufferType.tp_as_sequence = &BufferAsSeq;
1859 BufferType.tp_as_mapping = &BufferAsMapping;
1860 BufferType.tp_getattro = BufferGetattro;
1861 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1862 BufferType.tp_doc = "vim buffer object";
1863 BufferType.tp_methods = BufferMethods;
1864 BufferType.tp_alloc = call_PyType_GenericAlloc;
1865 BufferType.tp_new = call_PyType_GenericNew;
1866 BufferType.tp_free = call_PyObject_Free;
1867
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001868 vim_memset(&WindowType, 0, sizeof(WindowType));
1869 WindowType.tp_name = "vim.window";
1870 WindowType.tp_basicsize = sizeof(WindowObject);
1871 WindowType.tp_dealloc = WindowDestructor;
1872 WindowType.tp_repr = WindowRepr;
1873 WindowType.tp_getattro = WindowGetattro;
1874 WindowType.tp_setattro = WindowSetattro;
1875 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1876 WindowType.tp_doc = "vim Window object";
1877 WindowType.tp_methods = WindowMethods;
1878 WindowType.tp_alloc = call_PyType_GenericAlloc;
1879 WindowType.tp_new = call_PyType_GenericNew;
1880 WindowType.tp_free = call_PyObject_Free;
1881
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001882 vim_memset(&BufListType, 0, sizeof(BufListType));
1883 BufListType.tp_name = "vim.bufferlist";
1884 BufListType.tp_basicsize = sizeof(BufListObject);
1885 BufListType.tp_as_sequence = &BufListAsSeq;
1886 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1887 BufferType.tp_doc = "vim buffer list";
1888
1889 vim_memset(&WinListType, 0, sizeof(WinListType));
1890 WinListType.tp_name = "vim.windowlist";
1891 WinListType.tp_basicsize = sizeof(WinListType);
1892 WinListType.tp_as_sequence = &WinListAsSeq;
1893 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1894 WinListType.tp_doc = "vim window list";
1895
1896 vim_memset(&RangeType, 0, sizeof(RangeType));
1897 RangeType.tp_name = "vim.range";
1898 RangeType.tp_basicsize = sizeof(RangeObject);
1899 RangeType.tp_dealloc = RangeDestructor;
1900 RangeType.tp_repr = RangeRepr;
1901 RangeType.tp_as_sequence = &RangeAsSeq;
1902 RangeType.tp_as_mapping = &RangeAsMapping;
1903 RangeType.tp_getattro = RangeGetattro;
1904 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1905 RangeType.tp_doc = "vim Range object";
1906 RangeType.tp_methods = RangeMethods;
1907 RangeType.tp_alloc = call_PyType_GenericAlloc;
1908 RangeType.tp_new = call_PyType_GenericNew;
1909 RangeType.tp_free = call_PyObject_Free;
1910
1911 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1912 CurrentType.tp_name = "vim.currentdata";
1913 CurrentType.tp_basicsize = sizeof(CurrentObject);
1914 CurrentType.tp_getattro = CurrentGetattro;
1915 CurrentType.tp_setattro = CurrentSetattro;
1916 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1917 CurrentType.tp_doc = "vim current object";
1918
Bram Moolenaardb913952012-06-29 12:54:53 +02001919 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1920 DictionaryType.tp_name = "vim.dictionary";
1921 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
1922 DictionaryType.tp_dealloc = DictionaryDestructor;
1923 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1924 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1925 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1926 DictionaryType.tp_methods = DictionaryMethods;
1927
1928 vim_memset(&ListType, 0, sizeof(ListType));
1929 ListType.tp_name = "vim.list";
1930 ListType.tp_dealloc = ListDestructor;
1931 ListType.tp_basicsize = sizeof(ListObject);
1932 ListType.tp_as_sequence = &ListAsSeq;
1933 ListType.tp_as_mapping = &ListAsMapping;
1934 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1935 ListType.tp_doc = "list pushing modifications to vim structure";
1936 ListType.tp_methods = ListMethods;
1937
1938 vim_memset(&FunctionType, 0, sizeof(FunctionType));
1939 FunctionType.tp_name = "vim.list";
1940 FunctionType.tp_basicsize = sizeof(FunctionObject);
1941 FunctionType.tp_getattro = FunctionGetattro;
1942 FunctionType.tp_dealloc = FunctionDestructor;
1943 FunctionType.tp_call = FunctionCall;
1944 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1945 FunctionType.tp_doc = "object that calls vim function";
1946 FunctionType.tp_methods = FunctionMethods;
1947
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001948 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1949 vimmodule.m_name = "vim";
1950 vimmodule.m_doc = vim_module_doc;
1951 vimmodule.m_size = -1;
1952 vimmodule.m_methods = VimMethods;
1953}