blob: 2a66cfada652ff192ba842eed527874e976a130a [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
Bram Moolenaar4d369872013-02-20 16:09:43 +0100125# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200126# define PyErr_NoMemory py3_PyErr_NoMemory
127# define PyErr_Occurred py3_PyErr_Occurred
128# define PyErr_SetNone py3_PyErr_SetNone
129# define PyErr_SetString py3_PyErr_SetString
130# define PyEval_InitThreads py3_PyEval_InitThreads
131# define PyEval_RestoreThread py3_PyEval_RestoreThread
132# define PyEval_SaveThread py3_PyEval_SaveThread
133# define PyGILState_Ensure py3_PyGILState_Ensure
134# define PyGILState_Release py3_PyGILState_Release
135# define PyLong_AsLong py3_PyLong_AsLong
136# define PyLong_FromLong py3_PyLong_FromLong
137# define PyList_GetItem py3_PyList_GetItem
138# define PyList_Append py3_PyList_Append
139# define PyList_New py3_PyList_New
140# define PyList_SetItem py3_PyList_SetItem
141# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200142# define PySequence_Check py3_PySequence_Check
143# define PySequence_Size py3_PySequence_Size
144# define PySequence_GetItem py3_PySequence_GetItem
145# define PyTuple_Size py3_PyTuple_Size
146# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200147# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
148# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200149# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PyObject_Init py3__PyObject_Init
151# define PyDict_New py3_PyDict_New
152# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200153# define PyDict_Next py3_PyDict_Next
154# define PyMapping_Check py3_PyMapping_Check
155# define PyMapping_Items py3_PyMapping_Items
156# define PyIter_Next py3_PyIter_Next
157# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200158# define PyModule_GetDict py3_PyModule_GetDict
159#undef PyRun_SimpleString
160# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200161#undef PyRun_String
162# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200163# define PySys_SetObject py3_PySys_SetObject
164# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165# define PyType_Ready py3_PyType_Ready
166#undef Py_BuildValue
167# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100168# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200169# define Py_Initialize py3_Py_Initialize
170# define Py_Finalize py3_Py_Finalize
171# define Py_IsInitialized py3_Py_IsInitialized
172# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200173# define _Py_FalseStruct (*py3__Py_FalseStruct)
174# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200175# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200176# define PyModule_AddObject py3_PyModule_AddObject
177# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200178# if PY_VERSION_HEX >= 0x030300f0
179# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200180# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200181# else
182# define _PyUnicode_AsString py3__PyUnicode_AsString
183# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200184# undef PyUnicode_AsEncodedString
185# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
186# undef PyBytes_AsString
187# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200188# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200189# undef PyBytes_FromString
190# define PyBytes_FromString py3_PyBytes_FromString
191# define PyFloat_FromDouble py3_PyFloat_FromDouble
192# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200194# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200196# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200197# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200198# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200199# ifdef Py_DEBUG
200# define _Py_NegativeRefcount py3__Py_NegativeRefcount
201# define _Py_RefTotal (*py3__Py_RefTotal)
202# define _Py_Dealloc py3__Py_Dealloc
203# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
204# define _PyObject_DebugFree py3__PyObject_DebugFree
205# else
206# define PyObject_Malloc py3_PyObject_Malloc
207# define PyObject_Free py3_PyObject_Free
208# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200209# define PyType_GenericAlloc py3_PyType_GenericAlloc
210# define PyType_GenericNew py3_PyType_GenericNew
211# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200212# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200213# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200214# undef PyUnicode_Decode
215# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200216# define PyType_IsSubtype py3_PyType_IsSubtype
217# define PyCapsule_New py3_PyCapsule_New
218# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200219
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200220# ifdef Py_DEBUG
221# undef PyObject_NEW
222# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200223( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200224 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200225# endif
226
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200227/*
228 * Pointers for dynamic link
229 */
230static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100231static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200232static void (*py3_Py_Initialize)(void);
233static PyObject* (*py3_PyList_New)(Py_ssize_t size);
234static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
235static void (*py3_PyGILState_Release)(PyGILState_STATE);
236static int (*py3_PySys_SetObject)(char *, PyObject *);
237static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
238static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200239static int (*py3_PySequence_Check)(PyObject *);
240static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
241static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
242static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
243static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
244static int (*py3_PyMapping_Check)(PyObject *);
245static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200246static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200247 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200248static PyObject* (*py3_PyErr_NoMemory)(void);
249static void (*py3_Py_Finalize)(void);
250static void (*py3_PyErr_SetString)(PyObject *, const char *);
251static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200252static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200253static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
254static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200255static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200256static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200257static PyObject* (*py3_PyErr_Occurred)(void);
258static PyObject* (*py3_PyModule_GetDict)(PyObject *);
259static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
260static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200261static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200262static PyObject* (*py3_PyLong_FromLong)(long);
263static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200264static PyObject* (*py3_PyIter_Next)(PyObject *);
265static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200266static PyObject* (*py3_Py_BuildValue)(char *, ...);
267static int (*py3_PyType_Ready)(PyTypeObject *type);
268static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
269static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200270static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
271 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272static long (*py3_PyLong_AsLong)(PyObject *);
273static void (*py3_PyErr_SetNone)(PyObject *);
274static void (*py3_PyEval_InitThreads)(void);
275static void(*py3_PyEval_RestoreThread)(PyThreadState *);
276static PyThreadState*(*py3_PyEval_SaveThread)(void);
277static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
278static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200279static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200280static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200281static int (*py3_Py_IsInitialized)(void);
282static void (*py3_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100283static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200285static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200287static PyObject* py3__Py_FalseStruct;
288static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
290static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200291# if PY_VERSION_HEX >= 0x030300f0
292static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
293# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200294static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200295# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200296static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
297static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200298static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200299static PyObject* (*py3_PyBytes_FromString)(char *str);
300static PyObject* (*py3_PyFloat_FromDouble)(double num);
301static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200302static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
303static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
304static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
305static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200306static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200308static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200309static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200310static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200311static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
312static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200313# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200314 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
315 static Py_ssize_t* py3__Py_RefTotal;
316 static void (*py3__Py_Dealloc)(PyObject *obj);
317 static void (*py3__PyObject_DebugFree)(void*);
318 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200319# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200320 static void (*py3_PyObject_Free)(void*);
321 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200322# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200323static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200324
325static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
326
327/* Imported exception objects */
328static PyObject *p3imp_PyExc_AttributeError;
329static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200330static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200331static PyObject *p3imp_PyExc_KeyboardInterrupt;
332static PyObject *p3imp_PyExc_TypeError;
333static PyObject *p3imp_PyExc_ValueError;
334
335# define PyExc_AttributeError p3imp_PyExc_AttributeError
336# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200337# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200338# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
339# define PyExc_TypeError p3imp_PyExc_TypeError
340# define PyExc_ValueError p3imp_PyExc_ValueError
341
342/*
343 * Table of name to function pointer of python.
344 */
345# define PYTHON_PROC FARPROC
346static struct
347{
348 char *name;
349 PYTHON_PROC *ptr;
350} py3_funcname_table[] =
351{
352 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100353 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200354 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200355# ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200356 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200357 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200358# else
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200359 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
360 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200361# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200362 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200363 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200364 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
365 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
366 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
367 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
368 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
369 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200370 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
371 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
372 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
373 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
374 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200375 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
376 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
377 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
378 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
379 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200380 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200381 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
382 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200383 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200385 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
386 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
387 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
388 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200389 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
390 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
391 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
392 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
393 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200394 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
395 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200396 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
397 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
398 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
399 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
400 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
401 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
402 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
403 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200404 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200405 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200406 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200407 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
408 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200409 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100410 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
412 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
413 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200414# if PY_VERSION_HEX >= 0x030300f0
415 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
416# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200417 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200418# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200419 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200420 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200421 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
422 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
423 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
425 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
426 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
427 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200428 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200430 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200431 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200432 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200433# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200434 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
435 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
436 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
437 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
438 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200439# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200440 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
441 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200442# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200443 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
444 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
445 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200446 {"", NULL},
447};
448
449/*
450 * Free python.dll
451 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200452 static void
453end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200454{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200455 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200456 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200457 close_dll(hinstPy3);
458 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200459 }
460}
461
462/*
463 * Load library and get all pointers.
464 * Parameter 'libname' provides name of DLL.
465 * Return OK or FAIL.
466 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200467 static int
468py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469{
470 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200471 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200472
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100473# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200474 /* Can't have Python and Python3 loaded at the same time.
475 * It cause a crash, because RTLD_GLOBAL is needed for
476 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200477 if (python_loaded())
478 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200479 if (verbose)
480 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200481 return FAIL;
482 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200483# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200484
485 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200486 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200487 hinstPy3 = load_dll(libname);
488
489 if (!hinstPy3)
490 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200491 if (verbose)
492 EMSG2(_(e_loadlib), libname);
493 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494 }
495
496 for (i = 0; py3_funcname_table[i].ptr; ++i)
497 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200498 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
499 py3_funcname_table[i].name)) == NULL)
500 {
501 close_dll(hinstPy3);
502 hinstPy3 = 0;
503 if (verbose)
504 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
505 return FAIL;
506 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200507 }
508
Bram Moolenaar69154f22010-07-18 21:42:34 +0200509 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
510 * will be present in the library. */
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200511# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200512 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
513 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
514 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
515 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200516# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200517 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200518 ucs_decode = symbol_from_dll(hinstPy3,
519 "PyUnicodeUCS2_Decode");
520 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
521 "PyUnicodeUCS2_AsEncodedString");
522 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200523 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200524 ucs_from_string = symbol_from_dll(hinstPy3,
525 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200526 ucs_decode = symbol_from_dll(hinstPy3,
527 "PyUnicodeUCS4_Decode");
528 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
529 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200530 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200531# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200532 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200533 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200534 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200535 py3_PyUnicode_Decode = ucs_decode;
536 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200537 }
538 else
539 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200540 close_dll(hinstPy3);
541 hinstPy3 = 0;
542 if (verbose)
543 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
544 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200545 }
546
547 return OK;
548}
549
550/*
551 * If python is enabled (there is installed python on Windows system) return
552 * TRUE, else FALSE.
553 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200554 int
555python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200556{
557 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
558}
559
560/* Load the standard Python exceptions - don't import the symbols from the
561 * DLL, as this can cause errors (importing data symbols is not reliable).
562 */
563static void get_py3_exceptions __ARGS((void));
564
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200565 static void
566get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200567{
568 PyObject *exmod = PyImport_ImportModule("builtins");
569 PyObject *exdict = PyModule_GetDict(exmod);
570 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
571 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200572 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200573 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
574 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
575 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
576 Py_XINCREF(p3imp_PyExc_AttributeError);
577 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200578 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200579 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
580 Py_XINCREF(p3imp_PyExc_TypeError);
581 Py_XINCREF(p3imp_PyExc_ValueError);
582 Py_XDECREF(exmod);
583}
584#endif /* DYNAMIC_PYTHON3 */
585
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586static PyObject *BufferNew (buf_T *);
587static PyObject *WindowNew(win_T *);
588static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100589static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590
591static PyTypeObject RangeType;
592
Bram Moolenaardb913952012-06-29 12:54:53 +0200593static int py3initialised = 0;
594
595#define PYINITIALISED py3initialised
596
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200597#define DICTKEY_DECL PyObject *bytes = NULL;
598
Bram Moolenaardb913952012-06-29 12:54:53 +0200599#define DICTKEY_GET(err) \
600 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200601 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200602 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200603 return err; \
604 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200605 else if (PyUnicode_Check(keyObject)) \
606 { \
607 bytes = PyString_AsBytes(keyObject); \
608 if (bytes == NULL) \
609 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200610 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200611 return err; \
612 } \
613 else \
614 { \
615 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
616 return err; \
617 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200618
Bram Moolenaardb913952012-06-29 12:54:53 +0200619#define DICTKEY_UNREF \
620 if (bytes != NULL) \
621 Py_XDECREF(bytes);
622
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200623/*
624 * Include the code shared with if_python.c
625 */
626#include "if_py_both.h"
627
Bram Moolenaar77045652012-09-21 13:46:06 +0200628#define GET_ATTR_STRING(name, nameobj) \
629 char *name = ""; \
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200630 if (PyUnicode_Check(nameobj)) \
631 name = _PyUnicode_AsString(nameobj)
Bram Moolenaar77045652012-09-21 13:46:06 +0200632
Bram Moolenaardb913952012-06-29 12:54:53 +0200633#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
634
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200635 static void
636call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200637{
638#ifdef Py_DEBUG
639 _PyObject_DebugFree(p);
640#else
641 PyObject_Free(p);
642#endif
643}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200644
645 static PyObject *
646call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200647{
648 return PyType_GenericNew(type,args,kwds);
649}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200650
651 static PyObject *
652call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200653{
654 return PyType_GenericAlloc(type,nitems);
655}
656
657/******************************************************
658 * Internal function prototypes.
659 */
660
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200661static Py_ssize_t RangeStart;
662static Py_ssize_t RangeEnd;
663
Bram Moolenaardb913952012-06-29 12:54:53 +0200664static PyObject *globals;
665
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200666static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100667static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200668
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200669/******************************************************
670 * 1. Python interpreter main program.
671 */
672
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200673static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
674
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200675 void
676python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200677{
678 static int recurse = 0;
679
680 /* If a crash occurs while doing this, don't try again. */
681 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200682 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200683
684 ++recurse;
685
686#ifdef DYNAMIC_PYTHON3
687 if (hinstPy3)
688#endif
689 if (Py_IsInitialized())
690 {
691 // acquire lock before finalizing
692 pygilstate = PyGILState_Ensure();
693
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200694 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200695 }
696
697#ifdef DYNAMIC_PYTHON3
698 end_dynamic_python3();
699#endif
700
701 --recurse;
702}
703
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200704#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
705 int
706python3_loaded()
707{
708 return (hinstPy3 != 0);
709}
710#endif
711
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200712 static int
713Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200714{
715 if (!py3initialised)
716 {
717#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200718 if (!python3_enabled(TRUE))
719 {
720 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
721 goto fail;
722 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200723#endif
724
725 init_structs();
726
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100727
728#ifdef PYTHON3_HOME
729 Py_SetPythonHome(PYTHON3_HOME);
730#endif
731
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200732 PyImport_AppendInittab("vim", Py3Init_vim);
733
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200734#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200735 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200737 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200738#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100739 /* Initialise threads, and below save the state using
740 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
741 * specific state (such as the system trace hook), will be lost
742 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200743 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200744#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200745 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200746#endif
747
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200748 if (PythonIO_Init())
749 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200750
Bram Moolenaardb913952012-06-29 12:54:53 +0200751 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
752
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200753 /* Remove the element from sys.path that was added because of our
754 * argv[0] value in Py3Init_vim(). Previously we used an empty
755 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200756 * the current directory in sys.path.
757 * Only after vim has been imported, the element does exist in
758 * sys.path.
759 */
760 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 +0200761
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100762 /* lock is created and acquired in PyEval_InitThreads() and thread
763 * state is created in Py_Initialize()
764 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
765 * (python must have threads enabled!)
766 * so the following does both: unlock GIL and save thread state in TLS
767 * without deleting thread state
768 */
769 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200771 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200772 }
773
774 return 0;
775
776fail:
777 /* We call PythonIO_Flush() here to print any Python errors.
778 * This is OK, as it is possible to call this function even
779 * if PythonIO_Init() has not completed successfully (it will
780 * not do anything in this case).
781 */
782 PythonIO_Flush();
783 return -1;
784}
785
786/*
787 * External interface
788 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200789 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200790DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200791{
792#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200793 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200794#endif
795#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200796 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200797#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200798 PyObject *cmdstr;
799 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200800
801#if defined(MACOS) && !defined(MACOS_X_UNIX)
802 GetPort(&oldPort);
803 /* Check if the Python library is available */
804 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200805 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200806#endif
807 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200808 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809
Bram Moolenaardb913952012-06-29 12:54:53 +0200810 if (rettv == NULL)
811 {
812 RangeStart = eap->line1;
813 RangeEnd = eap->line2;
814 }
815 else
816 {
817 RangeStart = (PyInt) curwin->w_cursor.lnum;
818 RangeEnd = RangeStart;
819 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200820 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200821
822#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
823 /* Python only works properly when the LC_NUMERIC locale is "C". */
824 saved_locale = setlocale(LC_NUMERIC, NULL);
825 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200826 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200827 else
828 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200829 /* Need to make a copy, value may change when setting new locale. */
830 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
831 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200832 }
833#endif
834
835 pygilstate = PyGILState_Ensure();
836
Bram Moolenaar19e60942011-06-19 00:27:51 +0200837 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
838 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200839 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
840 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
841 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200842 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200843 if (rettv == NULL)
844 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
845 else
846 {
847 PyObject *r;
848
849 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
850 globals, globals);
851 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100852 {
853 if (PyErr_Occurred() && !msg_silent)
854 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200855 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100856 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200857 else
858 {
859 if (ConvertFromPyObject(r, rettv) == -1)
860 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
861 Py_DECREF(r);
862 }
863 PyErr_Clear();
864 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200865 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200866
867 PyGILState_Release(pygilstate);
868
869#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
870 if (saved_locale != NULL)
871 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200872 (void)setlocale(LC_NUMERIC, saved_locale);
873 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200874 }
875#endif
876
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200877 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200878 PythonIO_Flush();
879#if defined(MACOS) && !defined(MACOS_X_UNIX)
880 SetPort(oldPort);
881#endif
882
883theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200884 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200885}
886
887/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200888 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200889 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200890 void
891ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200892{
893 char_u *script;
894
895 script = script_get(eap, eap->arg);
896 if (!eap->skip)
897 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200898 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200900 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200901 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200902 }
903 vim_free(script);
904}
905
906#define BUFFER_SIZE 2048
907
908/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200909 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200910 */
911 void
912ex_py3file(exarg_T *eap)
913{
914 static char buffer[BUFFER_SIZE];
915 const char *file;
916 char *p;
917 int i;
918
919 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
920 * stdio file pointer, but Vim and the Python DLL are compiled with
921 * different options under Windows, meaning that stdio pointers aren't
922 * compatible between the two. Yuk.
923 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200924 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
925 *
926 * Using bytes so that Python can detect the source encoding as it normally
927 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200928 *
929 * We need to escape any backslashes or single quotes in the file name, so that
930 * Python won't mangle the file name.
931 */
932
933 strcpy(buffer, "exec(compile(open('");
934 p = buffer + 19; /* size of "exec(compile(open('" */
935
936 for (i=0; i<2; ++i)
937 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200938 file = (char *)eap->arg;
939 while (*file && p < buffer + (BUFFER_SIZE - 3))
940 {
941 if (*file == '\\' || *file == '\'')
942 *p++ = '\\';
943 *p++ = *file++;
944 }
945 /* If we didn't finish the file name, we hit a buffer overflow */
946 if (*file != '\0')
947 return;
948 if (i==0)
949 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200950 strcpy(p,"','rb').read(),'");
951 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200952 }
953 else
954 {
955 strcpy(p,"','exec'))");
956 p += 10;
957 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200958 }
959
960
961 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200962 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200963}
964
965/******************************************************
966 * 2. Python output stream: writes output via [e]msg().
967 */
968
969/* Implementation functions
970 */
971
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200972 static PyObject *
973OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200974{
Bram Moolenaar77045652012-09-21 13:46:06 +0200975 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200976
977 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200978 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979
980 return PyObject_GenericGetAttr(self, nameobj);
981}
982
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200983 static int
984OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985{
Bram Moolenaar77045652012-09-21 13:46:06 +0200986 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987
Bram Moolenaar77045652012-09-21 13:46:06 +0200988 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200989}
990
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991/***************/
992
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200993 static int
994PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200995{
996 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200997 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200998}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200999
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001000/******************************************************
1001 * 3. Implementation of the Vim module for Python
1002 */
1003
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004/* Window type - Implementation functions
1005 * --------------------------------------
1006 */
1007
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001008#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1009
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010/* Buffer type - Implementation functions
1011 * --------------------------------------
1012 */
1013
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1015
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016static Py_ssize_t BufferLength(PyObject *);
1017static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001018static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1019static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021
1022/* Line range type - Implementation functions
1023 * --------------------------------------
1024 */
1025
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1027
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001028static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001029static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001030static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001031
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032/* Current objects type - Implementation functions
1033 * -----------------------------------------------
1034 */
1035
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001036static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001037 (lenfunc) BufferLength, /* sq_length, len(x) */
1038 (binaryfunc) 0, /* sq_concat, x+y */
1039 (ssizeargfunc) 0, /* sq_repeat, x*n */
1040 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1041 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001042 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001043 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001044 0, /* sq_contains */
1045 0, /* sq_inplace_concat */
1046 0, /* sq_inplace_repeat */
1047};
1048
1049PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001050 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001051 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001052 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001053};
1054
1055
1056/* Buffer object - Definitions
1057 */
1058
1059static PyTypeObject BufferType;
1060
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001061 static PyObject *
1062BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001063{
1064 /* We need to handle deletion of buffers underneath us.
1065 * If we add a "b_python3_ref" field to the buf_T structure,
1066 * then we can get at it in buf_freeall() in vim. We then
1067 * need to create only ONE Python object per buffer - if
1068 * we try to create a second, just INCREF the existing one
1069 * and return it. The (single) Python object referring to
1070 * the buffer is stored in "b_python3_ref".
1071 * Question: what to do on a buf_freeall(). We'll probably
1072 * have to either delete the Python object (DECREF it to
1073 * zero - a bad idea, as it leaves dangling refs!) or
1074 * set the buf_T * value to an invalid value (-1?), which
1075 * means we need checks in all access functions... Bah.
1076 */
1077
1078 BufferObject *self;
1079
1080 if (buf->b_python3_ref != NULL)
1081 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001082 self = buf->b_python3_ref;
1083 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001084 }
1085 else
1086 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001087 self = PyObject_NEW(BufferObject, &BufferType);
1088 buf->b_python3_ref = self;
1089 if (self == NULL)
1090 return NULL;
1091 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092 }
1093
1094 return (PyObject *)(self);
1095}
1096
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001097 static void
1098BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001099{
1100 BufferObject *this = (BufferObject *)(self);
1101
1102 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001103 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001104
1105 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001106}
1107
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001108 static PyObject *
1109BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110{
1111 BufferObject *this = (BufferObject *)(self);
1112
Bram Moolenaar77045652012-09-21 13:46:06 +02001113 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001114
1115 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001116 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001117
1118 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001119 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001120 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001121 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001122 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001123 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124}
1125
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001126 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001127BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1128{
1129 return Py_BuildValue("[sssss]", "name", "number",
1130 "append", "mark", "range");
1131}
1132
1133 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001134BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135{
1136 static char repr[100];
1137 BufferObject *this = (BufferObject *)(self);
1138
1139 if (this->buf == INVALID_BUFFER_VALUE)
1140 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001141 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1142 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001143 }
1144 else
1145 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001146 char *name = (char *)this->buf->b_fname;
1147 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001148
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001149 if (name == NULL)
1150 name = "";
1151 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001152
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001153 if (len > 35)
1154 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001156 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001157
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001158 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001159 }
1160}
1161
1162/******************/
1163
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001164 static Py_ssize_t
1165BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001166{
1167 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001168 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169
1170 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1171}
1172
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001173 static PyObject *
1174BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001175{
1176 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001177 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001178}
1179
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001180 static PyObject *
1181BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1182{
1183 return RBSlice((BufferObject *)(self), lo, hi, 1,
1184 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1185}
1186
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001187 static PyObject *
1188BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189{
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 if (PyLong_Check(idx))
1191 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001192 long _idx = PyLong_AsLong(idx);
1193 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001194 } else if (PySlice_Check(idx))
1195 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001196 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001198 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001199 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1200 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001201 &step, &slicelen) < 0)
1202 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001203 return NULL;
1204 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001205 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001206 }
1207 else
1208 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001209 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1210 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211 }
1212}
1213
Bram Moolenaar19e60942011-06-19 00:27:51 +02001214 static Py_ssize_t
1215BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1216{
Bram Moolenaardb913952012-06-29 12:54:53 +02001217 if (PyLong_Check(idx))
1218 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001219 long n = PyLong_AsLong(idx);
1220 return RBAsItem((BufferObject *)(self), n, val, 1,
1221 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1222 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001223 } else if (PySlice_Check(idx))
1224 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001225 Py_ssize_t start, stop, step, slicelen;
1226
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001227 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001228 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1229 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001230 &step, &slicelen) < 0)
1231 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001232 return -1;
1233 }
1234 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1235 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1236 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001237 }
1238 else
1239 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001240 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1241 return -1;
1242 }
1243}
1244
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001245static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 (lenfunc) RangeLength, /* sq_length, len(x) */
1247 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1248 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1249 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1250 0, /* was_sq_slice, x[i:j] */
1251 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1252 0, /* sq_ass_slice, x[i:j]=v */
1253 0, /* sq_contains */
1254 0, /* sq_inplace_concat */
1255 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256};
1257
1258PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001259 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001261 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001262};
1263
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001264/* Line range object - Implementation
1265 */
1266
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001267 static void
1268RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001269{
1270 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001271 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001272}
1273
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001274 static PyObject *
1275RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001276{
Bram Moolenaar77045652012-09-21 13:46:06 +02001277 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001278
1279 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001280 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001281 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001282 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001283 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001284 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285}
1286
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287/****************/
1288
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001289 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001290RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001291{
1292 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001293 ((RangeObject *)(self))->start,
1294 ((RangeObject *)(self))->end,
1295 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001296}
1297
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001298 static Py_ssize_t
1299RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1300{
1301 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1302 ((RangeObject *)(self))->start,
1303 ((RangeObject *)(self))->end,
1304 &((RangeObject *)(self))->end);
1305}
1306
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001307 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001308RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001309{
Bram Moolenaardb913952012-06-29 12:54:53 +02001310 if (PyLong_Check(idx))
1311 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 long _idx = PyLong_AsLong(idx);
1313 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001314 } else if (PySlice_Check(idx))
1315 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001318 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001319 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1320 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001321 &step, &slicelen) < 0)
1322 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001323 return NULL;
1324 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001325 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001326 }
1327 else
1328 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001329 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1330 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331 }
1332}
1333
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001334 static Py_ssize_t
1335RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1336{
Bram Moolenaardb913952012-06-29 12:54:53 +02001337 if (PyLong_Check(idx))
1338 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001339 long n = PyLong_AsLong(idx);
1340 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001341 } else if (PySlice_Check(idx))
1342 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001343 Py_ssize_t start, stop, step, slicelen;
1344
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001345 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001346 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1347 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001348 &step, &slicelen) < 0)
1349 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001350 return -1;
1351 }
1352 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001353 }
1354 else
1355 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001356 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1357 return -1;
1358 }
1359}
1360
1361
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001362/* Buffer list object - Definitions
1363 */
1364
1365typedef struct
1366{
1367 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001368} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001369
1370static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001371 (lenfunc) BufListLength, /* sq_length, len(x) */
1372 (binaryfunc) 0, /* sq_concat, x+y */
1373 (ssizeargfunc) 0, /* sq_repeat, x*n */
1374 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1375 0, /* was_sq_slice, x[i:j] */
1376 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1377 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001378 0, /* sq_contains */
1379 0, /* sq_inplace_concat */
1380 0, /* sq_inplace_repeat */
1381};
1382
1383static PyTypeObject BufListType;
1384
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001385/* Window object - Definitions
1386 */
1387
1388static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001389 /* name, function, calling, documentation */
1390 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001391};
1392
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001393static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001394
1395/* Window object - Implementation
1396 */
1397
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001398 static PyObject *
1399WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001400{
1401 /* We need to handle deletion of windows underneath us.
1402 * If we add a "w_python3_ref" field to the win_T structure,
1403 * then we can get at it in win_free() in vim. We then
1404 * need to create only ONE Python object per window - if
1405 * we try to create a second, just INCREF the existing one
1406 * and return it. The (single) Python object referring to
1407 * the window is stored in "w_python3_ref".
1408 * On a win_free() we set the Python object's win_T* field
1409 * to an invalid value. We trap all uses of a window
1410 * object, and reject them if the win_T* field is invalid.
1411 */
1412
1413 WindowObject *self;
1414
1415 if (win->w_python3_ref)
1416 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001417 self = win->w_python3_ref;
1418 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001419 }
1420 else
1421 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001422 self = PyObject_NEW(WindowObject, &WindowType);
1423 if (self == NULL)
1424 return NULL;
1425 self->win = win;
1426 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427 }
1428
1429 return (PyObject *)(self);
1430}
1431
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001432 static void
1433WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434{
1435 WindowObject *this = (WindowObject *)(self);
1436
1437 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001438 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001439
1440 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001441}
1442
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001443 static PyObject *
1444WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001445{
1446 WindowObject *this = (WindowObject *)(self);
1447
Bram Moolenaar77045652012-09-21 13:46:06 +02001448 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001449
1450 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001451 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001452
1453 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001454 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001455 else if (strcmp(name, "cursor") == 0)
1456 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001457 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001459 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460 }
1461 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001462 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001463#ifdef FEAT_VERTSPLIT
1464 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001465 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001466#endif
1467 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001468 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001469 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001470 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471}
1472
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001473 static int
1474WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001475{
Bram Moolenaar77045652012-09-21 13:46:06 +02001476 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001478 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001479}
1480
1481/* Window list object - Definitions
1482 */
1483
1484typedef struct
1485{
1486 PyObject_HEAD
1487}
1488WinListObject;
1489
1490static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001491 (lenfunc) WinListLength, /* sq_length, len(x) */
1492 (binaryfunc) 0, /* sq_concat, x+y */
1493 (ssizeargfunc) 0, /* sq_repeat, x*n */
1494 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1495 0, /* sq_slice, x[i:j] */
1496 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1497 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498 0, /* sq_contains */
1499 0, /* sq_inplace_concat */
1500 0, /* sq_inplace_repeat */
1501};
1502
1503static PyTypeObject WinListType;
1504
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505/* Current items object - Definitions
1506 */
1507
1508typedef struct
1509{
1510 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001511} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001512
1513static PyTypeObject CurrentType;
1514
1515/* Current items object - Implementation
1516 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001517 static PyObject *
1518CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001519{
Bram Moolenaar77045652012-09-21 13:46:06 +02001520 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521
1522 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001523 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001524 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001526 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001527 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001528 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001529 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001530 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001531 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001532 else
1533 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001534 PyErr_SetString(PyExc_AttributeError, name);
1535 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001536 }
1537}
1538
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001539 static int
1540CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001541{
1542 char *name = "";
1543 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001544 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001545
1546 if (strcmp(name, "line") == 0)
1547 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001548 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1549 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001550
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001551 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001552 }
1553 else
1554 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001555 PyErr_SetString(PyExc_AttributeError, name);
1556 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001557 }
1558}
1559
Bram Moolenaardb913952012-06-29 12:54:53 +02001560/* Dictionary object - Definitions
1561 */
1562
1563static PyInt DictionaryLength(PyObject *);
1564
1565static PyMappingMethods DictionaryAsMapping = {
1566 /* mp_length */ (lenfunc) DictionaryLength,
1567 /* mp_subscript */ (binaryfunc) DictionaryItem,
1568 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1569};
1570
Bram Moolenaar66b79852012-09-21 14:00:35 +02001571 static PyObject *
1572DictionaryGetattro(PyObject *self, PyObject *nameobj)
1573{
1574 DictionaryObject *this = ((DictionaryObject *) (self));
1575
1576 GET_ATTR_STRING(name, nameobj);
1577
1578 if (strcmp(name, "locked") == 0)
1579 return PyLong_FromLong(this->dict->dv_lock);
1580 else if (strcmp(name, "scope") == 0)
1581 return PyLong_FromLong(this->dict->dv_scope);
1582
1583 return PyObject_GenericGetAttr(self, nameobj);
1584}
1585
1586 static int
1587DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1588{
1589 GET_ATTR_STRING(name, nameobj);
1590 return DictionarySetattr((DictionaryObject *) self, name, val);
1591}
1592
Bram Moolenaardb913952012-06-29 12:54:53 +02001593static PyTypeObject DictionaryType;
1594
1595 static void
1596DictionaryDestructor(PyObject *self)
1597{
1598 DictionaryObject *this = (DictionaryObject *)(self);
1599
1600 pyll_remove(&this->ref, &lastdict);
1601 dict_unref(this->dict);
1602
1603 Py_TYPE(self)->tp_free((PyObject*)self);
1604}
1605
1606/* List object - Definitions
1607 */
1608
1609static PyInt ListLength(PyObject *);
1610static PyObject *ListItem(PyObject *, Py_ssize_t);
1611
1612static PySequenceMethods ListAsSeq = {
1613 (lenfunc) ListLength, /* sq_length, len(x) */
1614 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1615 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1616 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1617 (void *) 0, /* was_sq_slice, x[i:j] */
1618 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1619 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1620 0, /* sq_contains */
1621 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1622 0, /* sq_inplace_repeat */
1623};
1624
1625static PyObject *ListSubscript(PyObject *, PyObject *);
1626static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1627
1628static PyMappingMethods ListAsMapping = {
1629 /* mp_length */ (lenfunc) ListLength,
1630 /* mp_subscript */ (binaryfunc) ListSubscript,
1631 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1632};
1633
1634static PyTypeObject ListType;
1635
1636 static PyObject *
1637ListSubscript(PyObject *self, PyObject* idxObject)
1638{
1639 if (PyLong_Check(idxObject))
1640 {
1641 long idx = PyLong_AsLong(idxObject);
1642 return ListItem(self, idx);
1643 }
1644 else if (PySlice_Check(idxObject))
1645 {
1646 Py_ssize_t start, stop, step, slicelen;
1647
1648 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1649 &step, &slicelen) < 0)
1650 return NULL;
1651 return ListSlice(self, start, stop);
1652 }
1653 else
1654 {
1655 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1656 return NULL;
1657 }
1658}
1659
1660 static Py_ssize_t
1661ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1662{
1663 if (PyLong_Check(idxObject))
1664 {
1665 long idx = PyLong_AsLong(idxObject);
1666 return ListAssItem(self, idx, obj);
1667 }
1668 else if (PySlice_Check(idxObject))
1669 {
1670 Py_ssize_t start, stop, step, slicelen;
1671
1672 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1673 &step, &slicelen) < 0)
1674 return -1;
1675 return ListAssSlice(self, start, stop, obj);
1676 }
1677 else
1678 {
1679 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1680 return -1;
1681 }
1682}
1683
Bram Moolenaar66b79852012-09-21 14:00:35 +02001684 static PyObject *
1685ListGetattro(PyObject *self, PyObject *nameobj)
1686{
1687 GET_ATTR_STRING(name, nameobj);
1688
1689 if (strcmp(name, "locked") == 0)
1690 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1691
1692 return PyObject_GenericGetAttr(self, nameobj);
1693}
1694
1695 static int
1696ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1697{
1698 GET_ATTR_STRING(name, nameobj);
1699 return ListSetattr((ListObject *) self, name, val);
1700}
1701
Bram Moolenaardb913952012-06-29 12:54:53 +02001702 static void
1703ListDestructor(PyObject *self)
1704{
1705 ListObject *this = (ListObject *)(self);
1706
1707 pyll_remove(&this->ref, &lastlist);
1708 list_unref(this->list);
1709
1710 Py_TYPE(self)->tp_free((PyObject*)self);
1711}
1712
1713/* Function object - Definitions
1714 */
1715
1716 static void
1717FunctionDestructor(PyObject *self)
1718{
1719 FunctionObject *this = (FunctionObject *) (self);
1720
1721 func_unref(this->name);
1722 PyMem_Del(this->name);
1723
1724 Py_TYPE(self)->tp_free((PyObject*)self);
1725}
1726
1727 static PyObject *
1728FunctionGetattro(PyObject *self, PyObject *nameobj)
1729{
1730 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001731
1732 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001733
1734 if (strcmp(name, "name") == 0)
1735 return PyUnicode_FromString((char *)(this->name));
1736
1737 return PyObject_GenericGetAttr(self, nameobj);
1738}
1739
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001740/* External interface
1741 */
1742
1743 void
1744python3_buffer_free(buf_T *buf)
1745{
1746 if (buf->b_python3_ref != NULL)
1747 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001748 BufferObject *bp = buf->b_python3_ref;
1749 bp->buf = INVALID_BUFFER_VALUE;
1750 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001751 }
1752}
1753
1754#if defined(FEAT_WINDOWS) || defined(PROTO)
1755 void
1756python3_window_free(win_T *win)
1757{
1758 if (win->w_python3_ref != NULL)
1759 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001760 WindowObject *wp = win->w_python3_ref;
1761 wp->win = INVALID_WINDOW_VALUE;
1762 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001763 }
1764}
1765#endif
1766
1767static BufListObject TheBufferList =
1768{
1769 PyObject_HEAD_INIT(&BufListType)
1770};
1771
1772static WinListObject TheWindowList =
1773{
1774 PyObject_HEAD_INIT(&WinListType)
1775};
1776
1777static CurrentObject TheCurrent =
1778{
1779 PyObject_HEAD_INIT(&CurrentType)
1780};
1781
1782PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1783
1784static struct PyModuleDef vimmodule;
1785
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001786 static PyObject *
1787Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001788{
1789 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001790 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001791 /* The special value is removed from sys.path in Python3_Init(). */
1792 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1793
1794 PyType_Ready(&BufferType);
1795 PyType_Ready(&RangeType);
1796 PyType_Ready(&WindowType);
1797 PyType_Ready(&BufListType);
1798 PyType_Ready(&WinListType);
1799 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001800 PyType_Ready(&DictionaryType);
1801 PyType_Ready(&ListType);
1802 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001803
1804 /* Set sys.argv[] to avoid a crash in warn(). */
1805 PySys_SetArgv(1, argv);
1806
1807 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001808 if (mod == NULL)
1809 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001810
Bram Moolenaar19e60942011-06-19 00:27:51 +02001811 VimError = PyErr_NewException("vim.error", NULL, NULL);
1812 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001813
1814 PyModule_AddObject(mod, "error", VimError);
1815 Py_INCREF((PyObject *)(void *)&TheBufferList);
1816 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1817 Py_INCREF((PyObject *)(void *)&TheCurrent);
1818 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1819 Py_INCREF((PyObject *)(void *)&TheWindowList);
1820 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1821
Bram Moolenaar66b79852012-09-21 14:00:35 +02001822#define ADD_INT_CONSTANT(name, value) \
1823 tmp = PyLong_FromLong(value); \
1824 Py_INCREF(tmp); \
1825 PyModule_AddObject(mod, name, tmp)
1826
1827 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1828 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1829 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1830 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1831
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001832 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001833 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001834
1835 return mod;
1836}
1837
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}