blob: 8acab9d555a01785a7aeb93ec166aea034760969 [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;
339
340# define PyExc_AttributeError p3imp_PyExc_AttributeError
341# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200342# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200343# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
344# define PyExc_TypeError p3imp_PyExc_TypeError
345# define PyExc_ValueError p3imp_PyExc_ValueError
346
347/*
348 * Table of name to function pointer of python.
349 */
350# define PYTHON_PROC FARPROC
351static struct
352{
353 char *name;
354 PYTHON_PROC *ptr;
355} py3_funcname_table[] =
356{
357 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100358 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200360# ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200361 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200362 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200363# else
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200364 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
365 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200366# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200367 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200368 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200369 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
370 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
371 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
372 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
373 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
374 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200375 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
376 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
377 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
378 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
379 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200380 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
381 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
382 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
383 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200384 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200385 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200386 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200387 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
388 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200389 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200391 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
392 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
393 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
394 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200395 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
396 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
397 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
398 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
399 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200400 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200401 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
402 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200403 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
404 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
405 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
406 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
407 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
408 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
409 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
410 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200411 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200412 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200413 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200414 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
415 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200416 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100417 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200418 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
419 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
420 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200421# if PY_VERSION_HEX >= 0x030300f0
422 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
423# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200424 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200425# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200426 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200427 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200428 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
429 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
430 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200431 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
432 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
433 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
434 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200435 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200436 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200437 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200438 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200439 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200440# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200441 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
442 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
443 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
444 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
445 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200446# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200447 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
448 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200449# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
451 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
452 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200453 {"", NULL},
454};
455
456/*
457 * Free python.dll
458 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200459 static void
460end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200461{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200462 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200464 close_dll(hinstPy3);
465 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466 }
467}
468
469/*
470 * Load library and get all pointers.
471 * Parameter 'libname' provides name of DLL.
472 * Return OK or FAIL.
473 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200474 static int
475py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200476{
477 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200478 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100480# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200481 /* Can't have Python and Python3 loaded at the same time.
482 * It cause a crash, because RTLD_GLOBAL is needed for
483 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200484 if (python_loaded())
485 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200486 if (verbose)
487 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200488 return FAIL;
489 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200490# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200491
492 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200493 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494 hinstPy3 = load_dll(libname);
495
496 if (!hinstPy3)
497 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200498 if (verbose)
499 EMSG2(_(e_loadlib), libname);
500 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200501 }
502
503 for (i = 0; py3_funcname_table[i].ptr; ++i)
504 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200505 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
506 py3_funcname_table[i].name)) == NULL)
507 {
508 close_dll(hinstPy3);
509 hinstPy3 = 0;
510 if (verbose)
511 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
512 return FAIL;
513 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200514 }
515
Bram Moolenaar69154f22010-07-18 21:42:34 +0200516 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
517 * will be present in the library. */
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200518# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200519 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
520 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
521 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
522 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200523# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200524 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200525 ucs_decode = symbol_from_dll(hinstPy3,
526 "PyUnicodeUCS2_Decode");
527 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
528 "PyUnicodeUCS2_AsEncodedString");
529 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200530 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200531 ucs_from_string = symbol_from_dll(hinstPy3,
532 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200533 ucs_decode = symbol_from_dll(hinstPy3,
534 "PyUnicodeUCS4_Decode");
535 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
536 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200537 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200538# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200539 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200540 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200541 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200542 py3_PyUnicode_Decode = ucs_decode;
543 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200544 }
545 else
546 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200547 close_dll(hinstPy3);
548 hinstPy3 = 0;
549 if (verbose)
550 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
551 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200552 }
553
554 return OK;
555}
556
557/*
558 * If python is enabled (there is installed python on Windows system) return
559 * TRUE, else FALSE.
560 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200561 int
562python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200563{
564 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
565}
566
567/* Load the standard Python exceptions - don't import the symbols from the
568 * DLL, as this can cause errors (importing data symbols is not reliable).
569 */
570static void get_py3_exceptions __ARGS((void));
571
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200572 static void
573get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200574{
575 PyObject *exmod = PyImport_ImportModule("builtins");
576 PyObject *exdict = PyModule_GetDict(exmod);
577 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
578 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200579 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200580 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
581 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
582 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
583 Py_XINCREF(p3imp_PyExc_AttributeError);
584 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200585 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200586 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
587 Py_XINCREF(p3imp_PyExc_TypeError);
588 Py_XINCREF(p3imp_PyExc_ValueError);
589 Py_XDECREF(exmod);
590}
591#endif /* DYNAMIC_PYTHON3 */
592
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593static PyObject *BufferNew (buf_T *);
594static PyObject *WindowNew(win_T *);
595static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100596static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200597
Bram Moolenaardb913952012-06-29 12:54:53 +0200598static int py3initialised = 0;
599
600#define PYINITIALISED py3initialised
601
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200602#define DICTKEY_DECL PyObject *bytes = NULL;
603
Bram Moolenaardb913952012-06-29 12:54:53 +0200604#define DICTKEY_GET(err) \
605 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200606 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200607 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200608 return err; \
609 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200610 else if (PyUnicode_Check(keyObject)) \
611 { \
612 bytes = PyString_AsBytes(keyObject); \
613 if (bytes == NULL) \
614 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200615 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200616 return err; \
617 } \
618 else \
619 { \
620 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
621 return err; \
622 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200623
Bram Moolenaardb913952012-06-29 12:54:53 +0200624#define DICTKEY_UNREF \
625 if (bytes != NULL) \
626 Py_XDECREF(bytes);
627
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200628#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaardb913952012-06-29 12:54:53 +0200629
Bram Moolenaar971db462013-05-12 18:44:48 +0200630#define WIN_PYTHON_REF(win) win->w_python3_ref
631#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200632#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200633
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200634 static void
635call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200636{
637#ifdef Py_DEBUG
638 _PyObject_DebugFree(p);
639#else
640 PyObject_Free(p);
641#endif
642}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200643
644 static PyObject *
645call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200646{
647 return PyType_GenericNew(type,args,kwds);
648}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200649
650 static PyObject *
651call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200652{
653 return PyType_GenericAlloc(type,nitems);
654}
655
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200656static PyObject *OutputGetattro(PyObject *, PyObject *);
657static int OutputSetattro(PyObject *, PyObject *, PyObject *);
658static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200659static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200660static PyObject *WindowGetattro(PyObject *, PyObject *);
661static int WindowSetattro(PyObject *, PyObject *, PyObject *);
662static PyObject *RangeGetattro(PyObject *, PyObject *);
663static PyObject *CurrentGetattro(PyObject *, PyObject *);
664static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
665static PyObject *DictionaryGetattro(PyObject *, PyObject *);
666static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
667static PyObject *ListGetattro(PyObject *, PyObject *);
668static int ListSetattro(PyObject *, PyObject *, PyObject *);
669static PyObject *FunctionGetattro(PyObject *, PyObject *);
670
671static struct PyModuleDef vimmodule;
672
673/*
674 * Include the code shared with if_python.c
675 */
676#include "if_py_both.h"
677
678#define GET_ATTR_STRING(name, nameobj) \
679 char *name = ""; \
680 if (PyUnicode_Check(nameobj)) \
681 name = _PyUnicode_AsString(nameobj)
682
683#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
684
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200685/******************************************************
686 * Internal function prototypes.
687 */
688
Bram Moolenaardb913952012-06-29 12:54:53 +0200689static PyObject *globals;
690
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200691static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100692static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200693
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200694/******************************************************
695 * 1. Python interpreter main program.
696 */
697
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200698static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
699
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200700 void
701python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200702{
703 static int recurse = 0;
704
705 /* If a crash occurs while doing this, don't try again. */
706 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200707 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200708
709 ++recurse;
710
711#ifdef DYNAMIC_PYTHON3
712 if (hinstPy3)
713#endif
714 if (Py_IsInitialized())
715 {
716 // acquire lock before finalizing
717 pygilstate = PyGILState_Ensure();
718
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200719 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200720 }
721
722#ifdef DYNAMIC_PYTHON3
723 end_dynamic_python3();
724#endif
725
726 --recurse;
727}
728
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200729#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
730 int
731python3_loaded()
732{
733 return (hinstPy3 != 0);
734}
735#endif
736
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200737 static int
738Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200739{
740 if (!py3initialised)
741 {
742#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200743 if (!python3_enabled(TRUE))
744 {
745 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
746 goto fail;
747 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200748#endif
749
750 init_structs();
751
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100752
753#ifdef PYTHON3_HOME
754 Py_SetPythonHome(PYTHON3_HOME);
755#endif
756
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200757 PyImport_AppendInittab("vim", Py3Init_vim);
758
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200759#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200760 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200761#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200762 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100764 /* Initialise threads, and below save the state using
765 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
766 * specific state (such as the system trace hook), will be lost
767 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200768 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200769#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200770 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200771#endif
772
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 if (PythonIO_Init())
774 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200775
Bram Moolenaardb913952012-06-29 12:54:53 +0200776 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
777
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200778 /* Remove the element from sys.path that was added because of our
779 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200780 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200781 * the current directory in sys.path.
782 * Only after vim has been imported, the element does exist in
783 * sys.path.
784 */
785 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 +0200786
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100787 /* lock is created and acquired in PyEval_InitThreads() and thread
788 * state is created in Py_Initialize()
789 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
790 * (python must have threads enabled!)
791 * so the following does both: unlock GIL and save thread state in TLS
792 * without deleting thread state
793 */
794 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200795
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200796 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200797 }
798
799 return 0;
800
801fail:
802 /* We call PythonIO_Flush() here to print any Python errors.
803 * This is OK, as it is possible to call this function even
804 * if PythonIO_Init() has not completed successfully (it will
805 * not do anything in this case).
806 */
807 PythonIO_Flush();
808 return -1;
809}
810
811/*
812 * External interface
813 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200814 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200815DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200816{
817#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200818 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200819#endif
820#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200821 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200822#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200823 PyObject *cmdstr;
824 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200825
826#if defined(MACOS) && !defined(MACOS_X_UNIX)
827 GetPort(&oldPort);
828 /* Check if the Python library is available */
829 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200830 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200831#endif
832 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200833 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200834
Bram Moolenaardb913952012-06-29 12:54:53 +0200835 if (rettv == NULL)
836 {
837 RangeStart = eap->line1;
838 RangeEnd = eap->line2;
839 }
840 else
841 {
842 RangeStart = (PyInt) curwin->w_cursor.lnum;
843 RangeEnd = RangeStart;
844 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200845 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200846
847#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
848 /* Python only works properly when the LC_NUMERIC locale is "C". */
849 saved_locale = setlocale(LC_NUMERIC, NULL);
850 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200851 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852 else
853 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200854 /* Need to make a copy, value may change when setting new locale. */
855 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
856 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200857 }
858#endif
859
860 pygilstate = PyGILState_Ensure();
861
Bram Moolenaar19e60942011-06-19 00:27:51 +0200862 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
863 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200864 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
865 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
866 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200867 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200868 if (rettv == NULL)
869 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
870 else
871 {
872 PyObject *r;
873
874 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
875 globals, globals);
876 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100877 {
878 if (PyErr_Occurred() && !msg_silent)
879 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200880 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100881 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200882 else
883 {
884 if (ConvertFromPyObject(r, rettv) == -1)
885 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
886 Py_DECREF(r);
887 }
888 PyErr_Clear();
889 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200890 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200891
892 PyGILState_Release(pygilstate);
893
894#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
895 if (saved_locale != NULL)
896 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200897 (void)setlocale(LC_NUMERIC, saved_locale);
898 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200899 }
900#endif
901
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200902 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903 PythonIO_Flush();
904#if defined(MACOS) && !defined(MACOS_X_UNIX)
905 SetPort(oldPort);
906#endif
907
908theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200909 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200910}
911
912/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200913 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200914 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200915 void
916ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200917{
918 char_u *script;
919
920 script = script_get(eap, eap->arg);
921 if (!eap->skip)
922 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200923 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200924 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200925 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200926 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200927 }
928 vim_free(script);
929}
930
931#define BUFFER_SIZE 2048
932
933/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200934 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200935 */
936 void
937ex_py3file(exarg_T *eap)
938{
939 static char buffer[BUFFER_SIZE];
940 const char *file;
941 char *p;
942 int i;
943
944 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
945 * stdio file pointer, but Vim and the Python DLL are compiled with
946 * different options under Windows, meaning that stdio pointers aren't
947 * compatible between the two. Yuk.
948 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200949 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
950 *
951 * Using bytes so that Python can detect the source encoding as it normally
952 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200953 *
954 * We need to escape any backslashes or single quotes in the file name, so that
955 * Python won't mangle the file name.
956 */
957
958 strcpy(buffer, "exec(compile(open('");
959 p = buffer + 19; /* size of "exec(compile(open('" */
960
961 for (i=0; i<2; ++i)
962 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200963 file = (char *)eap->arg;
964 while (*file && p < buffer + (BUFFER_SIZE - 3))
965 {
966 if (*file == '\\' || *file == '\'')
967 *p++ = '\\';
968 *p++ = *file++;
969 }
970 /* If we didn't finish the file name, we hit a buffer overflow */
971 if (*file != '\0')
972 return;
973 if (i==0)
974 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200975 strcpy(p,"','rb').read(),'");
976 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200977 }
978 else
979 {
980 strcpy(p,"','exec'))");
981 p += 10;
982 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200983 }
984
985
986 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200987 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988}
989
990/******************************************************
991 * 2. Python output stream: writes output via [e]msg().
992 */
993
994/* Implementation functions
995 */
996
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200997 static PyObject *
998OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999{
Bram Moolenaar77045652012-09-21 13:46:06 +02001000 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001
1002 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001003 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004
1005 return PyObject_GenericGetAttr(self, nameobj);
1006}
1007
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001008 static int
1009OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010{
Bram Moolenaar77045652012-09-21 13:46:06 +02001011 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001012
Bram Moolenaar77045652012-09-21 13:46:06 +02001013 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014}
1015
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016/***************/
1017
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001018 static int
1019PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001020{
1021 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001022 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001023}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001024
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001025/******************************************************
1026 * 3. Implementation of the Vim module for Python
1027 */
1028
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001029/* Window type - Implementation functions
1030 * --------------------------------------
1031 */
1032
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001033#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1034
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001035/* Buffer type - Implementation functions
1036 * --------------------------------------
1037 */
1038
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1040
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001041static Py_ssize_t BufferLength(PyObject *);
1042static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001043static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1044static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001045
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001046
1047/* Line range type - Implementation functions
1048 * --------------------------------------
1049 */
1050
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001051#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1052
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001053static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001055static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001056
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001057/* Current objects type - Implementation functions
1058 * -----------------------------------------------
1059 */
1060
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001061static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001062 (lenfunc) BufferLength, /* sq_length, len(x) */
1063 (binaryfunc) 0, /* sq_concat, x+y */
1064 (ssizeargfunc) 0, /* sq_repeat, x*n */
1065 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1066 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001067 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001068 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001069 0, /* sq_contains */
1070 0, /* sq_inplace_concat */
1071 0, /* sq_inplace_repeat */
1072};
1073
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001074static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001075 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001076 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001077 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001078};
1079
1080
Bram Moolenaar971db462013-05-12 18:44:48 +02001081/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001082 */
1083
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001084 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001085BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001087 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001088
Bram Moolenaar77045652012-09-21 13:46:06 +02001089 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001090
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001091 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001092 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001093
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001094 r = BufferAttr((BufferObject *)(self), name);
1095 if (r || PyErr_Occurred())
1096 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001098 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001099}
1100
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001102BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1103{
1104 return Py_BuildValue("[sssss]", "name", "number",
1105 "append", "mark", "range");
1106}
1107
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001108/******************/
1109
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001110 static PyObject *
1111BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001112{
Bram Moolenaardb913952012-06-29 12:54:53 +02001113 if (PyLong_Check(idx))
1114 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001115 long _idx = PyLong_AsLong(idx);
1116 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001117 } else if (PySlice_Check(idx))
1118 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001119 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001120
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001121 if (CheckBuffer((BufferObject *) self))
1122 return NULL;
1123
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001124 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001125 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001126 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001127 &step, &slicelen) < 0)
1128 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001129 return NULL;
1130 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001131 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001132 }
1133 else
1134 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001135 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1136 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001137 }
1138}
1139
Bram Moolenaar19e60942011-06-19 00:27:51 +02001140 static Py_ssize_t
1141BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1142{
Bram Moolenaardb913952012-06-29 12:54:53 +02001143 if (PyLong_Check(idx))
1144 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001145 long n = PyLong_AsLong(idx);
1146 return RBAsItem((BufferObject *)(self), n, val, 1,
1147 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1148 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001149 } else if (PySlice_Check(idx))
1150 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001151 Py_ssize_t start, stop, step, slicelen;
1152
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001153 if (CheckBuffer((BufferObject *) self))
1154 return -1;
1155
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001156 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001157 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001158 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001159 &step, &slicelen) < 0)
1160 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001161 return -1;
1162 }
1163 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1164 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1165 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001166 }
1167 else
1168 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001169 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1170 return -1;
1171 }
1172}
1173
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001174static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001175 (lenfunc) RangeLength, /* sq_length, len(x) */
1176 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1177 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1178 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1179 0, /* was_sq_slice, x[i:j] */
1180 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1181 0, /* sq_ass_slice, x[i:j]=v */
1182 0, /* sq_contains */
1183 0, /* sq_inplace_concat */
1184 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001185};
1186
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001187static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001188 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001190 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191};
1192
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001193/* Line range object - Implementation
1194 */
1195
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001196 static PyObject *
1197RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001198{
Bram Moolenaar77045652012-09-21 13:46:06 +02001199 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001200
1201 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001204 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001205 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001206 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207}
1208
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209/****************/
1210
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001211 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001212RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001213{
1214 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001215 ((RangeObject *)(self))->start,
1216 ((RangeObject *)(self))->end,
1217 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001218}
1219
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001220 static Py_ssize_t
1221RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1222{
1223 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1224 ((RangeObject *)(self))->start,
1225 ((RangeObject *)(self))->end,
1226 &((RangeObject *)(self))->end);
1227}
1228
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001229 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001230RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231{
Bram Moolenaardb913952012-06-29 12:54:53 +02001232 if (PyLong_Check(idx))
1233 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001234 long _idx = PyLong_AsLong(idx);
1235 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001236 } else if (PySlice_Check(idx))
1237 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001238 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001240 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001241 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1242 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001243 &step, &slicelen) < 0)
1244 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001245 return NULL;
1246 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001247 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001248 }
1249 else
1250 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1252 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001253 }
1254}
1255
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001256 static Py_ssize_t
1257RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1258{
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 if (PyLong_Check(idx))
1260 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001261 long n = PyLong_AsLong(idx);
1262 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001263 } else if (PySlice_Check(idx))
1264 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001265 Py_ssize_t start, stop, step, slicelen;
1266
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001267 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001268 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1269 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001270 &step, &slicelen) < 0)
1271 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001272 return -1;
1273 }
1274 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001275 }
1276 else
1277 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001278 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1279 return -1;
1280 }
1281}
1282
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001283/* TabPage object - Implementation
1284 */
1285
1286 static PyObject *
1287TabPageGetattro(PyObject *self, PyObject *nameobj)
1288{
1289 PyObject *r;
1290
1291 GET_ATTR_STRING(name, nameobj);
1292
1293 if (CheckTabPage((TabPageObject *)(self)))
1294 return NULL;
1295
1296 r = TabPageAttr((TabPageObject *)(self), name);
1297 if (r || PyErr_Occurred())
1298 return r;
1299 else
1300 return PyObject_GenericGetAttr(self, nameobj);
1301}
1302
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001303/* Window object - Implementation
1304 */
1305
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001306 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001307WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001309 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001310
Bram Moolenaar77045652012-09-21 13:46:06 +02001311 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001312
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001313 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001316 r = WindowAttr((WindowObject *)(self), name);
1317 if (r || PyErr_Occurred())
1318 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001319 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001320 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001321}
1322
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001323 static int
1324WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001325{
Bram Moolenaar77045652012-09-21 13:46:06 +02001326 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001327
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001328 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001329}
1330
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001331/* Tab page list object - Definitions
1332 */
1333
1334static PySequenceMethods TabListAsSeq = {
1335 (lenfunc) TabListLength, /* sq_length, len(x) */
1336 (binaryfunc) 0, /* sq_concat, x+y */
1337 (ssizeargfunc) 0, /* sq_repeat, x*n */
1338 (ssizeargfunc) TabListItem, /* sq_item, x[i] */
1339 0, /* sq_slice, x[i:j] */
1340 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1341 0, /* sq_ass_slice, x[i:j]=v */
1342 0, /* sq_contains */
1343 0, /* sq_inplace_concat */
1344 0, /* sq_inplace_repeat */
1345};
1346
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001347/* Window list object - Definitions
1348 */
1349
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001350static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 (lenfunc) WinListLength, /* sq_length, len(x) */
1352 (binaryfunc) 0, /* sq_concat, x+y */
1353 (ssizeargfunc) 0, /* sq_repeat, x*n */
1354 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1355 0, /* sq_slice, x[i:j] */
1356 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1357 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001358 0, /* sq_contains */
1359 0, /* sq_inplace_concat */
1360 0, /* sq_inplace_repeat */
1361};
1362
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363/* Current items object - Implementation
1364 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001365 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001366CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001367{
Bram Moolenaar77045652012-09-21 13:46:06 +02001368 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001369 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001370}
1371
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001372 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001373CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001374{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001375 GET_ATTR_STRING(name, nameobj);
1376 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001377}
1378
Bram Moolenaardb913952012-06-29 12:54:53 +02001379/* Dictionary object - Definitions
1380 */
1381
1382static PyInt DictionaryLength(PyObject *);
1383
Bram Moolenaar66b79852012-09-21 14:00:35 +02001384 static PyObject *
1385DictionaryGetattro(PyObject *self, PyObject *nameobj)
1386{
1387 DictionaryObject *this = ((DictionaryObject *) (self));
1388
1389 GET_ATTR_STRING(name, nameobj);
1390
1391 if (strcmp(name, "locked") == 0)
1392 return PyLong_FromLong(this->dict->dv_lock);
1393 else if (strcmp(name, "scope") == 0)
1394 return PyLong_FromLong(this->dict->dv_scope);
1395
1396 return PyObject_GenericGetAttr(self, nameobj);
1397}
1398
1399 static int
1400DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1401{
1402 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001403 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001404}
1405
1406/* List object - Definitions
1407 */
1408
1409static PyInt ListLength(PyObject *);
1410static PyObject *ListItem(PyObject *, Py_ssize_t);
1411
1412static PySequenceMethods ListAsSeq = {
1413 (lenfunc) ListLength, /* sq_length, len(x) */
1414 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1415 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1416 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1417 (void *) 0, /* was_sq_slice, x[i:j] */
1418 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1419 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1420 0, /* sq_contains */
1421 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1422 0, /* sq_inplace_repeat */
1423};
1424
1425static PyObject *ListSubscript(PyObject *, PyObject *);
1426static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1427
1428static PyMappingMethods ListAsMapping = {
1429 /* mp_length */ (lenfunc) ListLength,
1430 /* mp_subscript */ (binaryfunc) ListSubscript,
1431 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1432};
1433
Bram Moolenaardb913952012-06-29 12:54:53 +02001434 static PyObject *
1435ListSubscript(PyObject *self, PyObject* idxObject)
1436{
1437 if (PyLong_Check(idxObject))
1438 {
1439 long idx = PyLong_AsLong(idxObject);
1440 return ListItem(self, idx);
1441 }
1442 else if (PySlice_Check(idxObject))
1443 {
1444 Py_ssize_t start, stop, step, slicelen;
1445
1446 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1447 &step, &slicelen) < 0)
1448 return NULL;
1449 return ListSlice(self, start, stop);
1450 }
1451 else
1452 {
1453 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1454 return NULL;
1455 }
1456}
1457
1458 static Py_ssize_t
1459ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1460{
1461 if (PyLong_Check(idxObject))
1462 {
1463 long idx = PyLong_AsLong(idxObject);
1464 return ListAssItem(self, idx, obj);
1465 }
1466 else if (PySlice_Check(idxObject))
1467 {
1468 Py_ssize_t start, stop, step, slicelen;
1469
1470 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1471 &step, &slicelen) < 0)
1472 return -1;
1473 return ListAssSlice(self, start, stop, obj);
1474 }
1475 else
1476 {
1477 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1478 return -1;
1479 }
1480}
1481
Bram Moolenaar66b79852012-09-21 14:00:35 +02001482 static PyObject *
1483ListGetattro(PyObject *self, PyObject *nameobj)
1484{
1485 GET_ATTR_STRING(name, nameobj);
1486
1487 if (strcmp(name, "locked") == 0)
1488 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1489
1490 return PyObject_GenericGetAttr(self, nameobj);
1491}
1492
1493 static int
1494ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1495{
1496 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001497 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001498}
1499
1500/* Function object - Definitions
1501 */
1502
Bram Moolenaardb913952012-06-29 12:54:53 +02001503 static PyObject *
1504FunctionGetattro(PyObject *self, PyObject *nameobj)
1505{
1506 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001507
1508 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001509
1510 if (strcmp(name, "name") == 0)
1511 return PyUnicode_FromString((char *)(this->name));
1512
1513 return PyObject_GenericGetAttr(self, nameobj);
1514}
1515
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516/* External interface
1517 */
1518
1519 void
1520python3_buffer_free(buf_T *buf)
1521{
Bram Moolenaar971db462013-05-12 18:44:48 +02001522 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001523 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001524 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001526 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527 }
1528}
1529
1530#if defined(FEAT_WINDOWS) || defined(PROTO)
1531 void
1532python3_window_free(win_T *win)
1533{
Bram Moolenaar971db462013-05-12 18:44:48 +02001534 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001535 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001536 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001537 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001538 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001539 }
1540}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001541
1542 void
1543python3_tabpage_free(tabpage_T *tab)
1544{
1545 if (TAB_PYTHON_REF(tab) != NULL)
1546 {
1547 TabPageObject *tp = TAB_PYTHON_REF(tab);
1548 tp->tab = INVALID_TABPAGE_VALUE;
1549 TAB_PYTHON_REF(tab) = NULL;
1550 }
1551}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001552#endif
1553
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001554static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001555{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001556 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001557};
1558
1559static WinListObject TheWindowList =
1560{
1561 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001562 NULL
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001563};
1564
1565static CurrentObject TheCurrent =
1566{
1567 PyObject_HEAD_INIT(&CurrentType)
1568};
1569
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001570static TabListObject TheTabPageList =
1571{
1572 PyObject_HEAD_INIT(&TabListType)
1573};
1574
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001575 static PyObject *
1576Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001577{
1578 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001579 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001580 /* The special value is removed from sys.path in Python3_Init(). */
1581 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1582
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001583 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001584 PyType_Ready(&BufferType);
1585 PyType_Ready(&RangeType);
1586 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001587 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001588 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001589 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001590 PyType_Ready(&TabListType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001591 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001592 PyType_Ready(&DictionaryType);
1593 PyType_Ready(&ListType);
1594 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001595 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001596
1597 /* Set sys.argv[] to avoid a crash in warn(). */
1598 PySys_SetArgv(1, argv);
1599
1600 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001601 if (mod == NULL)
1602 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603
Bram Moolenaar19e60942011-06-19 00:27:51 +02001604 VimError = PyErr_NewException("vim.error", NULL, NULL);
1605 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001606
1607 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001608 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1609 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001610 Py_INCREF((PyObject *)(void *)&TheCurrent);
1611 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1612 Py_INCREF((PyObject *)(void *)&TheWindowList);
1613 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001614 Py_INCREF((PyObject *)(void *)&TheTabPageList);
1615 PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001616
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001617 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1618 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001619 PyModule_AddObject(mod, "options",
1620 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001621
Bram Moolenaar66b79852012-09-21 14:00:35 +02001622#define ADD_INT_CONSTANT(name, value) \
1623 tmp = PyLong_FromLong(value); \
1624 Py_INCREF(tmp); \
1625 PyModule_AddObject(mod, name, tmp)
1626
1627 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1628 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1629 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1630 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1631
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001633 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001634
1635 return mod;
1636}
1637
1638/*************************************************************************
1639 * 4. Utility functions for handling the interface between Vim and Python.
1640 */
1641
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001642/* Convert a Vim line into a Python string.
1643 * All internal newlines are replaced by null characters.
1644 *
1645 * On errors, the Python exception data is set, and NULL is returned.
1646 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001647 static PyObject *
1648LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001649{
1650 PyObject *result;
1651 Py_ssize_t len = strlen(str);
1652 char *tmp,*p;
1653
1654 tmp = (char *)alloc((unsigned)(len+1));
1655 p = tmp;
1656 if (p == NULL)
1657 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001658 PyErr_NoMemory();
1659 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001660 }
1661
1662 while (*str)
1663 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001664 if (*str == '\n')
1665 *p = '\0';
1666 else
1667 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001668
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001669 ++p;
1670 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001671 }
1672 *p = '\0';
1673
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001674 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001675
1676 vim_free(tmp);
1677 return result;
1678}
1679
Bram Moolenaardb913952012-06-29 12:54:53 +02001680 void
1681do_py3eval (char_u *str, typval_T *rettv)
1682{
1683 DoPy3Command(NULL, (char *) str, rettv);
1684 switch(rettv->v_type)
1685 {
1686 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1687 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1688 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001689 case VAR_UNKNOWN:
1690 rettv->v_type = VAR_NUMBER;
1691 rettv->vval.v_number = 0;
1692 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001693 }
1694}
1695
1696 void
1697set_ref_in_python3 (int copyID)
1698{
1699 set_ref_in_py(copyID);
1700}