blob: 6dc75517c65e14389240d47dc40fafb3f1db0475 [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#ifdef F_BLANK
46# undef F_BLANK
47#endif
48
Bram Moolenaar6df6f472010-07-18 18:04:50 +020049#ifdef HAVE_STDARG_H
50# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
51#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020052#ifdef _POSIX_C_SOURCE /* defined in feature.h */
53# undef _POSIX_C_SOURCE
54#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020055#ifdef _XOPEN_SOURCE
56# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
57#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020058
59#include <Python.h>
60#if defined(MACOS) && !defined(MACOS_X_UNIX)
61# include "macglue.h"
62# include <CodeFragments.h>
63#endif
64#undef main /* Defined in python.h - aargh */
65#undef HAVE_FCNTL_H /* Clash with os_win32.h */
66
Bram Moolenaare8cdcef2012-09-12 20:21:43 +020067#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
68# define PY_SSIZE_T_CLEAN
69#endif
70
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020071static void init_structs(void);
72
Bram Moolenaar3d64a312011-07-15 15:54:44 +020073/* The "surrogateescape" error handler is new in Python 3.1 */
74#if PY_VERSION_HEX >= 0x030100f0
75# define CODEC_ERROR_HANDLER "surrogateescape"
76#else
77# define CODEC_ERROR_HANDLER NULL
78#endif
79
Bram Moolenaar2afa3232012-06-29 16:28:28 +020080/* Python 3 does not support CObjects, always use Capsules */
81#define PY_USE_CAPSULE
82
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020083#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020084#define PyString_Check(obj) PyUnicode_Check(obj)
Bram Moolenaardb913952012-06-29 12:54:53 +020085#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
Bram Moolenaar19e60942011-06-19 00:27:51 +020086#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
87#define PyString_AsString(obj) PyBytes_AsString(obj)
88#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020089#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +020090#define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
Bram Moolenaar77045652012-09-21 13:46:06 +020091#define PyInt_Check(obj) PyLong_Check(obj)
92#define PyInt_FromLong(i) PyLong_FromLong(i)
93#define PyInt_AsLong(obj) PyLong_AsLong(obj)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020094
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010095#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020096
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020097# ifndef WIN3264
98# include <dlfcn.h>
99# define FARPROC void*
100# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100101# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200102# define load_dll(n) dlopen((n), RTLD_LAZY)
103# else
104# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
105# endif
106# define close_dll dlclose
107# define symbol_from_dll dlsym
108# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200109# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# define close_dll FreeLibrary
111# define symbol_from_dll GetProcAddress
112# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200113/*
114 * Wrapper defines
115 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200116# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200117# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200119# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200120# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200121# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200122# define PyDict_SetItemString py3_PyDict_SetItemString
123# define PyErr_BadArgument py3_PyErr_BadArgument
124# define PyErr_Clear py3_PyErr_Clear
125# define PyErr_NoMemory py3_PyErr_NoMemory
126# define PyErr_Occurred py3_PyErr_Occurred
127# define PyErr_SetNone py3_PyErr_SetNone
128# define PyErr_SetString py3_PyErr_SetString
129# define PyEval_InitThreads py3_PyEval_InitThreads
130# define PyEval_RestoreThread py3_PyEval_RestoreThread
131# define PyEval_SaveThread py3_PyEval_SaveThread
132# define PyGILState_Ensure py3_PyGILState_Ensure
133# define PyGILState_Release py3_PyGILState_Release
134# define PyLong_AsLong py3_PyLong_AsLong
135# define PyLong_FromLong py3_PyLong_FromLong
136# define PyList_GetItem py3_PyList_GetItem
137# define PyList_Append py3_PyList_Append
138# define PyList_New py3_PyList_New
139# define PyList_SetItem py3_PyList_SetItem
140# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200141# define PySequence_Check py3_PySequence_Check
142# define PySequence_Size py3_PySequence_Size
143# define PySequence_GetItem py3_PySequence_GetItem
144# define PyTuple_Size py3_PyTuple_Size
145# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200146# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
147# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200148# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200149# define PyObject_Init py3__PyObject_Init
150# define PyDict_New py3_PyDict_New
151# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200152# define PyDict_Next py3_PyDict_Next
153# define PyMapping_Check py3_PyMapping_Check
154# define PyMapping_Items py3_PyMapping_Items
155# define PyIter_Next py3_PyIter_Next
156# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200157# define PyModule_GetDict py3_PyModule_GetDict
158#undef PyRun_SimpleString
159# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200160#undef PyRun_String
161# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200162# define PySys_SetObject py3_PySys_SetObject
163# define PySys_SetArgv py3_PySys_SetArgv
164# define PyType_Type (*py3_PyType_Type)
165# define PyType_Ready py3_PyType_Ready
166#undef Py_BuildValue
167# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100168# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200169# define Py_Initialize py3_Py_Initialize
170# define Py_Finalize py3_Py_Finalize
171# define Py_IsInitialized py3_Py_IsInitialized
172# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200173# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200174# define PyModule_AddObject py3_PyModule_AddObject
175# define PyImport_AppendInittab py3_PyImport_AppendInittab
176# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200177# undef PyUnicode_AsEncodedString
178# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
179# undef PyBytes_AsString
180# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200181# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200182# undef PyBytes_FromString
183# define PyBytes_FromString py3_PyBytes_FromString
184# define PyFloat_FromDouble py3_PyFloat_FromDouble
185# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200186# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
187# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200188# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200189# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200190# ifdef Py_DEBUG
191# define _Py_NegativeRefcount py3__Py_NegativeRefcount
192# define _Py_RefTotal (*py3__Py_RefTotal)
193# define _Py_Dealloc py3__Py_Dealloc
194# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
195# define _PyObject_DebugFree py3__PyObject_DebugFree
196# else
197# define PyObject_Malloc py3_PyObject_Malloc
198# define PyObject_Free py3_PyObject_Free
199# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200200# define PyType_GenericAlloc py3_PyType_GenericAlloc
201# define PyType_GenericNew py3_PyType_GenericNew
202# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200203# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200205# undef PyUnicode_Decode
206# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200207# define PyType_IsSubtype py3_PyType_IsSubtype
208# define PyCapsule_New py3_PyCapsule_New
209# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200210
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200211# ifdef Py_DEBUG
212# undef PyObject_NEW
213# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200214( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200215 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200216# endif
217
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218/*
219 * Pointers for dynamic link
220 */
221static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100222static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200223static void (*py3_Py_Initialize)(void);
224static PyObject* (*py3_PyList_New)(Py_ssize_t size);
225static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
226static void (*py3_PyGILState_Release)(PyGILState_STATE);
227static int (*py3_PySys_SetObject)(char *, PyObject *);
228static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
229static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200230static int (*py3_PySequence_Check)(PyObject *);
231static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
232static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
233static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
234static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
235static int (*py3_PyMapping_Check)(PyObject *);
236static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200237static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200238 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200239static PyObject* (*py3_PyErr_NoMemory)(void);
240static void (*py3_Py_Finalize)(void);
241static void (*py3_PyErr_SetString)(PyObject *, const char *);
242static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200243static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200244static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
245static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200246static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200247static int (*py3_PyErr_BadArgument)(void);
248static PyTypeObject* py3_PyType_Type;
249static PyObject* (*py3_PyErr_Occurred)(void);
250static PyObject* (*py3_PyModule_GetDict)(PyObject *);
251static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
252static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200253static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200254static PyObject* (*py3_PyLong_FromLong)(long);
255static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200256static PyObject* (*py3_PyIter_Next)(PyObject *);
257static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258static PyObject* (*py3_Py_BuildValue)(char *, ...);
259static int (*py3_PyType_Ready)(PyTypeObject *type);
260static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
261static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200262static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
263 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200264static long (*py3_PyLong_AsLong)(PyObject *);
265static void (*py3_PyErr_SetNone)(PyObject *);
266static void (*py3_PyEval_InitThreads)(void);
267static void(*py3_PyEval_RestoreThread)(PyThreadState *);
268static PyThreadState*(*py3_PyEval_SaveThread)(void);
269static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
270static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200271static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200272static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200273static int (*py3_Py_IsInitialized)(void);
274static void (*py3_PyErr_Clear)(void);
275static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200276static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277static PyObject* py3__Py_NoneStruct;
278static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
279static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
280static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200281static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
282static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200283static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200284static PyObject* (*py3_PyBytes_FromString)(char *str);
285static PyObject* (*py3_PyFloat_FromDouble)(double num);
286static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
288static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
289static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
290static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
291static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200292static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200293static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200294static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
295static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200296# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
298 static Py_ssize_t* py3__Py_RefTotal;
299 static void (*py3__Py_Dealloc)(PyObject *obj);
300 static void (*py3__PyObject_DebugFree)(void*);
301 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200302# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200303 static void (*py3_PyObject_Free)(void*);
304 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200305# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200306static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307
308static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
309
310/* Imported exception objects */
311static PyObject *p3imp_PyExc_AttributeError;
312static PyObject *p3imp_PyExc_IndexError;
313static PyObject *p3imp_PyExc_KeyboardInterrupt;
314static PyObject *p3imp_PyExc_TypeError;
315static PyObject *p3imp_PyExc_ValueError;
316
317# define PyExc_AttributeError p3imp_PyExc_AttributeError
318# define PyExc_IndexError p3imp_PyExc_IndexError
319# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
320# define PyExc_TypeError p3imp_PyExc_TypeError
321# define PyExc_ValueError p3imp_PyExc_ValueError
322
323/*
324 * Table of name to function pointer of python.
325 */
326# define PYTHON_PROC FARPROC
327static struct
328{
329 char *name;
330 PYTHON_PROC *ptr;
331} py3_funcname_table[] =
332{
333 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100334 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200335 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200336#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200337 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200338 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
339#else
340 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
341 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
342#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200343 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200344 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
346 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
347 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
348 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
349 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
350 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200351 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
352 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
353 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
354 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
355 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
357 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
358 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
359 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
360 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200361 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200362 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
363 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200364 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200365 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
366 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
367 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
368 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
369 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
370 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200371 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
372 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
373 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
374 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
375 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200376 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
377 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
379 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
380 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
381 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
382 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
383 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
384 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
385 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200386 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200387 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
389 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
390 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
391 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
392 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
393 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200394 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200395 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200396 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
397 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
398 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200399 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
400 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
401 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
402 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
403 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200404 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200405 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200406# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200407 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
408 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
409 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
410 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
411 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200412# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
414 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200415# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200416 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
417 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
418 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200419 {"", NULL},
420};
421
422/*
423 * Free python.dll
424 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200425 static void
426end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200427{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200428 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200430 close_dll(hinstPy3);
431 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200432 }
433}
434
435/*
436 * Load library and get all pointers.
437 * Parameter 'libname' provides name of DLL.
438 * Return OK or FAIL.
439 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440 static int
441py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200442{
443 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200444 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200445
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100446# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200447 /* Can't have Python and Python3 loaded at the same time.
448 * It cause a crash, because RTLD_GLOBAL is needed for
449 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200450 if (python_loaded())
451 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200452 if (verbose)
453 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200454 return FAIL;
455 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200456# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200457
458 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200459 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200460 hinstPy3 = load_dll(libname);
461
462 if (!hinstPy3)
463 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200464 if (verbose)
465 EMSG2(_(e_loadlib), libname);
466 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200467 }
468
469 for (i = 0; py3_funcname_table[i].ptr; ++i)
470 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200471 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
472 py3_funcname_table[i].name)) == NULL)
473 {
474 close_dll(hinstPy3);
475 hinstPy3 = 0;
476 if (verbose)
477 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
478 return FAIL;
479 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480 }
481
Bram Moolenaar69154f22010-07-18 21:42:34 +0200482 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
483 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200485 ucs_decode = symbol_from_dll(hinstPy3,
486 "PyUnicodeUCS2_Decode");
487 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
488 "PyUnicodeUCS2_AsEncodedString");
489 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200490 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200491 ucs_from_string = symbol_from_dll(hinstPy3,
492 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200493 ucs_decode = symbol_from_dll(hinstPy3,
494 "PyUnicodeUCS4_Decode");
495 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
496 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200498 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200499 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200500 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200501 py3_PyUnicode_Decode = ucs_decode;
502 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503 }
504 else
505 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200506 close_dll(hinstPy3);
507 hinstPy3 = 0;
508 if (verbose)
509 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
510 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 }
512
513 return OK;
514}
515
516/*
517 * If python is enabled (there is installed python on Windows system) return
518 * TRUE, else FALSE.
519 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200520 int
521python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200522{
523 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
524}
525
526/* Load the standard Python exceptions - don't import the symbols from the
527 * DLL, as this can cause errors (importing data symbols is not reliable).
528 */
529static void get_py3_exceptions __ARGS((void));
530
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200531 static void
532get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200533{
534 PyObject *exmod = PyImport_ImportModule("builtins");
535 PyObject *exdict = PyModule_GetDict(exmod);
536 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
537 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
538 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
539 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
540 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
541 Py_XINCREF(p3imp_PyExc_AttributeError);
542 Py_XINCREF(p3imp_PyExc_IndexError);
543 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
544 Py_XINCREF(p3imp_PyExc_TypeError);
545 Py_XINCREF(p3imp_PyExc_ValueError);
546 Py_XDECREF(exmod);
547}
548#endif /* DYNAMIC_PYTHON3 */
549
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200550static PyObject *BufferNew (buf_T *);
551static PyObject *WindowNew(win_T *);
552static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100553static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200554
555static PyTypeObject RangeType;
556
Bram Moolenaardb913952012-06-29 12:54:53 +0200557static int py3initialised = 0;
558
559#define PYINITIALISED py3initialised
560
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200561#define DICTKEY_DECL PyObject *bytes = NULL;
562
Bram Moolenaardb913952012-06-29 12:54:53 +0200563#define DICTKEY_GET(err) \
564 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200565 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200566 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200567 return err; \
568 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200569 else if (PyUnicode_Check(keyObject)) \
570 { \
571 bytes = PyString_AsBytes(keyObject); \
572 if (bytes == NULL) \
573 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200574 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200575 return err; \
576 } \
577 else \
578 { \
579 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
580 return err; \
581 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200582
Bram Moolenaardb913952012-06-29 12:54:53 +0200583#define DICTKEY_UNREF \
584 if (bytes != NULL) \
585 Py_XDECREF(bytes);
586
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200587/*
588 * Include the code shared with if_python.c
589 */
590#include "if_py_both.h"
591
Bram Moolenaar77045652012-09-21 13:46:06 +0200592#define GET_ATTR_STRING(name, nameobj) \
593 char *name = ""; \
594 if(PyUnicode_Check(nameobj)) \
595 name = _PyUnicode_AsString(nameobj)
596
Bram Moolenaardb913952012-06-29 12:54:53 +0200597#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
598
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200599 static void
600call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200601{
602#ifdef Py_DEBUG
603 _PyObject_DebugFree(p);
604#else
605 PyObject_Free(p);
606#endif
607}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200608
609 static PyObject *
610call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200611{
612 return PyType_GenericNew(type,args,kwds);
613}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200614
615 static PyObject *
616call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200617{
618 return PyType_GenericAlloc(type,nitems);
619}
620
621/******************************************************
622 * Internal function prototypes.
623 */
624
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200625static Py_ssize_t RangeStart;
626static Py_ssize_t RangeEnd;
627
Bram Moolenaardb913952012-06-29 12:54:53 +0200628static PyObject *globals;
629
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630static int PythonIO_Init(void);
631static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200632PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200633
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200634/******************************************************
635 * 1. Python interpreter main program.
636 */
637
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200638static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
639
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200640 void
641python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642{
643 static int recurse = 0;
644
645 /* If a crash occurs while doing this, don't try again. */
646 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200647 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200648
649 ++recurse;
650
651#ifdef DYNAMIC_PYTHON3
652 if (hinstPy3)
653#endif
654 if (Py_IsInitialized())
655 {
656 // acquire lock before finalizing
657 pygilstate = PyGILState_Ensure();
658
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200659 PythonIO_Fini();
660 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200661 }
662
663#ifdef DYNAMIC_PYTHON3
664 end_dynamic_python3();
665#endif
666
667 --recurse;
668}
669
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200670#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
671 int
672python3_loaded()
673{
674 return (hinstPy3 != 0);
675}
676#endif
677
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200678 static int
679Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200680{
681 if (!py3initialised)
682 {
683#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200684 if (!python3_enabled(TRUE))
685 {
686 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
687 goto fail;
688 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689#endif
690
691 init_structs();
692
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100693
694#ifdef PYTHON3_HOME
695 Py_SetPythonHome(PYTHON3_HOME);
696#endif
697
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200698#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200699 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200700#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200701 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200702#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200703 /* initialise threads, must be after Py_Initialize() */
704 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200705
706#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200707 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200708#endif
709
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 if (PythonIO_Init())
711 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200712
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200713 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200714
Bram Moolenaardb913952012-06-29 12:54:53 +0200715 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
716
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200717 /* Remove the element from sys.path that was added because of our
718 * argv[0] value in Py3Init_vim(). Previously we used an empty
719 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200720 * the current directory in sys.path.
721 * Only after vim has been imported, the element does exist in
722 * sys.path.
723 */
724 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 +0200725
726 // lock is created and acquired in PyEval_InitThreads() and thread
727 // state is created in Py_Initialize()
728 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
729 // (python must have threads enabled!)
730 // so the following does both: unlock GIL and save thread state in TLS
731 // without deleting thread state
732 PyGILState_Release(pygilstate);
733
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200734 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200735 }
736
737 return 0;
738
739fail:
740 /* We call PythonIO_Flush() here to print any Python errors.
741 * This is OK, as it is possible to call this function even
742 * if PythonIO_Init() has not completed successfully (it will
743 * not do anything in this case).
744 */
745 PythonIO_Flush();
746 return -1;
747}
748
749/*
750 * External interface
751 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200752 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200753DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200754{
755#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200756 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200757#endif
758#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200760#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200761 PyObject *cmdstr;
762 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763
764#if defined(MACOS) && !defined(MACOS_X_UNIX)
765 GetPort(&oldPort);
766 /* Check if the Python library is available */
767 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200768 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200769#endif
770 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200771 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200772
Bram Moolenaardb913952012-06-29 12:54:53 +0200773 if (rettv == NULL)
774 {
775 RangeStart = eap->line1;
776 RangeEnd = eap->line2;
777 }
778 else
779 {
780 RangeStart = (PyInt) curwin->w_cursor.lnum;
781 RangeEnd = RangeStart;
782 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200783 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200784
785#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
786 /* Python only works properly when the LC_NUMERIC locale is "C". */
787 saved_locale = setlocale(LC_NUMERIC, NULL);
788 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200789 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790 else
791 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 /* Need to make a copy, value may change when setting new locale. */
793 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
794 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200795 }
796#endif
797
798 pygilstate = PyGILState_Ensure();
799
Bram Moolenaar19e60942011-06-19 00:27:51 +0200800 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
801 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200802 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
803 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
804 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200805 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200806 if (rettv == NULL)
807 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
808 else
809 {
810 PyObject *r;
811
812 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
813 globals, globals);
814 if (r == NULL)
815 EMSG(_("E860: Eval did not return a valid python 3 object"));
816 else
817 {
818 if (ConvertFromPyObject(r, rettv) == -1)
819 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
820 Py_DECREF(r);
821 }
822 PyErr_Clear();
823 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200824 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200825
826 PyGILState_Release(pygilstate);
827
828#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
829 if (saved_locale != NULL)
830 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200831 (void)setlocale(LC_NUMERIC, saved_locale);
832 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200833 }
834#endif
835
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200836 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837 PythonIO_Flush();
838#if defined(MACOS) && !defined(MACOS_X_UNIX)
839 SetPort(oldPort);
840#endif
841
842theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200843 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200844}
845
846/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200847 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200849 void
850ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200851{
852 char_u *script;
853
854 script = script_get(eap, eap->arg);
855 if (!eap->skip)
856 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200857 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200858 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200859 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200860 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861 }
862 vim_free(script);
863}
864
865#define BUFFER_SIZE 2048
866
867/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200868 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200869 */
870 void
871ex_py3file(exarg_T *eap)
872{
873 static char buffer[BUFFER_SIZE];
874 const char *file;
875 char *p;
876 int i;
877
878 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
879 * stdio file pointer, but Vim and the Python DLL are compiled with
880 * different options under Windows, meaning that stdio pointers aren't
881 * compatible between the two. Yuk.
882 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200883 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
884 *
885 * Using bytes so that Python can detect the source encoding as it normally
886 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200887 *
888 * We need to escape any backslashes or single quotes in the file name, so that
889 * Python won't mangle the file name.
890 */
891
892 strcpy(buffer, "exec(compile(open('");
893 p = buffer + 19; /* size of "exec(compile(open('" */
894
895 for (i=0; i<2; ++i)
896 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200897 file = (char *)eap->arg;
898 while (*file && p < buffer + (BUFFER_SIZE - 3))
899 {
900 if (*file == '\\' || *file == '\'')
901 *p++ = '\\';
902 *p++ = *file++;
903 }
904 /* If we didn't finish the file name, we hit a buffer overflow */
905 if (*file != '\0')
906 return;
907 if (i==0)
908 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200909 strcpy(p,"','rb').read(),'");
910 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200911 }
912 else
913 {
914 strcpy(p,"','exec'))");
915 p += 10;
916 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917 }
918
919
920 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200921 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200922}
923
924/******************************************************
925 * 2. Python output stream: writes output via [e]msg().
926 */
927
928/* Implementation functions
929 */
930
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200931 static PyObject *
932OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200933{
Bram Moolenaar77045652012-09-21 13:46:06 +0200934 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935
936 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200937 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200938
939 return PyObject_GenericGetAttr(self, nameobj);
940}
941
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200942 static int
943OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200944{
Bram Moolenaar77045652012-09-21 13:46:06 +0200945 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200946
Bram Moolenaar77045652012-09-21 13:46:06 +0200947 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200948}
949
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950/***************/
951
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200952 static int
953PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200954{
955 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200956 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200958
959 static void
960PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200961{
962 PySys_SetObject("stdout", NULL);
963 PySys_SetObject("stderr", NULL);
964}
965
966/******************************************************
967 * 3. Implementation of the Vim module for Python
968 */
969
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200970/* Window type - Implementation functions
971 * --------------------------------------
972 */
973
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
975
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976/* Buffer type - Implementation functions
977 * --------------------------------------
978 */
979
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200980#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
981
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200982static Py_ssize_t BufferLength(PyObject *);
983static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200984static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
985static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987
988/* Line range type - Implementation functions
989 * --------------------------------------
990 */
991
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200992#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
993
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200994static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200995static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200996static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200998/* Current objects type - Implementation functions
999 * -----------------------------------------------
1000 */
1001
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001003 (lenfunc) BufferLength, /* sq_length, len(x) */
1004 (binaryfunc) 0, /* sq_concat, x+y */
1005 (ssizeargfunc) 0, /* sq_repeat, x*n */
1006 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1007 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001008 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001009 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010 0, /* sq_contains */
1011 0, /* sq_inplace_concat */
1012 0, /* sq_inplace_repeat */
1013};
1014
1015PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001016 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001018 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019};
1020
1021
1022/* Buffer object - Definitions
1023 */
1024
1025static PyTypeObject BufferType;
1026
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001027 static PyObject *
1028BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001029{
1030 /* We need to handle deletion of buffers underneath us.
1031 * If we add a "b_python3_ref" field to the buf_T structure,
1032 * then we can get at it in buf_freeall() in vim. We then
1033 * need to create only ONE Python object per buffer - if
1034 * we try to create a second, just INCREF the existing one
1035 * and return it. The (single) Python object referring to
1036 * the buffer is stored in "b_python3_ref".
1037 * Question: what to do on a buf_freeall(). We'll probably
1038 * have to either delete the Python object (DECREF it to
1039 * zero - a bad idea, as it leaves dangling refs!) or
1040 * set the buf_T * value to an invalid value (-1?), which
1041 * means we need checks in all access functions... Bah.
1042 */
1043
1044 BufferObject *self;
1045
1046 if (buf->b_python3_ref != NULL)
1047 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001048 self = buf->b_python3_ref;
1049 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001050 }
1051 else
1052 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001053 self = PyObject_NEW(BufferObject, &BufferType);
1054 buf->b_python3_ref = self;
1055 if (self == NULL)
1056 return NULL;
1057 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001058 }
1059
1060 return (PyObject *)(self);
1061}
1062
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001063 static void
1064BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001065{
1066 BufferObject *this = (BufferObject *)(self);
1067
1068 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001069 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001070
1071 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072}
1073
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001074 static PyObject *
1075BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001076{
1077 BufferObject *this = (BufferObject *)(self);
1078
Bram Moolenaar77045652012-09-21 13:46:06 +02001079 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001080
1081 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001082 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083
1084 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001085 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001087 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001088 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001089 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001090}
1091
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001092 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001093BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1094{
1095 return Py_BuildValue("[sssss]", "name", "number",
1096 "append", "mark", "range");
1097}
1098
1099 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001100BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001101{
1102 static char repr[100];
1103 BufferObject *this = (BufferObject *)(self);
1104
1105 if (this->buf == INVALID_BUFFER_VALUE)
1106 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001107 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1108 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109 }
1110 else
1111 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001112 char *name = (char *)this->buf->b_fname;
1113 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001114
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001115 if (name == NULL)
1116 name = "";
1117 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001119 if (len > 35)
1120 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001121
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001125 }
1126}
1127
1128/******************/
1129
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001130 static Py_ssize_t
1131BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001132{
1133 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001134 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135
1136 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1137}
1138
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001139 static PyObject *
1140BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001141{
1142 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001143 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001144}
1145
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001146 static PyObject *
1147BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1148{
1149 return RBSlice((BufferObject *)(self), lo, hi, 1,
1150 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1151}
1152
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001153 static PyObject *
1154BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155{
Bram Moolenaardb913952012-06-29 12:54:53 +02001156 if (PyLong_Check(idx))
1157 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001158 long _idx = PyLong_AsLong(idx);
1159 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001160 } else if (PySlice_Check(idx))
1161 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001162 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001163
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001164 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001165 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1166 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001167 &step, &slicelen) < 0)
1168 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001169 return NULL;
1170 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001171 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001172 }
1173 else
1174 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1176 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177 }
1178}
1179
Bram Moolenaar19e60942011-06-19 00:27:51 +02001180 static Py_ssize_t
1181BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1182{
Bram Moolenaardb913952012-06-29 12:54:53 +02001183 if (PyLong_Check(idx))
1184 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001185 long n = PyLong_AsLong(idx);
1186 return RBAsItem((BufferObject *)(self), n, val, 1,
1187 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1188 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001189 } else if (PySlice_Check(idx))
1190 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001191 Py_ssize_t start, stop, step, slicelen;
1192
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001193 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001194 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1195 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001196 &step, &slicelen) < 0)
1197 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001198 return -1;
1199 }
1200 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1201 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1202 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001203 }
1204 else
1205 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001206 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1207 return -1;
1208 }
1209}
1210
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001212 (lenfunc) RangeLength, /* sq_length, len(x) */
1213 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1214 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1215 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1216 0, /* was_sq_slice, x[i:j] */
1217 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1218 0, /* sq_ass_slice, x[i:j]=v */
1219 0, /* sq_contains */
1220 0, /* sq_inplace_concat */
1221 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222};
1223
1224PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001225 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001226 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001227 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001228};
1229
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001230/* Line range object - Implementation
1231 */
1232
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001233 static void
1234RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235{
1236 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001237 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001238}
1239
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001240 static PyObject *
1241RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242{
Bram Moolenaar77045652012-09-21 13:46:06 +02001243 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244
1245 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001249 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001250 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251}
1252
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253/****************/
1254
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001255 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001256RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001257{
1258 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 ((RangeObject *)(self))->start,
1260 ((RangeObject *)(self))->end,
1261 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262}
1263
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001264 static Py_ssize_t
1265RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1266{
1267 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1268 ((RangeObject *)(self))->start,
1269 ((RangeObject *)(self))->end,
1270 &((RangeObject *)(self))->end);
1271}
1272
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001273 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001274RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001275{
Bram Moolenaardb913952012-06-29 12:54:53 +02001276 if (PyLong_Check(idx))
1277 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001278 long _idx = PyLong_AsLong(idx);
1279 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001280 } else if (PySlice_Check(idx))
1281 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001282 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001283
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001284 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001285 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1286 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001287 &step, &slicelen) < 0)
1288 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001289 return NULL;
1290 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001291 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001292 }
1293 else
1294 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001295 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1296 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001297 }
1298}
1299
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001300 static Py_ssize_t
1301RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1302{
Bram Moolenaardb913952012-06-29 12:54:53 +02001303 if (PyLong_Check(idx))
1304 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001305 long n = PyLong_AsLong(idx);
1306 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001307 } else if (PySlice_Check(idx))
1308 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001309 Py_ssize_t start, stop, step, slicelen;
1310
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001311 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001312 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1313 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001314 &step, &slicelen) < 0)
1315 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001316 return -1;
1317 }
1318 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001319 }
1320 else
1321 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001322 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1323 return -1;
1324 }
1325}
1326
1327
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328/* Buffer list object - Definitions
1329 */
1330
1331typedef struct
1332{
1333 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001334} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001335
1336static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001337 (lenfunc) BufListLength, /* sq_length, len(x) */
1338 (binaryfunc) 0, /* sq_concat, x+y */
1339 (ssizeargfunc) 0, /* sq_repeat, x*n */
1340 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1341 0, /* was_sq_slice, x[i:j] */
1342 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1343 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001344 0, /* sq_contains */
1345 0, /* sq_inplace_concat */
1346 0, /* sq_inplace_repeat */
1347};
1348
1349static PyTypeObject BufListType;
1350
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001351/* Window object - Definitions
1352 */
1353
1354static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001355 /* name, function, calling, documentation */
1356 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001357};
1358
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001359static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001360
1361/* Window object - Implementation
1362 */
1363
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001364 static PyObject *
1365WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001366{
1367 /* We need to handle deletion of windows underneath us.
1368 * If we add a "w_python3_ref" field to the win_T structure,
1369 * then we can get at it in win_free() in vim. We then
1370 * need to create only ONE Python object per window - if
1371 * we try to create a second, just INCREF the existing one
1372 * and return it. The (single) Python object referring to
1373 * the window is stored in "w_python3_ref".
1374 * On a win_free() we set the Python object's win_T* field
1375 * to an invalid value. We trap all uses of a window
1376 * object, and reject them if the win_T* field is invalid.
1377 */
1378
1379 WindowObject *self;
1380
1381 if (win->w_python3_ref)
1382 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001383 self = win->w_python3_ref;
1384 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001385 }
1386 else
1387 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 self = PyObject_NEW(WindowObject, &WindowType);
1389 if (self == NULL)
1390 return NULL;
1391 self->win = win;
1392 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001393 }
1394
1395 return (PyObject *)(self);
1396}
1397
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001398 static void
1399WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001400{
1401 WindowObject *this = (WindowObject *)(self);
1402
1403 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001404 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001405
1406 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001407}
1408
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001409 static PyObject *
1410WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001411{
1412 WindowObject *this = (WindowObject *)(self);
1413
Bram Moolenaar77045652012-09-21 13:46:06 +02001414 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415
1416 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418
1419 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001420 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001421 else if (strcmp(name, "cursor") == 0)
1422 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001424
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001425 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426 }
1427 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001428 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001429#ifdef FEAT_VERTSPLIT
1430 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432#endif
1433 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001436 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001437}
1438
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001439 static int
1440WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001441{
Bram Moolenaar77045652012-09-21 13:46:06 +02001442 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001444 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001445}
1446
1447/* Window list object - Definitions
1448 */
1449
1450typedef struct
1451{
1452 PyObject_HEAD
1453}
1454WinListObject;
1455
1456static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001457 (lenfunc) WinListLength, /* sq_length, len(x) */
1458 (binaryfunc) 0, /* sq_concat, x+y */
1459 (ssizeargfunc) 0, /* sq_repeat, x*n */
1460 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1461 0, /* sq_slice, x[i:j] */
1462 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1463 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001464 0, /* sq_contains */
1465 0, /* sq_inplace_concat */
1466 0, /* sq_inplace_repeat */
1467};
1468
1469static PyTypeObject WinListType;
1470
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471/* Current items object - Definitions
1472 */
1473
1474typedef struct
1475{
1476 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001477} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478
1479static PyTypeObject CurrentType;
1480
1481/* Current items object - Implementation
1482 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001483 static PyObject *
1484CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001485{
Bram Moolenaar77045652012-09-21 13:46:06 +02001486 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487
1488 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001489 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001490 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001491 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001492 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001493 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001495 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001496 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001497 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498 else
1499 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001500 PyErr_SetString(PyExc_AttributeError, name);
1501 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502 }
1503}
1504
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001505 static int
1506CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507{
1508 char *name = "";
1509 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001510 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511
1512 if (strcmp(name, "line") == 0)
1513 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001514 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1515 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001517 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518 }
1519 else
1520 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 PyErr_SetString(PyExc_AttributeError, name);
1522 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001523 }
1524}
1525
Bram Moolenaardb913952012-06-29 12:54:53 +02001526/* Dictionary object - Definitions
1527 */
1528
1529static PyInt DictionaryLength(PyObject *);
1530
1531static PyMappingMethods DictionaryAsMapping = {
1532 /* mp_length */ (lenfunc) DictionaryLength,
1533 /* mp_subscript */ (binaryfunc) DictionaryItem,
1534 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1535};
1536
1537static PyTypeObject DictionaryType;
1538
1539 static void
1540DictionaryDestructor(PyObject *self)
1541{
1542 DictionaryObject *this = (DictionaryObject *)(self);
1543
1544 pyll_remove(&this->ref, &lastdict);
1545 dict_unref(this->dict);
1546
1547 Py_TYPE(self)->tp_free((PyObject*)self);
1548}
1549
1550/* List object - Definitions
1551 */
1552
1553static PyInt ListLength(PyObject *);
1554static PyObject *ListItem(PyObject *, Py_ssize_t);
1555
1556static PySequenceMethods ListAsSeq = {
1557 (lenfunc) ListLength, /* sq_length, len(x) */
1558 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1559 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1560 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1561 (void *) 0, /* was_sq_slice, x[i:j] */
1562 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1563 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1564 0, /* sq_contains */
1565 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1566 0, /* sq_inplace_repeat */
1567};
1568
1569static PyObject *ListSubscript(PyObject *, PyObject *);
1570static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1571
1572static PyMappingMethods ListAsMapping = {
1573 /* mp_length */ (lenfunc) ListLength,
1574 /* mp_subscript */ (binaryfunc) ListSubscript,
1575 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1576};
1577
1578static PyTypeObject ListType;
1579
1580 static PyObject *
1581ListSubscript(PyObject *self, PyObject* idxObject)
1582{
1583 if (PyLong_Check(idxObject))
1584 {
1585 long idx = PyLong_AsLong(idxObject);
1586 return ListItem(self, idx);
1587 }
1588 else if (PySlice_Check(idxObject))
1589 {
1590 Py_ssize_t start, stop, step, slicelen;
1591
1592 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1593 &step, &slicelen) < 0)
1594 return NULL;
1595 return ListSlice(self, start, stop);
1596 }
1597 else
1598 {
1599 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1600 return NULL;
1601 }
1602}
1603
1604 static Py_ssize_t
1605ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1606{
1607 if (PyLong_Check(idxObject))
1608 {
1609 long idx = PyLong_AsLong(idxObject);
1610 return ListAssItem(self, idx, obj);
1611 }
1612 else if (PySlice_Check(idxObject))
1613 {
1614 Py_ssize_t start, stop, step, slicelen;
1615
1616 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1617 &step, &slicelen) < 0)
1618 return -1;
1619 return ListAssSlice(self, start, stop, obj);
1620 }
1621 else
1622 {
1623 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1624 return -1;
1625 }
1626}
1627
1628 static void
1629ListDestructor(PyObject *self)
1630{
1631 ListObject *this = (ListObject *)(self);
1632
1633 pyll_remove(&this->ref, &lastlist);
1634 list_unref(this->list);
1635
1636 Py_TYPE(self)->tp_free((PyObject*)self);
1637}
1638
1639/* Function object - Definitions
1640 */
1641
1642 static void
1643FunctionDestructor(PyObject *self)
1644{
1645 FunctionObject *this = (FunctionObject *) (self);
1646
1647 func_unref(this->name);
1648 PyMem_Del(this->name);
1649
1650 Py_TYPE(self)->tp_free((PyObject*)self);
1651}
1652
1653 static PyObject *
1654FunctionGetattro(PyObject *self, PyObject *nameobj)
1655{
1656 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001657
1658 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001659
1660 if (strcmp(name, "name") == 0)
1661 return PyUnicode_FromString((char *)(this->name));
1662
1663 return PyObject_GenericGetAttr(self, nameobj);
1664}
1665
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001666/* External interface
1667 */
1668
1669 void
1670python3_buffer_free(buf_T *buf)
1671{
1672 if (buf->b_python3_ref != NULL)
1673 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001674 BufferObject *bp = buf->b_python3_ref;
1675 bp->buf = INVALID_BUFFER_VALUE;
1676 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001677 }
1678}
1679
1680#if defined(FEAT_WINDOWS) || defined(PROTO)
1681 void
1682python3_window_free(win_T *win)
1683{
1684 if (win->w_python3_ref != NULL)
1685 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 WindowObject *wp = win->w_python3_ref;
1687 wp->win = INVALID_WINDOW_VALUE;
1688 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001689 }
1690}
1691#endif
1692
1693static BufListObject TheBufferList =
1694{
1695 PyObject_HEAD_INIT(&BufListType)
1696};
1697
1698static WinListObject TheWindowList =
1699{
1700 PyObject_HEAD_INIT(&WinListType)
1701};
1702
1703static CurrentObject TheCurrent =
1704{
1705 PyObject_HEAD_INIT(&CurrentType)
1706};
1707
1708PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1709
1710static struct PyModuleDef vimmodule;
1711
Bram Moolenaar69154f22010-07-18 21:42:34 +02001712#ifndef PROTO
1713PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001714{
1715 PyObject *mod;
1716 /* The special value is removed from sys.path in Python3_Init(). */
1717 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1718
1719 PyType_Ready(&BufferType);
1720 PyType_Ready(&RangeType);
1721 PyType_Ready(&WindowType);
1722 PyType_Ready(&BufListType);
1723 PyType_Ready(&WinListType);
1724 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001725 PyType_Ready(&DictionaryType);
1726 PyType_Ready(&ListType);
1727 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001728
1729 /* Set sys.argv[] to avoid a crash in warn(). */
1730 PySys_SetArgv(1, argv);
1731
1732 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001733 if (mod == NULL)
1734 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001735
Bram Moolenaar19e60942011-06-19 00:27:51 +02001736 VimError = PyErr_NewException("vim.error", NULL, NULL);
1737 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001738
1739 PyModule_AddObject(mod, "error", VimError);
1740 Py_INCREF((PyObject *)(void *)&TheBufferList);
1741 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1742 Py_INCREF((PyObject *)(void *)&TheCurrent);
1743 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1744 Py_INCREF((PyObject *)(void *)&TheWindowList);
1745 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1746
1747 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001748 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001749
1750 return mod;
1751}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001752#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001753
1754/*************************************************************************
1755 * 4. Utility functions for handling the interface between Vim and Python.
1756 */
1757
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001758/* Convert a Vim line into a Python string.
1759 * All internal newlines are replaced by null characters.
1760 *
1761 * On errors, the Python exception data is set, and NULL is returned.
1762 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001763 static PyObject *
1764LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001765{
1766 PyObject *result;
1767 Py_ssize_t len = strlen(str);
1768 char *tmp,*p;
1769
1770 tmp = (char *)alloc((unsigned)(len+1));
1771 p = tmp;
1772 if (p == NULL)
1773 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001774 PyErr_NoMemory();
1775 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001776 }
1777
1778 while (*str)
1779 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001780 if (*str == '\n')
1781 *p = '\0';
1782 else
1783 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001784
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001785 ++p;
1786 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001787 }
1788 *p = '\0';
1789
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001790 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001791
1792 vim_free(tmp);
1793 return result;
1794}
1795
Bram Moolenaardb913952012-06-29 12:54:53 +02001796 void
1797do_py3eval (char_u *str, typval_T *rettv)
1798{
1799 DoPy3Command(NULL, (char *) str, rettv);
1800 switch(rettv->v_type)
1801 {
1802 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1803 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1804 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001805 case VAR_UNKNOWN:
1806 rettv->v_type = VAR_NUMBER;
1807 rettv->vval.v_number = 0;
1808 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001809 }
1810}
1811
1812 void
1813set_ref_in_python3 (int copyID)
1814{
1815 set_ref_in_py(copyID);
1816}
1817
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001818 static void
1819init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001820{
1821 vim_memset(&OutputType, 0, sizeof(OutputType));
1822 OutputType.tp_name = "vim.message";
1823 OutputType.tp_basicsize = sizeof(OutputObject);
1824 OutputType.tp_getattro = OutputGetattro;
1825 OutputType.tp_setattro = OutputSetattro;
1826 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1827 OutputType.tp_doc = "vim message object";
1828 OutputType.tp_methods = OutputMethods;
1829 OutputType.tp_alloc = call_PyType_GenericAlloc;
1830 OutputType.tp_new = call_PyType_GenericNew;
1831 OutputType.tp_free = call_PyObject_Free;
1832
1833 vim_memset(&BufferType, 0, sizeof(BufferType));
1834 BufferType.tp_name = "vim.buffer";
1835 BufferType.tp_basicsize = sizeof(BufferType);
1836 BufferType.tp_dealloc = BufferDestructor;
1837 BufferType.tp_repr = BufferRepr;
1838 BufferType.tp_as_sequence = &BufferAsSeq;
1839 BufferType.tp_as_mapping = &BufferAsMapping;
1840 BufferType.tp_getattro = BufferGetattro;
1841 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1842 BufferType.tp_doc = "vim buffer object";
1843 BufferType.tp_methods = BufferMethods;
1844 BufferType.tp_alloc = call_PyType_GenericAlloc;
1845 BufferType.tp_new = call_PyType_GenericNew;
1846 BufferType.tp_free = call_PyObject_Free;
1847
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001848 vim_memset(&WindowType, 0, sizeof(WindowType));
1849 WindowType.tp_name = "vim.window";
1850 WindowType.tp_basicsize = sizeof(WindowObject);
1851 WindowType.tp_dealloc = WindowDestructor;
1852 WindowType.tp_repr = WindowRepr;
1853 WindowType.tp_getattro = WindowGetattro;
1854 WindowType.tp_setattro = WindowSetattro;
1855 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1856 WindowType.tp_doc = "vim Window object";
1857 WindowType.tp_methods = WindowMethods;
1858 WindowType.tp_alloc = call_PyType_GenericAlloc;
1859 WindowType.tp_new = call_PyType_GenericNew;
1860 WindowType.tp_free = call_PyObject_Free;
1861
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001862 vim_memset(&BufListType, 0, sizeof(BufListType));
1863 BufListType.tp_name = "vim.bufferlist";
1864 BufListType.tp_basicsize = sizeof(BufListObject);
1865 BufListType.tp_as_sequence = &BufListAsSeq;
1866 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1867 BufferType.tp_doc = "vim buffer list";
1868
1869 vim_memset(&WinListType, 0, sizeof(WinListType));
1870 WinListType.tp_name = "vim.windowlist";
1871 WinListType.tp_basicsize = sizeof(WinListType);
1872 WinListType.tp_as_sequence = &WinListAsSeq;
1873 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1874 WinListType.tp_doc = "vim window list";
1875
1876 vim_memset(&RangeType, 0, sizeof(RangeType));
1877 RangeType.tp_name = "vim.range";
1878 RangeType.tp_basicsize = sizeof(RangeObject);
1879 RangeType.tp_dealloc = RangeDestructor;
1880 RangeType.tp_repr = RangeRepr;
1881 RangeType.tp_as_sequence = &RangeAsSeq;
1882 RangeType.tp_as_mapping = &RangeAsMapping;
1883 RangeType.tp_getattro = RangeGetattro;
1884 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1885 RangeType.tp_doc = "vim Range object";
1886 RangeType.tp_methods = RangeMethods;
1887 RangeType.tp_alloc = call_PyType_GenericAlloc;
1888 RangeType.tp_new = call_PyType_GenericNew;
1889 RangeType.tp_free = call_PyObject_Free;
1890
1891 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1892 CurrentType.tp_name = "vim.currentdata";
1893 CurrentType.tp_basicsize = sizeof(CurrentObject);
1894 CurrentType.tp_getattro = CurrentGetattro;
1895 CurrentType.tp_setattro = CurrentSetattro;
1896 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1897 CurrentType.tp_doc = "vim current object";
1898
Bram Moolenaardb913952012-06-29 12:54:53 +02001899 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1900 DictionaryType.tp_name = "vim.dictionary";
1901 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
1902 DictionaryType.tp_dealloc = DictionaryDestructor;
1903 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1904 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1905 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1906 DictionaryType.tp_methods = DictionaryMethods;
1907
1908 vim_memset(&ListType, 0, sizeof(ListType));
1909 ListType.tp_name = "vim.list";
1910 ListType.tp_dealloc = ListDestructor;
1911 ListType.tp_basicsize = sizeof(ListObject);
1912 ListType.tp_as_sequence = &ListAsSeq;
1913 ListType.tp_as_mapping = &ListAsMapping;
1914 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1915 ListType.tp_doc = "list pushing modifications to vim structure";
1916 ListType.tp_methods = ListMethods;
1917
1918 vim_memset(&FunctionType, 0, sizeof(FunctionType));
1919 FunctionType.tp_name = "vim.list";
1920 FunctionType.tp_basicsize = sizeof(FunctionObject);
1921 FunctionType.tp_getattro = FunctionGetattro;
1922 FunctionType.tp_dealloc = FunctionDestructor;
1923 FunctionType.tp_call = FunctionCall;
1924 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1925 FunctionType.tp_doc = "object that calls vim function";
1926 FunctionType.tp_methods = FunctionMethods;
1927
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001928 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1929 vimmodule.m_name = "vim";
1930 vimmodule.m_doc = vim_module_doc;
1931 vimmodule.m_size = -1;
1932 vimmodule.m_methods = VimMethods;
1933}