blob: 0aeb2cd00a9f252697619425eb7b518e3aed7da6 [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 Moolenaardb913952012-06-29 12:54:53 +0200706static PyObject *globals;
707
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200708static int PythonIO_Init(void);
Bram Moolenaar7854e3a2012-11-28 15:33:14 +0100709static PyObject *Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200710
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200711/******************************************************
712 * 1. Python interpreter main program.
713 */
714
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200715 void
716python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200717{
718 static int recurse = 0;
719
720 /* If a crash occurs while doing this, don't try again. */
721 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200722 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200723
724 ++recurse;
725
726#ifdef DYNAMIC_PYTHON3
727 if (hinstPy3)
728#endif
729 if (Py_IsInitialized())
730 {
731 // acquire lock before finalizing
Bram Moolenaar71700b82013-05-15 17:49:05 +0200732 PyGILState_Ensure();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200733
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200734 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200735 }
736
737#ifdef DYNAMIC_PYTHON3
738 end_dynamic_python3();
739#endif
740
741 --recurse;
742}
743
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200744#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
745 int
746python3_loaded()
747{
748 return (hinstPy3 != 0);
749}
750#endif
751
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200752 static int
753Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200754{
755 if (!py3initialised)
756 {
757#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200758 if (!python3_enabled(TRUE))
759 {
760 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
761 goto fail;
762 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200763#endif
764
765 init_structs();
766
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100767
768#ifdef PYTHON3_HOME
769 Py_SetPythonHome(PYTHON3_HOME);
770#endif
771
Bram Moolenaar7bc4f932012-10-14 03:22:56 +0200772 PyImport_AppendInittab("vim", Py3Init_vim);
773
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200774#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200775 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200776#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200777 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200778#endif
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100779 /* Initialise threads, and below save the state using
780 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
781 * specific state (such as the system trace hook), will be lost
782 * between invocations of Python code. */
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200783 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200784#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200785 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200786#endif
787
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200788 if (PythonIO_Init())
789 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200790
Bram Moolenaardb913952012-06-29 12:54:53 +0200791 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
792
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200793 /* Remove the element from sys.path that was added because of our
794 * argv[0] value in Py3Init_vim(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200795 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200796 * the current directory in sys.path.
797 * Only after vim has been imported, the element does exist in
798 * sys.path.
799 */
800 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 +0200801
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100802 /* lock is created and acquired in PyEval_InitThreads() and thread
803 * state is created in Py_Initialize()
804 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
805 * (python must have threads enabled!)
806 * so the following does both: unlock GIL and save thread state in TLS
807 * without deleting thread state
808 */
809 PyEval_SaveThread();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200810
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200811 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200812 }
813
814 return 0;
815
816fail:
817 /* We call PythonIO_Flush() here to print any Python errors.
818 * This is OK, as it is possible to call this function even
819 * if PythonIO_Init() has not completed successfully (it will
820 * not do anything in this case).
821 */
822 PythonIO_Flush();
823 return -1;
824}
825
826/*
827 * External interface
828 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200829 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200830DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200831{
832#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200833 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200834#endif
835#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200836 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200838 PyObject *cmdstr;
839 PyObject *cmdbytes;
Bram Moolenaar71700b82013-05-15 17:49:05 +0200840 PyGILState_STATE pygilstate;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841
842#if defined(MACOS) && !defined(MACOS_X_UNIX)
843 GetPort(&oldPort);
844 /* Check if the Python library is available */
845 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200846 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200847#endif
848 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200849 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200850
Bram Moolenaardb913952012-06-29 12:54:53 +0200851 if (rettv == NULL)
852 {
853 RangeStart = eap->line1;
854 RangeEnd = eap->line2;
855 }
856 else
857 {
858 RangeStart = (PyInt) curwin->w_cursor.lnum;
859 RangeEnd = RangeStart;
860 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200861 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200862
863#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
864 /* Python only works properly when the LC_NUMERIC locale is "C". */
865 saved_locale = setlocale(LC_NUMERIC, NULL);
866 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200867 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200868 else
869 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200870 /* Need to make a copy, value may change when setting new locale. */
871 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
872 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200873 }
874#endif
875
876 pygilstate = PyGILState_Ensure();
877
Bram Moolenaar19e60942011-06-19 00:27:51 +0200878 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
879 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200880 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
881 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
882 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200883 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200884 if (rettv == NULL)
885 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
886 else
887 {
888 PyObject *r;
889
890 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
891 globals, globals);
892 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100893 {
894 if (PyErr_Occurred() && !msg_silent)
895 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200896 EMSG(_("E860: Eval did not return a valid python 3 object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100897 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200898 else
899 {
900 if (ConvertFromPyObject(r, rettv) == -1)
901 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
902 Py_DECREF(r);
903 }
904 PyErr_Clear();
905 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200906 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200907
908 PyGILState_Release(pygilstate);
909
910#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
911 if (saved_locale != NULL)
912 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200913 (void)setlocale(LC_NUMERIC, saved_locale);
914 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200915 }
916#endif
917
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200918 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200919 PythonIO_Flush();
920#if defined(MACOS) && !defined(MACOS_X_UNIX)
921 SetPort(oldPort);
922#endif
923
924theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200925 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200926}
927
928/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200929 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200930 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200931 void
932ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200933{
934 char_u *script;
935
936 script = script_get(eap, eap->arg);
937 if (!eap->skip)
938 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200939 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200940 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200941 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200943 }
944 vim_free(script);
945}
946
947#define BUFFER_SIZE 2048
948
949/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200950 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200951 */
952 void
953ex_py3file(exarg_T *eap)
954{
955 static char buffer[BUFFER_SIZE];
956 const char *file;
957 char *p;
958 int i;
959
960 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
961 * stdio file pointer, but Vim and the Python DLL are compiled with
962 * different options under Windows, meaning that stdio pointers aren't
963 * compatible between the two. Yuk.
964 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200965 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
966 *
967 * Using bytes so that Python can detect the source encoding as it normally
968 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200969 *
970 * We need to escape any backslashes or single quotes in the file name, so that
971 * Python won't mangle the file name.
972 */
973
974 strcpy(buffer, "exec(compile(open('");
975 p = buffer + 19; /* size of "exec(compile(open('" */
976
977 for (i=0; i<2; ++i)
978 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200979 file = (char *)eap->arg;
980 while (*file && p < buffer + (BUFFER_SIZE - 3))
981 {
982 if (*file == '\\' || *file == '\'')
983 *p++ = '\\';
984 *p++ = *file++;
985 }
986 /* If we didn't finish the file name, we hit a buffer overflow */
987 if (*file != '\0')
988 return;
989 if (i==0)
990 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200991 strcpy(p,"','rb').read(),'");
992 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200993 }
994 else
995 {
996 strcpy(p,"','exec'))");
997 p += 10;
998 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200999 }
1000
1001
1002 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +02001003 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001004}
1005
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001006 void
1007ex_py3do(exarg_T *eap)
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001008{
1009 linenr_T i;
1010 const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
1011 const char *s = (const char *) eap->arg;
1012 size_t len;
1013 char *code;
1014 int status;
1015 PyObject *pyfunc, *pymain;
1016 PyGILState_STATE pygilstate;
1017
1018 if (Python3_Init())
1019 goto theend;
1020
1021 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1022 {
1023 EMSG(_("cannot save undo information"));
1024 return;
1025 }
1026 len = strlen(code_hdr) + strlen(s);
1027 code = malloc(len + 1);
1028 STRCPY(code, code_hdr);
1029 STRNCAT(code, s, len + 1);
1030 pygilstate = PyGILState_Ensure();
1031 status = PyRun_SimpleString(code);
1032 vim_free(code);
1033 if (status)
1034 {
1035 EMSG(_("failed to run the code"));
1036 return;
1037 }
1038 status = 0; /* good */
1039 pymain = PyImport_AddModule("__main__");
1040 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
1041 PyGILState_Release(pygilstate);
1042
1043 for (i = eap->line1; i <= eap->line2; i++)
1044 {
1045 const char *line;
1046 PyObject *pyline, *pylinenr, *pyret, *pybytes;
1047
1048 line = (char *)ml_get(i);
1049 pygilstate = PyGILState_Ensure();
1050 pyline = PyUnicode_Decode(line, strlen(line),
1051 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1052 pylinenr = PyLong_FromLong(i);
1053 pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
1054 Py_DECREF(pyline);
1055 Py_DECREF(pylinenr);
1056 if (!pyret)
1057 {
1058 PyErr_PrintEx(0);
1059 PythonIO_Flush();
1060 status = 1;
1061 goto out;
1062 }
1063
1064 if (pyret && pyret != Py_None)
1065 {
1066 if (!PyUnicode_Check(pyret))
1067 {
Bram Moolenaar54e8f002013-05-15 19:44:39 +02001068 EMSG(_("E863: return value must be an instance of str"));
Bram Moolenaar3dab2802013-05-15 18:28:13 +02001069 Py_XDECREF(pyret);
1070 status = 1;
1071 goto out;
1072 }
1073 pybytes = PyUnicode_AsEncodedString(pyret,
1074 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
1075 ml_replace(i, (char_u *) PyBytes_AsString(pybytes), 1);
1076 Py_DECREF(pybytes);
1077 changed();
1078#ifdef SYNTAX_HL
1079 syn_changed(i); /* recompute syntax hl. for this line */
1080#endif
1081 }
1082 Py_XDECREF(pyret);
1083 PythonIO_Flush();
1084 PyGILState_Release(pygilstate);
1085 }
1086 pygilstate = PyGILState_Ensure();
1087out:
1088 Py_DECREF(pyfunc);
1089 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
1090 PyGILState_Release(pygilstate);
1091 if (status)
1092 return;
1093 check_cursor();
1094 update_curbuf(NOT_VALID);
1095
1096theend:
1097 return;
1098}
1099
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001100/******************************************************
1101 * 2. Python output stream: writes output via [e]msg().
1102 */
1103
1104/* Implementation functions
1105 */
1106
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001107 static PyObject *
1108OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001109{
Bram Moolenaar77045652012-09-21 13:46:06 +02001110 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001111
1112 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001113 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001114
1115 return PyObject_GenericGetAttr(self, nameobj);
1116}
1117
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001118 static int
1119OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001120{
Bram Moolenaar77045652012-09-21 13:46:06 +02001121 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001122
Bram Moolenaar77045652012-09-21 13:46:06 +02001123 return OutputSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124}
1125
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001126/***************/
1127
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001128 static int
1129PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001130{
1131 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001132 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001133}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001134
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001135/******************************************************
1136 * 3. Implementation of the Vim module for Python
1137 */
1138
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001139/* Window type - Implementation functions
1140 * --------------------------------------
1141 */
1142
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001143#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
1144
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001145/* Buffer type - Implementation functions
1146 * --------------------------------------
1147 */
1148
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001149#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
1150
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001151static Py_ssize_t BufferLength(PyObject *);
1152static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001153static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
1154static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001155
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001156
1157/* Line range type - Implementation functions
1158 * --------------------------------------
1159 */
1160
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001161#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
1162
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001163static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001165static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001166
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001167/* Current objects type - Implementation functions
1168 * -----------------------------------------------
1169 */
1170
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001171static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001172 (lenfunc) BufferLength, /* sq_length, len(x) */
1173 (binaryfunc) 0, /* sq_concat, x+y */
1174 (ssizeargfunc) 0, /* sq_repeat, x*n */
1175 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1176 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001177 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001178 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001179 0, /* sq_contains */
1180 0, /* sq_inplace_concat */
1181 0, /* sq_inplace_repeat */
1182};
1183
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001184static PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001185 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001186 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001187 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001188};
1189
1190
Bram Moolenaar971db462013-05-12 18:44:48 +02001191/* Buffer object
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001192 */
1193
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001194 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001195BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001196{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001197 PyObject *r;
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
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001201 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001202 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001203
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001204 r = BufferAttr((BufferObject *)(self), name);
1205 if (r || PyErr_Occurred())
1206 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001207 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001208 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001209}
1210
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001211 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001212BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1213{
1214 return Py_BuildValue("[sssss]", "name", "number",
1215 "append", "mark", "range");
1216}
1217
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001218/******************/
1219
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001220 static PyObject *
1221BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001222{
Bram Moolenaardb913952012-06-29 12:54:53 +02001223 if (PyLong_Check(idx))
1224 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001225 long _idx = PyLong_AsLong(idx);
1226 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001227 } else if (PySlice_Check(idx))
1228 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001229 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001230
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001231 if (CheckBuffer((BufferObject *) self))
1232 return NULL;
1233
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001234 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001235 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001236 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001237 &step, &slicelen) < 0)
1238 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001239 return NULL;
1240 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001241 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001242 }
1243 else
1244 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001245 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247 }
1248}
1249
Bram Moolenaar19e60942011-06-19 00:27:51 +02001250 static Py_ssize_t
1251BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1252{
Bram Moolenaardb913952012-06-29 12:54:53 +02001253 if (PyLong_Check(idx))
1254 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001255 long n = PyLong_AsLong(idx);
1256 return RBAsItem((BufferObject *)(self), n, val, 1,
1257 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1258 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001259 } else if (PySlice_Check(idx))
1260 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001261 Py_ssize_t start, stop, step, slicelen;
1262
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001263 if (CheckBuffer((BufferObject *) self))
1264 return -1;
1265
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001266 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarbd80f352013-05-12 21:16:23 +02001267 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001268 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001269 &step, &slicelen) < 0)
1270 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001271 return -1;
1272 }
1273 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1274 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1275 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001276 }
1277 else
1278 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001279 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar19e60942011-06-19 00:27:51 +02001280 return -1;
1281 }
1282}
1283
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001284static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001285 (lenfunc) RangeLength, /* sq_length, len(x) */
1286 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1287 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1288 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1289 0, /* was_sq_slice, x[i:j] */
1290 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1291 0, /* sq_ass_slice, x[i:j]=v */
1292 0, /* sq_contains */
1293 0, /* sq_inplace_concat */
1294 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001295};
1296
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001297static PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001299 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001300 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001301};
1302
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001303/* Line range object - Implementation
1304 */
1305
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001306 static PyObject *
1307RangeGetattro(PyObject *self, PyObject *nameobj)
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
1311 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001312 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001313 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001314 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001315 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001316 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001317}
1318
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001319/****************/
1320
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001321 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001322RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001323{
1324 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001325 ((RangeObject *)(self))->start,
1326 ((RangeObject *)(self))->end,
1327 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001328}
1329
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001330 static Py_ssize_t
1331RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1332{
1333 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1334 ((RangeObject *)(self))->start,
1335 ((RangeObject *)(self))->end,
1336 &((RangeObject *)(self))->end);
1337}
1338
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001339 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001340RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001341{
Bram Moolenaardb913952012-06-29 12:54:53 +02001342 if (PyLong_Check(idx))
1343 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001344 long _idx = PyLong_AsLong(idx);
1345 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001346 } else if (PySlice_Check(idx))
1347 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001348 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001349
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001350 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001351 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1352 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001353 &step, &slicelen) < 0)
1354 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001355 return NULL;
1356 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001357 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001358 }
1359 else
1360 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001361 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001362 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363 }
1364}
1365
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001366 static Py_ssize_t
1367RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1368{
Bram Moolenaardb913952012-06-29 12:54:53 +02001369 if (PyLong_Check(idx))
1370 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001371 long n = PyLong_AsLong(idx);
1372 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001373 } else if (PySlice_Check(idx))
1374 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001375 Py_ssize_t start, stop, step, slicelen;
1376
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001377 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001378 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1379 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001380 &step, &slicelen) < 0)
1381 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001382 return -1;
1383 }
1384 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001385 }
1386 else
1387 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001388 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001389 return -1;
1390 }
1391}
1392
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001393/* TabPage object - Implementation
1394 */
1395
1396 static PyObject *
1397TabPageGetattro(PyObject *self, PyObject *nameobj)
1398{
1399 PyObject *r;
1400
1401 GET_ATTR_STRING(name, nameobj);
1402
1403 if (CheckTabPage((TabPageObject *)(self)))
1404 return NULL;
1405
1406 r = TabPageAttr((TabPageObject *)(self), name);
1407 if (r || PyErr_Occurred())
1408 return r;
1409 else
1410 return PyObject_GenericGetAttr(self, nameobj);
1411}
1412
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001413/* Window object - Implementation
1414 */
1415
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001416 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001417WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001418{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001419 PyObject *r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
Bram Moolenaar77045652012-09-21 13:46:06 +02001421 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001422
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001423 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001424 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001425
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001426 r = WindowAttr((WindowObject *)(self), name);
1427 if (r || PyErr_Occurred())
1428 return r;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001429 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001430 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001431}
1432
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001433 static int
1434WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435{
Bram Moolenaar77045652012-09-21 13:46:06 +02001436 GET_ATTR_STRING(name, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001437
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001438 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001439}
1440
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001441/* Tab page list object - Definitions
1442 */
1443
1444static PySequenceMethods TabListAsSeq = {
1445 (lenfunc) TabListLength, /* sq_length, len(x) */
1446 (binaryfunc) 0, /* sq_concat, x+y */
1447 (ssizeargfunc) 0, /* sq_repeat, x*n */
1448 (ssizeargfunc) TabListItem, /* sq_item, x[i] */
1449 0, /* sq_slice, x[i:j] */
1450 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1451 0, /* sq_ass_slice, x[i:j]=v */
1452 0, /* sq_contains */
1453 0, /* sq_inplace_concat */
1454 0, /* sq_inplace_repeat */
1455};
1456
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001457/* Window list object - Definitions
1458 */
1459
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001460static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001461 (lenfunc) WinListLength, /* sq_length, len(x) */
1462 (binaryfunc) 0, /* sq_concat, x+y */
1463 (ssizeargfunc) 0, /* sq_repeat, x*n */
1464 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1465 0, /* sq_slice, x[i:j] */
1466 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1467 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001468 0, /* sq_contains */
1469 0, /* sq_inplace_concat */
1470 0, /* sq_inplace_repeat */
1471};
1472
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473/* Current items object - Implementation
1474 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001475 static PyObject *
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001476CurrentGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477{
Bram Moolenaar77045652012-09-21 13:46:06 +02001478 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001479 return CurrentGetattr(self, name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480}
1481
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001482 static int
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001483CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001485 GET_ATTR_STRING(name, nameobj);
1486 return CurrentSetattr(self, name, value);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487}
1488
Bram Moolenaardb913952012-06-29 12:54:53 +02001489/* Dictionary object - Definitions
1490 */
1491
1492static PyInt DictionaryLength(PyObject *);
1493
Bram Moolenaar66b79852012-09-21 14:00:35 +02001494 static PyObject *
1495DictionaryGetattro(PyObject *self, PyObject *nameobj)
1496{
1497 DictionaryObject *this = ((DictionaryObject *) (self));
1498
1499 GET_ATTR_STRING(name, nameobj);
1500
1501 if (strcmp(name, "locked") == 0)
1502 return PyLong_FromLong(this->dict->dv_lock);
1503 else if (strcmp(name, "scope") == 0)
1504 return PyLong_FromLong(this->dict->dv_scope);
1505
1506 return PyObject_GenericGetAttr(self, nameobj);
1507}
1508
1509 static int
1510DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1511{
1512 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001513 return DictionarySetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001514}
1515
1516/* List object - Definitions
1517 */
1518
1519static PyInt ListLength(PyObject *);
1520static PyObject *ListItem(PyObject *, Py_ssize_t);
1521
1522static PySequenceMethods ListAsSeq = {
1523 (lenfunc) ListLength, /* sq_length, len(x) */
1524 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1525 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1526 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1527 (void *) 0, /* was_sq_slice, x[i:j] */
1528 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1529 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1530 0, /* sq_contains */
1531 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1532 0, /* sq_inplace_repeat */
1533};
1534
1535static PyObject *ListSubscript(PyObject *, PyObject *);
1536static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1537
1538static PyMappingMethods ListAsMapping = {
1539 /* mp_length */ (lenfunc) ListLength,
1540 /* mp_subscript */ (binaryfunc) ListSubscript,
1541 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1542};
1543
Bram Moolenaardb913952012-06-29 12:54:53 +02001544 static PyObject *
1545ListSubscript(PyObject *self, PyObject* idxObject)
1546{
1547 if (PyLong_Check(idxObject))
1548 {
1549 long idx = PyLong_AsLong(idxObject);
1550 return ListItem(self, idx);
1551 }
1552 else if (PySlice_Check(idxObject))
1553 {
1554 Py_ssize_t start, stop, step, slicelen;
1555
1556 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1557 &step, &slicelen) < 0)
1558 return NULL;
1559 return ListSlice(self, start, stop);
1560 }
1561 else
1562 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001563 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001564 return NULL;
1565 }
1566}
1567
1568 static Py_ssize_t
1569ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1570{
1571 if (PyLong_Check(idxObject))
1572 {
1573 long idx = PyLong_AsLong(idxObject);
1574 return ListAssItem(self, idx, obj);
1575 }
1576 else if (PySlice_Check(idxObject))
1577 {
1578 Py_ssize_t start, stop, step, slicelen;
1579
1580 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1581 &step, &slicelen) < 0)
1582 return -1;
1583 return ListAssSlice(self, start, stop, obj);
1584 }
1585 else
1586 {
Bram Moolenaar8661b172013-05-15 15:44:28 +02001587 PyErr_SetString(PyExc_TypeError, _("index must be int or slice"));
Bram Moolenaardb913952012-06-29 12:54:53 +02001588 return -1;
1589 }
1590}
1591
Bram Moolenaar66b79852012-09-21 14:00:35 +02001592 static PyObject *
1593ListGetattro(PyObject *self, PyObject *nameobj)
1594{
1595 GET_ATTR_STRING(name, nameobj);
1596
1597 if (strcmp(name, "locked") == 0)
1598 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock);
1599
1600 return PyObject_GenericGetAttr(self, nameobj);
1601}
1602
1603 static int
1604ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
1605{
1606 GET_ATTR_STRING(name, nameobj);
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001607 return ListSetattr(self, name, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001608}
1609
1610/* Function object - Definitions
1611 */
1612
Bram Moolenaardb913952012-06-29 12:54:53 +02001613 static PyObject *
1614FunctionGetattro(PyObject *self, PyObject *nameobj)
1615{
1616 FunctionObject *this = (FunctionObject *)(self);
Bram Moolenaar77045652012-09-21 13:46:06 +02001617
1618 GET_ATTR_STRING(name, nameobj);
Bram Moolenaardb913952012-06-29 12:54:53 +02001619
1620 if (strcmp(name, "name") == 0)
1621 return PyUnicode_FromString((char *)(this->name));
1622
1623 return PyObject_GenericGetAttr(self, nameobj);
1624}
1625
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001626/* External interface
1627 */
1628
1629 void
1630python3_buffer_free(buf_T *buf)
1631{
Bram Moolenaar971db462013-05-12 18:44:48 +02001632 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001633 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001634 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001635 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001636 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001637 }
1638}
1639
1640#if defined(FEAT_WINDOWS) || defined(PROTO)
1641 void
1642python3_window_free(win_T *win)
1643{
Bram Moolenaar971db462013-05-12 18:44:48 +02001644 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001645 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001646 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001647 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001648 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001649 }
1650}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001651
1652 void
1653python3_tabpage_free(tabpage_T *tab)
1654{
1655 if (TAB_PYTHON_REF(tab) != NULL)
1656 {
1657 TabPageObject *tp = TAB_PYTHON_REF(tab);
1658 tp->tab = INVALID_TABPAGE_VALUE;
1659 TAB_PYTHON_REF(tab) = NULL;
1660 }
1661}
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001662#endif
1663
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001664static BufMapObject TheBufferMap =
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001665{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001666 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001667};
1668
1669static WinListObject TheWindowList =
1670{
1671 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001672 NULL
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001673};
1674
1675static CurrentObject TheCurrent =
1676{
1677 PyObject_HEAD_INIT(&CurrentType)
1678};
1679
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001680static TabListObject TheTabPageList =
1681{
1682 PyObject_HEAD_INIT(&TabListType)
1683};
1684
Bram Moolenaar7854e3a2012-11-28 15:33:14 +01001685 static PyObject *
1686Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001687{
1688 PyObject *mod;
Bram Moolenaar66b79852012-09-21 14:00:35 +02001689 PyObject *tmp;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690 /* The special value is removed from sys.path in Python3_Init(). */
1691 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1692
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001693 PyType_Ready(&IterType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001694 PyType_Ready(&BufferType);
1695 PyType_Ready(&RangeType);
1696 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001697 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001698 PyType_Ready(&BufMapType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001699 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001700 PyType_Ready(&TabListType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001701 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001702 PyType_Ready(&DictionaryType);
1703 PyType_Ready(&ListType);
1704 PyType_Ready(&FunctionType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001705 PyType_Ready(&OptionsType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001706
1707 /* Set sys.argv[] to avoid a crash in warn(). */
1708 PySys_SetArgv(1, argv);
1709
1710 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001711 if (mod == NULL)
1712 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001713
Bram Moolenaar19e60942011-06-19 00:27:51 +02001714 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001715
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001716 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001717 PyModule_AddObject(mod, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001718 Py_INCREF((PyObject *)(void *)&TheBufferMap);
1719 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720 Py_INCREF((PyObject *)(void *)&TheCurrent);
1721 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1722 Py_INCREF((PyObject *)(void *)&TheWindowList);
1723 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001724 Py_INCREF((PyObject *)(void *)&TheTabPageList);
1725 PyModule_AddObject(mod, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001726
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001727 PyModule_AddObject(mod, "vars", DictionaryNew(&globvardict));
1728 PyModule_AddObject(mod, "vvars", DictionaryNew(&vimvardict));
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001729 PyModule_AddObject(mod, "options",
1730 OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL));
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001731
Bram Moolenaar66b79852012-09-21 14:00:35 +02001732#define ADD_INT_CONSTANT(name, value) \
1733 tmp = PyLong_FromLong(value); \
1734 Py_INCREF(tmp); \
1735 PyModule_AddObject(mod, name, tmp)
1736
1737 ADD_INT_CONSTANT("VAR_LOCKED", VAR_LOCKED);
1738 ADD_INT_CONSTANT("VAR_FIXED", VAR_FIXED);
1739 ADD_INT_CONSTANT("VAR_SCOPE", VAR_SCOPE);
1740 ADD_INT_CONSTANT("VAR_DEF_SCOPE", VAR_DEF_SCOPE);
1741
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001742 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001743 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001744
1745 return mod;
1746}
1747
1748/*************************************************************************
1749 * 4. Utility functions for handling the interface between Vim and Python.
1750 */
1751
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001752/* Convert a Vim line into a Python string.
1753 * All internal newlines are replaced by null characters.
1754 *
1755 * On errors, the Python exception data is set, and NULL is returned.
1756 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001757 static PyObject *
1758LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001759{
1760 PyObject *result;
1761 Py_ssize_t len = strlen(str);
1762 char *tmp,*p;
1763
1764 tmp = (char *)alloc((unsigned)(len+1));
1765 p = tmp;
1766 if (p == NULL)
1767 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001768 PyErr_NoMemory();
1769 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001770 }
1771
1772 while (*str)
1773 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001774 if (*str == '\n')
1775 *p = '\0';
1776 else
1777 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001778
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001779 ++p;
1780 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001781 }
1782 *p = '\0';
1783
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001784 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001785
1786 vim_free(tmp);
1787 return result;
1788}
1789
Bram Moolenaardb913952012-06-29 12:54:53 +02001790 void
1791do_py3eval (char_u *str, typval_T *rettv)
1792{
1793 DoPy3Command(NULL, (char *) str, rettv);
1794 switch(rettv->v_type)
1795 {
1796 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1797 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1798 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001799 case VAR_UNKNOWN:
1800 rettv->v_type = VAR_NUMBER;
1801 rettv->vval.v_number = 0;
1802 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001803 }
1804}
1805
1806 void
1807set_ref_in_python3 (int copyID)
1808{
1809 set_ref_in_py(copyID);
1810}