blob: c4e122c29020c7e59c8ef4f9e3e2afbc37e574b9 [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#define PY_SSIZE_T_CLEAN
46
47#ifdef F_BLANK
48# undef F_BLANK
49#endif
50
Bram Moolenaar6df6f472010-07-18 18:04:50 +020051#ifdef HAVE_STDARG_H
52# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
53#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020054#ifdef _POSIX_C_SOURCE /* defined in feature.h */
55# undef _POSIX_C_SOURCE
56#endif
Bram Moolenaar6df6f472010-07-18 18:04:50 +020057#ifdef _XOPEN_SOURCE
58# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
59#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020060
61#include <Python.h>
62#if defined(MACOS) && !defined(MACOS_X_UNIX)
63# include "macglue.h"
64# include <CodeFragments.h>
65#endif
66#undef main /* Defined in python.h - aargh */
67#undef HAVE_FCNTL_H /* Clash with os_win32.h */
68
69static void init_structs(void);
70
Bram Moolenaar3d64a312011-07-15 15:54:44 +020071/* The "surrogateescape" error handler is new in Python 3.1 */
72#if PY_VERSION_HEX >= 0x030100f0
73# define CODEC_ERROR_HANDLER "surrogateescape"
74#else
75# define CODEC_ERROR_HANDLER NULL
76#endif
77
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020078#define PyInt Py_ssize_t
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020079#define PyString_Check(obj) PyUnicode_Check(obj)
Bram Moolenaardb913952012-06-29 12:54:53 +020080#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
Bram Moolenaar19e60942011-06-19 00:27:51 +020081#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
82#define PyString_AsString(obj) PyBytes_AsString(obj)
83#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Bram Moolenaarca8a4df2010-07-31 19:54:14 +020084#define PyString_FromString(repr) PyUnicode_FromString(repr)
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020085
Bram Moolenaar0c1f3f42011-02-25 15:18:50 +010086#if defined(DYNAMIC_PYTHON3) || defined(PROTO)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020087
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020088# ifndef WIN3264
89# include <dlfcn.h>
90# define FARPROC void*
91# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +010092# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +020093# define load_dll(n) dlopen((n), RTLD_LAZY)
94# else
95# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
96# endif
97# define close_dll dlclose
98# define symbol_from_dll dlsym
99# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200100# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200101# define close_dll FreeLibrary
102# define symbol_from_dll GetProcAddress
103# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200104/*
105 * Wrapper defines
106 */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200107# undef PyArg_Parse
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200108# define PyArg_Parse py3_PyArg_Parse
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200109# undef PyArg_ParseTuple
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200110# define PyArg_ParseTuple py3_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200111# define PyMem_Free py3_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200112# define PyMem_Malloc py3_PyMem_Malloc
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200113# define PyDict_SetItemString py3_PyDict_SetItemString
114# define PyErr_BadArgument py3_PyErr_BadArgument
115# define PyErr_Clear py3_PyErr_Clear
116# define PyErr_NoMemory py3_PyErr_NoMemory
117# define PyErr_Occurred py3_PyErr_Occurred
118# define PyErr_SetNone py3_PyErr_SetNone
119# define PyErr_SetString py3_PyErr_SetString
120# define PyEval_InitThreads py3_PyEval_InitThreads
121# define PyEval_RestoreThread py3_PyEval_RestoreThread
122# define PyEval_SaveThread py3_PyEval_SaveThread
123# define PyGILState_Ensure py3_PyGILState_Ensure
124# define PyGILState_Release py3_PyGILState_Release
125# define PyLong_AsLong py3_PyLong_AsLong
126# define PyLong_FromLong py3_PyLong_FromLong
127# define PyList_GetItem py3_PyList_GetItem
128# define PyList_Append py3_PyList_Append
129# define PyList_New py3_PyList_New
130# define PyList_SetItem py3_PyList_SetItem
131# define PyList_Size py3_PyList_Size
Bram Moolenaardb913952012-06-29 12:54:53 +0200132# define PySequence_Check py3_PySequence_Check
133# define PySequence_Size py3_PySequence_Size
134# define PySequence_GetItem py3_PySequence_GetItem
135# define PyTuple_Size py3_PyTuple_Size
136# define PyTuple_GetItem py3_PyTuple_GetItem
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200137# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx
138# define PyImport_ImportModule py3_PyImport_ImportModule
Bram Moolenaardb913952012-06-29 12:54:53 +0200139# define PyImport_AddModule py3_PyImport_AddModule
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200140# define PyObject_Init py3__PyObject_Init
141# define PyDict_New py3_PyDict_New
142# define PyDict_GetItemString py3_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200143# define PyDict_Next py3_PyDict_Next
144# define PyMapping_Check py3_PyMapping_Check
145# define PyMapping_Items py3_PyMapping_Items
146# define PyIter_Next py3_PyIter_Next
147# define PyObject_GetIter py3_PyObject_GetIter
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200148# define PyModule_GetDict py3_PyModule_GetDict
149#undef PyRun_SimpleString
150# define PyRun_SimpleString py3_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200151#undef PyRun_String
152# define PyRun_String py3_PyRun_String
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200153# define PySys_SetObject py3_PySys_SetObject
154# define PySys_SetArgv py3_PySys_SetArgv
155# define PyType_Type (*py3_PyType_Type)
156# define PyType_Ready py3_PyType_Ready
157#undef Py_BuildValue
158# define Py_BuildValue py3_Py_BuildValue
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100159# define Py_SetPythonHome py3_Py_SetPythonHome
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200160# define Py_Initialize py3_Py_Initialize
161# define Py_Finalize py3_Py_Finalize
162# define Py_IsInitialized py3_Py_IsInitialized
163# define _Py_NoneStruct (*py3__Py_NoneStruct)
Bram Moolenaardb913952012-06-29 12:54:53 +0200164# define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200165# define PyModule_AddObject py3_PyModule_AddObject
166# define PyImport_AppendInittab py3_PyImport_AppendInittab
167# define _PyUnicode_AsString py3__PyUnicode_AsString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200168# undef PyUnicode_AsEncodedString
169# define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
170# undef PyBytes_AsString
171# define PyBytes_AsString py3_PyBytes_AsString
Bram Moolenaardb913952012-06-29 12:54:53 +0200172# undef PyBytes_FromString
173# define PyBytes_FromString py3_PyBytes_FromString
174# define PyFloat_FromDouble py3_PyFloat_FromDouble
175# define PyFloat_AsDouble py3_PyFloat_AsDouble
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200176# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
177# define PySlice_Type (*py3_PySlice_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200178# define PyFloat_Type (*py3_PyFloat_Type)
Bram Moolenaar19e60942011-06-19 00:27:51 +0200179# define PyErr_NewException py3_PyErr_NewException
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200180# ifdef Py_DEBUG
181# define _Py_NegativeRefcount py3__Py_NegativeRefcount
182# define _Py_RefTotal (*py3__Py_RefTotal)
183# define _Py_Dealloc py3__Py_Dealloc
184# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc
185# define _PyObject_DebugFree py3__PyObject_DebugFree
186# else
187# define PyObject_Malloc py3_PyObject_Malloc
188# define PyObject_Free py3_PyObject_Free
189# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200190# define PyType_GenericAlloc py3_PyType_GenericAlloc
191# define PyType_GenericNew py3_PyType_GenericNew
192# define PyModule_Create2 py3_PyModule_Create2
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200193# undef PyUnicode_FromString
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194# define PyUnicode_FromString py3_PyUnicode_FromString
Bram Moolenaar19e60942011-06-19 00:27:51 +0200195# undef PyUnicode_Decode
196# define PyUnicode_Decode py3_PyUnicode_Decode
Bram Moolenaardb913952012-06-29 12:54:53 +0200197# define PyType_IsSubtype py3_PyType_IsSubtype
198# define PyCapsule_New py3_PyCapsule_New
199# define PyCapsule_GetPointer py3_PyCapsule_GetPointer
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200200
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200201# ifdef Py_DEBUG
202# undef PyObject_NEW
203# define PyObject_NEW(type, typeobj) \
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204( (type *) PyObject_Init( \
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200205 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) )
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200206# endif
207
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208/*
209 * Pointers for dynamic link
210 */
211static int (*py3_PySys_SetArgv)(int, wchar_t **);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100212static void (*py3_Py_SetPythonHome)(wchar_t *home);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200213static void (*py3_Py_Initialize)(void);
214static PyObject* (*py3_PyList_New)(Py_ssize_t size);
215static PyGILState_STATE (*py3_PyGILState_Ensure)(void);
216static void (*py3_PyGILState_Release)(PyGILState_STATE);
217static int (*py3_PySys_SetObject)(char *, PyObject *);
218static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *);
219static Py_ssize_t (*py3_PyList_Size)(PyObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200220static int (*py3_PySequence_Check)(PyObject *);
221static Py_ssize_t (*py3_PySequence_Size)(PyObject *);
222static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t);
223static Py_ssize_t (*py3_PyTuple_Size)(PyObject *);
224static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t);
225static int (*py3_PyMapping_Check)(PyObject *);
226static PyObject* (*py3_PyMapping_Items)(PyObject *);
Bram Moolenaar314ed4b2011-09-14 18:59:39 +0200227static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length,
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200228 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200229static PyObject* (*py3_PyErr_NoMemory)(void);
230static void (*py3_Py_Finalize)(void);
231static void (*py3_PyErr_SetString)(PyObject *, const char *);
232static int (*py3_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200233static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200234static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t);
235static PyObject* (*py3_PyImport_ImportModule)(const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200236static PyObject* (*py3_PyImport_AddModule)(const char *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200237static int (*py3_PyErr_BadArgument)(void);
238static PyTypeObject* py3_PyType_Type;
239static PyObject* (*py3_PyErr_Occurred)(void);
240static PyObject* (*py3_PyModule_GetDict)(PyObject *);
241static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *);
242static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200243static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200244static PyObject* (*py3_PyLong_FromLong)(long);
245static PyObject* (*py3_PyDict_New)(void);
Bram Moolenaardb913952012-06-29 12:54:53 +0200246static PyObject* (*py3_PyIter_Next)(PyObject *);
247static PyObject* (*py3_PyObject_GetIter)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200248static PyObject* (*py3_Py_BuildValue)(char *, ...);
249static int (*py3_PyType_Ready)(PyTypeObject *type);
250static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
251static PyObject* (*py3_PyUnicode_FromString)(const char *u);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200252static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
253 const char *encoding, const char *errors);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200254static long (*py3_PyLong_AsLong)(PyObject *);
255static void (*py3_PyErr_SetNone)(PyObject *);
256static void (*py3_PyEval_InitThreads)(void);
257static void(*py3_PyEval_RestoreThread)(PyThreadState *);
258static PyThreadState*(*py3_PyEval_SaveThread)(void);
259static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
260static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200261static int (*py3_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200262static void* (*py3_PyMem_Malloc)(size_t);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200263static int (*py3_Py_IsInitialized)(void);
264static void (*py3_PyErr_Clear)(void);
265static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200266static iternextfunc py3__PyObject_NextNotImplemented;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200267static PyObject* py3__Py_NoneStruct;
268static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
269static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
270static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200271static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
272static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Bram Moolenaardb913952012-06-29 12:54:53 +0200273static PyObject* (*py3_PyBytes_FromString)(char *str);
274static PyObject* (*py3_PyFloat_FromDouble)(double num);
275static double (*py3_PyFloat_AsDouble)(PyObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200276static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
277static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
278static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
279static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
280static PyTypeObject* py3_PySlice_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200281static PyTypeObject* py3_PyFloat_Type;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200282static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
Bram Moolenaardb913952012-06-29 12:54:53 +0200283static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
284static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200285# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
287 static Py_ssize_t* py3__Py_RefTotal;
288 static void (*py3__Py_Dealloc)(PyObject *obj);
289 static void (*py3__PyObject_DebugFree)(void*);
290 static void* (*py3__PyObject_DebugMalloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200291# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200292 static void (*py3_PyObject_Free)(void*);
293 static void* (*py3_PyObject_Malloc)(size_t);
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200294# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200295static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200296
297static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */
298
299/* Imported exception objects */
300static PyObject *p3imp_PyExc_AttributeError;
301static PyObject *p3imp_PyExc_IndexError;
302static PyObject *p3imp_PyExc_KeyboardInterrupt;
303static PyObject *p3imp_PyExc_TypeError;
304static PyObject *p3imp_PyExc_ValueError;
305
306# define PyExc_AttributeError p3imp_PyExc_AttributeError
307# define PyExc_IndexError p3imp_PyExc_IndexError
308# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt
309# define PyExc_TypeError p3imp_PyExc_TypeError
310# define PyExc_ValueError p3imp_PyExc_ValueError
311
312/*
313 * Table of name to function pointer of python.
314 */
315# define PYTHON_PROC FARPROC
316static struct
317{
318 char *name;
319 PYTHON_PROC *ptr;
320} py3_funcname_table[] =
321{
322 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv},
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100323 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200324 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
325 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200326 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200327 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200328 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
329 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
330 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
331 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject},
332 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append},
333 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size},
Bram Moolenaardb913952012-06-29 12:54:53 +0200334 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check},
335 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size},
336 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem},
337 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size},
338 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200339 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx},
340 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory},
341 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize},
342 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString},
343 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200344 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200345 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem},
346 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule},
Bram Moolenaardb913952012-06-29 12:54:53 +0200347 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200348 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument},
349 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type},
350 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred},
351 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict},
352 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem},
353 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200354 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next},
355 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check},
356 {"PyMapping_Items", (PYTHON_PROC*)&py3_PyMapping_Items},
357 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next},
358 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200359 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong},
360 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New},
361 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue},
362 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready},
363 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString},
364 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong},
365 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone},
366 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads},
367 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
368 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
369 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200370 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
Bram Moolenaardb913952012-06-29 12:54:53 +0200371 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200372 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
373 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
374 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init},
375 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
376 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
377 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200378 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200379 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString},
380 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble},
381 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200382 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
383 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
384 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
385 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
386 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200387 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200388 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200389# ifdef Py_DEBUG
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200390 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
391 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
392 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc},
393 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree},
394 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200395# else
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200396 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc},
397 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free},
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200398# endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200399 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype},
400 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New},
401 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer},
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200402 {"", NULL},
403};
404
405/*
406 * Free python.dll
407 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200408 static void
409end_dynamic_python3(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200410{
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200411 if (hinstPy3 != 0)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200412 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200413 close_dll(hinstPy3);
414 hinstPy3 = 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200415 }
416}
417
418/*
419 * Load library and get all pointers.
420 * Parameter 'libname' provides name of DLL.
421 * Return OK or FAIL.
422 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200423 static int
424py3_runtime_link_init(char *libname, int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200425{
426 int i;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200427 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200428
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100429# if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200430 /* Can't have Python and Python3 loaded at the same time.
431 * It cause a crash, because RTLD_GLOBAL is needed for
432 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200433 if (python_loaded())
434 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200435 if (verbose)
436 EMSG(_("E837: This Vim cannot execute :py3 after using :python"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200437 return FAIL;
438 }
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200439# endif
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200440
441 if (hinstPy3 != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200442 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200443 hinstPy3 = load_dll(libname);
444
445 if (!hinstPy3)
446 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200447 if (verbose)
448 EMSG2(_(e_loadlib), libname);
449 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200450 }
451
452 for (i = 0; py3_funcname_table[i].ptr; ++i)
453 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200454 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3,
455 py3_funcname_table[i].name)) == NULL)
456 {
457 close_dll(hinstPy3);
458 hinstPy3 = 0;
459 if (verbose)
460 EMSG2(_(e_loadfunc), py3_funcname_table[i].name);
461 return FAIL;
462 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200463 }
464
Bram Moolenaar69154f22010-07-18 21:42:34 +0200465 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
466 * will be present in the library. */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200467 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200468 ucs_decode = symbol_from_dll(hinstPy3,
469 "PyUnicodeUCS2_Decode");
470 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
471 "PyUnicodeUCS2_AsEncodedString");
472 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200473 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200474 ucs_from_string = symbol_from_dll(hinstPy3,
475 "PyUnicodeUCS4_FromString");
Bram Moolenaar19e60942011-06-19 00:27:51 +0200476 ucs_decode = symbol_from_dll(hinstPy3,
477 "PyUnicodeUCS4_Decode");
478 ucs_as_encoded_string = symbol_from_dll(hinstPy3,
479 "PyUnicodeUCS4_AsEncodedString");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200480 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200481 if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200482 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200483 py3_PyUnicode_FromString = ucs_from_string;
Bram Moolenaar19e60942011-06-19 00:27:51 +0200484 py3_PyUnicode_Decode = ucs_decode;
485 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200486 }
487 else
488 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200489 close_dll(hinstPy3);
490 hinstPy3 = 0;
491 if (verbose)
492 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
493 return FAIL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200494 }
495
496 return OK;
497}
498
499/*
500 * If python is enabled (there is installed python on Windows system) return
501 * TRUE, else FALSE.
502 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200503 int
504python3_enabled(int verbose)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200505{
506 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK;
507}
508
509/* Load the standard Python exceptions - don't import the symbols from the
510 * DLL, as this can cause errors (importing data symbols is not reliable).
511 */
512static void get_py3_exceptions __ARGS((void));
513
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200514 static void
515get_py3_exceptions()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516{
517 PyObject *exmod = PyImport_ImportModule("builtins");
518 PyObject *exdict = PyModule_GetDict(exmod);
519 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
520 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
521 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
522 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
523 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
524 Py_XINCREF(p3imp_PyExc_AttributeError);
525 Py_XINCREF(p3imp_PyExc_IndexError);
526 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt);
527 Py_XINCREF(p3imp_PyExc_TypeError);
528 Py_XINCREF(p3imp_PyExc_ValueError);
529 Py_XDECREF(exmod);
530}
531#endif /* DYNAMIC_PYTHON3 */
532
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200533static PyObject *BufferNew (buf_T *);
534static PyObject *WindowNew(win_T *);
535static PyObject *LineToString(const char *);
Bram Moolenaar7f85d292012-02-04 20:17:26 +0100536static PyObject *BufferDir(PyObject *, PyObject *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200537
538static PyTypeObject RangeType;
539
Bram Moolenaardb913952012-06-29 12:54:53 +0200540static int py3initialised = 0;
541
542#define PYINITIALISED py3initialised
543
544/* Add conversion from PyInt? */
545#define DICTKEY_GET(err) \
546 if (PyBytes_Check(keyObject)) \
547 key = (char_u *) PyBytes_AsString(keyObject); \
548 else if (PyUnicode_Check(keyObject)) \
549 { \
550 bytes = PyString_AsBytes(keyObject); \
551 if (bytes == NULL) \
552 return err; \
553 key = (char_u *) PyBytes_AsString(bytes); \
554 if (key == NULL) \
555 return err; \
556 } \
557 else \
558 { \
559 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
560 return err; \
561 }
562#define DICTKEY_UNREF \
563 if (bytes != NULL) \
564 Py_XDECREF(bytes);
565
566#define DICTKEY_DECL PyObject *bytes = NULL;
567
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200568/*
569 * Include the code shared with if_python.c
570 */
571#include "if_py_both.h"
572
Bram Moolenaardb913952012-06-29 12:54:53 +0200573#define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0)
574
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200575 static void
576call_PyObject_Free(void *p)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200577{
578#ifdef Py_DEBUG
579 _PyObject_DebugFree(p);
580#else
581 PyObject_Free(p);
582#endif
583}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200584
585 static PyObject *
586call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200587{
588 return PyType_GenericNew(type,args,kwds);
589}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200590
591 static PyObject *
592call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200593{
594 return PyType_GenericAlloc(type,nitems);
595}
596
597/******************************************************
598 * Internal function prototypes.
599 */
600
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200601static Py_ssize_t RangeStart;
602static Py_ssize_t RangeEnd;
603
Bram Moolenaardb913952012-06-29 12:54:53 +0200604static PyObject *globals;
605
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200606static int PythonIO_Init(void);
607static void PythonIO_Fini(void);
Bram Moolenaar69154f22010-07-18 21:42:34 +0200608PyMODINIT_FUNC Py3Init_vim(void);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200609
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200610/******************************************************
611 * 1. Python interpreter main program.
612 */
613
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200614static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
615
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200616 void
617python3_end()
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200618{
619 static int recurse = 0;
620
621 /* If a crash occurs while doing this, don't try again. */
622 if (recurse != 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200623 return;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624
625 ++recurse;
626
627#ifdef DYNAMIC_PYTHON3
628 if (hinstPy3)
629#endif
630 if (Py_IsInitialized())
631 {
632 // acquire lock before finalizing
633 pygilstate = PyGILState_Ensure();
634
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200635 PythonIO_Fini();
636 Py_Finalize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200637 }
638
639#ifdef DYNAMIC_PYTHON3
640 end_dynamic_python3();
641#endif
642
643 --recurse;
644}
645
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200646#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO)
647 int
648python3_loaded()
649{
650 return (hinstPy3 != 0);
651}
652#endif
653
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200654 static int
655Python3_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200656{
657 if (!py3initialised)
658 {
659#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200660 if (!python3_enabled(TRUE))
661 {
662 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
663 goto fail;
664 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200665#endif
666
667 init_structs();
668
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100669
670#ifdef PYTHON3_HOME
671 Py_SetPythonHome(PYTHON3_HOME);
672#endif
673
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200674#if !defined(MACOS) || defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200675 Py_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200676#else
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200677 PyMac_Initialize();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200678#endif
Bram Moolenaar456f2bb2011-06-12 21:37:13 +0200679 /* initialise threads, must be after Py_Initialize() */
680 PyEval_InitThreads();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200681
682#ifdef DYNAMIC_PYTHON3
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200683 get_py3_exceptions();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200684#endif
685
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200686 if (PythonIO_Init())
687 goto fail;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200688
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200689 PyImport_AppendInittab("vim", Py3Init_vim);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200690
Bram Moolenaardb913952012-06-29 12:54:53 +0200691 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
692
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200693 /* Remove the element from sys.path that was added because of our
694 * argv[0] value in Py3Init_vim(). Previously we used an empty
695 * string, but dependinding on the OS we then get an empty entry or
Bram Moolenaar19e60942011-06-19 00:27:51 +0200696 * the current directory in sys.path.
697 * Only after vim has been imported, the element does exist in
698 * sys.path.
699 */
700 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 +0200701
702 // lock is created and acquired in PyEval_InitThreads() and thread
703 // state is created in Py_Initialize()
704 // there _PyGILState_NoteThreadState() also sets gilcounter to 1
705 // (python must have threads enabled!)
706 // so the following does both: unlock GIL and save thread state in TLS
707 // without deleting thread state
708 PyGILState_Release(pygilstate);
709
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200710 py3initialised = 1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200711 }
712
713 return 0;
714
715fail:
716 /* We call PythonIO_Flush() here to print any Python errors.
717 * This is OK, as it is possible to call this function even
718 * if PythonIO_Init() has not completed successfully (it will
719 * not do anything in this case).
720 */
721 PythonIO_Flush();
722 return -1;
723}
724
725/*
726 * External interface
727 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200728 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200729DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200730{
731#if defined(MACOS) && !defined(MACOS_X_UNIX)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200732 GrafPtr oldPort;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200733#endif
734#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200735 char *saved_locale;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200736#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200737 PyObject *cmdstr;
738 PyObject *cmdbytes;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200739
740#if defined(MACOS) && !defined(MACOS_X_UNIX)
741 GetPort(&oldPort);
742 /* Check if the Python library is available */
743 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200744 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200745#endif
746 if (Python3_Init())
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200747 goto theend;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200748
Bram Moolenaardb913952012-06-29 12:54:53 +0200749 if (rettv == NULL)
750 {
751 RangeStart = eap->line1;
752 RangeEnd = eap->line2;
753 }
754 else
755 {
756 RangeStart = (PyInt) curwin->w_cursor.lnum;
757 RangeEnd = RangeStart;
758 }
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200759 Python_Release_Vim(); /* leave vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200760
761#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
762 /* Python only works properly when the LC_NUMERIC locale is "C". */
763 saved_locale = setlocale(LC_NUMERIC, NULL);
764 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200765 saved_locale = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200766 else
767 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200768 /* Need to make a copy, value may change when setting new locale. */
769 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
770 (void)setlocale(LC_NUMERIC, "C");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200771 }
772#endif
773
774 pygilstate = PyGILState_Ensure();
775
Bram Moolenaar19e60942011-06-19 00:27:51 +0200776 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
777 * SyntaxError (unicode error). */
Bram Moolenaar3d64a312011-07-15 15:54:44 +0200778 cmdstr = PyUnicode_Decode(cmd, strlen(cmd),
779 (char *)ENC_OPT, CODEC_ERROR_HANDLER);
780 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200781 Py_XDECREF(cmdstr);
Bram Moolenaardb913952012-06-29 12:54:53 +0200782 if (rettv == NULL)
783 PyRun_SimpleString(PyBytes_AsString(cmdbytes));
784 else
785 {
786 PyObject *r;
787
788 r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
789 globals, globals);
790 if (r == NULL)
791 EMSG(_("E860: Eval did not return a valid python 3 object"));
792 else
793 {
794 if (ConvertFromPyObject(r, rettv) == -1)
795 EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
796 Py_DECREF(r);
797 }
798 PyErr_Clear();
799 }
Bram Moolenaar19e60942011-06-19 00:27:51 +0200800 Py_XDECREF(cmdbytes);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200801
802 PyGILState_Release(pygilstate);
803
804#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
805 if (saved_locale != NULL)
806 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200807 (void)setlocale(LC_NUMERIC, saved_locale);
808 vim_free(saved_locale);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200809 }
810#endif
811
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200812 Python_Lock_Vim(); /* enter vim */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200813 PythonIO_Flush();
814#if defined(MACOS) && !defined(MACOS_X_UNIX)
815 SetPort(oldPort);
816#endif
817
818theend:
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200819 return; /* keeps lint happy */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200820}
821
822/*
Bram Moolenaar368373e2010-07-19 20:46:22 +0200823 * ":py3"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200824 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200825 void
826ex_py3(exarg_T *eap)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200827{
828 char_u *script;
829
830 script = script_get(eap, eap->arg);
831 if (!eap->skip)
832 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200833 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 DoPy3Command(eap, (char *)eap->arg, NULL);
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200835 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200836 DoPy3Command(eap, (char *)script, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200837 }
838 vim_free(script);
839}
840
841#define BUFFER_SIZE 2048
842
843/*
Bram Moolenaar6df6f472010-07-18 18:04:50 +0200844 * ":py3file"
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200845 */
846 void
847ex_py3file(exarg_T *eap)
848{
849 static char buffer[BUFFER_SIZE];
850 const char *file;
851 char *p;
852 int i;
853
854 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
855 * stdio file pointer, but Vim and the Python DLL are compiled with
856 * different options under Windows, meaning that stdio pointers aren't
857 * compatible between the two. Yuk.
858 *
Bram Moolenaar19e60942011-06-19 00:27:51 +0200859 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
860 *
861 * Using bytes so that Python can detect the source encoding as it normally
862 * does. The doc does not say "compile" accept bytes, though.
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200863 *
864 * We need to escape any backslashes or single quotes in the file name, so that
865 * Python won't mangle the file name.
866 */
867
868 strcpy(buffer, "exec(compile(open('");
869 p = buffer + 19; /* size of "exec(compile(open('" */
870
871 for (i=0; i<2; ++i)
872 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200873 file = (char *)eap->arg;
874 while (*file && p < buffer + (BUFFER_SIZE - 3))
875 {
876 if (*file == '\\' || *file == '\'')
877 *p++ = '\\';
878 *p++ = *file++;
879 }
880 /* If we didn't finish the file name, we hit a buffer overflow */
881 if (*file != '\0')
882 return;
883 if (i==0)
884 {
Bram Moolenaar19e60942011-06-19 00:27:51 +0200885 strcpy(p,"','rb').read(),'");
886 p += 16;
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200887 }
888 else
889 {
890 strcpy(p,"','exec'))");
891 p += 10;
892 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200893 }
894
895
896 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200897 DoPy3Command(eap, buffer, NULL);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200898}
899
900/******************************************************
901 * 2. Python output stream: writes output via [e]msg().
902 */
903
904/* Implementation functions
905 */
906
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200907 static PyObject *
908OutputGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200909{
910 char *name = "";
911 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200912 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200913
914 if (strcmp(name, "softspace") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200915 return PyLong_FromLong(((OutputObject *)(self))->softspace);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200916
917 return PyObject_GenericGetAttr(self, nameobj);
918}
919
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200920 static int
921OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200922{
923 char *name = "";
924 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200925 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200926
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 if (val == NULL)
928 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200929 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
930 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200931 }
932
933 if (strcmp(name, "softspace") == 0)
934 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 if (!PyLong_Check(val))
936 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200937 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
938 return -1;
939 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200940
Bram Moolenaar55d5c032010-07-17 23:52:29 +0200941 ((OutputObject *)(self))->softspace = PyLong_AsLong(val);
942 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200943 }
944
945 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
946 return -1;
947}
948
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200949/***************/
950
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200951 static int
952PythonIO_Init(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200953{
954 PyType_Ready(&OutputType);
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200955 return PythonIO_Init_io();
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200956}
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200957
958 static void
959PythonIO_Fini(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200960{
961 PySys_SetObject("stdout", NULL);
962 PySys_SetObject("stderr", NULL);
963}
964
965/******************************************************
966 * 3. Implementation of the Vim module for Python
967 */
968
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200969/* Window type - Implementation functions
970 * --------------------------------------
971 */
972
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200973#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType)
974
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200975/* Buffer type - Implementation functions
976 * --------------------------------------
977 */
978
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200979#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType)
980
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200981static Py_ssize_t BufferLength(PyObject *);
982static PyObject *BufferItem(PyObject *, Py_ssize_t);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200983static PyObject* BufferSubscript(PyObject *self, PyObject *idx);
984static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200985
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200986
987/* Line range type - Implementation functions
988 * --------------------------------------
989 */
990
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200991#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType)
992
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200993static PyObject* RangeSubscript(PyObject *self, PyObject *idx);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200994static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *);
Bram Moolenaarba4897e2011-09-14 15:01:58 +0200995static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200996
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200997/* Current objects type - Implementation functions
998 * -----------------------------------------------
999 */
1000
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001001static PySequenceMethods BufferAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001002 (lenfunc) BufferLength, /* sq_length, len(x) */
1003 (binaryfunc) 0, /* sq_concat, x+y */
1004 (ssizeargfunc) 0, /* sq_repeat, x*n */
1005 (ssizeargfunc) BufferItem, /* sq_item, x[i] */
1006 0, /* was_sq_slice, x[i:j] */
Bram Moolenaar19e60942011-06-19 00:27:51 +02001007 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001008 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001009 0, /* sq_contains */
1010 0, /* sq_inplace_concat */
1011 0, /* sq_inplace_repeat */
1012};
1013
1014PyMappingMethods BufferAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001015 /* mp_length */ (lenfunc)BufferLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001016 /* mp_subscript */ (binaryfunc)BufferSubscript,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001017 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001018};
1019
1020
1021/* Buffer object - Definitions
1022 */
1023
1024static PyTypeObject BufferType;
1025
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001026 static PyObject *
1027BufferNew(buf_T *buf)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001028{
1029 /* We need to handle deletion of buffers underneath us.
1030 * If we add a "b_python3_ref" field to the buf_T structure,
1031 * then we can get at it in buf_freeall() in vim. We then
1032 * need to create only ONE Python object per buffer - if
1033 * we try to create a second, just INCREF the existing one
1034 * and return it. The (single) Python object referring to
1035 * the buffer is stored in "b_python3_ref".
1036 * Question: what to do on a buf_freeall(). We'll probably
1037 * have to either delete the Python object (DECREF it to
1038 * zero - a bad idea, as it leaves dangling refs!) or
1039 * set the buf_T * value to an invalid value (-1?), which
1040 * means we need checks in all access functions... Bah.
1041 */
1042
1043 BufferObject *self;
1044
1045 if (buf->b_python3_ref != NULL)
1046 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001047 self = buf->b_python3_ref;
1048 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001049 }
1050 else
1051 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001052 self = PyObject_NEW(BufferObject, &BufferType);
1053 buf->b_python3_ref = self;
1054 if (self == NULL)
1055 return NULL;
1056 self->buf = buf;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001057 }
1058
1059 return (PyObject *)(self);
1060}
1061
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001062 static void
1063BufferDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001064{
1065 BufferObject *this = (BufferObject *)(self);
1066
1067 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001068 this->buf->b_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001069
1070 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001071}
1072
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001073 static PyObject *
1074BufferGetattro(PyObject *self, PyObject*nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001075{
1076 BufferObject *this = (BufferObject *)(self);
1077
1078 char *name = "";
1079 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001080 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001081
1082 if (CheckBuffer(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001083 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001084
1085 if (strcmp(name, "name") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001086 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001087 else if (strcmp(name, "number") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001088 return Py_BuildValue("n", this->buf->b_fnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001089 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001090 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001091}
1092
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001093 static PyObject *
Bram Moolenaar7f85d292012-02-04 20:17:26 +01001094BufferDir(PyObject *self UNUSED, PyObject *args UNUSED)
1095{
1096 return Py_BuildValue("[sssss]", "name", "number",
1097 "append", "mark", "range");
1098}
1099
1100 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001101BufferRepr(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001102{
1103 static char repr[100];
1104 BufferObject *this = (BufferObject *)(self);
1105
1106 if (this->buf == INVALID_BUFFER_VALUE)
1107 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001108 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
1109 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001110 }
1111 else
1112 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001113 char *name = (char *)this->buf->b_fname;
1114 Py_ssize_t len;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001115
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001116 if (name == NULL)
1117 name = "";
1118 len = strlen(name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001119
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001120 if (len > 35)
1121 name = name + (35 - len);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001122
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001123 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001124
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001125 return PyUnicode_FromString(repr);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001126 }
1127}
1128
1129/******************/
1130
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001131 static Py_ssize_t
1132BufferLength(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001133{
1134 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001135 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001136
1137 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
1138}
1139
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001140 static PyObject *
1141BufferItem(PyObject *self, Py_ssize_t n)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001142{
1143 return RBItem((BufferObject *)(self), n, 1,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001144 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001145}
1146
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001147 static PyObject *
1148BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
1149{
1150 return RBSlice((BufferObject *)(self), lo, hi, 1,
1151 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1152}
1153
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001154 static PyObject *
1155BufferSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001156{
Bram Moolenaardb913952012-06-29 12:54:53 +02001157 if (PyLong_Check(idx))
1158 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001159 long _idx = PyLong_AsLong(idx);
1160 return BufferItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001161 } else if (PySlice_Check(idx))
1162 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001163 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001164
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001165 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001166 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1167 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001168 &step, &slicelen) < 0)
1169 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001170 return NULL;
1171 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001172 return BufferSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001173 }
1174 else
1175 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001176 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1177 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001178 }
1179}
1180
Bram Moolenaar19e60942011-06-19 00:27:51 +02001181 static Py_ssize_t
1182BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
1183{
Bram Moolenaardb913952012-06-29 12:54:53 +02001184 if (PyLong_Check(idx))
1185 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001186 long n = PyLong_AsLong(idx);
1187 return RBAsItem((BufferObject *)(self), n, val, 1,
1188 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1189 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001190 } else if (PySlice_Check(idx))
1191 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001192 Py_ssize_t start, stop, step, slicelen;
1193
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001194 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar19e60942011-06-19 00:27:51 +02001195 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
1196 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001197 &step, &slicelen) < 0)
1198 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001199 return -1;
1200 }
1201 return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
1202 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
1203 NULL);
Bram Moolenaardb913952012-06-29 12:54:53 +02001204 }
1205 else
1206 {
Bram Moolenaar19e60942011-06-19 00:27:51 +02001207 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1208 return -1;
1209 }
1210}
1211
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001212static PySequenceMethods RangeAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001213 (lenfunc) RangeLength, /* sq_length, len(x) */
1214 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1215 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1216 (ssizeargfunc) RangeItem, /* sq_item, x[i] */
1217 0, /* was_sq_slice, x[i:j] */
1218 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */
1219 0, /* sq_ass_slice, x[i:j]=v */
1220 0, /* sq_contains */
1221 0, /* sq_inplace_concat */
1222 0, /* sq_inplace_repeat */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001223};
1224
1225PyMappingMethods RangeAsMapping = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001226 /* mp_length */ (lenfunc)RangeLength,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001227 /* mp_subscript */ (binaryfunc)RangeSubscript,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001228 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript,
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001229};
1230
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001231/* Line range object - Implementation
1232 */
1233
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001234 static void
1235RangeDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001236{
1237 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001238 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001239}
1240
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001241 static PyObject *
1242RangeGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001243{
1244 char *name = "";
1245 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001246 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001247
1248 if (strcmp(name, "start") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001249 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001250 else if (strcmp(name, "end") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001251 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001252 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001253 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001254}
1255
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001256/****************/
1257
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001258 static Py_ssize_t
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001259RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001260{
1261 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001262 ((RangeObject *)(self))->start,
1263 ((RangeObject *)(self))->end,
1264 &((RangeObject *)(self))->end);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001265}
1266
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001267 static Py_ssize_t
1268RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val)
1269{
1270 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
1271 ((RangeObject *)(self))->start,
1272 ((RangeObject *)(self))->end,
1273 &((RangeObject *)(self))->end);
1274}
1275
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001276 static PyObject *
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001277RangeSubscript(PyObject *self, PyObject* idx)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001278{
Bram Moolenaardb913952012-06-29 12:54:53 +02001279 if (PyLong_Check(idx))
1280 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001281 long _idx = PyLong_AsLong(idx);
1282 return RangeItem(self,_idx);
Bram Moolenaardb913952012-06-29 12:54:53 +02001283 } else if (PySlice_Check(idx))
1284 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001285 Py_ssize_t start, stop, step, slicelen;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001286
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001287 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001288 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1289 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001290 &step, &slicelen) < 0)
1291 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001292 return NULL;
1293 }
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001294 return RangeSlice(self, start, stop);
Bram Moolenaardb913952012-06-29 12:54:53 +02001295 }
1296 else
1297 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001298 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1299 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001300 }
1301}
1302
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001303 static Py_ssize_t
1304RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val)
1305{
Bram Moolenaardb913952012-06-29 12:54:53 +02001306 if (PyLong_Check(idx))
1307 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001308 long n = PyLong_AsLong(idx);
1309 return RangeAsItem(self, n, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001310 } else if (PySlice_Check(idx))
1311 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001312 Py_ssize_t start, stop, step, slicelen;
1313
Bram Moolenaar9e8edf62011-09-14 15:41:58 +02001314 if (PySlice_GetIndicesEx((PyObject *)idx,
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001315 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1,
1316 &start, &stop,
Bram Moolenaardb913952012-06-29 12:54:53 +02001317 &step, &slicelen) < 0)
1318 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001319 return -1;
1320 }
1321 return RangeAsSlice(self, start, stop, val);
Bram Moolenaardb913952012-06-29 12:54:53 +02001322 }
1323 else
1324 {
Bram Moolenaarba4897e2011-09-14 15:01:58 +02001325 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1326 return -1;
1327 }
1328}
1329
1330
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001331/* Buffer list object - Definitions
1332 */
1333
1334typedef struct
1335{
1336 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001337} BufListObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001338
1339static PySequenceMethods BufListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001340 (lenfunc) BufListLength, /* sq_length, len(x) */
1341 (binaryfunc) 0, /* sq_concat, x+y */
1342 (ssizeargfunc) 0, /* sq_repeat, x*n */
1343 (ssizeargfunc) BufListItem, /* sq_item, x[i] */
1344 0, /* was_sq_slice, x[i:j] */
1345 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */
1346 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001347 0, /* sq_contains */
1348 0, /* sq_inplace_concat */
1349 0, /* sq_inplace_repeat */
1350};
1351
1352static PyTypeObject BufListType;
1353
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001354/* Window object - Definitions
1355 */
1356
1357static struct PyMethodDef WindowMethods[] = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001358 /* name, function, calling, documentation */
1359 { NULL, NULL, 0, NULL }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001360};
1361
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001362static PyTypeObject WindowType;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001363
1364/* Window object - Implementation
1365 */
1366
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001367 static PyObject *
1368WindowNew(win_T *win)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001369{
1370 /* We need to handle deletion of windows underneath us.
1371 * If we add a "w_python3_ref" field to the win_T structure,
1372 * then we can get at it in win_free() in vim. We then
1373 * need to create only ONE Python object per window - if
1374 * we try to create a second, just INCREF the existing one
1375 * and return it. The (single) Python object referring to
1376 * the window is stored in "w_python3_ref".
1377 * On a win_free() we set the Python object's win_T* field
1378 * to an invalid value. We trap all uses of a window
1379 * object, and reject them if the win_T* field is invalid.
1380 */
1381
1382 WindowObject *self;
1383
1384 if (win->w_python3_ref)
1385 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001386 self = win->w_python3_ref;
1387 Py_INCREF(self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001388 }
1389 else
1390 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001391 self = PyObject_NEW(WindowObject, &WindowType);
1392 if (self == NULL)
1393 return NULL;
1394 self->win = win;
1395 win->w_python3_ref = self;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001396 }
1397
1398 return (PyObject *)(self);
1399}
1400
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001401 static void
1402WindowDestructor(PyObject *self)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001403{
1404 WindowObject *this = (WindowObject *)(self);
1405
1406 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001407 this->win->w_python3_ref = NULL;
Bram Moolenaar19e60942011-06-19 00:27:51 +02001408
1409 Py_TYPE(self)->tp_free((PyObject*)self);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001410}
1411
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001412 static PyObject *
1413WindowGetattro(PyObject *self, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001414{
1415 WindowObject *this = (WindowObject *)(self);
1416
1417 char *name = "";
1418 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001419 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001420
1421
1422 if (CheckWindow(this))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001423 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001424
1425 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001426 return (PyObject *)BufferNew(this->win->w_buffer);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001427 else if (strcmp(name, "cursor") == 0)
1428 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001429 pos_T *pos = &this->win->w_cursor;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001430
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001431 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001432 }
1433 else if (strcmp(name, "height") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001434 return Py_BuildValue("l", (long)(this->win->w_height));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001435#ifdef FEAT_VERTSPLIT
1436 else if (strcmp(name, "width") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001437 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001438#endif
1439 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001440 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001441 else
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001442 return PyObject_GenericGetAttr(self, nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001443}
1444
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001445 static int
1446WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001447{
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001448 char *name = "";
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001449
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001450 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001451 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001452
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001453 return WindowSetattr(self, name, val);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001454}
1455
1456/* Window list object - Definitions
1457 */
1458
1459typedef struct
1460{
1461 PyObject_HEAD
1462}
1463WinListObject;
1464
1465static PySequenceMethods WinListAsSeq = {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001466 (lenfunc) WinListLength, /* sq_length, len(x) */
1467 (binaryfunc) 0, /* sq_concat, x+y */
1468 (ssizeargfunc) 0, /* sq_repeat, x*n */
1469 (ssizeargfunc) WinListItem, /* sq_item, x[i] */
1470 0, /* sq_slice, x[i:j] */
1471 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */
1472 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001473 0, /* sq_contains */
1474 0, /* sq_inplace_concat */
1475 0, /* sq_inplace_repeat */
1476};
1477
1478static PyTypeObject WinListType;
1479
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480/* Current items object - Definitions
1481 */
1482
1483typedef struct
1484{
1485 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001486} CurrentObject;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001487
1488static PyTypeObject CurrentType;
1489
1490/* Current items object - Implementation
1491 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001492 static PyObject *
1493CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494{
1495 char *name = "";
1496 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001497 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001498
1499 if (strcmp(name, "buffer") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001500 return (PyObject *)BufferNew(curbuf);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501 else if (strcmp(name, "window") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001502 return (PyObject *)WindowNew(curwin);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001503 else if (strcmp(name, "line") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001504 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505 else if (strcmp(name, "range") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001506 return RangeNew(curbuf, RangeStart, RangeEnd);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507 else if (strcmp(name,"__members__") == 0)
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001508 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509 else
1510 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001511 PyErr_SetString(PyExc_AttributeError, name);
1512 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001513 }
1514}
1515
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001516 static int
1517CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518{
1519 char *name = "";
1520 if (PyUnicode_Check(nameobj))
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001521 name = _PyUnicode_AsString(nameobj);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001522
1523 if (strcmp(name, "line") == 0)
1524 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001525 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL)
1526 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001527
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001528 return 0;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001529 }
1530 else
1531 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001532 PyErr_SetString(PyExc_AttributeError, name);
1533 return -1;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001534 }
1535}
1536
Bram Moolenaardb913952012-06-29 12:54:53 +02001537/* Dictionary object - Definitions
1538 */
1539
1540static PyInt DictionaryLength(PyObject *);
1541
1542static PyMappingMethods DictionaryAsMapping = {
1543 /* mp_length */ (lenfunc) DictionaryLength,
1544 /* mp_subscript */ (binaryfunc) DictionaryItem,
1545 /* mp_ass_subscript */ (objobjargproc) DictionaryAssItem,
1546};
1547
1548static PyTypeObject DictionaryType;
1549
1550 static void
1551DictionaryDestructor(PyObject *self)
1552{
1553 DictionaryObject *this = (DictionaryObject *)(self);
1554
1555 pyll_remove(&this->ref, &lastdict);
1556 dict_unref(this->dict);
1557
1558 Py_TYPE(self)->tp_free((PyObject*)self);
1559}
1560
1561/* List object - Definitions
1562 */
1563
1564static PyInt ListLength(PyObject *);
1565static PyObject *ListItem(PyObject *, Py_ssize_t);
1566
1567static PySequenceMethods ListAsSeq = {
1568 (lenfunc) ListLength, /* sq_length, len(x) */
1569 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */
1570 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */
1571 (ssizeargfunc) ListItem, /* sq_item, x[i] */
1572 (void *) 0, /* was_sq_slice, x[i:j] */
1573 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */
1574 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */
1575 0, /* sq_contains */
1576 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */
1577 0, /* sq_inplace_repeat */
1578};
1579
1580static PyObject *ListSubscript(PyObject *, PyObject *);
1581static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *);
1582
1583static PyMappingMethods ListAsMapping = {
1584 /* mp_length */ (lenfunc) ListLength,
1585 /* mp_subscript */ (binaryfunc) ListSubscript,
1586 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript,
1587};
1588
1589static PyTypeObject ListType;
1590
1591 static PyObject *
1592ListSubscript(PyObject *self, PyObject* idxObject)
1593{
1594 if (PyLong_Check(idxObject))
1595 {
1596 long idx = PyLong_AsLong(idxObject);
1597 return ListItem(self, idx);
1598 }
1599 else if (PySlice_Check(idxObject))
1600 {
1601 Py_ssize_t start, stop, step, slicelen;
1602
1603 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1604 &step, &slicelen) < 0)
1605 return NULL;
1606 return ListSlice(self, start, stop);
1607 }
1608 else
1609 {
1610 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1611 return NULL;
1612 }
1613}
1614
1615 static Py_ssize_t
1616ListAsSubscript(PyObject *self, PyObject *idxObject, PyObject *obj)
1617{
1618 if (PyLong_Check(idxObject))
1619 {
1620 long idx = PyLong_AsLong(idxObject);
1621 return ListAssItem(self, idx, obj);
1622 }
1623 else if (PySlice_Check(idxObject))
1624 {
1625 Py_ssize_t start, stop, step, slicelen;
1626
1627 if (PySlice_GetIndicesEx(idxObject, ListLength(self), &start, &stop,
1628 &step, &slicelen) < 0)
1629 return -1;
1630 return ListAssSlice(self, start, stop, obj);
1631 }
1632 else
1633 {
1634 PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
1635 return -1;
1636 }
1637}
1638
1639 static void
1640ListDestructor(PyObject *self)
1641{
1642 ListObject *this = (ListObject *)(self);
1643
1644 pyll_remove(&this->ref, &lastlist);
1645 list_unref(this->list);
1646
1647 Py_TYPE(self)->tp_free((PyObject*)self);
1648}
1649
1650/* Function object - Definitions
1651 */
1652
1653 static void
1654FunctionDestructor(PyObject *self)
1655{
1656 FunctionObject *this = (FunctionObject *) (self);
1657
1658 func_unref(this->name);
1659 PyMem_Del(this->name);
1660
1661 Py_TYPE(self)->tp_free((PyObject*)self);
1662}
1663
1664 static PyObject *
1665FunctionGetattro(PyObject *self, PyObject *nameobj)
1666{
1667 FunctionObject *this = (FunctionObject *)(self);
1668 char *name = "";
1669 if (PyUnicode_Check(nameobj))
1670 name = _PyUnicode_AsString(nameobj);
1671
1672 if (strcmp(name, "name") == 0)
1673 return PyUnicode_FromString((char *)(this->name));
1674
1675 return PyObject_GenericGetAttr(self, nameobj);
1676}
1677
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001678/* External interface
1679 */
1680
1681 void
1682python3_buffer_free(buf_T *buf)
1683{
1684 if (buf->b_python3_ref != NULL)
1685 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001686 BufferObject *bp = buf->b_python3_ref;
1687 bp->buf = INVALID_BUFFER_VALUE;
1688 buf->b_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001689 }
1690}
1691
1692#if defined(FEAT_WINDOWS) || defined(PROTO)
1693 void
1694python3_window_free(win_T *win)
1695{
1696 if (win->w_python3_ref != NULL)
1697 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001698 WindowObject *wp = win->w_python3_ref;
1699 wp->win = INVALID_WINDOW_VALUE;
1700 win->w_python3_ref = NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001701 }
1702}
1703#endif
1704
1705static BufListObject TheBufferList =
1706{
1707 PyObject_HEAD_INIT(&BufListType)
1708};
1709
1710static WinListObject TheWindowList =
1711{
1712 PyObject_HEAD_INIT(&WinListType)
1713};
1714
1715static CurrentObject TheCurrent =
1716{
1717 PyObject_HEAD_INIT(&CurrentType)
1718};
1719
1720PyDoc_STRVAR(vim_module_doc,"vim python interface\n");
1721
1722static struct PyModuleDef vimmodule;
1723
Bram Moolenaar69154f22010-07-18 21:42:34 +02001724#ifndef PROTO
1725PyMODINIT_FUNC Py3Init_vim(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001726{
1727 PyObject *mod;
1728 /* The special value is removed from sys.path in Python3_Init(). */
1729 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL};
1730
1731 PyType_Ready(&BufferType);
1732 PyType_Ready(&RangeType);
1733 PyType_Ready(&WindowType);
1734 PyType_Ready(&BufListType);
1735 PyType_Ready(&WinListType);
1736 PyType_Ready(&CurrentType);
Bram Moolenaardb913952012-06-29 12:54:53 +02001737 PyType_Ready(&DictionaryType);
1738 PyType_Ready(&ListType);
1739 PyType_Ready(&FunctionType);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001740
1741 /* Set sys.argv[] to avoid a crash in warn(). */
1742 PySys_SetArgv(1, argv);
1743
1744 mod = PyModule_Create(&vimmodule);
Bram Moolenaar19e60942011-06-19 00:27:51 +02001745 if (mod == NULL)
1746 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001747
Bram Moolenaar19e60942011-06-19 00:27:51 +02001748 VimError = PyErr_NewException("vim.error", NULL, NULL);
1749 Py_INCREF(VimError);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001750
1751 PyModule_AddObject(mod, "error", VimError);
1752 Py_INCREF((PyObject *)(void *)&TheBufferList);
1753 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList);
1754 Py_INCREF((PyObject *)(void *)&TheCurrent);
1755 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent);
1756 Py_INCREF((PyObject *)(void *)&TheWindowList);
1757 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList);
1758
1759 if (PyErr_Occurred())
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001760 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001761
1762 return mod;
1763}
Bram Moolenaar69154f22010-07-18 21:42:34 +02001764#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001765
1766/*************************************************************************
1767 * 4. Utility functions for handling the interface between Vim and Python.
1768 */
1769
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001770/* Convert a Vim line into a Python string.
1771 * All internal newlines are replaced by null characters.
1772 *
1773 * On errors, the Python exception data is set, and NULL is returned.
1774 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001775 static PyObject *
1776LineToString(const char *str)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001777{
1778 PyObject *result;
1779 Py_ssize_t len = strlen(str);
1780 char *tmp,*p;
1781
1782 tmp = (char *)alloc((unsigned)(len+1));
1783 p = tmp;
1784 if (p == NULL)
1785 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001786 PyErr_NoMemory();
1787 return NULL;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001788 }
1789
1790 while (*str)
1791 {
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001792 if (*str == '\n')
1793 *p = '\0';
1794 else
1795 *p = *str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001796
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001797 ++p;
1798 ++str;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001799 }
1800 *p = '\0';
1801
Bram Moolenaar3d64a312011-07-15 15:54:44 +02001802 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001803
1804 vim_free(tmp);
1805 return result;
1806}
1807
Bram Moolenaardb913952012-06-29 12:54:53 +02001808 void
1809do_py3eval (char_u *str, typval_T *rettv)
1810{
1811 DoPy3Command(NULL, (char *) str, rettv);
1812 switch(rettv->v_type)
1813 {
1814 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1815 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1816 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
1817 }
1818}
1819
1820 void
1821set_ref_in_python3 (int copyID)
1822{
1823 set_ref_in_py(copyID);
1824}
1825
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001826 static void
1827init_structs(void)
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001828{
1829 vim_memset(&OutputType, 0, sizeof(OutputType));
1830 OutputType.tp_name = "vim.message";
1831 OutputType.tp_basicsize = sizeof(OutputObject);
1832 OutputType.tp_getattro = OutputGetattro;
1833 OutputType.tp_setattro = OutputSetattro;
1834 OutputType.tp_flags = Py_TPFLAGS_DEFAULT;
1835 OutputType.tp_doc = "vim message object";
1836 OutputType.tp_methods = OutputMethods;
1837 OutputType.tp_alloc = call_PyType_GenericAlloc;
1838 OutputType.tp_new = call_PyType_GenericNew;
1839 OutputType.tp_free = call_PyObject_Free;
1840
1841 vim_memset(&BufferType, 0, sizeof(BufferType));
1842 BufferType.tp_name = "vim.buffer";
1843 BufferType.tp_basicsize = sizeof(BufferType);
1844 BufferType.tp_dealloc = BufferDestructor;
1845 BufferType.tp_repr = BufferRepr;
1846 BufferType.tp_as_sequence = &BufferAsSeq;
1847 BufferType.tp_as_mapping = &BufferAsMapping;
1848 BufferType.tp_getattro = BufferGetattro;
1849 BufferType.tp_flags = Py_TPFLAGS_DEFAULT;
1850 BufferType.tp_doc = "vim buffer object";
1851 BufferType.tp_methods = BufferMethods;
1852 BufferType.tp_alloc = call_PyType_GenericAlloc;
1853 BufferType.tp_new = call_PyType_GenericNew;
1854 BufferType.tp_free = call_PyObject_Free;
1855
Bram Moolenaar55d5c032010-07-17 23:52:29 +02001856 vim_memset(&WindowType, 0, sizeof(WindowType));
1857 WindowType.tp_name = "vim.window";
1858 WindowType.tp_basicsize = sizeof(WindowObject);
1859 WindowType.tp_dealloc = WindowDestructor;
1860 WindowType.tp_repr = WindowRepr;
1861 WindowType.tp_getattro = WindowGetattro;
1862 WindowType.tp_setattro = WindowSetattro;
1863 WindowType.tp_flags = Py_TPFLAGS_DEFAULT;
1864 WindowType.tp_doc = "vim Window object";
1865 WindowType.tp_methods = WindowMethods;
1866 WindowType.tp_alloc = call_PyType_GenericAlloc;
1867 WindowType.tp_new = call_PyType_GenericNew;
1868 WindowType.tp_free = call_PyObject_Free;
1869
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001870 vim_memset(&BufListType, 0, sizeof(BufListType));
1871 BufListType.tp_name = "vim.bufferlist";
1872 BufListType.tp_basicsize = sizeof(BufListObject);
1873 BufListType.tp_as_sequence = &BufListAsSeq;
1874 BufListType.tp_flags = Py_TPFLAGS_DEFAULT;
1875 BufferType.tp_doc = "vim buffer list";
1876
1877 vim_memset(&WinListType, 0, sizeof(WinListType));
1878 WinListType.tp_name = "vim.windowlist";
1879 WinListType.tp_basicsize = sizeof(WinListType);
1880 WinListType.tp_as_sequence = &WinListAsSeq;
1881 WinListType.tp_flags = Py_TPFLAGS_DEFAULT;
1882 WinListType.tp_doc = "vim window list";
1883
1884 vim_memset(&RangeType, 0, sizeof(RangeType));
1885 RangeType.tp_name = "vim.range";
1886 RangeType.tp_basicsize = sizeof(RangeObject);
1887 RangeType.tp_dealloc = RangeDestructor;
1888 RangeType.tp_repr = RangeRepr;
1889 RangeType.tp_as_sequence = &RangeAsSeq;
1890 RangeType.tp_as_mapping = &RangeAsMapping;
1891 RangeType.tp_getattro = RangeGetattro;
1892 RangeType.tp_flags = Py_TPFLAGS_DEFAULT;
1893 RangeType.tp_doc = "vim Range object";
1894 RangeType.tp_methods = RangeMethods;
1895 RangeType.tp_alloc = call_PyType_GenericAlloc;
1896 RangeType.tp_new = call_PyType_GenericNew;
1897 RangeType.tp_free = call_PyObject_Free;
1898
1899 vim_memset(&CurrentType, 0, sizeof(CurrentType));
1900 CurrentType.tp_name = "vim.currentdata";
1901 CurrentType.tp_basicsize = sizeof(CurrentObject);
1902 CurrentType.tp_getattro = CurrentGetattro;
1903 CurrentType.tp_setattro = CurrentSetattro;
1904 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT;
1905 CurrentType.tp_doc = "vim current object";
1906
Bram Moolenaardb913952012-06-29 12:54:53 +02001907 vim_memset(&DictionaryType, 0, sizeof(DictionaryType));
1908 DictionaryType.tp_name = "vim.dictionary";
1909 DictionaryType.tp_basicsize = sizeof(DictionaryObject);
1910 DictionaryType.tp_dealloc = DictionaryDestructor;
1911 DictionaryType.tp_as_mapping = &DictionaryAsMapping;
1912 DictionaryType.tp_flags = Py_TPFLAGS_DEFAULT;
1913 DictionaryType.tp_doc = "dictionary pushing modifications to vim structure";
1914 DictionaryType.tp_methods = DictionaryMethods;
1915
1916 vim_memset(&ListType, 0, sizeof(ListType));
1917 ListType.tp_name = "vim.list";
1918 ListType.tp_dealloc = ListDestructor;
1919 ListType.tp_basicsize = sizeof(ListObject);
1920 ListType.tp_as_sequence = &ListAsSeq;
1921 ListType.tp_as_mapping = &ListAsMapping;
1922 ListType.tp_flags = Py_TPFLAGS_DEFAULT;
1923 ListType.tp_doc = "list pushing modifications to vim structure";
1924 ListType.tp_methods = ListMethods;
1925
1926 vim_memset(&FunctionType, 0, sizeof(FunctionType));
1927 FunctionType.tp_name = "vim.list";
1928 FunctionType.tp_basicsize = sizeof(FunctionObject);
1929 FunctionType.tp_getattro = FunctionGetattro;
1930 FunctionType.tp_dealloc = FunctionDestructor;
1931 FunctionType.tp_call = FunctionCall;
1932 FunctionType.tp_flags = Py_TPFLAGS_DEFAULT;
1933 FunctionType.tp_doc = "object that calls vim function";
1934 FunctionType.tp_methods = FunctionMethods;
1935
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001936 vim_memset(&vimmodule, 0, sizeof(vimmodule));
1937 vimmodule.m_name = "vim";
1938 vimmodule.m_doc = vim_module_doc;
1939 vimmodule.m_size = -1;
1940 vimmodule.m_methods = VimMethods;
1941}