blob: e9f7400f80bc73ab2c38463dd0b4552004048cda [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
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200164# define PyType_Ready py3_PyType_Ready
165#undef Py_BuildValue
166# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100167# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200168# define Py_Initialize py3_Py_Initialize
169# define Py_Finalize py3_Py_Finalize
170# define Py_IsInitialized py3_Py_IsInitialized
171# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200172# define _Py_FalseStruct (*py3__Py_FalseStruct)
173# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200174# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200175# define PyModule_AddObject py3_PyModule_AddObject
176# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200177# if PY_VERSION_HEX >= 0x030300f0
178# undef _PyUnicode_AsString
179# define _PyUnicode_AsString py3_PyUnicode_AsUTF8String
180# else
181# define _PyUnicode_AsString py3__PyUnicode_AsString
182# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200183# undef PyUnicode_AsEncodedString
184# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
185# undef PyBytes_AsString
186# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200187# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200188# undef PyBytes_FromString
189# define PyBytes_FromString py3_PyBytes_FromString
190# define PyFloat_FromDouble py3_PyFloat_FromDouble
191# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200192# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200193# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200195# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200196# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200197# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200198# ifdef Py_DEBUG
199# define _Py_NegativeRefcount py3__Py_NegativeRefcount
200# define _Py_RefTotal (*py3__Py_RefTotal)
201# define _Py_Dealloc py3__Py_Dealloc
202# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
203# define _PyObject_DebugFree py3__PyObject_DebugFree
204# else
205# define PyObject_Malloc py3_PyObject_Malloc
206# define PyObject_Free py3_PyObject_Free
207# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208# define PyType_GenericAlloc py3_PyType_GenericAlloc
209# define PyType_GenericNew py3_PyType_GenericNew
210# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200211# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200212# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200213# undef PyUnicode_Decode
214# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200215# define PyType_IsSubtype py3_PyType_IsSubtype
216# define PyCapsule_New py3_PyCapsule_New
217# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200219# ifdef Py_DEBUG
220# undef PyObject_NEW
221# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200222( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200223 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200224# endif
225
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226/*
227 * Pointers for dynamic link
228 */
229static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100230static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200231static void (*py3_Py_Initialize)(void);
232static PyObject* (*py3_PyList_New)(Py_ssize_t size);
233static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
234static void (*py3_PyGILState_Release)(PyGILState_STATE);
235static int (*py3_PySys_SetObject)(char *, PyObject *);
236static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
237static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200238static int (*py3_PySequence_Check)(PyObject *);
239static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
240static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
241static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
242static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
243static int (*py3_PyMapping_Check)(PyObject *);
244static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200245static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200246 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200247static PyObject* (*py3_PyErr_NoMemory)(void);
248static void (*py3_Py_Finalize)(void);
249static void (*py3_PyErr_SetString)(PyObject *, const char *);
250static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200251static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200252static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
253static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200254static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200255static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200256static PyObject* (*py3_PyErr_Occurred)(void);
257static PyObject* (*py3_PyModule_GetDict)(PyObject *);
258static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
259static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200260static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261static PyObject* (*py3_PyLong_FromLong)(long);
262static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200263static PyObject* (*py3_PyIter_Next)(PyObject *);
264static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200265static PyObject* (*py3_Py_BuildValue)(char *, ...);
266static int (*py3_PyType_Ready)(PyTypeObject *type);
267static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
268static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200269static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
270 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271static long (*py3_PyLong_AsLong)(PyObject *);
272static void (*py3_PyErr_SetNone)(PyObject *);
273static void (*py3_PyEval_InitThreads)(void);
274static void(*py3_PyEval_RestoreThread)(PyThreadState *);
275static PyThreadState*(*py3_PyEval_SaveThread)(void);
276static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
277static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200278static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200279static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200280static int (*py3_Py_IsInitialized)(void);
281static void (*py3_PyErr_Clear)(void);
282static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200283static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200285static PyObject* py3__Py_FalseStruct;
286static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
288static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200289#if PY_VERSION_HEX >= 0x030300f0
290static char* (*py3_PyUnicode_AsUTF8String)(PyObject *unicode);
291#else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200293#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200294static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
295static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200296static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200297static PyObject* (*py3_PyBytes_FromString)(char *str);
298static PyObject* (*py3_PyFloat_FromDouble)(double num);
299static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200300static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
301static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
302static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
303static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200304static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200305static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200306static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200307static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200308static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200309static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
310static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200311# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200312 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
313 static Py_ssize_t* py3__Py_RefTotal;
314 static void (*py3__Py_Dealloc)(PyObject *obj);
315 static void (*py3__PyObject_DebugFree)(void*);
316 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200317# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200318 static void (*py3_PyObject_Free)(void*);
319 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200320# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200321static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200322
323static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
324
325/* Imported exception objects */
326static PyObject *p3imp_PyExc_AttributeError;
327static PyObject *p3imp_PyExc_IndexError;
328static PyObject *p3imp_PyExc_KeyboardInterrupt;
329static PyObject *p3imp_PyExc_TypeError;
330static PyObject *p3imp_PyExc_ValueError;
331
332# define PyExc_AttributeError p3imp_PyExc_AttributeError
333# define PyExc_IndexError p3imp_PyExc_IndexError
334# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
335# define PyExc_TypeError p3imp_PyExc_TypeError
336# define PyExc_ValueError p3imp_PyExc_ValueError
337
338/*
339 * Table of name to function pointer of python.
340 */
341# define PYTHON_PROC FARPROC
342static struct
343{
344 char *name;
345 PYTHON_PROC *ptr;
346} py3_funcname_table[] =
347{
348 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100349 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200350 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200351#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200352 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200353 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
354#else
355 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
356 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
357#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200358 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200359 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200360 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
361 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
362 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
363 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
364 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
365 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200366 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
367 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
368 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
369 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
370 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200371 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
372 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
373 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
374 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
375 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200376 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200377 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
378 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200379 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200381 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
382 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
383 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
384 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200385 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
386 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
387 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
388 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
389 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
391 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
393 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
394 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
395 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
396 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
397 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
398 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
399 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200400 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200401 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200402 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200403 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
404 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200405 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
406 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
407 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
408 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200409#if PY_VERSION_HEX >= 0x030300f0
410 {"PyUnicode_AsUTF8String", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8String},
411#else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200412 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200413#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200414 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200415 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200416 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
417 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
418 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200419 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
420 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
421 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
422 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200423 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200425 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200426 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200427 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200428# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
430 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
431 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
432 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
433 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200434# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200435 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
436 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200437# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200438 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
439 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
440 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200441 {"", NULL},
442};
443
444/*
445 * Free python.dll
446 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200447 static void
448end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200449{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200450 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200451 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200452 close_dll(hinstPy3);
453 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200454 }
455}
456
457/*
458 * Load library and get all pointers.
459 * Parameter 'libname' provides name of DLL.
460 * Return OK or FAIL.
461 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200462 static int
463py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200464{
465 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200466 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200467
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100468# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200469 /* Can't have Python and Python3 loaded at the same time.
470 * It cause a crash, because RTLD_GLOBAL is needed for
471 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200472 if (python_loaded())
473 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200474 if (verbose)
475 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200476 return FAIL;
477 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200478# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200479
480 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200481 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200482 hinstPy3 = load_dll(libname);
483
484 if (!hinstPy3)
485 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200486 if (verbose)
487 EMSG2(_(e_loadlib), libname);
488 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200489 }
490
491 for (i = 0; py3_funcname_table[i].ptr; ++i)
492 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200493 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
494 py3_funcname_table[i].name)) == NULL)
495 {
496 close_dll(hinstPy3);
497 hinstPy3 = 0;
498 if (verbose)
499 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
500 return FAIL;
501 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200502 }
503
Bram Moolenaar69154f22010-07-18 21:42:34 +0200504 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
505 * will be present in the library. */
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200506#if PY_VERSION_HEX >= 0x030300f0
507 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
508 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
509 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
510 "PyUnicode_AsEncodedString");
511#else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200512 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200513 ucs_decode = symbol_from_dll(hinstPy3,
514 "PyUnicodeUCS2_Decode");
515 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
516 "PyUnicodeUCS2_AsEncodedString");
517 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200518 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200519 ucs_from_string = symbol_from_dll(hinstPy3,
520 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200521 ucs_decode = symbol_from_dll(hinstPy3,
522 "PyUnicodeUCS4_Decode");
523 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
524 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200525 }
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200526#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200527 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200528 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200529 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200530 py3_PyUnicode_Decode = ucs_decode;
531 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532 }
533 else
534 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200535 close_dll(hinstPy3);
536 hinstPy3 = 0;
537 if (verbose)
538 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
539 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200540 }
541
542 return OK;
543}
544
545/*
546 * If python is enabled (there is installed python on Windows system) return
547 * TRUE, else FALSE.
548 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200549 int
550python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200551{
552 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
553}
554
555/* Load the standard Python exceptions - don't import the symbols from the
556 * DLL, as this can cause errors (importing data symbols is not reliable).
557 */
558static void get_py3_exceptions __ARGS((void));
559
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200560 static void
561get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200562{
563 PyObject *exmod = PyImport_ImportModule("builtins");
564 PyObject *exdict = PyModule_GetDict(exmod);
565 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
566 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
567 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
568 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
569 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
570 Py_XINCREF(p3imp_PyExc_AttributeError);
571 Py_XINCREF(p3imp_PyExc_IndexError);
572 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
573 Py_XINCREF(p3imp_PyExc_TypeError);
574 Py_XINCREF(p3imp_PyExc_ValueError);
575 Py_XDECREF(exmod);
576}
577#endif /* DYNAMIC_PYTHON3 */
578
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200579static PyObject *BufferNew (buf_T *);
580static PyObject *WindowNew(win_T *);
581static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100582static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200583
584static PyTypeObject RangeType;
585
Bram Moolenaardb913952012-06-29 12:54:53 +0200586static int py3initialised = 0;
587
588#define PYINITIALISED py3initialised
589
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200590#define DICTKEY_DECL PyObject *bytes = NULL;
591
Bram Moolenaardb913952012-06-29 12:54:53 +0200592#define DICTKEY_GET(err) \
593 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200594 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200595 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200596 return err; \
597 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200598 else if (PyUnicode_Check(keyObject)) \
599 { \
600 bytes = PyString_AsBytes(keyObject); \
601 if (bytes == NULL) \
602 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200603 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200604 return err; \
605 } \
606 else \
607 { \
608 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
609 return err; \
610 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200611
Bram Moolenaardb913952012-06-29 12:54:53 +0200612#define DICTKEY_UNREF \
613 if (bytes != NULL) \
614 Py_XDECREF(bytes);
615
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200616/*
617 * Include the code shared with if_python.c
618 */
619#include "if_py_both.h"
620
Bram Moolenaar77045652012-09-21 13:46:06 +0200621#define GET_ATTR_STRING(name, nameobj) \
622 char *name = ""; \
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200623 if (PyUnicode_Check(nameobj)) \
624 name = _PyUnicode_AsString(nameobj)
Bram Moolenaar77045652012-09-21 13:46:06 +0200625
Bram Moolenaardb913952012-06-29 12:54:53 +0200626#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
627
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200628 static void
629call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200630{
631#ifdef Py_DEBUG
632 _PyObject_DebugFree(p);
633#else
634 PyObject_Free(p);
635#endif
636}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200637
638 static PyObject *
639call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200640{
641 return PyType_GenericNew(type,args,kwds);
642}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200643
644 static PyObject *
645call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200646{
647 return PyType_GenericAlloc(type,nitems);
648}
649
650/******************************************************
651 * Internal function prototypes.
652 */
653
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200654static Py_ssize_t RangeStart;
655static Py_ssize_t RangeEnd;
656
Bram Moolenaardb913952012-06-29 12:54:53 +0200657static PyObject *globals;
658
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200659static int PythonIO_Init(void);
660static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200661PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200662
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200663/******************************************************
664 * 1. Python interpreter main program.
665 */
666
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200667static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
668
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200669 void
670python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200671{
672 static int recurse = 0;
673
674 /* If a crash occurs while doing this, don't try again. */
675 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200676 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200677
678 ++recurse;
679
680#ifdef DYNAMIC_PYTHON3
681 if (hinstPy3)
682#endif
683 if (Py_IsInitialized())
684 {
685 // acquire lock before finalizing
686 pygilstate = PyGILState_Ensure();
687
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200688 PythonIO_Fini();
689 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200690 }
691
692#ifdef DYNAMIC_PYTHON3
693 end_dynamic_python3();
694#endif
695
696 --recurse;
697}
698
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200699#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
700 int
701python3_loaded()
702{
703 return (hinstPy3 != 0);
704}
705#endif
706
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200707 static int
708Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200709{
710 if (!py3initialised)
711 {
712#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200713 if (!python3_enabled(TRUE))
714 {
715 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
716 goto fail;
717 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200718#endif
719
720 init_structs();
721
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100722
723#ifdef PYTHON3_HOME
724 Py_SetPythonHome(PYTHON3_HOME);
725#endif
726
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200727 PyImport_AppendInittab("vim", Py3Init_vim);
728
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200729#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200730 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200731#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200733#endif
Bram Moolenaar003d14a2012-10-21 01:47:00 +0200734 /* Initialise threads, and save the state using PyGILState_Ensure.
735 * Without the call to PyGILState_Ensure, thread specific state (such
736 * as the system trace hook), will be lost between invocations of
737 * Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200738 PyEval_InitThreads();
Bram Moolenaar003d14a2012-10-21 01:47:00 +0200739 pygilstate = PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200740
741#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200742 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200743#endif
744
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200745 if (PythonIO_Init())
746 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200747
Bram Moolenaardb913952012-06-29 12:54:53 +0200748 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
749
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200750 /* Remove the element from sys.path that was added because of our
751 * argv[0] value in Py3Init_vim(). Previously we used an empty
752 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200753 * the current directory in sys.path.
754 * Only after vim has been imported, the element does exist in
755 * sys.path.
756 */
757 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 +0200758
759 // lock is created and acquired in PyEval_InitThreads() and thread
760 // state is created in Py_Initialize()
761 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
762 // (python must have threads enabled!)
763 // so the following does both: unlock GIL and save thread state in TLS
764 // without deleting thread state
765 PyGILState_Release(pygilstate);
766
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200767 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200768 }
769
770 return 0;
771
772fail:
773 /* We call PythonIO_Flush() here to print any Python errors.
774 * This is OK, as it is possible to call this function even
775 * if PythonIO_Init() has not completed successfully (it will
776 * not do anything in this case).
777 */
778 PythonIO_Flush();
779 return -1;
780}
781
782/*
783 * External interface
784 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200785 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200786DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200787{
788#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200789 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790#endif
791#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200792 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200793#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200794 PyObject *cmdstr;
795 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200796
797#if defined(MACOS) && !defined(MACOS_X_UNIX)
798 GetPort(&oldPort);
799 /* Check if the Python library is available */
800 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200801 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200802#endif
803 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200804 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200805
Bram Moolenaardb913952012-06-29 12:54:53 +0200806 if (rettv == NULL)
807 {
808 RangeStart = eap->line1;
809 RangeEnd = eap->line2;
810 }
811 else
812 {
813 RangeStart = (PyInt) curwin->w_cursor.lnum;
814 RangeEnd = RangeStart;
815 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200816 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200817
818#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
819 /* Python only works properly when the LC_NUMERIC locale is "C". */
820 saved_locale = setlocale(LC_NUMERIC, NULL);
821 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200822 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200823 else
824 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200825 /* Need to make a copy, value may change when setting new locale. */
826 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
827 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200828 }
829#endif
830
831 pygilstate = PyGILState_Ensure();
832
Bram Moolenaar19e60942011-06-19 00:27:51 +0200833 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
834 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200835 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
836 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
837 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200838 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200839 if (rettv == NULL)
840 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
841 else
842 {
843 PyObject *r;
844
845 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
846 globals, globals);
847 if (r == NULL)
848 EMSG(_("E860: Eval did not return a valid python 3 object"));
849 else
850 {
851 if (ConvertFromPyObject(r, rettv) == -1)
852 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
853 Py_DECREF(r);
854 }
855 PyErr_Clear();
856 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200857 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200858
859 PyGILState_Release(pygilstate);
860
861#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
862 if (saved_locale != NULL)
863 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200864 (void)setlocale(LC_NUMERIC, saved_locale);
865 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866 }
867#endif
868
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200869 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200870 PythonIO_Flush();
871#if defined(MACOS) && !defined(MACOS_X_UNIX)
872 SetPort(oldPort);
873#endif
874
875theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200876 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200877}
878
879/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200880 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200881 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200882 void
883ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200884{
885 char_u *script;
886
887 script = script_get(eap, eap->arg);
888 if (!eap->skip)
889 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200890 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200891 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200892 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200893 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200894 }
895 vim_free(script);
896}
897
898#define BUFFER_SIZE 2048
899
900/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200901 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200902 */
903 void
904ex_py3file(exarg_T *eap)
905{
906 static char buffer[BUFFER_SIZE];
907 const char *file;
908 char *p;
909 int i;
910
911 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
912 * stdio file pointer, but Vim and the Python DLL are compiled with
913 * different options under Windows, meaning that stdio pointers aren't
914 * compatible between the two. Yuk.
915 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200916 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
917 *
918 * Using bytes so that Python can detect the source encoding as it normally
919 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200920 *
921 * We need to escape any backslashes or single quotes in the file name, so that
922 * Python won't mangle the file name.
923 */
924
925 strcpy(buffer, "exec(compile(open('");
926 p = buffer + 19; /* size of "exec(compile(open('" */
927
928 for (i=0; i<2; ++i)
929 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200930 file = (char *)eap->arg;
931 while (*file && p < buffer + (BUFFER_SIZE - 3))
932 {
933 if (*file == '\\' || *file == '\'')
934 *p++ = '\\';
935 *p++ = *file++;
936 }
937 /* If we didn't finish the file name, we hit a buffer overflow */
938 if (*file != '\0')
939 return;
940 if (i==0)
941 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200942 strcpy(p,"','rb').read(),'");
943 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200944 }
945 else
946 {
947 strcpy(p,"','exec'))");
948 p += 10;
949 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200950 }
951
952
953 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200954 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200955}
956
957/******************************************************
958 * 2. Python output stream: writes output via [e]msg().
959 */
960
961/* Implementation functions
962 */
963
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200964 static PyObject *
965OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200966{
Bram Moolenaar77045652012-09-21 13:46:06 +0200967 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200968
969 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200970 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200971
972 return PyObject_GenericGetAttr(self, nameobj);
973}
974
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200975 static int
976OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200977{
Bram Moolenaar77045652012-09-21 13:46:06 +0200978 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979
Bram Moolenaar77045652012-09-21 13:46:06 +0200980 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981}
982
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983/***************/
984
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200985 static int
986PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987{
988 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200989 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200990}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200991
992 static void
993PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994{
995 PySys_SetObject("stdout", NULL);
996 PySys_SetObject("stderr", NULL);
997}
998
999/******************************************************
1000 * 3. Implementation of the Vim module for Python
1001 */
1002
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003/* Window type - Implementation functions
1004 * --------------------------------------
1005 */
1006
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001007#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1008
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001009/* Buffer type - Implementation functions
1010 * --------------------------------------
1011 */
1012
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001013#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1014
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015static Py_ssize_t BufferLength(PyObject *);
1016static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001017static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1018static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020
1021/* Line range type - Implementation functions
1022 * --------------------------------------
1023 */
1024
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001025#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1026
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001027static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001029static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001030
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001031/* Current objects type - Implementation functions
1032 * -----------------------------------------------
1033 */
1034
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001035static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001036 (lenfunc) BufferLength, /* sq_length, len(x) */
1037 (binaryfunc) 0, /* sq_concat, x+y */
1038 (ssizeargfunc) 0, /* sq_repeat, x*n */
1039 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1040 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001041 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001042 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043 0, /* sq_contains */
1044 0, /* sq_inplace_concat */
1045 0, /* sq_inplace_repeat */
1046};
1047
1048PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001049 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001050 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001051 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001052};
1053
1054
1055/* Buffer object - Definitions
1056 */
1057
1058static PyTypeObject BufferType;
1059
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001060 static PyObject *
1061BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001062{
1063 /* We need to handle deletion of buffers underneath us.
1064 * If we add a "b_python3_ref" field to the buf_T structure,
1065 * then we can get at it in buf_freeall() in vim. We then
1066 * need to create only ONE Python object per buffer - if
1067 * we try to create a second, just INCREF the existing one
1068 * and return it. The (single) Python object referring to
1069 * the buffer is stored in "b_python3_ref".
1070 * Question: what to do on a buf_freeall(). We'll probably
1071 * have to either delete the Python object (DECREF it to
1072 * zero - a bad idea, as it leaves dangling refs!) or
1073 * set the buf_T * value to an invalid value (-1?), which
1074 * means we need checks in all access functions... Bah.
1075 */
1076
1077 BufferObject *self;
1078
1079 if (buf->b_python3_ref != NULL)
1080 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001081 self = buf->b_python3_ref;
1082 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083 }
1084 else
1085 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001086 self = PyObject_NEW(BufferObject, &BufferType);
1087 buf->b_python3_ref = self;
1088 if (self == NULL)
1089 return NULL;
1090 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001091 }
1092
1093 return (PyObject *)(self);
1094}
1095
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001096 static void
1097BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001098{
1099 BufferObject *this = (BufferObject *)(self);
1100
1101 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001102 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001103
1104 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001105}
1106
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001107 static PyObject *
1108BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109{
1110 BufferObject *this = (BufferObject *)(self);
1111
Bram Moolenaar77045652012-09-21 13:46:06 +02001112 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001113
1114 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001115 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001116
1117 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001118 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001119 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001120 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001121 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123}
1124
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001125 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001126BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1127{
1128 return Py_BuildValue("[sssss]", "name", "number",
1129 "append", "mark", "range");
1130}
1131
1132 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001133BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001134{
1135 static char repr[100];
1136 BufferObject *this = (BufferObject *)(self);
1137
1138 if (this->buf == INVALID_BUFFER_VALUE)
1139 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001140 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1141 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001142 }
1143 else
1144 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001145 char *name = (char *)this->buf->b_fname;
1146 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001147
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001148 if (name == NULL)
1149 name = "";
1150 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001152 if (len > 35)
1153 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001154
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001155 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001156
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001157 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001158 }
1159}
1160
1161/******************/
1162
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001163 static Py_ssize_t
1164BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001165{
1166 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001167 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001168
1169 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1170}
1171
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001172 static PyObject *
1173BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001174{
1175 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177}
1178
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001179 static PyObject *
1180BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1181{
1182 return RBSlice((BufferObject *)(self), lo, hi, 1,
1183 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1184}
1185
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001186 static PyObject *
1187BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188{
Bram Moolenaardb913952012-06-29 12:54:53 +02001189 if (PyLong_Check(idx))
1190 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001191 long _idx = PyLong_AsLong(idx);
1192 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001193 } else if (PySlice_Check(idx))
1194 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001195 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001197 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001198 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1199 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001200 &step, &slicelen) < 0)
1201 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 return NULL;
1203 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001204 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001205 }
1206 else
1207 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1209 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001210 }
1211}
1212
Bram Moolenaar19e60942011-06-19 00:27:51 +02001213 static Py_ssize_t
1214BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1215{
Bram Moolenaardb913952012-06-29 12:54:53 +02001216 if (PyLong_Check(idx))
1217 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001218 long n = PyLong_AsLong(idx);
1219 return RBAsItem((BufferObject *)(self), n, val, 1,
1220 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1221 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001222 } else if (PySlice_Check(idx))
1223 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001224 Py_ssize_t start, stop, step, slicelen;
1225
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001226 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001227 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1228 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001229 &step, &slicelen) < 0)
1230 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001231 return -1;
1232 }
1233 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1234 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1235 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 }
1237 else
1238 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001239 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1240 return -1;
1241 }
1242}
1243
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001244static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 (lenfunc) RangeLength, /* sq_length, len(x) */
1246 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1247 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1248 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1249 0, /* was_sq_slice, x[i:j] */
1250 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1251 0, /* sq_ass_slice, x[i:j]=v */
1252 0, /* sq_contains */
1253 0, /* sq_inplace_concat */
1254 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001255};
1256
1257PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001258 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001259 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001260 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001261};
1262
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001263/* Line range object - Implementation
1264 */
1265
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001266 static void
1267RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001268{
1269 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001270 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001271}
1272
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001273 static PyObject *
1274RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001275{
Bram Moolenaar77045652012-09-21 13:46:06 +02001276 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001277
1278 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001279 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001280 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001282 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001283 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001284}
1285
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001286/****************/
1287
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001288 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001289RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001290{
1291 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001292 ((RangeObject *)(self))->start,
1293 ((RangeObject *)(self))->end,
1294 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001295}
1296
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001297 static Py_ssize_t
1298RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1299{
1300 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1301 ((RangeObject *)(self))->start,
1302 ((RangeObject *)(self))->end,
1303 &((RangeObject *)(self))->end);
1304}
1305
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001306 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001307RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308{
Bram Moolenaardb913952012-06-29 12:54:53 +02001309 if (PyLong_Check(idx))
1310 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001311 long _idx = PyLong_AsLong(idx);
1312 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001313 } else if (PySlice_Check(idx))
1314 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001315 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001316
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001317 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001318 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1319 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001320 &step, &slicelen) < 0)
1321 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001322 return NULL;
1323 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001324 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001325 }
1326 else
1327 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001328 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1329 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001330 }
1331}
1332
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001333 static Py_ssize_t
1334RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1335{
Bram Moolenaardb913952012-06-29 12:54:53 +02001336 if (PyLong_Check(idx))
1337 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001338 long n = PyLong_AsLong(idx);
1339 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001340 } else if (PySlice_Check(idx))
1341 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001342 Py_ssize_t start, stop, step, slicelen;
1343
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001344 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001345 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1346 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001347 &step, &slicelen) < 0)
1348 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001349 return -1;
1350 }
1351 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001352 }
1353 else
1354 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001355 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1356 return -1;
1357 }
1358}
1359
1360
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001361/* Buffer list object - Definitions
1362 */
1363
1364typedef struct
1365{
1366 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001367} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368
1369static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001370 (lenfunc) BufListLength, /* sq_length, len(x) */
1371 (binaryfunc) 0, /* sq_concat, x+y */
1372 (ssizeargfunc) 0, /* sq_repeat, x*n */
1373 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1374 0, /* was_sq_slice, x[i:j] */
1375 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1376 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001377 0, /* sq_contains */
1378 0, /* sq_inplace_concat */
1379 0, /* sq_inplace_repeat */
1380};
1381
1382static PyTypeObject BufListType;
1383
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001384/* Window object - Definitions
1385 */
1386
1387static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001388 /* name, function, calling, documentation */
1389 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001390};
1391
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001392static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001393
1394/* Window object - Implementation
1395 */
1396
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001397 static PyObject *
1398WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001399{
1400 /* We need to handle deletion of windows underneath us.
1401 * If we add a "w_python3_ref" field to the win_T structure,
1402 * then we can get at it in win_free() in vim. We then
1403 * need to create only ONE Python object per window - if
1404 * we try to create a second, just INCREF the existing one
1405 * and return it. The (single) Python object referring to
1406 * the window is stored in "w_python3_ref".
1407 * On a win_free() we set the Python object's win_T* field
1408 * to an invalid value. We trap all uses of a window
1409 * object, and reject them if the win_T* field is invalid.
1410 */
1411
1412 WindowObject *self;
1413
1414 if (win->w_python3_ref)
1415 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001416 self = win->w_python3_ref;
1417 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418 }
1419 else
1420 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001421 self = PyObject_NEW(WindowObject, &WindowType);
1422 if (self == NULL)
1423 return NULL;
1424 self->win = win;
1425 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001426 }
1427
1428 return (PyObject *)(self);
1429}
1430
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001431 static void
1432WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001433{
1434 WindowObject *this = (WindowObject *)(self);
1435
1436 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001438
1439 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001440}
1441
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001442 static PyObject *
1443WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001444{
1445 WindowObject *this = (WindowObject *)(self);
1446
Bram Moolenaar77045652012-09-21 13:46:06 +02001447 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001448
1449 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001450 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001451
1452 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001453 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001454 else if (strcmp(name, "cursor") == 0)
1455 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001456 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001457
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001458 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001459 }
1460 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001462#ifdef FEAT_VERTSPLIT
1463 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001464 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001465#endif
1466 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001467 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001468 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001469 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470}
1471
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001472 static int
1473WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001474{
Bram Moolenaar77045652012-09-21 13:46:06 +02001475 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001476
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001477 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001478}
1479
1480/* Window list object - Definitions
1481 */
1482
1483typedef struct
1484{
1485 PyObject_HEAD
1486}
1487WinListObject;
1488
1489static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001490 (lenfunc) WinListLength, /* sq_length, len(x) */
1491 (binaryfunc) 0, /* sq_concat, x+y */
1492 (ssizeargfunc) 0, /* sq_repeat, x*n */
1493 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1494 0, /* sq_slice, x[i:j] */
1495 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1496 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001497 0, /* sq_contains */
1498 0, /* sq_inplace_concat */
1499 0, /* sq_inplace_repeat */
1500};
1501
1502static PyTypeObject WinListType;
1503
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001504/* Current items object - Definitions
1505 */
1506
1507typedef struct
1508{
1509 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001510} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511
1512static PyTypeObject CurrentType;
1513
1514/* Current items object - Implementation
1515 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001516 static PyObject *
1517CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518{
Bram Moolenaar77045652012-09-21 13:46:06 +02001519 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520
1521 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001522 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001523 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001524 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001526 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001529 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001530 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001531 else
1532 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001533 PyErr_SetString(PyExc_AttributeError, name);
1534 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535 }
1536}
1537
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001538 static int
1539CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001540{
1541 char *name = "";
1542 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001543 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001544
1545 if (strcmp(name, "line") == 0)
1546 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001547 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1548 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001549
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001550 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001551 }
1552 else
1553 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001554 PyErr_SetString(PyExc_AttributeError, name);
1555 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001556 }
1557}
1558
Bram Moolenaardb913952012-06-29 12:54:53 +02001559/* Dictionary object - Definitions
1560 */
1561
1562static PyInt DictionaryLength(PyObject *);
1563
1564static PyMappingMethods DictionaryAsMapping = {
1565 /* mp_length */ (lenfunc) DictionaryLength,
1566 /* mp_subscript */ (binaryfunc) DictionaryItem,
1567 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1568};
1569
Bram Moolenaar66b79852012-09-21 14:00:35 +02001570 static PyObject *
1571DictionaryGetattro(PyObject *self, PyObject *nameobj)
1572{
1573 DictionaryObject *this = ((DictionaryObject *) (self));
1574
1575 GET_ATTR_STRING(name, nameobj);
1576
1577 if (strcmp(name, "locked") == 0)
1578 return PyLong_FromLong(this->dict->dv_lock);
1579 else if (strcmp(name, "scope") == 0)
1580 return PyLong_FromLong(this->dict->dv_scope);
1581
1582 return PyObject_GenericGetAttr(self, nameobj);
1583}
1584
1585 static int
1586DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1587{
1588 GET_ATTR_STRING(name, nameobj);
1589 return DictionarySetattr((DictionaryObject *) self, name, val);
1590}
1591
Bram Moolenaardb913952012-06-29 12:54:53 +02001592static PyTypeObject DictionaryType;
1593
1594 static void
1595DictionaryDestructor(PyObject *self)
1596{
1597 DictionaryObject *this = (DictionaryObject *)(self);
1598
1599 pyll_remove(&this->ref, &lastdict);
1600 dict_unref(this->dict);
1601
1602 Py_TYPE(self)->tp_free((PyObject*)self);
1603}
1604
1605/* List object - Definitions
1606 */
1607
1608static PyInt ListLength(PyObject *);
1609static PyObject *ListItem(PyObject *, Py_ssize_t);
1610
1611static PySequenceMethods ListAsSeq = {
1612 (lenfunc) ListLength, /* sq_length, len(x) */
1613 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1614 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1615 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1616 (void *) 0, /* was_sq_slice, x[i:j] */
1617 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1618 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1619 0, /* sq_contains */
1620 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1621 0, /* sq_inplace_repeat */
1622};
1623
1624static PyObject *ListSubscript(PyObject *, PyObject *);
1625static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1626
1627static PyMappingMethods ListAsMapping = {
1628 /* mp_length */ (lenfunc) ListLength,
1629 /* mp_subscript */ (binaryfunc) ListSubscript,
1630 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1631};
1632
1633static PyTypeObject ListType;
1634
1635 static PyObject *
1636ListSubscript(PyObject *self, PyObject* idxObject)
1637{
1638 if (PyLong_Check(idxObject))
1639 {
1640 long idx = PyLong_AsLong(idxObject);
1641 return ListItem(self, idx);
1642 }
1643 else if (PySlice_Check(idxObject))
1644 {
1645 Py_ssize_t start, stop, step, slicelen;
1646
1647 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1648 &step, &slicelen) < 0)
1649 return NULL;
1650 return ListSlice(self, start, stop);
1651 }
1652 else
1653 {
1654 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1655 return NULL;
1656 }
1657}
1658
1659 static Py_ssize_t
1660ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1661{
1662 if (PyLong_Check(idxObject))
1663 {
1664 long idx = PyLong_AsLong(idxObject);
1665 return ListAssItem(self, idx, obj);
1666 }
1667 else if (PySlice_Check(idxObject))
1668 {
1669 Py_ssize_t start, stop, step, slicelen;
1670
1671 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1672 &step, &slicelen) < 0)
1673 return -1;
1674 return ListAssSlice(self, start, stop, obj);
1675 }
1676 else
1677 {
1678 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1679 return -1;
1680 }
1681}
1682
Bram Moolenaar66b79852012-09-21 14:00:35 +02001683 static PyObject *
1684ListGetattro(PyObject *self, PyObject *nameobj)
1685{
1686 GET_ATTR_STRING(name, nameobj);
1687
1688 if (strcmp(name, "locked") == 0)
1689 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1690
1691 return PyObject_GenericGetAttr(self, nameobj);
1692}
1693
1694 static int
1695ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1696{
1697 GET_ATTR_STRING(name, nameobj);
1698 return ListSetattr((ListObject *) self, name, val);
1699}
1700
Bram Moolenaardb913952012-06-29 12:54:53 +02001701 static void
1702ListDestructor(PyObject *self)
1703{
1704 ListObject *this = (ListObject *)(self);
1705
1706 pyll_remove(&this->ref, &lastlist);
1707 list_unref(this->list);
1708
1709 Py_TYPE(self)->tp_free((PyObject*)self);
1710}
1711
1712/* Function object - Definitions
1713 */
1714
1715 static void
1716FunctionDestructor(PyObject *self)
1717{
1718 FunctionObject *this = (FunctionObject *) (self);
1719
1720 func_unref(this->name);
1721 PyMem_Del(this->name);
1722
1723 Py_TYPE(self)->tp_free((PyObject*)self);
1724}
1725
1726 static PyObject *
1727FunctionGetattro(PyObject *self, PyObject *nameobj)
1728{
1729 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001730
1731 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001732
1733 if (strcmp(name, "name") == 0)
1734 return PyUnicode_FromString((char *)(this->name));
1735
1736 return PyObject_GenericGetAttr(self, nameobj);
1737}
1738
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001739/* External interface
1740 */
1741
1742 void
1743python3_buffer_free(buf_T *buf)
1744{
1745 if (buf->b_python3_ref != NULL)
1746 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001747 BufferObject *bp = buf->b_python3_ref;
1748 bp->buf = INVALID_BUFFER_VALUE;
1749 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750 }
1751}
1752
1753#if defined(FEAT_WINDOWS) || defined(PROTO)
1754 void
1755python3_window_free(win_T *win)
1756{
1757 if (win->w_python3_ref != NULL)
1758 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001759 WindowObject *wp = win->w_python3_ref;
1760 wp->win = INVALID_WINDOW_VALUE;
1761 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001762 }
1763}
1764#endif
1765
1766static BufListObject TheBufferList =
1767{
1768 PyObject_HEAD_INIT(&BufListType)
1769};
1770
1771static WinListObject TheWindowList =
1772{
1773 PyObject_HEAD_INIT(&WinListType)
1774};
1775
1776static CurrentObject TheCurrent =
1777{
1778 PyObject_HEAD_INIT(&CurrentType)
1779};
1780
1781PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1782
1783static struct PyModuleDef vimmodule;
1784
Bram Moolenaar69154f22010-07-18 21:42:34 +02001785#ifndef PROTO
1786PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001787{
1788 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001789 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001790 /* The special value is removed from sys.path in Python3_Init(). */
1791 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1792
1793 PyType_Ready(&BufferType);
1794 PyType_Ready(&RangeType);
1795 PyType_Ready(&WindowType);
1796 PyType_Ready(&BufListType);
1797 PyType_Ready(&WinListType);
1798 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001799 PyType_Ready(&DictionaryType);
1800 PyType_Ready(&ListType);
1801 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001802
1803 /* Set sys.argv[] to avoid a crash in warn(). */
1804 PySys_SetArgv(1, argv);
1805
1806 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001807 if (mod == NULL)
1808 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001809
Bram Moolenaar19e60942011-06-19 00:27:51 +02001810 VimError = PyErr_NewException("vim.error", NULL, NULL);
1811 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001812
1813 PyModule_AddObject(mod, "error", VimError);
1814 Py_INCREF((PyObject *)(void *)&TheBufferList);
1815 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1816 Py_INCREF((PyObject *)(void *)&TheCurrent);
1817 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1818 Py_INCREF((PyObject *)(void *)&TheWindowList);
1819 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1820
Bram Moolenaar66b79852012-09-21 14:00:35 +02001821#define ADD_INT_CONSTANT(name, value) \
1822 tmp = PyLong_FromLong(value); \
1823 Py_INCREF(tmp); \
1824 PyModule_AddObject(mod, name, tmp)
1825
1826 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1827 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1828 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1829 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1830
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001831 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001832 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001833
1834 return mod;
1835}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001836#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001837
1838/*************************************************************************
1839 * 4. Utility functions for handling the interface between Vim and Python.
1840 */
1841
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001842/* Convert a Vim line into a Python string.
1843 * All internal newlines are replaced by null characters.
1844 *
1845 * On errors, the Python exception data is set, and NULL is returned.
1846 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001847 static PyObject *
1848LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001849{
1850 PyObject *result;
1851 Py_ssize_t len = strlen(str);
1852 char *tmp,*p;
1853
1854 tmp = (char *)alloc((unsigned)(len+1));
1855 p = tmp;
1856 if (p == NULL)
1857 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001858 PyErr_NoMemory();
1859 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001860 }
1861
1862 while (*str)
1863 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001864 if (*str == '\n')
1865 *p = '\0';
1866 else
1867 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001868
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001869 ++p;
1870 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001871 }
1872 *p = '\0';
1873
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001874 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001875
1876 vim_free(tmp);
1877 return result;
1878}
1879
Bram Moolenaardb913952012-06-29 12:54:53 +02001880 void
1881do_py3eval (char_u *str, typval_T *rettv)
1882{
1883 DoPy3Command(NULL, (char *) str, rettv);
1884 switch(rettv->v_type)
1885 {
1886 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1887 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1888 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001889 case VAR_UNKNOWN:
1890 rettv->v_type = VAR_NUMBER;
1891 rettv->vval.v_number = 0;
1892 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001893 }
1894}
1895
1896 void
1897set_ref_in_python3 (int copyID)
1898{
1899 set_ref_in_py(copyID);
1900}
1901
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001902 static void
1903init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001904{
1905 vim_memset(&OutputType, 0, sizeof(OutputType));
1906 OutputType.tp_name = "vim.message";
1907 OutputType.tp_basicsize = sizeof(OutputObject);
1908 OutputType.tp_getattro = OutputGetattro;
1909 OutputType.tp_setattro = OutputSetattro;
1910 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1911 OutputType.tp_doc = "vim message object";
1912 OutputType.tp_methods = OutputMethods;
1913 OutputType.tp_alloc = call_PyType_GenericAlloc;
1914 OutputType.tp_new = call_PyType_GenericNew;
1915 OutputType.tp_free = call_PyObject_Free;
1916
1917 vim_memset(&BufferType, 0, sizeof(BufferType));
1918 BufferType.tp_name = "vim.buffer";
1919 BufferType.tp_basicsize = sizeof(BufferType);
1920 BufferType.tp_dealloc = BufferDestructor;
1921 BufferType.tp_repr = BufferRepr;
1922 BufferType.tp_as_sequence = &BufferAsSeq;
1923 BufferType.tp_as_mapping = &BufferAsMapping;
1924 BufferType.tp_getattro = BufferGetattro;
1925 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1926 BufferType.tp_doc = "vim buffer object";
1927 BufferType.tp_methods = BufferMethods;
1928 BufferType.tp_alloc = call_PyType_GenericAlloc;
1929 BufferType.tp_new = call_PyType_GenericNew;
1930 BufferType.tp_free = call_PyObject_Free;
1931
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001932 vim_memset(&WindowType, 0, sizeof(WindowType));
1933 WindowType.tp_name = "vim.window";
1934 WindowType.tp_basicsize = sizeof(WindowObject);
1935 WindowType.tp_dealloc = WindowDestructor;
1936 WindowType.tp_repr = WindowRepr;
1937 WindowType.tp_getattro = WindowGetattro;
1938 WindowType.tp_setattro = WindowSetattro;
1939 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1940 WindowType.tp_doc = "vim Window object";
1941 WindowType.tp_methods = WindowMethods;
1942 WindowType.tp_alloc = call_PyType_GenericAlloc;
1943 WindowType.tp_new = call_PyType_GenericNew;
1944 WindowType.tp_free = call_PyObject_Free;
1945
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001946 vim_memset(&BufListType, 0, sizeof(BufListType));
1947 BufListType.tp_name = "vim.bufferlist";
1948 BufListType.tp_basicsize = sizeof(BufListObject);
1949 BufListType.tp_as_sequence = &BufListAsSeq;
1950 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1951 BufferType.tp_doc = "vim buffer list";
1952
1953 vim_memset(&WinListType, 0, sizeof(WinListType));
1954 WinListType.tp_name = "vim.windowlist";
1955 WinListType.tp_basicsize = sizeof(WinListType);
1956 WinListType.tp_as_sequence = &WinListAsSeq;
1957 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1958 WinListType.tp_doc = "vim window list";
1959
1960 vim_memset(&RangeType, 0, sizeof(RangeType));
1961 RangeType.tp_name = "vim.range";
1962 RangeType.tp_basicsize = sizeof(RangeObject);
1963 RangeType.tp_dealloc = RangeDestructor;
1964 RangeType.tp_repr = RangeRepr;
1965 RangeType.tp_as_sequence = &RangeAsSeq;
1966 RangeType.tp_as_mapping = &RangeAsMapping;
1967 RangeType.tp_getattro = RangeGetattro;
1968 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1969 RangeType.tp_doc = "vim Range object";
1970 RangeType.tp_methods = RangeMethods;
1971 RangeType.tp_alloc = call_PyType_GenericAlloc;
1972 RangeType.tp_new = call_PyType_GenericNew;
1973 RangeType.tp_free = call_PyObject_Free;
1974
1975 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1976 CurrentType.tp_name = "vim.currentdata";
1977 CurrentType.tp_basicsize = sizeof(CurrentObject);
1978 CurrentType.tp_getattro = CurrentGetattro;
1979 CurrentType.tp_setattro = CurrentSetattro;
1980 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1981 CurrentType.tp_doc = "vim current object";
1982
Bram Moolenaardb913952012-06-29 12:54:53 +02001983 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1984 DictionaryType.tp_name = "vim.dictionary";
1985 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001986 DictionaryType.tp_getattro = DictionaryGetattro;
1987 DictionaryType.tp_setattro = DictionarySetattro;
Bram Moolenaardb913952012-06-29 12:54:53 +02001988 DictionaryType.tp_dealloc = DictionaryDestructor;
1989 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1990 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1991 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1992 DictionaryType.tp_methods = DictionaryMethods;
1993
1994 vim_memset(&ListType, 0, sizeof(ListType));
1995 ListType.tp_name = "vim.list";
1996 ListType.tp_dealloc = ListDestructor;
1997 ListType.tp_basicsize = sizeof(ListObject);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001998 ListType.tp_getattro = ListGetattro;
1999 ListType.tp_setattro = ListSetattro;
Bram Moolenaardb913952012-06-29 12:54:53 +02002000 ListType.tp_as_sequence = &ListAsSeq;
2001 ListType.tp_as_mapping = &ListAsMapping;
2002 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
2003 ListType.tp_doc = "list pushing modifications to vim structure";
2004 ListType.tp_methods = ListMethods;
2005
2006 vim_memset(&FunctionType, 0, sizeof(FunctionType));
2007 FunctionType.tp_name = "vim.list";
2008 FunctionType.tp_basicsize = sizeof(FunctionObject);
2009 FunctionType.tp_getattro = FunctionGetattro;
2010 FunctionType.tp_dealloc = FunctionDestructor;
2011 FunctionType.tp_call = FunctionCall;
2012 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
2013 FunctionType.tp_doc = "object that calls vim function";
2014 FunctionType.tp_methods = FunctionMethods;
2015
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002016 vim_memset(&vimmodule, 0, sizeof(vimmodule));
2017 vimmodule.m_name = "vim";
2018 vimmodule.m_doc = vim_module_doc;
2019 vimmodule.m_size = -1;
2020 vimmodule.m_methods = VimMethods;
2021}