blob: 233283c21c25a603b633007dcbe7dd107232be59 [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
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200129# define PyErr_PrintEx py3_PyErr_PrintEx
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200130# define PyErr_SetNone py3_PyErr_SetNone
131# define PyErr_SetString py3_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200132# define PyErr_SetObject py3_PyErr_SetObject
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200133# define PyEval_InitThreads py3_PyEval_InitThreads
134# define PyEval_RestoreThread py3_PyEval_RestoreThread
135# define PyEval_SaveThread py3_PyEval_SaveThread
136# define PyGILState_Ensure py3_PyGILState_Ensure
137# define PyGILState_Release py3_PyGILState_Release
138# define PyLong_AsLong py3_PyLong_AsLong
139# define PyLong_FromLong py3_PyLong_FromLong
140# define PyList_GetItem py3_PyList_GetItem
141# define PyList_Append py3_PyList_Append
142# define PyList_New py3_PyList_New
143# define PyList_SetItem py3_PyList_SetItem
144# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200145# define PySequence_Check py3_PySequence_Check
146# define PySequence_Size py3_PySequence_Size
147# define PySequence_GetItem py3_PySequence_GetItem
148# define PyTuple_Size py3_PyTuple_Size
149# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200150# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
151# define PyImport_ImportModule py3_PyImport_ImportModule
152# 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 Moolenaar3dab2802013-05-15 18:28:13 +0200166# define PyObject_GetAttrString py3_PyObject_GetAttrString
167# define PyObject_SetAttrString py3_PyObject_SetAttrString
168# define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs
169# define PyEval_GetLocals py3_PyEval_GetLocals
170# define PyEval_GetGlobals py3_PyEval_GetGlobals
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200171# define PySys_SetObject py3_PySys_SetObject
172# define PySys_SetArgv py3_PySys_SetArgv
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200173# define PyType_Ready py3_PyType_Ready
174#undef Py_BuildValue
175# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100176# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200177# define Py_Initialize py3_Py_Initialize
178# define Py_Finalize py3_Py_Finalize
179# define Py_IsInitialized py3_Py_IsInitialized
180# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200181# define _Py_FalseStruct (*py3__Py_FalseStruct)
182# define _Py_TrueStruct (*py3__Py_TrueStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200183# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200184# define PyModule_AddObject py3_PyModule_AddObject
185# define PyImport_AppendInittab py3_PyImport_AppendInittab
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200186# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200187# if PY_VERSION_HEX >= 0x030300f0
188# undef _PyUnicode_AsString
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200189# define _PyUnicode_AsString py3_PyUnicode_AsUTF8
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200190# else
191# define _PyUnicode_AsString py3__PyUnicode_AsString
192# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200193# undef PyUnicode_AsEncodedString
194# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
195# undef PyBytes_AsString
196# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200197# define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# undef PyBytes_FromString
199# define PyBytes_FromString py3_PyBytes_FromString
200# define PyFloat_FromDouble py3_PyFloat_FromDouble
201# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200202# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
Bram Moolenaar66b79852012-09-21 14:00:35 +0200203# define PyType_Type (*py3_PyType_Type)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200205# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200206# define PyBool_Type (*py3_PyBool_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200207# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200208# ifdef Py_DEBUG
209# define _Py_NegativeRefcount py3__Py_NegativeRefcount
210# define _Py_RefTotal (*py3__Py_RefTotal)
211# define _Py_Dealloc py3__Py_Dealloc
212# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
213# define _PyObject_DebugFree py3__PyObject_DebugFree
214# else
215# define PyObject_Malloc py3_PyObject_Malloc
216# define PyObject_Free py3_PyObject_Free
217# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200218# define PyType_GenericAlloc py3_PyType_GenericAlloc
219# define PyType_GenericNew py3_PyType_GenericNew
220# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200221# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200222# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200223# undef PyUnicode_Decode
224# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200225# define PyType_IsSubtype py3_PyType_IsSubtype
226# define PyCapsule_New py3_PyCapsule_New
227# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200228
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200229# ifdef Py_DEBUG
230# undef PyObject_NEW
231# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200232( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200233 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200234# endif
235
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200236/*
237 * Pointers for dynamic link
238 */
239static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100240static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200241static void (*py3_Py_Initialize)(void);
242static PyObject* (*py3_PyList_New)(Py_ssize_t size);
243static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
244static void (*py3_PyGILState_Release)(PyGILState_STATE);
245static int (*py3_PySys_SetObject)(char *, PyObject *);
246static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
247static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200248static int (*py3_PySequence_Check)(PyObject *);
249static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
250static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
251static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
252static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
253static int (*py3_PyMapping_Check)(PyObject *);
254static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200255static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200256 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200257static PyObject* (*py3_PyErr_NoMemory)(void);
258static void (*py3_Py_Finalize)(void);
259static void (*py3_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200260static void (*py3_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200261static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200262static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200263static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *);
264static PyObject* (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
265static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...);
266static PyObject* (*py3_PyEval_GetGlobals)();
267static PyObject* (*py3_PyEval_GetLocals)();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200268static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
269static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200270static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200271static int (*py3_PyErr_BadArgument)(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272static PyObject* (*py3_PyErr_Occurred)(void);
273static PyObject* (*py3_PyModule_GetDict)(PyObject *);
274static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
275static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200276static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200277static PyObject* (*py3_PyLong_FromLong)(long);
278static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200279static PyObject* (*py3_PyIter_Next)(PyObject *);
280static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200281static int (*py3_PyObject_IsTrue)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200282static PyObject* (*py3_Py_BuildValue)(char *, ...);
283static int (*py3_PyType_Ready)(PyTypeObject *type);
284static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
285static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200286static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
287 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200288static long (*py3_PyLong_AsLong)(PyObject *);
289static void (*py3_PyErr_SetNone)(PyObject *);
290static void (*py3_PyEval_InitThreads)(void);
291static void(*py3_PyEval_RestoreThread)(PyThreadState *);
292static PyThreadState*(*py3_PyEval_SaveThread)(void);
293static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
294static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200295static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200296static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200297static int (*py3_Py_IsInitialized)(void);
298static void (*py3_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100299static void (*py3_PyErr_PrintEx)(int);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200300static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200301static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200302static PyObject* py3__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200303static PyObject* py3__Py_FalseStruct;
304static PyObject* py3__Py_TrueStruct;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200305static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
306static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200307# if PY_VERSION_HEX >= 0x030300f0
308static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode);
309# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200310static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200311# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200312static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
313static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200314static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Bram Moolenaardb913952012-06-29 12:54:53 +0200315static PyObject* (*py3_PyBytes_FromString)(char *str);
316static PyObject* (*py3_PyFloat_FromDouble)(double num);
317static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200318static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
319static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
320static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
321static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200322static PyTypeObject* py3_PyType_Type;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200323static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200324static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200325static PyTypeObject* py3_PyBool_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200326static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200327static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
328static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200329# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200330 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
331 static Py_ssize_t* py3__Py_RefTotal;
332 static void (*py3__Py_Dealloc)(PyObject *obj);
333 static void (*py3__PyObject_DebugFree)(void*);
334 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200335# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200336 static void (*py3_PyObject_Free)(void*);
337 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200338# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200339static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200340
341static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
342
343/* Imported exception objects */
344static PyObject *p3imp_PyExc_AttributeError;
345static PyObject *p3imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200346static PyObject *p3imp_PyExc_KeyError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200347static PyObject *p3imp_PyExc_KeyboardInterrupt;
348static PyObject *p3imp_PyExc_TypeError;
349static PyObject *p3imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200350static PyObject *p3imp_PyExc_RuntimeError;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200351
352# define PyExc_AttributeError p3imp_PyExc_AttributeError
353# define PyExc_IndexError p3imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200354# define PyExc_KeyError p3imp_PyExc_KeyError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200355# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
356# define PyExc_TypeError p3imp_PyExc_TypeError
357# define PyExc_ValueError p3imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200358# define PyExc_RuntimeError p3imp_PyExc_RuntimeError
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359
360/*
361 * Table of name to function pointer of python.
362 */
363# define PYTHON_PROC FARPROC
364static struct
365{
366 char *name;
367 PYTHON_PROC *ptr;
368} py3_funcname_table[] =
369{
370 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100371 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200373# ifndef PY_SSIZE_T_CLEAN
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200374 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200375 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200376# else
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200377 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
378 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200379# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200380 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200381 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200382 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
383 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
384 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
385 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
386 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
387 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200388 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
389 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
390 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
391 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
392 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200393 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
394 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
395 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
396 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200397 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200398 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200399 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200400 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString},
401 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString},
402 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs},
403 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals},
404 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200405 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
406 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200407 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200408 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200409 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
410 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
411 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
412 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200413 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
414 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
415 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
416 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
417 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200418 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200419 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
420 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200421 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
422 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
423 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
424 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
425 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
426 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
427 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
428 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200429 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200430 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200431 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200432 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct},
433 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200434 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100435 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200436 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
437 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
438 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200439# if PY_VERSION_HEX >= 0x030300f0
440 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8},
441# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200442 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200443# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200444 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200445 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize},
Bram Moolenaardb913952012-06-29 12:54:53 +0200446 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
447 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
448 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200449 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
450 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
451 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
452 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200453 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200454 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200455 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200456 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200457 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200458# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200459 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
460 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
461 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
462 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
463 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200464# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200465 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
466 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200467# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200468 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
469 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
470 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200471 {"", NULL},
472};
473
474/*
475 * Free python.dll
476 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200477 static void
478end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200479{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200480 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200481 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200482 close_dll(hinstPy3);
483 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200484 }
485}
486
487/*
488 * Load library and get all pointers.
489 * Parameter 'libname' provides name of DLL.
490 * Return OK or FAIL.
491 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200492 static int
493py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494{
495 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200496 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200497
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100498# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200499 /* Can't have Python and Python3 loaded at the same time.
500 * It cause a crash, because RTLD_GLOBAL is needed for
501 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200502 if (python_loaded())
503 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200504 if (verbose)
505 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200506 return FAIL;
507 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200508# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200509
510 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200511 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200512 hinstPy3 = load_dll(libname);
513
514 if (!hinstPy3)
515 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200516 if (verbose)
517 EMSG2(_(e_loadlib), libname);
518 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200519 }
520
521 for (i = 0; py3_funcname_table[i].ptr; ++i)
522 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200523 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
524 py3_funcname_table[i].name)) == NULL)
525 {
526 close_dll(hinstPy3);
527 hinstPy3 = 0;
528 if (verbose)
529 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
530 return FAIL;
531 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200532 }
533
Bram Moolenaar69154f22010-07-18 21:42:34 +0200534 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
535 * will be present in the library. */
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200536# if PY_VERSION_HEX >= 0x030300f0
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200537 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString");
538 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode");
539 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
540 "PyUnicode_AsEncodedString");
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200541# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200542 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200543 ucs_decode = symbol_from_dll(hinstPy3,
544 "PyUnicodeUCS2_Decode");
545 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
546 "PyUnicodeUCS2_AsEncodedString");
547 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200548 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200549 ucs_from_string = symbol_from_dll(hinstPy3,
550 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200551 ucs_decode = symbol_from_dll(hinstPy3,
552 "PyUnicodeUCS4_Decode");
553 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
554 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200555 }
Bram Moolenaar9c9cbf12012-10-23 05:17:37 +0200556# endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200557 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200558 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200559 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200560 py3_PyUnicode_Decode = ucs_decode;
561 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200562 }
563 else
564 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200565 close_dll(hinstPy3);
566 hinstPy3 = 0;
567 if (verbose)
568 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
569 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200570 }
571
572 return OK;
573}
574
575/*
576 * If python is enabled (there is installed python on Windows system) return
577 * TRUE, else FALSE.
578 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200579 int
580python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200581{
582 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
583}
584
585/* Load the standard Python exceptions - don't import the symbols from the
586 * DLL, as this can cause errors (importing data symbols is not reliable).
587 */
588static void get_py3_exceptions __ARGS((void));
589
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200590 static void
591get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200592{
593 PyObject *exmod = PyImport_ImportModule("builtins");
594 PyObject *exdict = PyModule_GetDict(exmod);
595 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
596 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200597 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200598 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
599 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
600 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200601 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200602 Py_XINCREF(p3imp_PyExc_AttributeError);
603 Py_XINCREF(p3imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200604 Py_XINCREF(p3imp_PyExc_KeyError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200605 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
606 Py_XINCREF(p3imp_PyExc_TypeError);
607 Py_XINCREF(p3imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200608 Py_XINCREF(p3imp_PyExc_RuntimeError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200609 Py_XDECREF(exmod);
610}
611#endif /* DYNAMIC_PYTHON3 */
612
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100613static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200614
Bram Moolenaardb913952012-06-29 12:54:53 +0200615static int py3initialised = 0;
616
617#define PYINITIALISED py3initialised
618
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200619#define DICTKEY_DECL PyObject *bytes = NULL;
620
Bram Moolenaardb913952012-06-29 12:54:53 +0200621#define DICTKEY_GET(err) \
622 if (PyBytes_Check(keyObject)) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200623 { \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200624 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200625 return err; \
626 } \
Bram Moolenaardb913952012-06-29 12:54:53 +0200627 else if (PyUnicode_Check(keyObject)) \
628 { \
629 bytes = PyString_AsBytes(keyObject); \
630 if (bytes == NULL) \
631 return err; \
Bram Moolenaarafa6b9a2012-09-05 19:09:11 +0200632 if (PyString_AsStringAndSize(bytes, (char **) &key, NULL) == -1) \
Bram Moolenaardb913952012-06-29 12:54:53 +0200633 return err; \
634 } \
635 else \
636 { \
637 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
638 return err; \
639 }
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200640
Bram Moolenaardb913952012-06-29 12:54:53 +0200641#define DICTKEY_UNREF \
642 if (bytes != NULL) \
643 Py_XDECREF(bytes);
644
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200645#define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaardb913952012-06-29 12:54:53 +0200646
Bram Moolenaar971db462013-05-12 18:44:48 +0200647#define WIN_PYTHON_REF(win) win->w_python3_ref
648#define BUF_PYTHON_REF(buf) buf->b_python3_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200649#define TAB_PYTHON_REF(tab) tab->tp_python3_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200650
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200651 static void
652call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200653{
654#ifdef Py_DEBUG
655 _PyObject_DebugFree(p);
656#else
657 PyObject_Free(p);
658#endif
659}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200660
661 static PyObject *
662call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200663{
664 return PyType_GenericNew(type,args,kwds);
665}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200666
667 static PyObject *
668call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200669{
670 return PyType_GenericAlloc(type,nitems);
671}
672
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200673static PyObject *OutputGetattro(PyObject *, PyObject *);
674static int OutputSetattro(PyObject *, PyObject *, PyObject *);
675static PyObject *BufferGetattro(PyObject *, PyObject *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200676static PyObject *TabPageGetattro(PyObject *, PyObject *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200677static PyObject *WindowGetattro(PyObject *, PyObject *);
678static int WindowSetattro(PyObject *, PyObject *, PyObject *);
679static PyObject *RangeGetattro(PyObject *, PyObject *);
680static PyObject *CurrentGetattro(PyObject *, PyObject *);
681static int CurrentSetattro(PyObject *, PyObject *, PyObject *);
682static PyObject *DictionaryGetattro(PyObject *, PyObject *);
683static int DictionarySetattro(PyObject *, PyObject *, PyObject *);
684static PyObject *ListGetattro(PyObject *, PyObject *);
685static int ListSetattro(PyObject *, PyObject *, PyObject *);
686static PyObject *FunctionGetattro(PyObject *, PyObject *);
687
688static struct PyModuleDef vimmodule;
689
690/*
691 * Include the code shared with if_python.c
692 */
693#include "if_py_both.h"
694
695#define GET_ATTR_STRING(name, nameobj) \
696 char *name = ""; \
697 if (PyUnicode_Check(nameobj)) \
698 name = _PyUnicode_AsString(nameobj)
699
700#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
701
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200702/******************************************************
703 * Internal function prototypes.
704 */
705
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200706static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100707static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200708
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200709/******************************************************
710 * 1. Python interpreter main program.
711 */
712
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200713 void
714python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200715{
716 static int recurse = 0;
717
718 /* If a crash occurs while doing this, don't try again. */
719 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200720 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200721
722 ++recurse;
723
724#ifdef DYNAMIC_PYTHON3
725 if (hinstPy3)
726#endif
727 if (Py_IsInitialized())
728 {
729 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200730 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200731
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200733 }
734
735#ifdef DYNAMIC_PYTHON3
736 end_dynamic_python3();
737#endif
738
739 --recurse;
740}
741
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200742#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
743 int
744python3_loaded()
745{
746 return (hinstPy3 != 0);
747}
748#endif
749
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200750 static int
751Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200752{
753 if (!py3initialised)
754 {
755#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200756 if (!python3_enabled(TRUE))
757 {
758 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
759 goto fail;
760 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200761#endif
762
763 init_structs();
764
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100765
766#ifdef PYTHON3_HOME
767 Py_SetPythonHome(PYTHON3_HOME);
768#endif
769
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200770 PyImport_AppendInittab("vim", Py3Init_vim);
771
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200772#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200773 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200774#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200775 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200776#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100777 /* Initialise threads, and below save the state using
778 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
779 * specific state (such as the system trace hook), will be lost
780 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200781 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200782#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200783 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200784#endif
785
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200786 if (PythonIO_Init())
787 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200788
Bram Moolenaardb913952012-06-29 12:54:53 +0200789 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
790
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200791 /* Remove the element from sys.path that was added because of our
792 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200793 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200794 * the current directory in sys.path.
795 * Only after vim has been imported, the element does exist in
796 * sys.path.
797 */
798 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 +0200799
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100800 /* lock is created and acquired in PyEval_InitThreads() and thread
801 * state is created in Py_Initialize()
802 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
803 * (python must have threads enabled!)
804 * so the following does both: unlock GIL and save thread state in TLS
805 * without deleting thread state
806 */
807 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200808
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200809 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200810 }
811
812 return 0;
813
814fail:
815 /* We call PythonIO_Flush() here to print any Python errors.
816 * This is OK, as it is possible to call this function even
817 * if PythonIO_Init() has not completed successfully (it will
818 * not do anything in this case).
819 */
820 PythonIO_Flush();
821 return -1;
822}
823
824/*
825 * External interface
826 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200827 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200828DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200829{
830#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200831 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200832#endif
833#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200834 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200835#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200836 PyObject *cmdstr;
837 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +0200838 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200839
840#if defined(MACOS) && !defined(MACOS_X_UNIX)
841 GetPort(&oldPort);
842 /* Check if the Python library is available */
843 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200844 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200845#endif
846 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200847 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200848
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200849 init_range(arg);
850
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200851 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200852
853#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
854 /* Python only works properly when the LC_NUMERIC locale is "C". */
855 saved_locale = setlocale(LC_NUMERIC, NULL);
856 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200857 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200858 else
859 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200860 /* Need to make a copy, value may change when setting new locale. */
861 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
862 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200863 }
864#endif
865
866 pygilstate = PyGILState_Ensure();
867
Bram Moolenaar19e60942011-06-19 00:27:51 +0200868 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
869 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200870 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
871 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
872 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200873 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200874
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200875 run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200876 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200877
878 PyGILState_Release(pygilstate);
879
880#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
881 if (saved_locale != NULL)
882 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200883 (void)setlocale(LC_NUMERIC, saved_locale);
884 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200885 }
886#endif
887
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200888 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200889 PythonIO_Flush();
890#if defined(MACOS) && !defined(MACOS_X_UNIX)
891 SetPort(oldPort);
892#endif
893
894theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200895 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200896}
897
898/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200899 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200900 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200901 void
902ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200903{
904 char_u *script;
905
906 script = script_get(eap, eap->arg);
907 if (!eap->skip)
908 {
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200909 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
910 (rangeinitializer) init_range_cmd,
911 (runner) run_cmd,
912 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913 }
914 vim_free(script);
915}
916
917#define BUFFER_SIZE 2048
918
919/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200920 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200921 */
922 void
923ex_py3file(exarg_T *eap)
924{
925 static char buffer[BUFFER_SIZE];
926 const char *file;
927 char *p;
928 int i;
929
930 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
931 * stdio file pointer, but Vim and the Python DLL are compiled with
932 * different options under Windows, meaning that stdio pointers aren't
933 * compatible between the two. Yuk.
934 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200935 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
936 *
937 * Using bytes so that Python can detect the source encoding as it normally
938 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200939 *
940 * We need to escape any backslashes or single quotes in the file name, so that
941 * Python won't mangle the file name.
942 */
943
944 strcpy(buffer, "exec(compile(open('");
945 p = buffer + 19; /* size of "exec(compile(open('" */
946
947 for (i=0; i<2; ++i)
948 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200949 file = (char *)eap->arg;
950 while (*file && p < buffer + (BUFFER_SIZE - 3))
951 {
952 if (*file == '\\' || *file == '\'')
953 *p++ = '\\';
954 *p++ = *file++;
955 }
956 /* If we didn't finish the file name, we hit a buffer overflow */
957 if (*file != '\0')
958 return;
959 if (i==0)
960 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200961 strcpy(p,"','rb').read(),'");
962 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200963 }
964 else
965 {
966 strcpy(p,"','exec'))");
967 p += 10;
968 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200969 }
970
971
972 /* Execute the file */
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200973 DoPyCommand(buffer,
974 (rangeinitializer) init_range_cmd,
975 (runner) run_cmd,
976 (void *) eap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200977}
978
Bram Moolenaar54e8f002013-05-15 19:44:39 +0200979 void
980ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200981{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200982 DoPyCommand((char *)eap->arg,
983 (rangeinitializer)init_range_cmd,
984 (runner)run_do,
985 (void *)eap);
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200986}
987
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200988/******************************************************
989 * 2. Python output stream: writes output via [e]msg().
990 */
991
992/* Implementation functions
993 */
994
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200995 static PyObject *
996OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997{
Bram Moolenaar77045652012-09-21 13:46:06 +0200998 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999
1000 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001001 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001002
1003 return PyObject_GenericGetAttr(self, nameobj);
1004}
1005
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001006 static int
1007OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001008{
Bram Moolenaar77045652012-09-21 13:46:06 +02001009 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001010
Bram Moolenaar77045652012-09-21 13:46:06 +02001011 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001012}
1013
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001014/***************/
1015
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001016 static int
1017PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001018{
1019 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001020 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001021}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001022
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001023/******************************************************
1024 * 3. Implementation of the Vim module for Python
1025 */
1026
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001027/* Window type - Implementation functions
1028 * --------------------------------------
1029 */
1030
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001031#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1032
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001033/* Buffer type - Implementation functions
1034 * --------------------------------------
1035 */
1036
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001037#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1038
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001039static Py_ssize_t BufferLength(PyObject *);
1040static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001041static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1042static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001043
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001044
1045/* Line range type - Implementation functions
1046 * --------------------------------------
1047 */
1048
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1050
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001051static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001052static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001053static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001054
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001055/* Current objects type - Implementation functions
1056 * -----------------------------------------------
1057 */
1058
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001059static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001060 (lenfunc) BufferLength, /* sq_length, len(x) */
1061 (binaryfunc) 0, /* sq_concat, x+y */
1062 (ssizeargfunc) 0, /* sq_repeat, x*n */
1063 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1064 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001065 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001066 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001067 0, /* sq_contains */
1068 0, /* sq_inplace_concat */
1069 0, /* sq_inplace_repeat */
1070};
1071
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001072static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001073 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001074 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001075 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001076};
1077
1078
Bram Moolenaar971db462013-05-12 18:44:48 +02001079/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001080 */
1081
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001082 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001083BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001084{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001085 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001086
Bram Moolenaar77045652012-09-21 13:46:06 +02001087 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001088
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001089 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001090 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001091
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001092 r = BufferAttr((BufferObject *)(self), name);
1093 if (r || PyErr_Occurred())
1094 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001095 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001096 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001097}
1098
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001099 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001100BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1101{
1102 return Py_BuildValue("[sssss]", "name", "number",
1103 "append", "mark", "range");
1104}
1105
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001106/******************/
1107
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001108 static PyObject *
1109BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110{
Bram Moolenaardb913952012-06-29 12:54:53 +02001111 if (PyLong_Check(idx))
1112 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001113 long _idx = PyLong_AsLong(idx);
1114 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001115 } else if (PySlice_Check(idx))
1116 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001117 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001118
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001119 if (CheckBuffer((BufferObject *) self))
1120 return NULL;
1121
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001122 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001123 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001124 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001125 &step, &slicelen) < 0)
1126 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001127 return NULL;
1128 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001129 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001130 }
1131 else
1132 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001133 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001134 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135 }
1136}
1137
Bram Moolenaar19e60942011-06-19 00:27:51 +02001138 static Py_ssize_t
1139BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1140{
Bram Moolenaardb913952012-06-29 12:54:53 +02001141 if (PyLong_Check(idx))
1142 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001143 long n = PyLong_AsLong(idx);
1144 return RBAsItem((BufferObject *)(self), n, val, 1,
1145 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1146 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001147 } else if (PySlice_Check(idx))
1148 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001149 Py_ssize_t start, stop, step, slicelen;
1150
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001151 if (CheckBuffer((BufferObject *) self))
1152 return -1;
1153
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001154 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001155 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001156 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001157 &step, &slicelen) < 0)
1158 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001159 return -1;
1160 }
1161 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1162 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1163 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001164 }
1165 else
1166 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001167 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar19e60942011-06-19 00:27:51 +02001168 return -1;
1169 }
1170}
1171
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001172static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001173 (lenfunc) RangeLength, /* sq_length, len(x) */
1174 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1175 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1176 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1177 0, /* was_sq_slice, x[i:j] */
1178 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1179 0, /* sq_ass_slice, x[i:j]=v */
1180 0, /* sq_contains */
1181 0, /* sq_inplace_concat */
1182 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001183};
1184
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001185static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001186 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001187 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001188 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001189};
1190
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001191/* Line range object - Implementation
1192 */
1193
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001194 static PyObject *
1195RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196{
Bram Moolenaar77045652012-09-21 13:46:06 +02001197 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001198
1199 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001200 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001201 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001204 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001205}
1206
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207/****************/
1208
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001209 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001210RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001211{
1212 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001213 ((RangeObject *)(self))->start,
1214 ((RangeObject *)(self))->end,
1215 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001216}
1217
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001218 static Py_ssize_t
1219RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1220{
1221 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1222 ((RangeObject *)(self))->start,
1223 ((RangeObject *)(self))->end,
1224 &((RangeObject *)(self))->end);
1225}
1226
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001227 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001228RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001229{
Bram Moolenaardb913952012-06-29 12:54:53 +02001230 if (PyLong_Check(idx))
1231 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001232 long _idx = PyLong_AsLong(idx);
1233 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001234 } else if (PySlice_Check(idx))
1235 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001237
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001238 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1240 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001241 &step, &slicelen) < 0)
1242 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001243 return NULL;
1244 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001245 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001246 }
1247 else
1248 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001249 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001250 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001251 }
1252}
1253
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001254 static Py_ssize_t
1255RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1256{
Bram Moolenaardb913952012-06-29 12:54:53 +02001257 if (PyLong_Check(idx))
1258 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001259 long n = PyLong_AsLong(idx);
1260 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001261 } else if (PySlice_Check(idx))
1262 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001263 Py_ssize_t start, stop, step, slicelen;
1264
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001265 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001266 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1267 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001268 &step, &slicelen) < 0)
1269 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001270 return -1;
1271 }
1272 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001273 }
1274 else
1275 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001276 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001277 return -1;
1278 }
1279}
1280
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001281/* TabPage object - Implementation
1282 */
1283
1284 static PyObject *
1285TabPageGetattro(PyObject *self, PyObject *nameobj)
1286{
1287 PyObject *r;
1288
1289 GET_ATTR_STRING(name, nameobj);
1290
1291 if (CheckTabPage((TabPageObject *)(self)))
1292 return NULL;
1293
1294 r = TabPageAttr((TabPageObject *)(self), name);
1295 if (r || PyErr_Occurred())
1296 return r;
1297 else
1298 return PyObject_GenericGetAttr(self, nameobj);
1299}
1300
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001301/* Window object - Implementation
1302 */
1303
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001304 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001305WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001306{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001307 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001308
Bram Moolenaar77045652012-09-21 13:46:06 +02001309 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001310
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001311 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001314 r = WindowAttr((WindowObject *)(self), name);
1315 if (r || PyErr_Occurred())
1316 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001318 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001319}
1320
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001321 static int
1322WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323{
Bram Moolenaar77045652012-09-21 13:46:06 +02001324 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001325
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001326 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001327}
1328
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001329/* Tab page list object - Definitions
1330 */
1331
1332static PySequenceMethods TabListAsSeq = {
1333 (lenfunc) TabListLength, /* sq_length, len(x) */
1334 (binaryfunc) 0, /* sq_concat, x+y */
1335 (ssizeargfunc) 0, /* sq_repeat, x*n */
1336 (ssizeargfunc) TabListItem, /* sq_item, x[i] */
1337 0, /* sq_slice, x[i:j] */
1338 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1339 0, /* sq_ass_slice, x[i:j]=v */
1340 0, /* sq_contains */
1341 0, /* sq_inplace_concat */
1342 0, /* sq_inplace_repeat */
1343};
1344
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001345/* Window list object - Definitions
1346 */
1347
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001348static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001349 (lenfunc) WinListLength, /* sq_length, len(x) */
1350 (binaryfunc) 0, /* sq_concat, x+y */
1351 (ssizeargfunc) 0, /* sq_repeat, x*n */
1352 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1353 0, /* sq_slice, x[i:j] */
1354 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1355 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001356 0, /* sq_contains */
1357 0, /* sq_inplace_concat */
1358 0, /* sq_inplace_repeat */
1359};
1360
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001361/* Current items object - Implementation
1362 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001363 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001364CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001365{
Bram Moolenaar77045652012-09-21 13:46:06 +02001366 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001367 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001368}
1369
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001370 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001371CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001372{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001373 GET_ATTR_STRING(name, nameobj);
1374 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001375}
1376
Bram Moolenaardb913952012-06-29 12:54:53 +02001377/* Dictionary object - Definitions
1378 */
1379
1380static PyInt DictionaryLength(PyObject *);
1381
Bram Moolenaar66b79852012-09-21 14:00:35 +02001382 static PyObject *
1383DictionaryGetattro(PyObject *self, PyObject *nameobj)
1384{
1385 DictionaryObject *this = ((DictionaryObject *) (self));
1386
1387 GET_ATTR_STRING(name, nameobj);
1388
1389 if (strcmp(name, "locked") == 0)
1390 return PyLong_FromLong(this->dict->dv_lock);
1391 else if (strcmp(name, "scope") == 0)
1392 return PyLong_FromLong(this->dict->dv_scope);
1393
1394 return PyObject_GenericGetAttr(self, nameobj);
1395}
1396
1397 static int
1398DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1399{
1400 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001401 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001402}
1403
1404/* List object - Definitions
1405 */
1406
1407static PyInt ListLength(PyObject *);
1408static PyObject *ListItem(PyObject *, Py_ssize_t);
1409
1410static PySequenceMethods ListAsSeq = {
1411 (lenfunc) ListLength, /* sq_length, len(x) */
1412 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1413 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1414 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1415 (void *) 0, /* was_sq_slice, x[i:j] */
1416 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1417 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1418 0, /* sq_contains */
1419 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1420 0, /* sq_inplace_repeat */
1421};
1422
1423static PyObject *ListSubscript(PyObject *, PyObject *);
1424static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1425
1426static PyMappingMethods ListAsMapping = {
1427 /* mp_length */ (lenfunc) ListLength,
1428 /* mp_subscript */ (binaryfunc) ListSubscript,
1429 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1430};
1431
Bram Moolenaardb913952012-06-29 12:54:53 +02001432 static PyObject *
1433ListSubscript(PyObject *self, PyObject* idxObject)
1434{
1435 if (PyLong_Check(idxObject))
1436 {
1437 long idx = PyLong_AsLong(idxObject);
1438 return ListItem(self, idx);
1439 }
1440 else if (PySlice_Check(idxObject))
1441 {
1442 Py_ssize_t start, stop, step, slicelen;
1443
1444 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1445 &step, &slicelen) < 0)
1446 return NULL;
1447 return ListSlice(self, start, stop);
1448 }
1449 else
1450 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001451 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001452 return NULL;
1453 }
1454}
1455
1456 static Py_ssize_t
1457ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1458{
1459 if (PyLong_Check(idxObject))
1460 {
1461 long idx = PyLong_AsLong(idxObject);
1462 return ListAssItem(self, idx, obj);
1463 }
1464 else if (PySlice_Check(idxObject))
1465 {
1466 Py_ssize_t start, stop, step, slicelen;
1467
1468 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1469 &step, &slicelen) < 0)
1470 return -1;
1471 return ListAssSlice(self, start, stop, obj);
1472 }
1473 else
1474 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001475 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001476 return -1;
1477 }
1478}
1479
Bram Moolenaar66b79852012-09-21 14:00:35 +02001480 static PyObject *
1481ListGetattro(PyObject *self, PyObject *nameobj)
1482{
1483 GET_ATTR_STRING(name, nameobj);
1484
1485 if (strcmp(name, "locked") == 0)
1486 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1487
1488 return PyObject_GenericGetAttr(self, nameobj);
1489}
1490
1491 static int
1492ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1493{
1494 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001495 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001496}
1497
1498/* Function object - Definitions
1499 */
1500
Bram Moolenaardb913952012-06-29 12:54:53 +02001501 static PyObject *
1502FunctionGetattro(PyObject *self, PyObject *nameobj)
1503{
1504 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001505
1506 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001507
1508 if (strcmp(name, "name") == 0)
1509 return PyUnicode_FromString((char *)(this->name));
1510
1511 return PyObject_GenericGetAttr(self, nameobj);
1512}
1513
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001514/* External interface
1515 */
1516
1517 void
1518python3_buffer_free(buf_T *buf)
1519{
Bram Moolenaar971db462013-05-12 18:44:48 +02001520 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001522 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001523 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001524 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525 }
1526}
1527
1528#if defined(FEAT_WINDOWS) || defined(PROTO)
1529 void
1530python3_window_free(win_T *win)
1531{
Bram Moolenaar971db462013-05-12 18:44:48 +02001532 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001533 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001534 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001535 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001536 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001537 }
1538}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001539
1540 void
1541python3_tabpage_free(tabpage_T *tab)
1542{
1543 if (TAB_PYTHON_REF(tab) != NULL)
1544 {
1545 TabPageObject *tp = TAB_PYTHON_REF(tab);
1546 tp->tab = INVALID_TABPAGE_VALUE;
1547 TAB_PYTHON_REF(tab) = NULL;
1548 }
1549}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001550#endif
1551
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001552static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001553{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001554 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001555};
1556
1557static WinListObject TheWindowList =
1558{
1559 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001560 NULL
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001561};
1562
1563static CurrentObject TheCurrent =
1564{
1565 PyObject_HEAD_INIT(&CurrentType)
1566};
1567
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001568static TabListObject TheTabPageList =
1569{
1570 PyObject_HEAD_INIT(&TabListType)
1571};
1572
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001573 static PyObject *
1574Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001575{
1576 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001577 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001578 /* The special value is removed from sys.path in Python3_Init(). */
1579 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1580
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001581 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001582 PyType_Ready(&BufferType);
1583 PyType_Ready(&RangeType);
1584 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001585 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001586 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001587 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001588 PyType_Ready(&TabListType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001589 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001590 PyType_Ready(&DictionaryType);
1591 PyType_Ready(&ListType);
1592 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001593 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001594
1595 /* Set sys.argv[] to avoid a crash in warn(). */
1596 PySys_SetArgv(1, argv);
1597
1598 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001599 if (mod == NULL)
1600 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001601
Bram Moolenaar19e60942011-06-19 00:27:51 +02001602 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001603
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001604 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001605 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001606 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1607 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001608 Py_INCREF((PyObject *)(void *)&TheCurrent);
1609 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1610 Py_INCREF((PyObject *)(void *)&TheWindowList);
1611 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001612 Py_INCREF((PyObject *)(void *)&TheTabPageList);
1613 PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001614
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001615 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1616 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001617 PyModule_AddObject(mod, "options",
1618 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001619
Bram Moolenaar66b79852012-09-21 14:00:35 +02001620#define ADD_INT_CONSTANT(name, value) \
1621 tmp = PyLong_FromLong(value); \
1622 Py_INCREF(tmp); \
1623 PyModule_AddObject(mod, name, tmp)
1624
1625 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1626 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1627 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1628 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1629
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001630 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001631 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001632
1633 return mod;
1634}
1635
1636/*************************************************************************
1637 * 4. Utility functions for handling the interface between Vim and Python.
1638 */
1639
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001640/* Convert a Vim line into a Python string.
1641 * All internal newlines are replaced by null characters.
1642 *
1643 * On errors, the Python exception data is set, and NULL is returned.
1644 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001645 static PyObject *
1646LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001647{
1648 PyObject *result;
1649 Py_ssize_t len = strlen(str);
1650 char *tmp,*p;
1651
1652 tmp = (char *)alloc((unsigned)(len+1));
1653 p = tmp;
1654 if (p == NULL)
1655 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001656 PyErr_NoMemory();
1657 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001658 }
1659
1660 while (*str)
1661 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001662 if (*str == '\n')
1663 *p = '\0';
1664 else
1665 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001666
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001667 ++p;
1668 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001669 }
1670 *p = '\0';
1671
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001672 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001673
1674 vim_free(tmp);
1675 return result;
1676}
1677
Bram Moolenaardb913952012-06-29 12:54:53 +02001678 void
1679do_py3eval (char_u *str, typval_T *rettv)
1680{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001681 DoPyCommand((char *) str,
1682 (rangeinitializer) init_range_eval,
1683 (runner) run_eval,
1684 (void *) rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001685 switch(rettv->v_type)
1686 {
1687 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1688 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1689 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001690 case VAR_UNKNOWN:
1691 rettv->v_type = VAR_NUMBER;
1692 rettv->vval.v_number = 0;
1693 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001694 }
1695}
1696
1697 void
1698set_ref_in_python3 (int copyID)
1699{
1700 set_ref_in_py(copyID);
1701}