blob: 477c367ef4ff21132db4aa45c40d10bb94c8c405 [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 Moolenaar4d1da492013-04-24 13:39:15 +020094#define Py_ssize_t_fmt "n"
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020095
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010096#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020097
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020098# ifndef WIN3264
99# include <dlfcn.h>
100# define FARPROC void*
101# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100102# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200103# define load_dll(n) dlopen((n), RTLD_LAZY)
104# else
105# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
106# endif
107# define close_dll dlclose
108# define symbol_from_dll dlsym
109# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200110# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200111# define close_dll FreeLibrary
112# define symbol_from_dll GetProcAddress
113# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200114/*
115 * Wrapper defines
116 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200117# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200118# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200119# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200120# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200121# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200122# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200123# define PyDict_SetItemString py3_PyDict_SetItemString
124# define PyErr_BadArgument py3_PyErr_BadArgument
125# define PyErr_Clear py3_PyErr_Clear
Bram Moolenaar4d369872013-02-20 16:09:43 +0100126# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200127# define PyErr_NoMemory py3_PyErr_NoMemory
128# define PyErr_Occurred py3_PyErr_Occurred
129# define PyErr_SetNone py3_PyErr_SetNone
130# define PyErr_SetString py3_PyErr_SetString
131# define PyEval_InitThreads py3_PyEval_InitThreads
132# define PyEval_RestoreThread py3_PyEval_RestoreThread
133# define PyEval_SaveThread py3_PyEval_SaveThread
134# define PyGILState_Ensure py3_PyGILState_Ensure
135# define PyGILState_Release py3_PyGILState_Release
136# define PyLong_AsLong py3_PyLong_AsLong
137# define PyLong_FromLong py3_PyLong_FromLong
138# define PyList_GetItem py3_PyList_GetItem
139# define PyList_Append py3_PyList_Append
140# define PyList_New py3_PyList_New
141# define PyList_SetItem py3_PyList_SetItem
142# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200143# define PySequence_Check py3_PySequence_Check
144# define PySequence_Size py3_PySequence_Size
145# define PySequence_GetItem py3_PySequence_GetItem
146# define PyTuple_Size py3_PyTuple_Size
147# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200148# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
149# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200150# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200151# define PyObject_Init py3__PyObject_Init
152# define PyDict_New py3_PyDict_New
153# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200154# define PyDict_Next py3_PyDict_Next
155# define PyMapping_Check py3_PyMapping_Check
156# define PyMapping_Items py3_PyMapping_Items
157# define PyIter_Next py3_PyIter_Next
158# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200159# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200160# define PyModule_GetDict py3_PyModule_GetDict
161#undef PyRun_SimpleString
162# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200163#undef PyRun_String
164# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165# define PySys_SetObject py3_PySys_SetObject
166# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200167# define PyType_Ready py3_PyType_Ready
168#undef Py_BuildValue
169# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100170# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200171# define Py_Initialize py3_Py_Initialize
172# define Py_Finalize py3_Py_Finalize
173# define Py_IsInitialized py3_Py_IsInitialized
174# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200175# define _Py_FalseStruct (*py3__Py_FalseStruct)
176# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200177# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200178# define PyModule_AddObject py3_PyModule_AddObject
179# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200180# if PY_VERSION_HEX >= 0x030300f0
181# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200182# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200183# else
184# define _PyUnicode_AsString py3__PyUnicode_AsString
185# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200186# undef PyUnicode_AsEncodedString
187# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
188# undef PyBytes_AsString
189# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200190# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200191# undef PyBytes_FromString
192# define PyBytes_FromString py3_PyBytes_FromString
193# define PyFloat_FromDouble py3_PyFloat_FromDouble
194# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200196# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200199# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200200# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200201# ifdef Py_DEBUG
202# define _Py_NegativeRefcount py3__Py_NegativeRefcount
203# define _Py_RefTotal (*py3__Py_RefTotal)
204# define _Py_Dealloc py3__Py_Dealloc
205# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
206# define _PyObject_DebugFree py3__PyObject_DebugFree
207# else
208# define PyObject_Malloc py3_PyObject_Malloc
209# define PyObject_Free py3_PyObject_Free
210# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200211# define PyType_GenericAlloc py3_PyType_GenericAlloc
212# define PyType_GenericNew py3_PyType_GenericNew
213# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200214# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200215# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200216# undef PyUnicode_Decode
217# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200218# define PyType_IsSubtype py3_PyType_IsSubtype
219# define PyCapsule_New py3_PyCapsule_New
220# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200221
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200222# ifdef Py_DEBUG
223# undef PyObject_NEW
224# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200225( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200226 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200227# endif
228
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229/*
230 * Pointers for dynamic link
231 */
232static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100233static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200234static void (*py3_Py_Initialize)(void);
235static PyObject* (*py3_PyList_New)(Py_ssize_t size);
236static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
237static void (*py3_PyGILState_Release)(PyGILState_STATE);
238static int (*py3_PySys_SetObject)(char *, PyObject *);
239static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
240static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200241static int (*py3_PySequence_Check)(PyObject *);
242static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
243static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
244static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
245static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
246static int (*py3_PyMapping_Check)(PyObject *);
247static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200248static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200249 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200250static PyObject* (*py3_PyErr_NoMemory)(void);
251static void (*py3_Py_Finalize)(void);
252static void (*py3_PyErr_SetString)(PyObject *, const char *);
253static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200254static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200255static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
256static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200257static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200258static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200259static PyObject* (*py3_PyErr_Occurred)(void);
260static PyObject* (*py3_PyModule_GetDict)(PyObject *);
261static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
262static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200263static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200264static PyObject* (*py3_PyLong_FromLong)(long);
265static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200266static PyObject* (*py3_PyIter_Next)(PyObject *);
267static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200268static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200269static PyObject* (*py3_Py_BuildValue)(char *, ...);
270static int (*py3_PyType_Ready)(PyTypeObject *type);
271static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
272static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200273static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
274 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200275static long (*py3_PyLong_AsLong)(PyObject *);
276static void (*py3_PyErr_SetNone)(PyObject *);
277static void (*py3_PyEval_InitThreads)(void);
278static void(*py3_PyEval_RestoreThread)(PyThreadState *);
279static PyThreadState*(*py3_PyEval_SaveThread)(void);
280static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
281static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200282static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200283static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200284static int (*py3_Py_IsInitialized)(void);
285static void (*py3_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100286static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200287static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200288static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200290static PyObject* py3__Py_FalseStruct;
291static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
293static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200294# if PY_VERSION_HEX >= 0x030300f0
295static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
296# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200298# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200299static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
300static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200301static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200302static PyObject* (*py3_PyBytes_FromString)(char *str);
303static PyObject* (*py3_PyFloat_FromDouble)(double num);
304static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200305static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
306static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
307static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
308static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200309static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200310static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200311static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200312static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200313static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200314static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
315static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200316# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200317 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
318 static Py_ssize_t* py3__Py_RefTotal;
319 static void (*py3__Py_Dealloc)(PyObject *obj);
320 static void (*py3__PyObject_DebugFree)(void*);
321 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200322# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323 static void (*py3_PyObject_Free)(void*);
324 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200325# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200326static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200327
328static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
329
330/* Imported exception objects */
331static PyObject *p3imp_PyExc_AttributeError;
332static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200333static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200334static PyObject *p3imp_PyExc_KeyboardInterrupt;
335static PyObject *p3imp_PyExc_TypeError;
336static PyObject *p3imp_PyExc_ValueError;
337
338# define PyExc_AttributeError p3imp_PyExc_AttributeError
339# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200340# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200341# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
342# define PyExc_TypeError p3imp_PyExc_TypeError
343# define PyExc_ValueError p3imp_PyExc_ValueError
344
345/*
346 * Table of name to function pointer of python.
347 */
348# define PYTHON_PROC FARPROC
349static struct
350{
351 char *name;
352 PYTHON_PROC *ptr;
353} py3_funcname_table[] =
354{
355 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100356 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200357 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200358# ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200360 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200361# else
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200362 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
363 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200364# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200365 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200366 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200367 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
368 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
369 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
370 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
371 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
372 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200373 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
374 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
375 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
376 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
377 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
379 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
380 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
381 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
382 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200383 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200384 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
385 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200386 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200387 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
389 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
390 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
391 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200392 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
393 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
394 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
395 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
396 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200397 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
399 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200400 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
401 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
402 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
403 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
404 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
405 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
406 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
407 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200409 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200410 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200411 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
412 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100414 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
416 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
417 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200418# if PY_VERSION_HEX >= 0x030300f0
419 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
420# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200422# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200423 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200424 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200425 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
426 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
427 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
429 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
430 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
431 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200432 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200433 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200434 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200435 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200436 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200437# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200438 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
439 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
440 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
441 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
442 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200443# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200444 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
445 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200446# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200447 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
448 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
449 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200450 {"", NULL},
451};
452
453/*
454 * Free python.dll
455 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200456 static void
457end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200458{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200459 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200460 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200461 close_dll(hinstPy3);
462 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463 }
464}
465
466/*
467 * Load library and get all pointers.
468 * Parameter 'libname' provides name of DLL.
469 * Return OK or FAIL.
470 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200471 static int
472py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473{
474 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200475 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200476
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100477# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200478 /* Can't have Python and Python3 loaded at the same time.
479 * It cause a crash, because RTLD_GLOBAL is needed for
480 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200481 if (python_loaded())
482 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200483 if (verbose)
484 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200485 return FAIL;
486 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200487# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200488
489 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200490 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491 hinstPy3 = load_dll(libname);
492
493 if (!hinstPy3)
494 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200495 if (verbose)
496 EMSG2(_(e_loadlib), libname);
497 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200498 }
499
500 for (i = 0; py3_funcname_table[i].ptr; ++i)
501 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200502 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
503 py3_funcname_table[i].name)) == NULL)
504 {
505 close_dll(hinstPy3);
506 hinstPy3 = 0;
507 if (verbose)
508 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
509 return FAIL;
510 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 }
512
Bram Moolenaar69154f22010-07-18 21:42:34 +0200513 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
514 * will be present in the library. */
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200515# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200516 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
517 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
518 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
519 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200520# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200521 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200522 ucs_decode = symbol_from_dll(hinstPy3,
523 "PyUnicodeUCS2_Decode");
524 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
525 "PyUnicodeUCS2_AsEncodedString");
526 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200527 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200528 ucs_from_string = symbol_from_dll(hinstPy3,
529 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200530 ucs_decode = symbol_from_dll(hinstPy3,
531 "PyUnicodeUCS4_Decode");
532 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
533 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200535# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200536 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200537 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200538 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200539 py3_PyUnicode_Decode = ucs_decode;
540 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 }
542 else
543 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200544 close_dll(hinstPy3);
545 hinstPy3 = 0;
546 if (verbose)
547 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
548 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200549 }
550
551 return OK;
552}
553
554/*
555 * If python is enabled (there is installed python on Windows system) return
556 * TRUE, else FALSE.
557 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200558 int
559python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200560{
561 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
562}
563
564/* Load the standard Python exceptions - don't import the symbols from the
565 * DLL, as this can cause errors (importing data symbols is not reliable).
566 */
567static void get_py3_exceptions __ARGS((void));
568
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200569 static void
570get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200571{
572 PyObject *exmod = PyImport_ImportModule("builtins");
573 PyObject *exdict = PyModule_GetDict(exmod);
574 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
575 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200576 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200577 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
578 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
579 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
580 Py_XINCREF(p3imp_PyExc_AttributeError);
581 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200582 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200583 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
584 Py_XINCREF(p3imp_PyExc_TypeError);
585 Py_XINCREF(p3imp_PyExc_ValueError);
586 Py_XDECREF(exmod);
587}
588#endif /* DYNAMIC_PYTHON3 */
589
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200590static PyObject *BufferNew (buf_T *);
591static PyObject *WindowNew(win_T *);
592static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100593static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200594
Bram Moolenaardb913952012-06-29 12:54:53 +0200595static int py3initialised = 0;
596
597#define PYINITIALISED py3initialised
598
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200599#define DICTKEY_DECL PyObject *bytes = NULL;
600
Bram Moolenaardb913952012-06-29 12:54:53 +0200601#define DICTKEY_GET(err) \
602 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200603 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200604 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200605 return err; \
606 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200607 else if (PyUnicode_Check(keyObject)) \
608 { \
609 bytes = PyString_AsBytes(keyObject); \
610 if (bytes == NULL) \
611 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200612 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200613 return err; \
614 } \
615 else \
616 { \
617 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
618 return err; \
619 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200620
Bram Moolenaardb913952012-06-29 12:54:53 +0200621#define DICTKEY_UNREF \
622 if (bytes != NULL) \
623 Py_XDECREF(bytes);
624
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200625#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaardb913952012-06-29 12:54:53 +0200626
Bram Moolenaar971db462013-05-12 18:44:48 +0200627#define WIN_PYTHON_REF(win) win->w_python3_ref
628#define BUF_PYTHON_REF(buf) buf->b_python3_ref
629
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200630 static void
631call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200632{
633#ifdef Py_DEBUG
634 _PyObject_DebugFree(p);
635#else
636 PyObject_Free(p);
637#endif
638}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200639
640 static PyObject *
641call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200642{
643 return PyType_GenericNew(type,args,kwds);
644}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200645
646 static PyObject *
647call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200648{
649 return PyType_GenericAlloc(type,nitems);
650}
651
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200652static PyObject *OutputGetattro(PyObject *, PyObject *);
653static int OutputSetattro(PyObject *, PyObject *, PyObject *);
654static PyObject *BufferGetattro(PyObject *, PyObject *);
655static PyObject *WindowGetattro(PyObject *, PyObject *);
656static int WindowSetattro(PyObject *, PyObject *, PyObject *);
657static PyObject *RangeGetattro(PyObject *, PyObject *);
658static PyObject *CurrentGetattro(PyObject *, PyObject *);
659static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
660static PyObject *DictionaryGetattro(PyObject *, PyObject *);
661static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
662static PyObject *ListGetattro(PyObject *, PyObject *);
663static int ListSetattro(PyObject *, PyObject *, PyObject *);
664static PyObject *FunctionGetattro(PyObject *, PyObject *);
665
666static struct PyModuleDef vimmodule;
667
668/*
669 * Include the code shared with if_python.c
670 */
671#include "if_py_both.h"
672
673#define GET_ATTR_STRING(name, nameobj) \
674 char *name = ""; \
675 if (PyUnicode_Check(nameobj)) \
676 name = _PyUnicode_AsString(nameobj)
677
678#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
679
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200680/******************************************************
681 * Internal function prototypes.
682 */
683
Bram Moolenaardb913952012-06-29 12:54:53 +0200684static PyObject *globals;
685
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200686static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100687static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200688
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689/******************************************************
690 * 1. Python interpreter main program.
691 */
692
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200693static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
694
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200695 void
696python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200697{
698 static int recurse = 0;
699
700 /* If a crash occurs while doing this, don't try again. */
701 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200702 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200703
704 ++recurse;
705
706#ifdef DYNAMIC_PYTHON3
707 if (hinstPy3)
708#endif
709 if (Py_IsInitialized())
710 {
711 // acquire lock before finalizing
712 pygilstate = PyGILState_Ensure();
713
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200714 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200715 }
716
717#ifdef DYNAMIC_PYTHON3
718 end_dynamic_python3();
719#endif
720
721 --recurse;
722}
723
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200724#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
725 int
726python3_loaded()
727{
728 return (hinstPy3 != 0);
729}
730#endif
731
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200732 static int
733Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200734{
735 if (!py3initialised)
736 {
737#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200738 if (!python3_enabled(TRUE))
739 {
740 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
741 goto fail;
742 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200743#endif
744
745 init_structs();
746
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100747
748#ifdef PYTHON3_HOME
749 Py_SetPythonHome(PYTHON3_HOME);
750#endif
751
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200752 PyImport_AppendInittab("vim", Py3Init_vim);
753
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200754#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200755 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200756#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200757 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200758#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100759 /* Initialise threads, and below save the state using
760 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
761 * specific state (such as the system trace hook), will be lost
762 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200763 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200764#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200765 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766#endif
767
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200768 if (PythonIO_Init())
769 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200770
Bram Moolenaardb913952012-06-29 12:54:53 +0200771 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
772
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 /* Remove the element from sys.path that was added because of our
774 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200775 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200776 * the current directory in sys.path.
777 * Only after vim has been imported, the element does exist in
778 * sys.path.
779 */
780 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 +0200781
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100782 /* lock is created and acquired in PyEval_InitThreads() and thread
783 * state is created in Py_Initialize()
784 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
785 * (python must have threads enabled!)
786 * so the following does both: unlock GIL and save thread state in TLS
787 * without deleting thread state
788 */
789 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200791 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200792 }
793
794 return 0;
795
796fail:
797 /* We call PythonIO_Flush() here to print any Python errors.
798 * This is OK, as it is possible to call this function even
799 * if PythonIO_Init() has not completed successfully (it will
800 * not do anything in this case).
801 */
802 PythonIO_Flush();
803 return -1;
804}
805
806/*
807 * External interface
808 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200809 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200810DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200811{
812#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200813 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200814#endif
815#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200816 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200817#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200818 PyObject *cmdstr;
819 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200820
821#if defined(MACOS) && !defined(MACOS_X_UNIX)
822 GetPort(&oldPort);
823 /* Check if the Python library is available */
824 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200825 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200826#endif
827 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200828 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200829
Bram Moolenaardb913952012-06-29 12:54:53 +0200830 if (rettv == NULL)
831 {
832 RangeStart = eap->line1;
833 RangeEnd = eap->line2;
834 }
835 else
836 {
837 RangeStart = (PyInt) curwin->w_cursor.lnum;
838 RangeEnd = RangeStart;
839 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200840 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841
842#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
843 /* Python only works properly when the LC_NUMERIC locale is "C". */
844 saved_locale = setlocale(LC_NUMERIC, NULL);
845 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200846 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200847 else
848 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200849 /* Need to make a copy, value may change when setting new locale. */
850 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
851 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852 }
853#endif
854
855 pygilstate = PyGILState_Ensure();
856
Bram Moolenaar19e60942011-06-19 00:27:51 +0200857 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
858 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200859 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
860 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
861 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200862 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200863 if (rettv == NULL)
864 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
865 else
866 {
867 PyObject *r;
868
869 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
870 globals, globals);
871 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100872 {
873 if (PyErr_Occurred() && !msg_silent)
874 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100876 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200877 else
878 {
879 if (ConvertFromPyObject(r, rettv) == -1)
880 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
881 Py_DECREF(r);
882 }
883 PyErr_Clear();
884 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200885 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200886
887 PyGILState_Release(pygilstate);
888
889#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
890 if (saved_locale != NULL)
891 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200892 (void)setlocale(LC_NUMERIC, saved_locale);
893 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200894 }
895#endif
896
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200897 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200898 PythonIO_Flush();
899#if defined(MACOS) && !defined(MACOS_X_UNIX)
900 SetPort(oldPort);
901#endif
902
903theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200904 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200905}
906
907/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200908 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200909 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200910 void
911ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200912{
913 char_u *script;
914
915 script = script_get(eap, eap->arg);
916 if (!eap->skip)
917 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200918 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200919 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200920 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200921 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200922 }
923 vim_free(script);
924}
925
926#define BUFFER_SIZE 2048
927
928/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200929 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930 */
931 void
932ex_py3file(exarg_T *eap)
933{
934 static char buffer[BUFFER_SIZE];
935 const char *file;
936 char *p;
937 int i;
938
939 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
940 * stdio file pointer, but Vim and the Python DLL are compiled with
941 * different options under Windows, meaning that stdio pointers aren't
942 * compatible between the two. Yuk.
943 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200944 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
945 *
946 * Using bytes so that Python can detect the source encoding as it normally
947 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200948 *
949 * We need to escape any backslashes or single quotes in the file name, so that
950 * Python won't mangle the file name.
951 */
952
953 strcpy(buffer, "exec(compile(open('");
954 p = buffer + 19; /* size of "exec(compile(open('" */
955
956 for (i=0; i<2; ++i)
957 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200958 file = (char *)eap->arg;
959 while (*file && p < buffer + (BUFFER_SIZE - 3))
960 {
961 if (*file == '\\' || *file == '\'')
962 *p++ = '\\';
963 *p++ = *file++;
964 }
965 /* If we didn't finish the file name, we hit a buffer overflow */
966 if (*file != '\0')
967 return;
968 if (i==0)
969 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200970 strcpy(p,"','rb').read(),'");
971 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200972 }
973 else
974 {
975 strcpy(p,"','exec'))");
976 p += 10;
977 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200978 }
979
980
981 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200982 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983}
984
985/******************************************************
986 * 2. Python output stream: writes output via [e]msg().
987 */
988
989/* Implementation functions
990 */
991
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200992 static PyObject *
993OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994{
Bram Moolenaar77045652012-09-21 13:46:06 +0200995 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996
997 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200998 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999
1000 return PyObject_GenericGetAttr(self, nameobj);
1001}
1002
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001003 static int
1004OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005{
Bram Moolenaar77045652012-09-21 13:46:06 +02001006 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001007
Bram Moolenaar77045652012-09-21 13:46:06 +02001008 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001009}
1010
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001011/***************/
1012
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001013 static int
1014PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015{
1016 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001017 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001018}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001019
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020/******************************************************
1021 * 3. Implementation of the Vim module for Python
1022 */
1023
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001024/* Window type - Implementation functions
1025 * --------------------------------------
1026 */
1027
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1029
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001030/* Buffer type - Implementation functions
1031 * --------------------------------------
1032 */
1033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001034#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1035
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001036static Py_ssize_t BufferLength(PyObject *);
1037static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001038static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1039static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001040
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001041
1042/* Line range type - Implementation functions
1043 * --------------------------------------
1044 */
1045
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001046#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1047
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001048static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001050static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001051
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001052/* Current objects type - Implementation functions
1053 * -----------------------------------------------
1054 */
1055
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001056static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001057 (lenfunc) BufferLength, /* sq_length, len(x) */
1058 (binaryfunc) 0, /* sq_concat, x+y */
1059 (ssizeargfunc) 0, /* sq_repeat, x*n */
1060 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1061 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001062 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001063 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001064 0, /* sq_contains */
1065 0, /* sq_inplace_concat */
1066 0, /* sq_inplace_repeat */
1067};
1068
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001069static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001070 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001071 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001072 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001073};
1074
1075
Bram Moolenaar971db462013-05-12 18:44:48 +02001076/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001077 */
1078
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001079 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001080BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001081{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001082 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001083
Bram Moolenaar77045652012-09-21 13:46:06 +02001084 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001085
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001086 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001087 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001088
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001089 r = BufferAttr((BufferObject *)(self), name);
1090 if (r || PyErr_Occurred())
1091 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001093 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001094}
1095
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001096 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001097BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1098{
1099 return Py_BuildValue("[sssss]", "name", "number",
1100 "append", "mark", "range");
1101}
1102
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103/******************/
1104
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001105 static PyObject *
1106BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001107{
Bram Moolenaardb913952012-06-29 12:54:53 +02001108 if (PyLong_Check(idx))
1109 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001110 long _idx = PyLong_AsLong(idx);
1111 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001112 } else if (PySlice_Check(idx))
1113 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001114 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001116 if (CheckBuffer((BufferObject *) self))
1117 return NULL;
1118
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001119 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001120 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001121 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001122 &step, &slicelen) < 0)
1123 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 return NULL;
1125 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001126 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001127 }
1128 else
1129 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1131 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001132 }
1133}
1134
Bram Moolenaar19e60942011-06-19 00:27:51 +02001135 static Py_ssize_t
1136BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1137{
Bram Moolenaardb913952012-06-29 12:54:53 +02001138 if (PyLong_Check(idx))
1139 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001140 long n = PyLong_AsLong(idx);
1141 return RBAsItem((BufferObject *)(self), n, val, 1,
1142 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1143 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001144 } else if (PySlice_Check(idx))
1145 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001146 Py_ssize_t start, stop, step, slicelen;
1147
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001148 if (CheckBuffer((BufferObject *) self))
1149 return -1;
1150
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001151 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001152 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001153 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001154 &step, &slicelen) < 0)
1155 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001156 return -1;
1157 }
1158 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1159 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1160 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001161 }
1162 else
1163 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001164 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1165 return -1;
1166 }
1167}
1168
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001169static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001170 (lenfunc) RangeLength, /* sq_length, len(x) */
1171 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1172 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1173 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1174 0, /* was_sq_slice, x[i:j] */
1175 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1176 0, /* sq_ass_slice, x[i:j]=v */
1177 0, /* sq_contains */
1178 0, /* sq_inplace_concat */
1179 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001180};
1181
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001182static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001183 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001184 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001185 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186};
1187
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188/* Line range object - Implementation
1189 */
1190
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001191 static PyObject *
1192RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001193{
Bram Moolenaar77045652012-09-21 13:46:06 +02001194 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001195
1196 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001197 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001198 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001199 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001200 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001201 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001202}
1203
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204/****************/
1205
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001206 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001207RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001208{
1209 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001210 ((RangeObject *)(self))->start,
1211 ((RangeObject *)(self))->end,
1212 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213}
1214
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001215 static Py_ssize_t
1216RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1217{
1218 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1219 ((RangeObject *)(self))->start,
1220 ((RangeObject *)(self))->end,
1221 &((RangeObject *)(self))->end);
1222}
1223
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001224 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001225RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001226{
Bram Moolenaardb913952012-06-29 12:54:53 +02001227 if (PyLong_Check(idx))
1228 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001229 long _idx = PyLong_AsLong(idx);
1230 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001231 } else if (PySlice_Check(idx))
1232 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001233 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001234
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001235 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1237 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001238 &step, &slicelen) < 0)
1239 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001240 return NULL;
1241 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001242 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001243 }
1244 else
1245 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1247 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001248 }
1249}
1250
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001251 static Py_ssize_t
1252RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1253{
Bram Moolenaardb913952012-06-29 12:54:53 +02001254 if (PyLong_Check(idx))
1255 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001256 long n = PyLong_AsLong(idx);
1257 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001258 } else if (PySlice_Check(idx))
1259 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001260 Py_ssize_t start, stop, step, slicelen;
1261
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001262 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001263 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1264 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001265 &step, &slicelen) < 0)
1266 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001267 return -1;
1268 }
1269 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001270 }
1271 else
1272 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001273 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1274 return -1;
1275 }
1276}
1277
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001278/* Window object - Implementation
1279 */
1280
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001281 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001282WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001283{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001284 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001285
Bram Moolenaar77045652012-09-21 13:46:06 +02001286 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001287
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001288 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001289 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001290
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001291 r = WindowAttr((WindowObject *)(self), name);
1292 if (r || PyErr_Occurred())
1293 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001294 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001295 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001296}
1297
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001298 static int
1299WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300{
Bram Moolenaar77045652012-09-21 13:46:06 +02001301 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001302
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001303 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001304}
1305
1306/* Window list object - Definitions
1307 */
1308
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001309static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001310 (lenfunc) WinListLength, /* sq_length, len(x) */
1311 (binaryfunc) 0, /* sq_concat, x+y */
1312 (ssizeargfunc) 0, /* sq_repeat, x*n */
1313 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1314 0, /* sq_slice, x[i:j] */
1315 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1316 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317 0, /* sq_contains */
1318 0, /* sq_inplace_concat */
1319 0, /* sq_inplace_repeat */
1320};
1321
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001322/* Current items object - Implementation
1323 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001324 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001325CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001326{
Bram Moolenaar77045652012-09-21 13:46:06 +02001327 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001328 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001329}
1330
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001331 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001332CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001334 GET_ATTR_STRING(name, nameobj);
1335 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001336}
1337
Bram Moolenaardb913952012-06-29 12:54:53 +02001338/* Dictionary object - Definitions
1339 */
1340
1341static PyInt DictionaryLength(PyObject *);
1342
Bram Moolenaar66b79852012-09-21 14:00:35 +02001343 static PyObject *
1344DictionaryGetattro(PyObject *self, PyObject *nameobj)
1345{
1346 DictionaryObject *this = ((DictionaryObject *) (self));
1347
1348 GET_ATTR_STRING(name, nameobj);
1349
1350 if (strcmp(name, "locked") == 0)
1351 return PyLong_FromLong(this->dict->dv_lock);
1352 else if (strcmp(name, "scope") == 0)
1353 return PyLong_FromLong(this->dict->dv_scope);
1354
1355 return PyObject_GenericGetAttr(self, nameobj);
1356}
1357
1358 static int
1359DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1360{
1361 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001362 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001363}
1364
1365/* List object - Definitions
1366 */
1367
1368static PyInt ListLength(PyObject *);
1369static PyObject *ListItem(PyObject *, Py_ssize_t);
1370
1371static PySequenceMethods ListAsSeq = {
1372 (lenfunc) ListLength, /* sq_length, len(x) */
1373 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1374 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1375 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1376 (void *) 0, /* was_sq_slice, x[i:j] */
1377 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1378 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1379 0, /* sq_contains */
1380 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1381 0, /* sq_inplace_repeat */
1382};
1383
1384static PyObject *ListSubscript(PyObject *, PyObject *);
1385static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1386
1387static PyMappingMethods ListAsMapping = {
1388 /* mp_length */ (lenfunc) ListLength,
1389 /* mp_subscript */ (binaryfunc) ListSubscript,
1390 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1391};
1392
Bram Moolenaardb913952012-06-29 12:54:53 +02001393 static PyObject *
1394ListSubscript(PyObject *self, PyObject* idxObject)
1395{
1396 if (PyLong_Check(idxObject))
1397 {
1398 long idx = PyLong_AsLong(idxObject);
1399 return ListItem(self, idx);
1400 }
1401 else if (PySlice_Check(idxObject))
1402 {
1403 Py_ssize_t start, stop, step, slicelen;
1404
1405 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1406 &step, &slicelen) < 0)
1407 return NULL;
1408 return ListSlice(self, start, stop);
1409 }
1410 else
1411 {
1412 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1413 return NULL;
1414 }
1415}
1416
1417 static Py_ssize_t
1418ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1419{
1420 if (PyLong_Check(idxObject))
1421 {
1422 long idx = PyLong_AsLong(idxObject);
1423 return ListAssItem(self, idx, obj);
1424 }
1425 else if (PySlice_Check(idxObject))
1426 {
1427 Py_ssize_t start, stop, step, slicelen;
1428
1429 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1430 &step, &slicelen) < 0)
1431 return -1;
1432 return ListAssSlice(self, start, stop, obj);
1433 }
1434 else
1435 {
1436 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1437 return -1;
1438 }
1439}
1440
Bram Moolenaar66b79852012-09-21 14:00:35 +02001441 static PyObject *
1442ListGetattro(PyObject *self, PyObject *nameobj)
1443{
1444 GET_ATTR_STRING(name, nameobj);
1445
1446 if (strcmp(name, "locked") == 0)
1447 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1448
1449 return PyObject_GenericGetAttr(self, nameobj);
1450}
1451
1452 static int
1453ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1454{
1455 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001456 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001457}
1458
1459/* Function object - Definitions
1460 */
1461
Bram Moolenaardb913952012-06-29 12:54:53 +02001462 static PyObject *
1463FunctionGetattro(PyObject *self, PyObject *nameobj)
1464{
1465 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001466
1467 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001468
1469 if (strcmp(name, "name") == 0)
1470 return PyUnicode_FromString((char *)(this->name));
1471
1472 return PyObject_GenericGetAttr(self, nameobj);
1473}
1474
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001475/* External interface
1476 */
1477
1478 void
1479python3_buffer_free(buf_T *buf)
1480{
Bram Moolenaar971db462013-05-12 18:44:48 +02001481 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001482 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001483 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001484 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001485 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001486 }
1487}
1488
1489#if defined(FEAT_WINDOWS) || defined(PROTO)
1490 void
1491python3_window_free(win_T *win)
1492{
Bram Moolenaar971db462013-05-12 18:44:48 +02001493 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001495 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001496 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001497 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498 }
1499}
1500#endif
1501
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001502static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001503{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001504 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505};
1506
1507static WinListObject TheWindowList =
1508{
1509 PyObject_HEAD_INIT(&WinListType)
1510};
1511
1512static CurrentObject TheCurrent =
1513{
1514 PyObject_HEAD_INIT(&CurrentType)
1515};
1516
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001517 static PyObject *
1518Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001519{
1520 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001521 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001522 /* The special value is removed from sys.path in Python3_Init(). */
1523 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1524
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001525 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001526 PyType_Ready(&BufferType);
1527 PyType_Ready(&RangeType);
1528 PyType_Ready(&WindowType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001529 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001530 PyType_Ready(&WinListType);
1531 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001532 PyType_Ready(&DictionaryType);
1533 PyType_Ready(&ListType);
1534 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001535 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001536
1537 /* Set sys.argv[] to avoid a crash in warn(). */
1538 PySys_SetArgv(1, argv);
1539
1540 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001541 if (mod == NULL)
1542 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001543
Bram Moolenaar19e60942011-06-19 00:27:51 +02001544 VimError = PyErr_NewException("vim.error", NULL, NULL);
1545 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001546
1547 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001548 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1549 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001550 Py_INCREF((PyObject *)(void *)&TheCurrent);
1551 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1552 Py_INCREF((PyObject *)(void *)&TheWindowList);
1553 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1554
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001555 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1556 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001557 PyModule_AddObject(mod, "options",
1558 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001559
Bram Moolenaar66b79852012-09-21 14:00:35 +02001560#define ADD_INT_CONSTANT(name, value) \
1561 tmp = PyLong_FromLong(value); \
1562 Py_INCREF(tmp); \
1563 PyModule_AddObject(mod, name, tmp)
1564
1565 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1566 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1567 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1568 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1569
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001570 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001571 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001572
1573 return mod;
1574}
1575
1576/*************************************************************************
1577 * 4. Utility functions for handling the interface between Vim and Python.
1578 */
1579
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001580/* Convert a Vim line into a Python string.
1581 * All internal newlines are replaced by null characters.
1582 *
1583 * On errors, the Python exception data is set, and NULL is returned.
1584 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001585 static PyObject *
1586LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587{
1588 PyObject *result;
1589 Py_ssize_t len = strlen(str);
1590 char *tmp,*p;
1591
1592 tmp = (char *)alloc((unsigned)(len+1));
1593 p = tmp;
1594 if (p == NULL)
1595 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001596 PyErr_NoMemory();
1597 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001598 }
1599
1600 while (*str)
1601 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001602 if (*str == '\n')
1603 *p = '\0';
1604 else
1605 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001606
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001607 ++p;
1608 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001609 }
1610 *p = '\0';
1611
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001612 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001613
1614 vim_free(tmp);
1615 return result;
1616}
1617
Bram Moolenaardb913952012-06-29 12:54:53 +02001618 void
1619do_py3eval (char_u *str, typval_T *rettv)
1620{
1621 DoPy3Command(NULL, (char *) str, rettv);
1622 switch(rettv->v_type)
1623 {
1624 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1625 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1626 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001627 case VAR_UNKNOWN:
1628 rettv->v_type = VAR_NUMBER;
1629 rettv->vval.v_number = 0;
1630 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001631 }
1632}
1633
1634 void
1635set_ref_in_python3 (int copyID)
1636{
1637 set_ref_in_py(copyID);
1638}