blob: 02da1b0c47162e800b440a5f3e89ee24a29dcd8c [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 Moolenaarbd5e15f2010-07-17 21:19:38 +0200702static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
703
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200704 void
705python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200706{
707 static int recurse = 0;
708
709 /* If a crash occurs while doing this, don't try again. */
710 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200711 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200712
713 ++recurse;
714
715#ifdef DYNAMIC_PYTHON3
716 if (hinstPy3)
717#endif
718 if (Py_IsInitialized())
719 {
720 // acquire lock before finalizing
721 pygilstate = PyGILState_Ensure();
722
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200723 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200724 }
725
726#ifdef DYNAMIC_PYTHON3
727 end_dynamic_python3();
728#endif
729
730 --recurse;
731}
732
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200733#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
734 int
735python3_loaded()
736{
737 return (hinstPy3 != 0);
738}
739#endif
740
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200741 static int
742Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200743{
744 if (!py3initialised)
745 {
746#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200747 if (!python3_enabled(TRUE))
748 {
749 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
750 goto fail;
751 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200752#endif
753
754 init_structs();
755
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100756
757#ifdef PYTHON3_HOME
758 Py_SetPythonHome(PYTHON3_HOME);
759#endif
760
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200761 PyImport_AppendInittab("vim", Py3Init_vim);
762
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200764 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200765#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200766 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200767#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100768 /* Initialise threads, and below save the state using
769 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
770 * specific state (such as the system trace hook), will be lost
771 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200772 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200773#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200774 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200775#endif
776
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200777 if (PythonIO_Init())
778 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200779
Bram Moolenaardb913952012-06-29 12:54:53 +0200780 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
781
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200782 /* Remove the element from sys.path that was added because of our
783 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200784 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200785 * the current directory in sys.path.
786 * Only after vim has been imported, the element does exist in
787 * sys.path.
788 */
789 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 +0200790
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100791 /* lock is created and acquired in PyEval_InitThreads() and thread
792 * state is created in Py_Initialize()
793 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
794 * (python must have threads enabled!)
795 * so the following does both: unlock GIL and save thread state in TLS
796 * without deleting thread state
797 */
798 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200799
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200800 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200801 }
802
803 return 0;
804
805fail:
806 /* We call PythonIO_Flush() here to print any Python errors.
807 * This is OK, as it is possible to call this function even
808 * if PythonIO_Init() has not completed successfully (it will
809 * not do anything in this case).
810 */
811 PythonIO_Flush();
812 return -1;
813}
814
815/*
816 * External interface
817 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200818 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200819DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200820{
821#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200822 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200823#endif
824#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200825 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200826#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200827 PyObject *cmdstr;
828 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200829
830#if defined(MACOS) && !defined(MACOS_X_UNIX)
831 GetPort(&oldPort);
832 /* Check if the Python library is available */
833 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200834 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200835#endif
836 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200837 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200838
Bram Moolenaardb913952012-06-29 12:54:53 +0200839 if (rettv == NULL)
840 {
841 RangeStart = eap->line1;
842 RangeEnd = eap->line2;
843 }
844 else
845 {
846 RangeStart = (PyInt) curwin->w_cursor.lnum;
847 RangeEnd = RangeStart;
848 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200849 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200850
851#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
852 /* Python only works properly when the LC_NUMERIC locale is "C". */
853 saved_locale = setlocale(LC_NUMERIC, NULL);
854 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200855 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200856 else
857 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200858 /* Need to make a copy, value may change when setting new locale. */
859 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
860 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200861 }
862#endif
863
864 pygilstate = PyGILState_Ensure();
865
Bram Moolenaar19e60942011-06-19 00:27:51 +0200866 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
867 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200868 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
869 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
870 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200871 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200872 if (rettv == NULL)
873 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
874 else
875 {
876 PyObject *r;
877
878 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
879 globals, globals);
880 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100881 {
882 if (PyErr_Occurred() && !msg_silent)
883 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200884 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100885 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200886 else
887 {
888 if (ConvertFromPyObject(r, rettv) == -1)
889 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
890 Py_DECREF(r);
891 }
892 PyErr_Clear();
893 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200894 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200895
896 PyGILState_Release(pygilstate);
897
898#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
899 if (saved_locale != NULL)
900 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200901 (void)setlocale(LC_NUMERIC, saved_locale);
902 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903 }
904#endif
905
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200906 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200907 PythonIO_Flush();
908#if defined(MACOS) && !defined(MACOS_X_UNIX)
909 SetPort(oldPort);
910#endif
911
912theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200913 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200914}
915
916/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200917 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200918 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200919 void
920ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921{
922 char_u *script;
923
924 script = script_get(eap, eap->arg);
925 if (!eap->skip)
926 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200927 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200929 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200930 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200931 }
932 vim_free(script);
933}
934
935#define BUFFER_SIZE 2048
936
937/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200938 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939 */
940 void
941ex_py3file(exarg_T *eap)
942{
943 static char buffer[BUFFER_SIZE];
944 const char *file;
945 char *p;
946 int i;
947
948 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
949 * stdio file pointer, but Vim and the Python DLL are compiled with
950 * different options under Windows, meaning that stdio pointers aren't
951 * compatible between the two. Yuk.
952 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200953 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
954 *
955 * Using bytes so that Python can detect the source encoding as it normally
956 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200957 *
958 * We need to escape any backslashes or single quotes in the file name, so that
959 * Python won't mangle the file name.
960 */
961
962 strcpy(buffer, "exec(compile(open('");
963 p = buffer + 19; /* size of "exec(compile(open('" */
964
965 for (i=0; i<2; ++i)
966 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200967 file = (char *)eap->arg;
968 while (*file && p < buffer + (BUFFER_SIZE - 3))
969 {
970 if (*file == '\\' || *file == '\'')
971 *p++ = '\\';
972 *p++ = *file++;
973 }
974 /* If we didn't finish the file name, we hit a buffer overflow */
975 if (*file != '\0')
976 return;
977 if (i==0)
978 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200979 strcpy(p,"','rb').read(),'");
980 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200981 }
982 else
983 {
984 strcpy(p,"','exec'))");
985 p += 10;
986 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200987 }
988
989
990 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200991 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200992}
993
994/******************************************************
995 * 2. Python output stream: writes output via [e]msg().
996 */
997
998/* Implementation functions
999 */
1000
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001001 static PyObject *
1002OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001003{
Bram Moolenaar77045652012-09-21 13:46:06 +02001004 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001005
1006 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001007 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001008
1009 return PyObject_GenericGetAttr(self, nameobj);
1010}
1011
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001012 static int
1013OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014{
Bram Moolenaar77045652012-09-21 13:46:06 +02001015 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016
Bram Moolenaar77045652012-09-21 13:46:06 +02001017 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001018}
1019
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020/***************/
1021
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001022 static int
1023PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001024{
1025 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001026 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001027}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001029/******************************************************
1030 * 3. Implementation of the Vim module for Python
1031 */
1032
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001033/* Window type - Implementation functions
1034 * --------------------------------------
1035 */
1036
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001037#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1038
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039/* Buffer type - Implementation functions
1040 * --------------------------------------
1041 */
1042
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1044
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001045static Py_ssize_t BufferLength(PyObject *);
1046static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001047static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1048static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001050
1051/* Line range type - Implementation functions
1052 * --------------------------------------
1053 */
1054
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001055#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1056
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001057static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001058static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001059static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001060
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001061/* Current objects type - Implementation functions
1062 * -----------------------------------------------
1063 */
1064
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001065static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001066 (lenfunc) BufferLength, /* sq_length, len(x) */
1067 (binaryfunc) 0, /* sq_concat, x+y */
1068 (ssizeargfunc) 0, /* sq_repeat, x*n */
1069 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1070 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001071 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001072 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001073 0, /* sq_contains */
1074 0, /* sq_inplace_concat */
1075 0, /* sq_inplace_repeat */
1076};
1077
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001078static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001079 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001080 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001081 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001082};
1083
1084
Bram Moolenaar971db462013-05-12 18:44:48 +02001085/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086 */
1087
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001088 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001089BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001090{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001091 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001092
Bram Moolenaar77045652012-09-21 13:46:06 +02001093 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001094
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001095 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001096 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001098 r = BufferAttr((BufferObject *)(self), name);
1099 if (r || PyErr_Occurred())
1100 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001101 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001102 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001103}
1104
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001105 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001106BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1107{
1108 return Py_BuildValue("[sssss]", "name", "number",
1109 "append", "mark", "range");
1110}
1111
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001112/******************/
1113
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001114 static PyObject *
1115BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001116{
Bram Moolenaardb913952012-06-29 12:54:53 +02001117 if (PyLong_Check(idx))
1118 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001119 long _idx = PyLong_AsLong(idx);
1120 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001121 } else if (PySlice_Check(idx))
1122 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001123 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001125 if (CheckBuffer((BufferObject *) self))
1126 return NULL;
1127
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001128 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001129 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001130 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001131 &step, &slicelen) < 0)
1132 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001133 return NULL;
1134 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001135 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001136 }
1137 else
1138 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001139 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001140 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001141 }
1142}
1143
Bram Moolenaar19e60942011-06-19 00:27:51 +02001144 static Py_ssize_t
1145BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1146{
Bram Moolenaardb913952012-06-29 12:54:53 +02001147 if (PyLong_Check(idx))
1148 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001149 long n = PyLong_AsLong(idx);
1150 return RBAsItem((BufferObject *)(self), n, val, 1,
1151 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1152 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001153 } else if (PySlice_Check(idx))
1154 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001155 Py_ssize_t start, stop, step, slicelen;
1156
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001157 if (CheckBuffer((BufferObject *) self))
1158 return -1;
1159
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001160 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001161 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001162 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001163 &step, &slicelen) < 0)
1164 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001165 return -1;
1166 }
1167 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1168 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1169 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001170 }
1171 else
1172 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001173 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar19e60942011-06-19 00:27:51 +02001174 return -1;
1175 }
1176}
1177
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001178static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001179 (lenfunc) RangeLength, /* sq_length, len(x) */
1180 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1181 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1182 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1183 0, /* was_sq_slice, x[i:j] */
1184 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1185 0, /* sq_ass_slice, x[i:j]=v */
1186 0, /* sq_contains */
1187 0, /* sq_inplace_concat */
1188 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189};
1190
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001191static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001192 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001193 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001194 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001195};
1196
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001197/* Line range object - Implementation
1198 */
1199
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001200 static PyObject *
1201RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001202{
Bram Moolenaar77045652012-09-21 13:46:06 +02001203 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001204
1205 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001206 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001210 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211}
1212
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213/****************/
1214
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001215 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001216RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001217{
1218 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001219 ((RangeObject *)(self))->start,
1220 ((RangeObject *)(self))->end,
1221 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222}
1223
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001224 static Py_ssize_t
1225RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1226{
1227 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1228 ((RangeObject *)(self))->start,
1229 ((RangeObject *)(self))->end,
1230 &((RangeObject *)(self))->end);
1231}
1232
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001233 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001234RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001235{
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 if (PyLong_Check(idx))
1237 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001238 long _idx = PyLong_AsLong(idx);
1239 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001240 } else if (PySlice_Check(idx))
1241 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001242 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001244 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1246 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001247 &step, &slicelen) < 0)
1248 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001249 return NULL;
1250 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001251 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001252 }
1253 else
1254 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001255 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001256 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001257 }
1258}
1259
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001260 static Py_ssize_t
1261RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1262{
Bram Moolenaardb913952012-06-29 12:54:53 +02001263 if (PyLong_Check(idx))
1264 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001265 long n = PyLong_AsLong(idx);
1266 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001267 } else if (PySlice_Check(idx))
1268 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001269 Py_ssize_t start, stop, step, slicelen;
1270
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001271 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001272 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1273 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001274 &step, &slicelen) < 0)
1275 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001276 return -1;
1277 }
1278 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001279 }
1280 else
1281 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001282 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001283 return -1;
1284 }
1285}
1286
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001287/* TabPage object - Implementation
1288 */
1289
1290 static PyObject *
1291TabPageGetattro(PyObject *self, PyObject *nameobj)
1292{
1293 PyObject *r;
1294
1295 GET_ATTR_STRING(name, nameobj);
1296
1297 if (CheckTabPage((TabPageObject *)(self)))
1298 return NULL;
1299
1300 r = TabPageAttr((TabPageObject *)(self), name);
1301 if (r || PyErr_Occurred())
1302 return r;
1303 else
1304 return PyObject_GenericGetAttr(self, nameobj);
1305}
1306
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001307/* Window object - Implementation
1308 */
1309
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001310 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001311WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001312{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001314
Bram Moolenaar77045652012-09-21 13:46:06 +02001315 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001316
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001317 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001318 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001319
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001320 r = WindowAttr((WindowObject *)(self), name);
1321 if (r || PyErr_Occurred())
1322 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001324 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001325}
1326
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001327 static int
1328WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001329{
Bram Moolenaar77045652012-09-21 13:46:06 +02001330 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001332 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001333}
1334
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001335/* Tab page list object - Definitions
1336 */
1337
1338static PySequenceMethods TabListAsSeq = {
1339 (lenfunc) TabListLength, /* sq_length, len(x) */
1340 (binaryfunc) 0, /* sq_concat, x+y */
1341 (ssizeargfunc) 0, /* sq_repeat, x*n */
1342 (ssizeargfunc) TabListItem, /* sq_item, x[i] */
1343 0, /* sq_slice, x[i:j] */
1344 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1345 0, /* sq_ass_slice, x[i:j]=v */
1346 0, /* sq_contains */
1347 0, /* sq_inplace_concat */
1348 0, /* sq_inplace_repeat */
1349};
1350
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001351/* Window list object - Definitions
1352 */
1353
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001354static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001355 (lenfunc) WinListLength, /* sq_length, len(x) */
1356 (binaryfunc) 0, /* sq_concat, x+y */
1357 (ssizeargfunc) 0, /* sq_repeat, x*n */
1358 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1359 0, /* sq_slice, x[i:j] */
1360 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1361 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001362 0, /* sq_contains */
1363 0, /* sq_inplace_concat */
1364 0, /* sq_inplace_repeat */
1365};
1366
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001367/* Current items object - Implementation
1368 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001369 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001370CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001371{
Bram Moolenaar77045652012-09-21 13:46:06 +02001372 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001373 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001374}
1375
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001376 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001377CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001378{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001379 GET_ATTR_STRING(name, nameobj);
1380 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001381}
1382
Bram Moolenaardb913952012-06-29 12:54:53 +02001383/* Dictionary object - Definitions
1384 */
1385
1386static PyInt DictionaryLength(PyObject *);
1387
Bram Moolenaar66b79852012-09-21 14:00:35 +02001388 static PyObject *
1389DictionaryGetattro(PyObject *self, PyObject *nameobj)
1390{
1391 DictionaryObject *this = ((DictionaryObject *) (self));
1392
1393 GET_ATTR_STRING(name, nameobj);
1394
1395 if (strcmp(name, "locked") == 0)
1396 return PyLong_FromLong(this->dict->dv_lock);
1397 else if (strcmp(name, "scope") == 0)
1398 return PyLong_FromLong(this->dict->dv_scope);
1399
1400 return PyObject_GenericGetAttr(self, nameobj);
1401}
1402
1403 static int
1404DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1405{
1406 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001407 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001408}
1409
1410/* List object - Definitions
1411 */
1412
1413static PyInt ListLength(PyObject *);
1414static PyObject *ListItem(PyObject *, Py_ssize_t);
1415
1416static PySequenceMethods ListAsSeq = {
1417 (lenfunc) ListLength, /* sq_length, len(x) */
1418 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1419 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1420 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1421 (void *) 0, /* was_sq_slice, x[i:j] */
1422 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1423 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1424 0, /* sq_contains */
1425 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1426 0, /* sq_inplace_repeat */
1427};
1428
1429static PyObject *ListSubscript(PyObject *, PyObject *);
1430static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1431
1432static PyMappingMethods ListAsMapping = {
1433 /* mp_length */ (lenfunc) ListLength,
1434 /* mp_subscript */ (binaryfunc) ListSubscript,
1435 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1436};
1437
Bram Moolenaardb913952012-06-29 12:54:53 +02001438 static PyObject *
1439ListSubscript(PyObject *self, PyObject* idxObject)
1440{
1441 if (PyLong_Check(idxObject))
1442 {
1443 long idx = PyLong_AsLong(idxObject);
1444 return ListItem(self, idx);
1445 }
1446 else if (PySlice_Check(idxObject))
1447 {
1448 Py_ssize_t start, stop, step, slicelen;
1449
1450 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1451 &step, &slicelen) < 0)
1452 return NULL;
1453 return ListSlice(self, start, stop);
1454 }
1455 else
1456 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001457 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001458 return NULL;
1459 }
1460}
1461
1462 static Py_ssize_t
1463ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1464{
1465 if (PyLong_Check(idxObject))
1466 {
1467 long idx = PyLong_AsLong(idxObject);
1468 return ListAssItem(self, idx, obj);
1469 }
1470 else if (PySlice_Check(idxObject))
1471 {
1472 Py_ssize_t start, stop, step, slicelen;
1473
1474 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1475 &step, &slicelen) < 0)
1476 return -1;
1477 return ListAssSlice(self, start, stop, obj);
1478 }
1479 else
1480 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001481 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001482 return -1;
1483 }
1484}
1485
Bram Moolenaar66b79852012-09-21 14:00:35 +02001486 static PyObject *
1487ListGetattro(PyObject *self, PyObject *nameobj)
1488{
1489 GET_ATTR_STRING(name, nameobj);
1490
1491 if (strcmp(name, "locked") == 0)
1492 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1493
1494 return PyObject_GenericGetAttr(self, nameobj);
1495}
1496
1497 static int
1498ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1499{
1500 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001501 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001502}
1503
1504/* Function object - Definitions
1505 */
1506
Bram Moolenaardb913952012-06-29 12:54:53 +02001507 static PyObject *
1508FunctionGetattro(PyObject *self, PyObject *nameobj)
1509{
1510 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001511
1512 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001513
1514 if (strcmp(name, "name") == 0)
1515 return PyUnicode_FromString((char *)(this->name));
1516
1517 return PyObject_GenericGetAttr(self, nameobj);
1518}
1519
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001520/* External interface
1521 */
1522
1523 void
1524python3_buffer_free(buf_T *buf)
1525{
Bram Moolenaar971db462013-05-12 18:44:48 +02001526 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001528 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001529 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001530 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001531 }
1532}
1533
1534#if defined(FEAT_WINDOWS) || defined(PROTO)
1535 void
1536python3_window_free(win_T *win)
1537{
Bram Moolenaar971db462013-05-12 18:44:48 +02001538 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001539 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001540 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001541 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001542 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001543 }
1544}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001545
1546 void
1547python3_tabpage_free(tabpage_T *tab)
1548{
1549 if (TAB_PYTHON_REF(tab) != NULL)
1550 {
1551 TabPageObject *tp = TAB_PYTHON_REF(tab);
1552 tp->tab = INVALID_TABPAGE_VALUE;
1553 TAB_PYTHON_REF(tab) = NULL;
1554 }
1555}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001556#endif
1557
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001558static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001559{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001560 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001561};
1562
1563static WinListObject TheWindowList =
1564{
1565 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001566 NULL
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001567};
1568
1569static CurrentObject TheCurrent =
1570{
1571 PyObject_HEAD_INIT(&CurrentType)
1572};
1573
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001574static TabListObject TheTabPageList =
1575{
1576 PyObject_HEAD_INIT(&TabListType)
1577};
1578
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001579 static PyObject *
1580Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001581{
1582 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001583 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584 /* The special value is removed from sys.path in Python3_Init(). */
1585 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1586
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001587 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001588 PyType_Ready(&BufferType);
1589 PyType_Ready(&RangeType);
1590 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001591 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001592 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001593 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001594 PyType_Ready(&TabListType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001595 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001596 PyType_Ready(&DictionaryType);
1597 PyType_Ready(&ListType);
1598 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001599 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001600
1601 /* Set sys.argv[] to avoid a crash in warn(). */
1602 PySys_SetArgv(1, argv);
1603
1604 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001605 if (mod == NULL)
1606 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001607
Bram Moolenaar19e60942011-06-19 00:27:51 +02001608 VimError = PyErr_NewException("vim.error", NULL, NULL);
1609 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610
1611 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001612 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1613 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614 Py_INCREF((PyObject *)(void *)&TheCurrent);
1615 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1616 Py_INCREF((PyObject *)(void *)&TheWindowList);
1617 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001618 Py_INCREF((PyObject *)(void *)&TheTabPageList);
1619 PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001620
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001621 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1622 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001623 PyModule_AddObject(mod, "options",
1624 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001625
Bram Moolenaar66b79852012-09-21 14:00:35 +02001626#define ADD_INT_CONSTANT(name, value) \
1627 tmp = PyLong_FromLong(value); \
1628 Py_INCREF(tmp); \
1629 PyModule_AddObject(mod, name, tmp)
1630
1631 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1632 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1633 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1634 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1635
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001636 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001637 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001638
1639 return mod;
1640}
1641
1642/*************************************************************************
1643 * 4. Utility functions for handling the interface between Vim and Python.
1644 */
1645
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001646/* Convert a Vim line into a Python string.
1647 * All internal newlines are replaced by null characters.
1648 *
1649 * On errors, the Python exception data is set, and NULL is returned.
1650 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001651 static PyObject *
1652LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001653{
1654 PyObject *result;
1655 Py_ssize_t len = strlen(str);
1656 char *tmp,*p;
1657
1658 tmp = (char *)alloc((unsigned)(len+1));
1659 p = tmp;
1660 if (p == NULL)
1661 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 PyErr_NoMemory();
1663 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001664 }
1665
1666 while (*str)
1667 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001668 if (*str == '\n')
1669 *p = '\0';
1670 else
1671 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001672
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001673 ++p;
1674 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001675 }
1676 *p = '\0';
1677
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001678 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001679
1680 vim_free(tmp);
1681 return result;
1682}
1683
Bram Moolenaardb913952012-06-29 12:54:53 +02001684 void
1685do_py3eval (char_u *str, typval_T *rettv)
1686{
1687 DoPy3Command(NULL, (char *) str, rettv);
1688 switch(rettv->v_type)
1689 {
1690 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1691 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1692 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001693 case VAR_UNKNOWN:
1694 rettv->v_type = VAR_NUMBER;
1695 rettv->vval.v_number = 0;
1696 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001697 }
1698}
1699
1700 void
1701set_ref_in_python3 (int copyID)
1702{
1703 set_ref_in_py(copyID);
1704}