blob: daf517f77abbb4b2ed2376728d1e8006e8053214 [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
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200131# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200132# define PyEval_InitThreads py3_PyEval_InitThreads
133# define PyEval_RestoreThread py3_PyEval_RestoreThread
134# define PyEval_SaveThread py3_PyEval_SaveThread
135# define PyGILState_Ensure py3_PyGILState_Ensure
136# define PyGILState_Release py3_PyGILState_Release
137# define PyLong_AsLong py3_PyLong_AsLong
138# define PyLong_FromLong py3_PyLong_FromLong
139# define PyList_GetItem py3_PyList_GetItem
140# define PyList_Append py3_PyList_Append
141# define PyList_New py3_PyList_New
142# define PyList_SetItem py3_PyList_SetItem
143# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200144# define PySequence_Check py3_PySequence_Check
145# define PySequence_Size py3_PySequence_Size
146# define PySequence_GetItem py3_PySequence_GetItem
147# define PyTuple_Size py3_PyTuple_Size
148# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200149# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
150# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200151# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200152# define PyObject_Init py3__PyObject_Init
153# define PyDict_New py3_PyDict_New
154# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200155# define PyDict_Next py3_PyDict_Next
156# define PyMapping_Check py3_PyMapping_Check
157# define PyMapping_Items py3_PyMapping_Items
158# define PyIter_Next py3_PyIter_Next
159# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200160# define PyObject_IsTrue py3_PyObject_IsTrue
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200161# define PyModule_GetDict py3_PyModule_GetDict
162#undef PyRun_SimpleString
163# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200164#undef PyRun_String
165# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200166# define PySys_SetObject py3_PySys_SetObject
167# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200168# define PyType_Ready py3_PyType_Ready
169#undef Py_BuildValue
170# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100171# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200172# define Py_Initialize py3_Py_Initialize
173# define Py_Finalize py3_Py_Finalize
174# define Py_IsInitialized py3_Py_IsInitialized
175# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200176# define _Py_FalseStruct (*py3__Py_FalseStruct)
177# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200178# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200179# define PyModule_AddObject py3_PyModule_AddObject
180# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200181# if PY_VERSION_HEX >= 0x030300f0
182# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200183# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200184# else
185# define _PyUnicode_AsString py3__PyUnicode_AsString
186# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200187# undef PyUnicode_AsEncodedString
188# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
189# undef PyBytes_AsString
190# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200191# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200192# undef PyBytes_FromString
193# define PyBytes_FromString py3_PyBytes_FromString
194# define PyFloat_FromDouble py3_PyFloat_FromDouble
195# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200197# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200198# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200199# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200200# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200201# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200202# ifdef Py_DEBUG
203# define _Py_NegativeRefcount py3__Py_NegativeRefcount
204# define _Py_RefTotal (*py3__Py_RefTotal)
205# define _Py_Dealloc py3__Py_Dealloc
206# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
207# define _PyObject_DebugFree py3__PyObject_DebugFree
208# else
209# define PyObject_Malloc py3_PyObject_Malloc
210# define PyObject_Free py3_PyObject_Free
211# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200212# define PyType_GenericAlloc py3_PyType_GenericAlloc
213# define PyType_GenericNew py3_PyType_GenericNew
214# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200215# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200216# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200217# undef PyUnicode_Decode
218# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200219# define PyType_IsSubtype py3_PyType_IsSubtype
220# define PyCapsule_New py3_PyCapsule_New
221# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200222
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200223# ifdef Py_DEBUG
224# undef PyObject_NEW
225# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200226( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200227 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200228# endif
229
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200230/*
231 * Pointers for dynamic link
232 */
233static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100234static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200235static void (*py3_Py_Initialize)(void);
236static PyObject* (*py3_PyList_New)(Py_ssize_t size);
237static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
238static void (*py3_PyGILState_Release)(PyGILState_STATE);
239static int (*py3_PySys_SetObject)(char *, PyObject *);
240static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
241static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200242static int (*py3_PySequence_Check)(PyObject *);
243static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
244static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
245static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
246static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
247static int (*py3_PyMapping_Check)(PyObject *);
248static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200249static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200250 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200251static PyObject* (*py3_PyErr_NoMemory)(void);
252static void (*py3_Py_Finalize)(void);
253static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200254static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200255static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200256static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200257static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
258static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200259static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200260static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261static PyObject* (*py3_PyErr_Occurred)(void);
262static PyObject* (*py3_PyModule_GetDict)(PyObject *);
263static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
264static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200265static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200266static PyObject* (*py3_PyLong_FromLong)(long);
267static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200268static PyObject* (*py3_PyIter_Next)(PyObject *);
269static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200270static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271static PyObject* (*py3_Py_BuildValue)(char *, ...);
272static int (*py3_PyType_Ready)(PyTypeObject *type);
273static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
274static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200275static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
276 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277static long (*py3_PyLong_AsLong)(PyObject *);
278static void (*py3_PyErr_SetNone)(PyObject *);
279static void (*py3_PyEval_InitThreads)(void);
280static void(*py3_PyEval_RestoreThread)(PyThreadState *);
281static PyThreadState*(*py3_PyEval_SaveThread)(void);
282static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
283static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200284static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200285static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286static int (*py3_Py_IsInitialized)(void);
287static void (*py3_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100288static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200289static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200290static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200291static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200292static PyObject* py3__Py_FalseStruct;
293static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200294static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
295static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200296# if PY_VERSION_HEX >= 0x030300f0
297static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
298# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200299static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200300# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200301static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
302static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200303static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200304static PyObject* (*py3_PyBytes_FromString)(char *str);
305static PyObject* (*py3_PyFloat_FromDouble)(double num);
306static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200307static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
308static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
309static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
310static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200311static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200312static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200313static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200314static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200315static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200316static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
317static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200318# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200319 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
320 static Py_ssize_t* py3__Py_RefTotal;
321 static void (*py3__Py_Dealloc)(PyObject *obj);
322 static void (*py3__PyObject_DebugFree)(void*);
323 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200324# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200325 static void (*py3_PyObject_Free)(void*);
326 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200327# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200328static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200329
330static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
331
332/* Imported exception objects */
333static PyObject *p3imp_PyExc_AttributeError;
334static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200335static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200336static PyObject *p3imp_PyExc_KeyboardInterrupt;
337static PyObject *p3imp_PyExc_TypeError;
338static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200339static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200340
341# define PyExc_AttributeError p3imp_PyExc_AttributeError
342# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200343# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200344# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
345# define PyExc_TypeError p3imp_PyExc_TypeError
346# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200347# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200348
349/*
350 * Table of name to function pointer of python.
351 */
352# define PYTHON_PROC FARPROC
353static struct
354{
355 char *name;
356 PYTHON_PROC *ptr;
357} py3_funcname_table[] =
358{
359 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100360 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200361 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200362# ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200363 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200364 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200365# else
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200366 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
367 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200368# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200369 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200370 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200371 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
372 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
373 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
374 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
375 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
376 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200377 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
378 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
379 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
380 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
381 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200382 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
383 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
384 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
385 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200386 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200387 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200388 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200389 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
390 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200391 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200393 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
394 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
395 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
396 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200397 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
398 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
399 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
400 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
401 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200402 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200403 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
404 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200405 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
406 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
407 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
408 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
409 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
410 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
411 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
412 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200414 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200416 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
417 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200418 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100419 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200420 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
421 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
422 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200423# if PY_VERSION_HEX >= 0x030300f0
424 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
425# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200426 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200427# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200428 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200429 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200430 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
431 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
432 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200433 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
434 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
435 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
436 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200437 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200438 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200439 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200440 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200441 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200442# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200443 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
444 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
445 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
446 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
447 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200448# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200449 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
450 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200451# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
453 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
454 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200455 {"", NULL},
456};
457
458/*
459 * Free python.dll
460 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200461 static void
462end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200464 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200465 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200466 close_dll(hinstPy3);
467 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200468 }
469}
470
471/*
472 * Load library and get all pointers.
473 * Parameter 'libname' provides name of DLL.
474 * Return OK or FAIL.
475 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200476 static int
477py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200478{
479 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200480 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100482# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200483 /* Can't have Python and Python3 loaded at the same time.
484 * It cause a crash, because RTLD_GLOBAL is needed for
485 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200486 if (python_loaded())
487 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200488 if (verbose)
489 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200490 return FAIL;
491 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200492# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200493
494 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200495 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200496 hinstPy3 = load_dll(libname);
497
498 if (!hinstPy3)
499 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200500 if (verbose)
501 EMSG2(_(e_loadlib), libname);
502 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200503 }
504
505 for (i = 0; py3_funcname_table[i].ptr; ++i)
506 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200507 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
508 py3_funcname_table[i].name)) == NULL)
509 {
510 close_dll(hinstPy3);
511 hinstPy3 = 0;
512 if (verbose)
513 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
514 return FAIL;
515 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516 }
517
Bram Moolenaar69154f22010-07-18 21:42:34 +0200518 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
519 * will be present in the library. */
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200520# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200521 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
522 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
523 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
524 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200525# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200527 ucs_decode = symbol_from_dll(hinstPy3,
528 "PyUnicodeUCS2_Decode");
529 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
530 "PyUnicodeUCS2_AsEncodedString");
531 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200533 ucs_from_string = symbol_from_dll(hinstPy3,
534 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200535 ucs_decode = symbol_from_dll(hinstPy3,
536 "PyUnicodeUCS4_Decode");
537 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
538 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200539 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200540# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200541 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200543 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200544 py3_PyUnicode_Decode = ucs_decode;
545 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200546 }
547 else
548 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200549 close_dll(hinstPy3);
550 hinstPy3 = 0;
551 if (verbose)
552 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
553 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200554 }
555
556 return OK;
557}
558
559/*
560 * If python is enabled (there is installed python on Windows system) return
561 * TRUE, else FALSE.
562 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200563 int
564python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200565{
566 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
567}
568
569/* Load the standard Python exceptions - don't import the symbols from the
570 * DLL, as this can cause errors (importing data symbols is not reliable).
571 */
572static void get_py3_exceptions __ARGS((void));
573
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200574 static void
575get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200576{
577 PyObject *exmod = PyImport_ImportModule("builtins");
578 PyObject *exdict = PyModule_GetDict(exmod);
579 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
580 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200581 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200582 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
583 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
584 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200585 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200586 Py_XINCREF(p3imp_PyExc_AttributeError);
587 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200588 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200589 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
590 Py_XINCREF(p3imp_PyExc_TypeError);
591 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200592 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200593 Py_XDECREF(exmod);
594}
595#endif /* DYNAMIC_PYTHON3 */
596
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597static PyObject *BufferNew (buf_T *);
598static PyObject *WindowNew(win_T *);
599static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100600static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601
Bram Moolenaardb913952012-06-29 12:54:53 +0200602static int py3initialised = 0;
603
604#define PYINITIALISED py3initialised
605
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200606#define DICTKEY_DECL PyObject *bytes = NULL;
607
Bram Moolenaardb913952012-06-29 12:54:53 +0200608#define DICTKEY_GET(err) \
609 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200610 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200611 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200612 return err; \
613 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200614 else if (PyUnicode_Check(keyObject)) \
615 { \
616 bytes = PyString_AsBytes(keyObject); \
617 if (bytes == NULL) \
618 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200619 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200620 return err; \
621 } \
622 else \
623 { \
624 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
625 return err; \
626 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200627
Bram Moolenaardb913952012-06-29 12:54:53 +0200628#define DICTKEY_UNREF \
629 if (bytes != NULL) \
630 Py_XDECREF(bytes);
631
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200632#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaardb913952012-06-29 12:54:53 +0200633
Bram Moolenaar971db462013-05-12 18:44:48 +0200634#define WIN_PYTHON_REF(win) win->w_python3_ref
635#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200636#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200637
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200638 static void
639call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200640{
641#ifdef Py_DEBUG
642 _PyObject_DebugFree(p);
643#else
644 PyObject_Free(p);
645#endif
646}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200647
648 static PyObject *
649call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200650{
651 return PyType_GenericNew(type,args,kwds);
652}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200653
654 static PyObject *
655call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200656{
657 return PyType_GenericAlloc(type,nitems);
658}
659
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200660static PyObject *OutputGetattro(PyObject *, PyObject *);
661static int OutputSetattro(PyObject *, PyObject *, PyObject *);
662static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200663static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200664static PyObject *WindowGetattro(PyObject *, PyObject *);
665static int WindowSetattro(PyObject *, PyObject *, PyObject *);
666static PyObject *RangeGetattro(PyObject *, PyObject *);
667static PyObject *CurrentGetattro(PyObject *, PyObject *);
668static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
669static PyObject *DictionaryGetattro(PyObject *, PyObject *);
670static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
671static PyObject *ListGetattro(PyObject *, PyObject *);
672static int ListSetattro(PyObject *, PyObject *, PyObject *);
673static PyObject *FunctionGetattro(PyObject *, PyObject *);
674
675static struct PyModuleDef vimmodule;
676
677/*
678 * Include the code shared with if_python.c
679 */
680#include "if_py_both.h"
681
682#define GET_ATTR_STRING(name, nameobj) \
683 char *name = ""; \
684 if (PyUnicode_Check(nameobj)) \
685 name = _PyUnicode_AsString(nameobj)
686
687#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
688
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200689/******************************************************
690 * Internal function prototypes.
691 */
692
Bram Moolenaardb913952012-06-29 12:54:53 +0200693static PyObject *globals;
694
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200695static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100696static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200697
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200698/******************************************************
699 * 1. Python interpreter main program.
700 */
701
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200702 void
703python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200704{
705 static int recurse = 0;
706
707 /* If a crash occurs while doing this, don't try again. */
708 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200709 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200710
711 ++recurse;
712
713#ifdef DYNAMIC_PYTHON3
714 if (hinstPy3)
715#endif
716 if (Py_IsInitialized())
717 {
718 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200719 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200720
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200721 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200722 }
723
724#ifdef DYNAMIC_PYTHON3
725 end_dynamic_python3();
726#endif
727
728 --recurse;
729}
730
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200731#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
732 int
733python3_loaded()
734{
735 return (hinstPy3 != 0);
736}
737#endif
738
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200739 static int
740Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200741{
742 if (!py3initialised)
743 {
744#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200745 if (!python3_enabled(TRUE))
746 {
747 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
748 goto fail;
749 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200750#endif
751
752 init_structs();
753
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100754
755#ifdef PYTHON3_HOME
756 Py_SetPythonHome(PYTHON3_HOME);
757#endif
758
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200759 PyImport_AppendInittab("vim", Py3Init_vim);
760
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200761#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200762 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200764 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200765#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100766 /* Initialise threads, and below save the state using
767 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
768 * specific state (such as the system trace hook), will be lost
769 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200770 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200771#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200772 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200773#endif
774
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200775 if (PythonIO_Init())
776 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200777
Bram Moolenaardb913952012-06-29 12:54:53 +0200778 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
779
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200780 /* Remove the element from sys.path that was added because of our
781 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200782 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200783 * the current directory in sys.path.
784 * Only after vim has been imported, the element does exist in
785 * sys.path.
786 */
787 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 +0200788
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100789 /* lock is created and acquired in PyEval_InitThreads() and thread
790 * state is created in Py_Initialize()
791 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
792 * (python must have threads enabled!)
793 * so the following does both: unlock GIL and save thread state in TLS
794 * without deleting thread state
795 */
796 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200797
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200798 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200799 }
800
801 return 0;
802
803fail:
804 /* We call PythonIO_Flush() here to print any Python errors.
805 * This is OK, as it is possible to call this function even
806 * if PythonIO_Init() has not completed successfully (it will
807 * not do anything in this case).
808 */
809 PythonIO_Flush();
810 return -1;
811}
812
813/*
814 * External interface
815 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200816 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200817DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200818{
819#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200820 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200821#endif
822#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200823 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200824#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200825 PyObject *cmdstr;
826 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +0200827 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200828
829#if defined(MACOS) && !defined(MACOS_X_UNIX)
830 GetPort(&oldPort);
831 /* Check if the Python library is available */
832 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200833 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200834#endif
835 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200836 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837
Bram Moolenaardb913952012-06-29 12:54:53 +0200838 if (rettv == NULL)
839 {
840 RangeStart = eap->line1;
841 RangeEnd = eap->line2;
842 }
843 else
844 {
845 RangeStart = (PyInt) curwin->w_cursor.lnum;
846 RangeEnd = RangeStart;
847 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200848 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200849
850#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
851 /* Python only works properly when the LC_NUMERIC locale is "C". */
852 saved_locale = setlocale(LC_NUMERIC, NULL);
853 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200854 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200855 else
856 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200857 /* Need to make a copy, value may change when setting new locale. */
858 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
859 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200860 }
861#endif
862
863 pygilstate = PyGILState_Ensure();
864
Bram Moolenaar19e60942011-06-19 00:27:51 +0200865 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
866 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200867 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
868 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
869 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200870 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200871 if (rettv == NULL)
872 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
873 else
874 {
875 PyObject *r;
876
877 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
878 globals, globals);
879 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100880 {
881 if (PyErr_Occurred() && !msg_silent)
882 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200883 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100884 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200885 else
886 {
887 if (ConvertFromPyObject(r, rettv) == -1)
888 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
889 Py_DECREF(r);
890 }
891 PyErr_Clear();
892 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200893 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200894
895 PyGILState_Release(pygilstate);
896
897#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
898 if (saved_locale != NULL)
899 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200900 (void)setlocale(LC_NUMERIC, saved_locale);
901 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200902 }
903#endif
904
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200905 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200906 PythonIO_Flush();
907#if defined(MACOS) && !defined(MACOS_X_UNIX)
908 SetPort(oldPort);
909#endif
910
911theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200912 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913}
914
915/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200916 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200918 void
919ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200920{
921 char_u *script;
922
923 script = script_get(eap, eap->arg);
924 if (!eap->skip)
925 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200926 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200928 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200929 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930 }
931 vim_free(script);
932}
933
934#define BUFFER_SIZE 2048
935
936/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200937 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200938 */
939 void
940ex_py3file(exarg_T *eap)
941{
942 static char buffer[BUFFER_SIZE];
943 const char *file;
944 char *p;
945 int i;
946
947 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
948 * stdio file pointer, but Vim and the Python DLL are compiled with
949 * different options under Windows, meaning that stdio pointers aren't
950 * compatible between the two. Yuk.
951 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200952 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
953 *
954 * Using bytes so that Python can detect the source encoding as it normally
955 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200956 *
957 * We need to escape any backslashes or single quotes in the file name, so that
958 * Python won't mangle the file name.
959 */
960
961 strcpy(buffer, "exec(compile(open('");
962 p = buffer + 19; /* size of "exec(compile(open('" */
963
964 for (i=0; i<2; ++i)
965 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200966 file = (char *)eap->arg;
967 while (*file && p < buffer + (BUFFER_SIZE - 3))
968 {
969 if (*file == '\\' || *file == '\'')
970 *p++ = '\\';
971 *p++ = *file++;
972 }
973 /* If we didn't finish the file name, we hit a buffer overflow */
974 if (*file != '\0')
975 return;
976 if (i==0)
977 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200978 strcpy(p,"','rb').read(),'");
979 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200980 }
981 else
982 {
983 strcpy(p,"','exec'))");
984 p += 10;
985 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986 }
987
988
989 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200990 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991}
992
993/******************************************************
994 * 2. Python output stream: writes output via [e]msg().
995 */
996
997/* Implementation functions
998 */
999
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001000 static PyObject *
1001OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002{
Bram Moolenaar77045652012-09-21 13:46:06 +02001003 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004
1005 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001006 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001007
1008 return PyObject_GenericGetAttr(self, nameobj);
1009}
1010
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001011 static int
1012OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001013{
Bram Moolenaar77045652012-09-21 13:46:06 +02001014 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001015
Bram Moolenaar77045652012-09-21 13:46:06 +02001016 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001017}
1018
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001019/***************/
1020
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001021 static int
1022PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001023{
1024 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001025 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001026}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001027
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028/******************************************************
1029 * 3. Implementation of the Vim module for Python
1030 */
1031
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001032/* Window type - Implementation functions
1033 * --------------------------------------
1034 */
1035
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001036#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1037
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001038/* Buffer type - Implementation functions
1039 * --------------------------------------
1040 */
1041
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001042#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001044static Py_ssize_t BufferLength(PyObject *);
1045static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001046static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1047static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001048
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049
1050/* Line range type - Implementation functions
1051 * --------------------------------------
1052 */
1053
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1055
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001056static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001057static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001058static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001059
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001060/* Current objects type - Implementation functions
1061 * -----------------------------------------------
1062 */
1063
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001064static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001065 (lenfunc) BufferLength, /* sq_length, len(x) */
1066 (binaryfunc) 0, /* sq_concat, x+y */
1067 (ssizeargfunc) 0, /* sq_repeat, x*n */
1068 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1069 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001070 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001071 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001072 0, /* sq_contains */
1073 0, /* sq_inplace_concat */
1074 0, /* sq_inplace_repeat */
1075};
1076
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001077static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001078 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001079 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001080 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001081};
1082
1083
Bram Moolenaar971db462013-05-12 18:44:48 +02001084/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001085 */
1086
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001087 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001088BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001090 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001091
Bram Moolenaar77045652012-09-21 13:46:06 +02001092 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001093
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001094 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001095 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001096
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001097 r = BufferAttr((BufferObject *)(self), name);
1098 if (r || PyErr_Occurred())
1099 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001100 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001101 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001102}
1103
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001104 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001105BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1106{
1107 return Py_BuildValue("[sssss]", "name", "number",
1108 "append", "mark", "range");
1109}
1110
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001111/******************/
1112
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001113 static PyObject *
1114BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115{
Bram Moolenaardb913952012-06-29 12:54:53 +02001116 if (PyLong_Check(idx))
1117 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001118 long _idx = PyLong_AsLong(idx);
1119 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001120 } else if (PySlice_Check(idx))
1121 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001122 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001123
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001124 if (CheckBuffer((BufferObject *) self))
1125 return NULL;
1126
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001127 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001128 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001129 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001130 &step, &slicelen) < 0)
1131 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001132 return NULL;
1133 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001134 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001135 }
1136 else
1137 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001138 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001139 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001140 }
1141}
1142
Bram Moolenaar19e60942011-06-19 00:27:51 +02001143 static Py_ssize_t
1144BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1145{
Bram Moolenaardb913952012-06-29 12:54:53 +02001146 if (PyLong_Check(idx))
1147 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001148 long n = PyLong_AsLong(idx);
1149 return RBAsItem((BufferObject *)(self), n, val, 1,
1150 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1151 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001152 } else if (PySlice_Check(idx))
1153 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001154 Py_ssize_t start, stop, step, slicelen;
1155
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001156 if (CheckBuffer((BufferObject *) self))
1157 return -1;
1158
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001159 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001160 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001161 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001162 &step, &slicelen) < 0)
1163 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001164 return -1;
1165 }
1166 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1167 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1168 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001169 }
1170 else
1171 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001172 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar19e60942011-06-19 00:27:51 +02001173 return -1;
1174 }
1175}
1176
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001177static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 (lenfunc) RangeLength, /* sq_length, len(x) */
1179 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1180 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1181 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1182 0, /* was_sq_slice, x[i:j] */
1183 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1184 0, /* sq_ass_slice, x[i:j]=v */
1185 0, /* sq_contains */
1186 0, /* sq_inplace_concat */
1187 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188};
1189
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001190static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001191 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001192 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001193 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001194};
1195
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196/* Line range object - Implementation
1197 */
1198
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001199 static PyObject *
1200RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201{
Bram Moolenaar77045652012-09-21 13:46:06 +02001202 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203
1204 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001205 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001206 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001207 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001208 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001209 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001210}
1211
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001212/****************/
1213
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001214 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001215RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001216{
1217 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001218 ((RangeObject *)(self))->start,
1219 ((RangeObject *)(self))->end,
1220 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001221}
1222
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001223 static Py_ssize_t
1224RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1225{
1226 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1227 ((RangeObject *)(self))->start,
1228 ((RangeObject *)(self))->end,
1229 &((RangeObject *)(self))->end);
1230}
1231
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001232 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001233RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001234{
Bram Moolenaardb913952012-06-29 12:54:53 +02001235 if (PyLong_Check(idx))
1236 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001237 long _idx = PyLong_AsLong(idx);
1238 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001239 } else if (PySlice_Check(idx))
1240 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001242
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001243 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001244 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1245 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001246 &step, &slicelen) < 0)
1247 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001248 return NULL;
1249 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001250 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001251 }
1252 else
1253 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001254 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001255 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256 }
1257}
1258
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001259 static Py_ssize_t
1260RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1261{
Bram Moolenaardb913952012-06-29 12:54:53 +02001262 if (PyLong_Check(idx))
1263 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001264 long n = PyLong_AsLong(idx);
1265 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001266 } else if (PySlice_Check(idx))
1267 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001268 Py_ssize_t start, stop, step, slicelen;
1269
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001270 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001271 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1272 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001273 &step, &slicelen) < 0)
1274 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001275 return -1;
1276 }
1277 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001278 }
1279 else
1280 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001281 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001282 return -1;
1283 }
1284}
1285
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001286/* TabPage object - Implementation
1287 */
1288
1289 static PyObject *
1290TabPageGetattro(PyObject *self, PyObject *nameobj)
1291{
1292 PyObject *r;
1293
1294 GET_ATTR_STRING(name, nameobj);
1295
1296 if (CheckTabPage((TabPageObject *)(self)))
1297 return NULL;
1298
1299 r = TabPageAttr((TabPageObject *)(self), name);
1300 if (r || PyErr_Occurred())
1301 return r;
1302 else
1303 return PyObject_GenericGetAttr(self, nameobj);
1304}
1305
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001306/* Window object - Implementation
1307 */
1308
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001309 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001310WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001311{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001312 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313
Bram Moolenaar77045652012-09-21 13:46:06 +02001314 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001316 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001317 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001318
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001319 r = WindowAttr((WindowObject *)(self), name);
1320 if (r || PyErr_Occurred())
1321 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001322 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001323 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001324}
1325
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001326 static int
1327WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328{
Bram Moolenaar77045652012-09-21 13:46:06 +02001329 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001330
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001331 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001332}
1333
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001334/* Tab page list object - Definitions
1335 */
1336
1337static PySequenceMethods TabListAsSeq = {
1338 (lenfunc) TabListLength, /* sq_length, len(x) */
1339 (binaryfunc) 0, /* sq_concat, x+y */
1340 (ssizeargfunc) 0, /* sq_repeat, x*n */
1341 (ssizeargfunc) TabListItem, /* sq_item, x[i] */
1342 0, /* sq_slice, x[i:j] */
1343 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1344 0, /* sq_ass_slice, x[i:j]=v */
1345 0, /* sq_contains */
1346 0, /* sq_inplace_concat */
1347 0, /* sq_inplace_repeat */
1348};
1349
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001350/* Window list object - Definitions
1351 */
1352
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001353static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001354 (lenfunc) WinListLength, /* sq_length, len(x) */
1355 (binaryfunc) 0, /* sq_concat, x+y */
1356 (ssizeargfunc) 0, /* sq_repeat, x*n */
1357 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1358 0, /* sq_slice, x[i:j] */
1359 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1360 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001361 0, /* sq_contains */
1362 0, /* sq_inplace_concat */
1363 0, /* sq_inplace_repeat */
1364};
1365
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001366/* Current items object - Implementation
1367 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001368 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001369CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001370{
Bram Moolenaar77045652012-09-21 13:46:06 +02001371 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001372 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001373}
1374
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001375 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001376CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001377{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001378 GET_ATTR_STRING(name, nameobj);
1379 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001380}
1381
Bram Moolenaardb913952012-06-29 12:54:53 +02001382/* Dictionary object - Definitions
1383 */
1384
1385static PyInt DictionaryLength(PyObject *);
1386
Bram Moolenaar66b79852012-09-21 14:00:35 +02001387 static PyObject *
1388DictionaryGetattro(PyObject *self, PyObject *nameobj)
1389{
1390 DictionaryObject *this = ((DictionaryObject *) (self));
1391
1392 GET_ATTR_STRING(name, nameobj);
1393
1394 if (strcmp(name, "locked") == 0)
1395 return PyLong_FromLong(this->dict->dv_lock);
1396 else if (strcmp(name, "scope") == 0)
1397 return PyLong_FromLong(this->dict->dv_scope);
1398
1399 return PyObject_GenericGetAttr(self, nameobj);
1400}
1401
1402 static int
1403DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1404{
1405 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001406 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001407}
1408
1409/* List object - Definitions
1410 */
1411
1412static PyInt ListLength(PyObject *);
1413static PyObject *ListItem(PyObject *, Py_ssize_t);
1414
1415static PySequenceMethods ListAsSeq = {
1416 (lenfunc) ListLength, /* sq_length, len(x) */
1417 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1418 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1419 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1420 (void *) 0, /* was_sq_slice, x[i:j] */
1421 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1422 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1423 0, /* sq_contains */
1424 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1425 0, /* sq_inplace_repeat */
1426};
1427
1428static PyObject *ListSubscript(PyObject *, PyObject *);
1429static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1430
1431static PyMappingMethods ListAsMapping = {
1432 /* mp_length */ (lenfunc) ListLength,
1433 /* mp_subscript */ (binaryfunc) ListSubscript,
1434 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1435};
1436
Bram Moolenaardb913952012-06-29 12:54:53 +02001437 static PyObject *
1438ListSubscript(PyObject *self, PyObject* idxObject)
1439{
1440 if (PyLong_Check(idxObject))
1441 {
1442 long idx = PyLong_AsLong(idxObject);
1443 return ListItem(self, idx);
1444 }
1445 else if (PySlice_Check(idxObject))
1446 {
1447 Py_ssize_t start, stop, step, slicelen;
1448
1449 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1450 &step, &slicelen) < 0)
1451 return NULL;
1452 return ListSlice(self, start, stop);
1453 }
1454 else
1455 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001456 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001457 return NULL;
1458 }
1459}
1460
1461 static Py_ssize_t
1462ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1463{
1464 if (PyLong_Check(idxObject))
1465 {
1466 long idx = PyLong_AsLong(idxObject);
1467 return ListAssItem(self, idx, obj);
1468 }
1469 else if (PySlice_Check(idxObject))
1470 {
1471 Py_ssize_t start, stop, step, slicelen;
1472
1473 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1474 &step, &slicelen) < 0)
1475 return -1;
1476 return ListAssSlice(self, start, stop, obj);
1477 }
1478 else
1479 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001480 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001481 return -1;
1482 }
1483}
1484
Bram Moolenaar66b79852012-09-21 14:00:35 +02001485 static PyObject *
1486ListGetattro(PyObject *self, PyObject *nameobj)
1487{
1488 GET_ATTR_STRING(name, nameobj);
1489
1490 if (strcmp(name, "locked") == 0)
1491 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1492
1493 return PyObject_GenericGetAttr(self, nameobj);
1494}
1495
1496 static int
1497ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1498{
1499 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001500 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001501}
1502
1503/* Function object - Definitions
1504 */
1505
Bram Moolenaardb913952012-06-29 12:54:53 +02001506 static PyObject *
1507FunctionGetattro(PyObject *self, PyObject *nameobj)
1508{
1509 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001510
1511 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001512
1513 if (strcmp(name, "name") == 0)
1514 return PyUnicode_FromString((char *)(this->name));
1515
1516 return PyObject_GenericGetAttr(self, nameobj);
1517}
1518
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001519/* External interface
1520 */
1521
1522 void
1523python3_buffer_free(buf_T *buf)
1524{
Bram Moolenaar971db462013-05-12 18:44:48 +02001525 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001526 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001527 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001529 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001530 }
1531}
1532
1533#if defined(FEAT_WINDOWS) || defined(PROTO)
1534 void
1535python3_window_free(win_T *win)
1536{
Bram Moolenaar971db462013-05-12 18:44:48 +02001537 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001538 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001539 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001540 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001541 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001542 }
1543}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001544
1545 void
1546python3_tabpage_free(tabpage_T *tab)
1547{
1548 if (TAB_PYTHON_REF(tab) != NULL)
1549 {
1550 TabPageObject *tp = TAB_PYTHON_REF(tab);
1551 tp->tab = INVALID_TABPAGE_VALUE;
1552 TAB_PYTHON_REF(tab) = NULL;
1553 }
1554}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001555#endif
1556
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001557static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001558{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001559 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001560};
1561
1562static WinListObject TheWindowList =
1563{
1564 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001565 NULL
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001566};
1567
1568static CurrentObject TheCurrent =
1569{
1570 PyObject_HEAD_INIT(&CurrentType)
1571};
1572
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001573static TabListObject TheTabPageList =
1574{
1575 PyObject_HEAD_INIT(&TabListType)
1576};
1577
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001578 static PyObject *
1579Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001580{
1581 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001582 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001583 /* The special value is removed from sys.path in Python3_Init(). */
1584 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1585
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001586 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587 PyType_Ready(&BufferType);
1588 PyType_Ready(&RangeType);
1589 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001590 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001591 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001592 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001593 PyType_Ready(&TabListType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001595 PyType_Ready(&DictionaryType);
1596 PyType_Ready(&ListType);
1597 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001598 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001599
1600 /* Set sys.argv[] to avoid a crash in warn(). */
1601 PySys_SetArgv(1, argv);
1602
1603 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001604 if (mod == NULL)
1605 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001606
Bram Moolenaar19e60942011-06-19 00:27:51 +02001607 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001608
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001609 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001611 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1612 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001613 Py_INCREF((PyObject *)(void *)&TheCurrent);
1614 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1615 Py_INCREF((PyObject *)(void *)&TheWindowList);
1616 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001617 Py_INCREF((PyObject *)(void *)&TheTabPageList);
1618 PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001619
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001620 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1621 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001622 PyModule_AddObject(mod, "options",
1623 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001624
Bram Moolenaar66b79852012-09-21 14:00:35 +02001625#define ADD_INT_CONSTANT(name, value) \
1626 tmp = PyLong_FromLong(value); \
1627 Py_INCREF(tmp); \
1628 PyModule_AddObject(mod, name, tmp)
1629
1630 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1631 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1632 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1633 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1634
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001635 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001636 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001637
1638 return mod;
1639}
1640
1641/*************************************************************************
1642 * 4. Utility functions for handling the interface between Vim and Python.
1643 */
1644
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001645/* Convert a Vim line into a Python string.
1646 * All internal newlines are replaced by null characters.
1647 *
1648 * On errors, the Python exception data is set, and NULL is returned.
1649 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001650 static PyObject *
1651LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001652{
1653 PyObject *result;
1654 Py_ssize_t len = strlen(str);
1655 char *tmp,*p;
1656
1657 tmp = (char *)alloc((unsigned)(len+1));
1658 p = tmp;
1659 if (p == NULL)
1660 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001661 PyErr_NoMemory();
1662 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001663 }
1664
1665 while (*str)
1666 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001667 if (*str == '\n')
1668 *p = '\0';
1669 else
1670 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001671
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001672 ++p;
1673 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001674 }
1675 *p = '\0';
1676
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001677 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001678
1679 vim_free(tmp);
1680 return result;
1681}
1682
Bram Moolenaardb913952012-06-29 12:54:53 +02001683 void
1684do_py3eval (char_u *str, typval_T *rettv)
1685{
1686 DoPy3Command(NULL, (char *) str, rettv);
1687 switch(rettv->v_type)
1688 {
1689 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1690 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1691 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001692 case VAR_UNKNOWN:
1693 rettv->v_type = VAR_NUMBER;
1694 rettv->vval.v_number = 0;
1695 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001696 }
1697}
1698
1699 void
1700set_ref_in_python3 (int copyID)
1701{
1702 set_ref_in_py(copyID);
1703}