blob: 1740979ac748918a1bf512c69b65a6dcb055c8bb [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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#include "vim.h"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#include <limits.h>
23
24/* Python.h defines _POSIX_THREADS itself (if needed) */
25#ifdef _POSIX_THREADS
26# undef _POSIX_THREADS
27#endif
28
Bram Moolenaar860cae12010-06-05 23:22:07 +020029#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# undef HAVE_FCNTL_H
31#endif
32
33#ifdef _DEBUG
34# undef _DEBUG
35#endif
36
37#ifdef HAVE_STDARG_H
38# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
39#endif
Bram Moolenaarbe2c9ae2009-11-11 14:06:59 +000040#ifdef _POSIX_C_SOURCE
41# undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */
42#endif
43#ifdef _XOPEN_SOURCE
44# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
45#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
Bram Moolenaar2c45e942008-06-04 11:35:26 +000047#define PY_SSIZE_T_CLEAN
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#include <Python.h>
50#if defined(MACOS) && !defined(MACOS_X_UNIX)
51# include "macglue.h"
52# include <CodeFragments.h>
53#endif
54#undef main /* Defined in python.h - aargh */
55#undef HAVE_FCNTL_H /* Clash with os_win32.h */
56
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020057static void init_structs(void);
58
Bram Moolenaardb913952012-06-29 12:54:53 +020059#define PyBytes_FromString PyString_FromString
60
Bram Moolenaar19e60942011-06-19 00:27:51 +020061/* No-op conversion functions, use with care! */
62#define PyString_AsBytes(obj) (obj)
63#define PyString_FreeBytes(obj)
64
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#if !defined(FEAT_PYTHON) && defined(PROTO)
66/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000067# define PyObject Py_ssize_t
68# define PyThreadState Py_ssize_t
69# define PyTypeObject Py_ssize_t
70struct PyMethodDef { Py_ssize_t a; };
71# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
73
Bram Moolenaar2afa3232012-06-29 16:28:28 +020074#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
75# define PY_USE_CAPSULE
76#endif
77
Bram Moolenaar2c45e942008-06-04 11:35:26 +000078#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
79# define PyInt Py_ssize_t
80# define PyInquiry lenfunc
81# define PyIntArgFunc ssizeargfunc
82# define PyIntIntArgFunc ssizessizeargfunc
83# define PyIntObjArgProc ssizeobjargproc
84# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000085# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000086#else
87# define PyInt int
88# define PyInquiry inquiry
89# define PyIntArgFunc intargfunc
90# define PyIntIntArgFunc intintargfunc
91# define PyIntObjArgProc intobjargproc
92# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000093# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000094#endif
95
Bram Moolenaar071d4272004-06-13 20:20:40 +000096/* Parser flags */
97#define single_input 256
98#define file_input 257
99#define eval_input 258
100
101#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
102 /* Python 2.3: can invoke ":python" recursively. */
103# define PY_CAN_RECURSE
104#endif
105
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200106# if defined(DYNAMIC_PYTHON) || defined(PROTO)
107# ifndef DYNAMIC_PYTHON
108# define HINSTANCE long_u /* for generating prototypes */
109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200111# ifndef WIN3264
112# include <dlfcn.h>
113# define FARPROC void*
114# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100115# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200116# define load_dll(n) dlopen((n), RTLD_LAZY)
117# else
118# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
119# endif
120# define close_dll dlclose
121# define symbol_from_dll dlsym
122# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200123# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200124# define close_dll FreeLibrary
125# define symbol_from_dll GetProcAddress
126# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200127
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000128/* This makes if_python.c compile without warnings against Python 2.5
129 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200130# undef PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200131# undef PyRun_String
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200132# undef PyArg_Parse
133# undef PyArg_ParseTuple
134# undef Py_BuildValue
135# undef Py_InitModule4
136# undef Py_InitModule4_64
Bram Moolenaardb913952012-06-29 12:54:53 +0200137# undef PyObject_CallMethod
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * Wrapper defines
141 */
142# define PyArg_Parse dll_PyArg_Parse
143# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200144# define PyMem_Free dll_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200145# define PyMem_Malloc dll_PyMem_Malloc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define PyDict_SetItemString dll_PyDict_SetItemString
147# define PyErr_BadArgument dll_PyErr_BadArgument
148# define PyErr_Clear dll_PyErr_Clear
149# define PyErr_NoMemory dll_PyErr_NoMemory
150# define PyErr_Occurred dll_PyErr_Occurred
151# define PyErr_SetNone dll_PyErr_SetNone
152# define PyErr_SetString dll_PyErr_SetString
153# define PyEval_InitThreads dll_PyEval_InitThreads
154# define PyEval_RestoreThread dll_PyEval_RestoreThread
155# define PyEval_SaveThread dll_PyEval_SaveThread
156# ifdef PY_CAN_RECURSE
157# define PyGILState_Ensure dll_PyGILState_Ensure
158# define PyGILState_Release dll_PyGILState_Release
159# endif
160# define PyInt_AsLong dll_PyInt_AsLong
161# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200162# define PyLong_AsLong dll_PyLong_AsLong
163# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200165# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000167# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# define PyList_New dll_PyList_New
169# define PyList_SetItem dll_PyList_SetItem
170# define PyList_Size dll_PyList_Size
171# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200172# define PySequence_Check dll_PySequence_Check
173# define PySequence_Size dll_PySequence_Size
174# define PySequence_GetItem dll_PySequence_GetItem
175# define PyTuple_Size dll_PyTuple_Size
176# define PyTuple_GetItem dll_PyTuple_GetItem
177# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000179# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200181# define PyDict_Next dll_PyDict_Next
182# ifdef PyMapping_Items
183# define PY_NO_MAPPING_ITEMS
184# else
185# define PyMapping_Items dll_PyMapping_Items
186# endif
187# define PyObject_CallMethod dll_PyObject_CallMethod
188# define PyMapping_Check dll_PyMapping_Check
189# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# define PyModule_GetDict dll_PyModule_GetDict
191# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200192# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# define PyString_AsString dll_PyString_AsString
194# define PyString_FromString dll_PyString_FromString
195# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
196# define PyString_Size dll_PyString_Size
197# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200199# undef PyUnicode_AsEncodedString
200# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200201# define PyFloat_AsDouble dll_PyFloat_AsDouble
202# define PyFloat_FromDouble dll_PyFloat_FromDouble
203# define PyFloat_Type (*dll_PyFloat_Type)
204# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205# define PySys_SetObject dll_PySys_SetObject
206# define PySys_SetArgv dll_PySys_SetArgv
207# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100208# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# define Py_BuildValue dll_Py_BuildValue
210# define Py_FindMethod dll_Py_FindMethod
211# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100212# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000214# define Py_Finalize dll_Py_Finalize
215# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200217# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
218# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define _Py_NoneStruct (*dll__Py_NoneStruct)
221# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200222# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
224# define PyType_IsSubtype dll_PyType_IsSubtype
225# endif
226# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
227# define PyObject_Malloc dll_PyObject_Malloc
228# define PyObject_Free dll_PyObject_Free
229# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200230# ifdef PY_USE_CAPSULE
231# define PyCapsule_New dll_PyCapsule_New
232# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
233# else
234# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
235# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237
238/*
239 * Pointers for dynamic link
240 */
241static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
242static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200243static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200244static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
246static int(*dll_PyErr_BadArgument)(void);
247static void(*dll_PyErr_Clear)(void);
248static PyObject*(*dll_PyErr_NoMemory)(void);
249static PyObject*(*dll_PyErr_Occurred)(void);
250static void(*dll_PyErr_SetNone)(PyObject *);
251static void(*dll_PyErr_SetString)(PyObject *, const char *);
252static void(*dll_PyEval_InitThreads)(void);
253static void(*dll_PyEval_RestoreThread)(PyThreadState *);
254static PyThreadState*(*dll_PyEval_SaveThread)(void);
255# ifdef PY_CAN_RECURSE
256static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
257static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259static long(*dll_PyInt_AsLong)(PyObject *);
260static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200261static long(*dll_PyLong_AsLong)(PyObject *);
262static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200264static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000265static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000266static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000267static PyObject*(*dll_PyList_New)(PyInt size);
268static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
269static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200271static int (*dll_PySequence_Check)(PyObject *);
272static PyInt(*dll_PySequence_Size)(PyObject *);
273static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
274static PyInt(*dll_PyTuple_Size)(PyObject *);
275static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
276static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000278static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200280static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
281# ifndef PY_NO_MAPPING_ITEMS
282static PyObject* (*dll_PyMapping_Items)(PyObject *);
283# endif
284static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
285static int (*dll_PyMapping_Check)(PyObject *);
286static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static PyObject*(*dll_PyModule_GetDict)(PyObject *);
288static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200289static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static char*(*dll_PyString_AsString)(PyObject *);
291static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000292static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
293static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200295static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200296static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200297static double(*dll_PyFloat_AsDouble)(PyObject *);
298static PyObject*(*dll_PyFloat_FromDouble)(double);
299static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300static int(*dll_PySys_SetObject)(char *, PyObject *);
301static int(*dll_PySys_SetArgv)(int, char **);
302static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100303static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static PyObject*(*dll_Py_BuildValue)(char *, ...);
305static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
306static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200307static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100308static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000310static void(*dll_Py_Finalize)(void);
311static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
313static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200314static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200315# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200316static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static PyObject* dll__Py_NoneStruct;
319# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
320static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
321# endif
322# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
323static void* (*dll_PyObject_Malloc)(size_t);
324static void (*dll_PyObject_Free)(void*);
325# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200326# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200327static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
328static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200329# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200330static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
331static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
334static HINSTANCE hinstPython = 0; /* Instance of python.dll */
335
336/* Imported exception objects */
337static PyObject *imp_PyExc_AttributeError;
338static PyObject *imp_PyExc_IndexError;
339static PyObject *imp_PyExc_KeyboardInterrupt;
340static PyObject *imp_PyExc_TypeError;
341static PyObject *imp_PyExc_ValueError;
342
343# define PyExc_AttributeError imp_PyExc_AttributeError
344# define PyExc_IndexError imp_PyExc_IndexError
345# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
346# define PyExc_TypeError imp_PyExc_TypeError
347# define PyExc_ValueError imp_PyExc_ValueError
348
349/*
350 * Table of name to function pointer of python.
351 */
352# define PYTHON_PROC FARPROC
353static struct
354{
355 char *name;
356 PYTHON_PROC *ptr;
357} python_funcname_table[] =
358{
359 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
360 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200361 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200362 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
364 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
365 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
366 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
367 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
368 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
369 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
370 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
371 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
372 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
373# ifdef PY_CAN_RECURSE
374 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
375 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
376# endif
377 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
378 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200379 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
380 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200382 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000384 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
386 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
387 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
388 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200389 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
390 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
391 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
392 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
393 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
394 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
396 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200397 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000398 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200399# ifndef PY_NO_MAPPING_ITEMS
400 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
401# endif
402 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
403 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
404 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
406 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200407 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
409 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
410 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
411 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
412 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200413 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200414 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
415 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
416 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
417 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
419 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
420 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100421 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
423 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200424# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
425 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000426 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
427# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000429# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100430 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000432 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
433 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
435 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200436 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200437# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200438 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
441# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
442 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
443# endif
444# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
445 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
446 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
447# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200448# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
450 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200451# else
452 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
453 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {"", NULL},
456};
457
458/*
459 * Free python.dll
460 */
461 static void
462end_dynamic_python(void)
463{
464 if (hinstPython)
465 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200466 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 hinstPython = 0;
468 }
469}
470
471/*
472 * Load library and get all pointers.
473 * Parameter 'libname' provides name of DLL.
474 * Return OK or FAIL.
475 */
476 static int
477python_runtime_link_init(char *libname, int verbose)
478{
479 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200480 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100482#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200483 /* Can't have Python and Python3 loaded at the same time.
484 * It cause a crash, because RTLD_GLOBAL is needed for
485 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200486 if (python3_loaded())
487 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200488 if (verbose)
489 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200490 return FAIL;
491 }
492#endif
493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 if (hinstPython)
495 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200496 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 if (!hinstPython)
498 {
499 if (verbose)
500 EMSG2(_(e_loadlib), libname);
501 return FAIL;
502 }
503
504 for (i = 0; python_funcname_table[i].ptr; ++i)
505 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200506 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 python_funcname_table[i].name)) == NULL)
508 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200509 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 hinstPython = 0;
511 if (verbose)
512 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
513 return FAIL;
514 }
515 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200516
517 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
518 * will be present in the library. */
519 ucs_as_encoded_string = symbol_from_dll(hinstPython,
520 "PyUnicodeUCS2_AsEncodedString");
521 if (ucs_as_encoded_string == NULL)
522 ucs_as_encoded_string = symbol_from_dll(hinstPython,
523 "PyUnicodeUCS4_AsEncodedString");
524 if (ucs_as_encoded_string != NULL)
525 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
526 else
527 {
528 close_dll(hinstPython);
529 hinstPython = 0;
530 if (verbose)
531 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
532 return FAIL;
533 }
534
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 return OK;
536}
537
538/*
539 * If python is enabled (there is installed python on Windows system) return
540 * TRUE, else FALSE.
541 */
542 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000543python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
546}
547
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200548/*
549 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 * DLL, as this can cause errors (importing data symbols is not reliable).
551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200553get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555 PyObject *exmod = PyImport_ImportModule("exceptions");
556 PyObject *exdict = PyModule_GetDict(exmod);
557 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
558 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
559 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
560 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
561 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
562 Py_XINCREF(imp_PyExc_AttributeError);
563 Py_XINCREF(imp_PyExc_IndexError);
564 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
565 Py_XINCREF(imp_PyExc_TypeError);
566 Py_XINCREF(imp_PyExc_ValueError);
567 Py_XDECREF(exmod);
568}
569#endif /* DYNAMIC_PYTHON */
570
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200571static PyObject *BufferNew (buf_T *);
572static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200573static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574static PyObject *LineToString(const char *);
575
576static PyTypeObject RangeType;
577
Bram Moolenaardb913952012-06-29 12:54:53 +0200578static int initialised = 0;
579#define PYINITIALISED initialised
580
581/* Add conversion from PyInt? */
582#define DICTKEY_GET(err) \
583 if (!PyString_Check(keyObject)) \
584 { \
585 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
586 return err; \
587 } \
588 key = (char_u *) PyString_AsString(keyObject);
589#define DICTKEY_UNREF
590#define DICTKEY_DECL
591
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200592/*
593 * Include the code shared with if_python3.c
594 */
595#include "if_py_both.h"
596
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598/******************************************************
599 * Internal function prototypes.
600 */
601
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000602static PyInt RangeStart;
603static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaardb913952012-06-29 12:54:53 +0200605static PyObject *globals;
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607static void PythonIO_Flush(void);
608static int PythonIO_Init(void);
609static int PythonMod_Init(void);
610
611/* Utility functions for the vim/python interface
612 * ----------------------------------------------
613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000615static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
618/******************************************************
619 * 1. Python interpreter main program.
620 */
621
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
623typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000626#ifdef PY_CAN_RECURSE
627static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
628#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000629static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632/*
633 * Suspend a thread of the Python interpreter, other threads are allowed to
634 * run.
635 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000636 static void
637Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639#ifdef PY_CAN_RECURSE
640 PyGILState_Release(pygilstate);
641#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644}
645
646/*
647 * Restore a thread of the Python interpreter, waits for other threads to
648 * block.
649 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000650 static void
651Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653#ifdef PY_CAN_RECURSE
654 pygilstate = PyGILState_Ensure();
655#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 PyEval_RestoreThread(saved_python_thread);
657 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000659}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 void
662python_end()
663{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000664 static int recurse = 0;
665
666 /* If a crash occurs while doing this, don't try again. */
667 if (recurse != 0)
668 return;
669
670 ++recurse;
671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000673 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000674 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000675 Python_RestoreThread(); /* enter python */
676 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000679#else
680 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000682 Python_RestoreThread(); /* enter python */
683 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000686
687 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688}
689
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200690#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
691 int
692python_loaded()
693{
694 return (hinstPython != 0);
695}
696#endif
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 static int
699Python_Init(void)
700{
701 if (!initialised)
702 {
703#ifdef DYNAMIC_PYTHON
704 if (!python_enabled(TRUE))
705 {
706 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
707 goto fail;
708 }
709#endif
710
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100711#ifdef PYTHON_HOME
712 Py_SetPythonHome(PYTHON_HOME);
713#endif
714
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200715 init_structs();
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#if !defined(MACOS) || defined(MACOS_X_UNIX)
718 Py_Initialize();
719#else
720 PyMac_Initialize();
721#endif
722 /* initialise threads */
723 PyEval_InitThreads();
724
725#ifdef DYNAMIC_PYTHON
726 get_exceptions();
727#endif
728
729 if (PythonIO_Init())
730 goto fail;
731
732 if (PythonMod_Init())
733 goto fail;
734
Bram Moolenaardb913952012-06-29 12:54:53 +0200735 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
736
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000737 /* Remove the element from sys.path that was added because of our
738 * argv[0] value in PythonMod_Init(). Previously we used an empty
739 * string, but dependinding on the OS we then get an empty entry or
740 * the current directory in sys.path. */
741 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
742
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000743 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746 initialised = 1;
747 }
748
749 return 0;
750
751fail:
752 /* We call PythonIO_Flush() here to print any Python errors.
753 * This is OK, as it is possible to call this function even
754 * if PythonIO_Init() has not completed successfully (it will
755 * not do anything in this case).
756 */
757 PythonIO_Flush();
758 return -1;
759}
760
761/*
762 * External interface
763 */
764 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200765DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000767#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 static int recursive = 0;
769#endif
770#if defined(MACOS) && !defined(MACOS_X_UNIX)
771 GrafPtr oldPort;
772#endif
773#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
774 char *saved_locale;
775#endif
776
777#ifndef PY_CAN_RECURSE
778 if (recursive)
779 {
780 EMSG(_("E659: Cannot invoke Python recursively"));
781 return;
782 }
783 ++recursive;
784#endif
785
786#if defined(MACOS) && !defined(MACOS_X_UNIX)
787 GetPort(&oldPort);
788 /* Check if the Python library is available */
789 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
790 goto theend;
791#endif
792 if (Python_Init())
793 goto theend;
794
Bram Moolenaardb913952012-06-29 12:54:53 +0200795 if (rettv == NULL)
796 {
797 RangeStart = eap->line1;
798 RangeEnd = eap->line2;
799 }
800 else
801 {
802 RangeStart = (PyInt) curwin->w_cursor.lnum;
803 RangeEnd = RangeStart;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 Python_Release_Vim(); /* leave vim */
806
807#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
808 /* Python only works properly when the LC_NUMERIC locale is "C". */
809 saved_locale = setlocale(LC_NUMERIC, NULL);
810 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
811 saved_locale = NULL;
812 else
813 {
814 /* Need to make a copy, value may change when setting new locale. */
815 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
816 (void)setlocale(LC_NUMERIC, "C");
817 }
818#endif
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
Bram Moolenaardb913952012-06-29 12:54:53 +0200822 if (rettv == NULL)
823 PyRun_SimpleString((char *)(cmd));
824 else
825 {
826 PyObject *r;
827
828 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
829 if (r == NULL)
830 EMSG(_("E858: Eval did not return a valid python object"));
831 else
832 {
833 if (ConvertFromPyObject(r, rettv) == -1)
834 EMSG(_("E859: Failed to convert returned python object to vim value"));
835 Py_DECREF(r);
836 }
837 PyErr_Clear();
838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
843 if (saved_locale != NULL)
844 {
845 (void)setlocale(LC_NUMERIC, saved_locale);
846 vim_free(saved_locale);
847 }
848#endif
849
850 Python_Lock_Vim(); /* enter vim */
851 PythonIO_Flush();
852#if defined(MACOS) && !defined(MACOS_X_UNIX)
853 SetPort(oldPort);
854#endif
855
856theend:
857#ifndef PY_CAN_RECURSE
858 --recursive;
859#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/*
864 * ":python"
865 */
866 void
867ex_python(exarg_T *eap)
868{
869 char_u *script;
870
871 script = script_get(eap, eap->arg);
872 if (!eap->skip)
873 {
874 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200875 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200877 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879 vim_free(script);
880}
881
882#define BUFFER_SIZE 1024
883
884/*
885 * ":pyfile"
886 */
887 void
888ex_pyfile(exarg_T *eap)
889{
890 static char buffer[BUFFER_SIZE];
891 const char *file = (char *)eap->arg;
892 char *p;
893
894 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
895 * stdio file pointer, but Vim and the Python DLL are compiled with
896 * different options under Windows, meaning that stdio pointers aren't
897 * compatible between the two. Yuk.
898 *
899 * Put the string "execfile('file')" into buffer. But, we need to
900 * escape any backslashes or single quotes in the file name, so that
901 * Python won't mangle the file name.
902 */
903 strcpy(buffer, "execfile('");
904 p = buffer + 10; /* size of "execfile('" */
905
906 while (*file && p < buffer + (BUFFER_SIZE - 3))
907 {
908 if (*file == '\\' || *file == '\'')
909 *p++ = '\\';
910 *p++ = *file++;
911 }
912
913 /* If we didn't finish the file name, we hit a buffer overflow */
914 if (*file != '\0')
915 return;
916
917 /* Put in the terminating "')" and a null */
918 *p++ = '\'';
919 *p++ = ')';
920 *p++ = '\0';
921
922 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200923 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
926/******************************************************
927 * 2. Python output stream: writes output via [e]msg().
928 */
929
930/* Implementation functions
931 */
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 static PyObject *
934OutputGetattr(PyObject *self, char *name)
935{
936 if (strcmp(name, "softspace") == 0)
937 return PyInt_FromLong(((OutputObject *)(self))->softspace);
938
939 return Py_FindMethod(OutputMethods, self, name);
940}
941
942 static int
943OutputSetattr(PyObject *self, char *name, PyObject *val)
944{
Bram Moolenaardb913952012-06-29 12:54:53 +0200945 if (val == NULL)
946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
948 return -1;
949 }
950
951 if (strcmp(name, "softspace") == 0)
952 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200953 if (!PyInt_Check(val))
954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
956 return -1;
957 }
958
959 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
960 return 0;
961 }
962
963 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
964 return -1;
965}
966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967/***************/
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 static int
970PythonIO_Init(void)
971{
972 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100973 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200975 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/******************************************************
979 * 3. Implementation of the Vim module for Python
980 */
981
Bram Moolenaardb913952012-06-29 12:54:53 +0200982static PyObject *ConvertToPyObject(typval_T *);
983static int ConvertFromPyObject(PyObject *, typval_T *);
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/* Window type - Implementation functions
986 * --------------------------------------
987 */
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991static void WindowDestructor(PyObject *);
992static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994/* Buffer type - Implementation functions
995 * --------------------------------------
996 */
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000static void BufferDestructor(PyObject *);
1001static PyObject *BufferGetattr(PyObject *, char *);
1002static PyObject *BufferRepr(PyObject *);
1003
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001004static PyInt BufferLength(PyObject *);
1005static PyObject *BufferItem(PyObject *, PyInt);
1006static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
1007static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1008static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/* Line range type - Implementation functions
1011 * --------------------------------------
1012 */
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1015
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001016static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1017static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019/* Current objects type - Implementation functions
1020 * -----------------------------------------------
1021 */
1022
1023static PyObject *CurrentGetattr(PyObject *, char *);
1024static int CurrentSetattr(PyObject *, char *, PyObject *);
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001027 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001029 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
1030 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1031 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1032 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
1033 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034};
1035
1036static PyTypeObject BufferType = {
1037 PyObject_HEAD_INIT(0)
1038 0,
1039 "buffer",
1040 sizeof(BufferObject),
1041 0,
1042
1043 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
1044 (printfunc) 0, /* tp_print, print x */
1045 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
1046 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1047 (cmpfunc) 0, /* tp_compare, x>y */
1048 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
1049
1050 0, /* as number */
1051 &BufferAsSeq, /* as sequence */
1052 0, /* as mapping */
1053
1054 (hashfunc) 0, /* tp_hash, dict(x) */
1055 (ternaryfunc) 0, /* tp_call, x() */
1056 (reprfunc) 0, /* tp_str, str(x) */
1057};
1058
1059/* Buffer object - Implementation
1060 */
1061
1062 static PyObject *
1063BufferNew(buf_T *buf)
1064{
1065 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001066 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 * then we can get at it in buf_freeall() in vim. We then
1068 * need to create only ONE Python object per buffer - if
1069 * we try to create a second, just INCREF the existing one
1070 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001071 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * Question: what to do on a buf_freeall(). We'll probably
1073 * have to either delete the Python object (DECREF it to
1074 * zero - a bad idea, as it leaves dangling refs!) or
1075 * set the buf_T * value to an invalid value (-1?), which
1076 * means we need checks in all access functions... Bah.
1077 */
1078
1079 BufferObject *self;
1080
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001083 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 Py_INCREF(self);
1085 }
1086 else
1087 {
1088 self = PyObject_NEW(BufferObject, &BufferType);
1089 if (self == NULL)
1090 return NULL;
1091 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001092 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 }
1094
1095 return (PyObject *)(self);
1096}
1097
1098 static void
1099BufferDestructor(PyObject *self)
1100{
1101 BufferObject *this = (BufferObject *)(self);
1102
1103 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001104 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaar658ada62006-10-03 13:02:36 +00001106 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107}
1108
1109 static PyObject *
1110BufferGetattr(PyObject *self, char *name)
1111{
1112 BufferObject *this = (BufferObject *)(self);
1113
1114 if (CheckBuffer(this))
1115 return NULL;
1116
1117 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001118 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001120 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (strcmp(name,"__members__") == 0)
1122 return Py_BuildValue("[ss]", "name", "number");
1123 else
1124 return Py_FindMethod(BufferMethods, self, name);
1125}
1126
1127 static PyObject *
1128BufferRepr(PyObject *self)
1129{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001130 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 BufferObject *this = (BufferObject *)(self);
1132
1133 if (this->buf == INVALID_BUFFER_VALUE)
1134 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001135 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 return PyString_FromString(repr);
1137 }
1138 else
1139 {
1140 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001141 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
1143 if (name == NULL)
1144 name = "";
1145 len = strlen(name);
1146
1147 if (len > 35)
1148 name = name + (35 - len);
1149
Bram Moolenaar555b2802005-05-19 21:08:39 +00001150 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
1152 return PyString_FromString(repr);
1153 }
1154}
1155
1156/******************/
1157
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001158 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159BufferLength(PyObject *self)
1160{
1161 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1162 if (CheckBuffer((BufferObject *)(self)))
1163 return -1; /* ??? */
1164
1165 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1166}
1167
1168 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001169BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 return RBItem((BufferObject *)(self), n, 1,
1172 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1173}
1174
1175 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001176BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
1178 return RBSlice((BufferObject *)(self), lo, hi, 1,
1179 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1180}
1181
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001182 static PyInt
1183BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001185 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001186 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 NULL);
1188}
1189
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001190 static PyInt
1191BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001193 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001194 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 NULL);
1196}
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001199 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001201 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1202 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1203 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1204 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1205 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206};
1207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208/* Line range object - Implementation
1209 */
1210
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 static void
1212RangeDestructor(PyObject *self)
1213{
1214 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001215 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216}
1217
1218 static PyObject *
1219RangeGetattr(PyObject *self, char *name)
1220{
1221 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001222 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001224 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 else
1226 return Py_FindMethod(RangeMethods, self, name);
1227}
1228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229/****************/
1230
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001231 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001232RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001234 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ((RangeObject *)(self))->start,
1236 ((RangeObject *)(self))->end,
1237 &((RangeObject *)(self))->end);
1238}
1239
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001240 static PyInt
1241RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001243 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ((RangeObject *)(self))->start,
1245 ((RangeObject *)(self))->end,
1246 &((RangeObject *)(self))->end);
1247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249/* Buffer list object - Definitions
1250 */
1251
1252typedef struct
1253{
1254 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001255} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001258 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001260 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1261 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1262 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1263 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1264 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265};
1266
1267static PyTypeObject BufListType = {
1268 PyObject_HEAD_INIT(0)
1269 0,
1270 "buffer list",
1271 sizeof(BufListObject),
1272 0,
1273
1274 (destructor) 0, /* tp_dealloc, refcount==0 */
1275 (printfunc) 0, /* tp_print, print x */
1276 (getattrfunc) 0, /* tp_getattr, x.attr */
1277 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1278 (cmpfunc) 0, /* tp_compare, x>y */
1279 (reprfunc) 0, /* tp_repr, `x`, print x */
1280
1281 0, /* as number */
1282 &BufListAsSeq, /* as sequence */
1283 0, /* as mapping */
1284
1285 (hashfunc) 0, /* tp_hash, dict(x) */
1286 (ternaryfunc) 0, /* tp_call, x() */
1287 (reprfunc) 0, /* tp_str, str(x) */
1288};
1289
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290/* Window object - Definitions
1291 */
1292
1293static struct PyMethodDef WindowMethods[] = {
1294 /* name, function, calling, documentation */
1295 { NULL, NULL, 0, NULL }
1296};
1297
1298static PyTypeObject WindowType = {
1299 PyObject_HEAD_INIT(0)
1300 0,
1301 "window",
1302 sizeof(WindowObject),
1303 0,
1304
1305 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1306 (printfunc) 0, /* tp_print, print x */
1307 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1308 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1309 (cmpfunc) 0, /* tp_compare, x>y */
1310 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1311
1312 0, /* as number */
1313 0, /* as sequence */
1314 0, /* as mapping */
1315
1316 (hashfunc) 0, /* tp_hash, dict(x) */
1317 (ternaryfunc) 0, /* tp_call, x() */
1318 (reprfunc) 0, /* tp_str, str(x) */
1319};
1320
1321/* Window object - Implementation
1322 */
1323
1324 static PyObject *
1325WindowNew(win_T *win)
1326{
1327 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001328 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 * then we can get at it in win_free() in vim. We then
1330 * need to create only ONE Python object per window - if
1331 * we try to create a second, just INCREF the existing one
1332 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001333 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * On a win_free() we set the Python object's win_T* field
1335 * to an invalid value. We trap all uses of a window
1336 * object, and reject them if the win_T* field is invalid.
1337 */
1338
1339 WindowObject *self;
1340
Bram Moolenaare344bea2005-09-01 20:46:49 +00001341 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001343 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 Py_INCREF(self);
1345 }
1346 else
1347 {
1348 self = PyObject_NEW(WindowObject, &WindowType);
1349 if (self == NULL)
1350 return NULL;
1351 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001352 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354
1355 return (PyObject *)(self);
1356}
1357
1358 static void
1359WindowDestructor(PyObject *self)
1360{
1361 WindowObject *this = (WindowObject *)(self);
1362
1363 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001364 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar658ada62006-10-03 13:02:36 +00001366 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
1368
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 static PyObject *
1370WindowGetattr(PyObject *self, char *name)
1371{
1372 WindowObject *this = (WindowObject *)(self);
1373
1374 if (CheckWindow(this))
1375 return NULL;
1376
1377 if (strcmp(name, "buffer") == 0)
1378 return (PyObject *)BufferNew(this->win->w_buffer);
1379 else if (strcmp(name, "cursor") == 0)
1380 {
1381 pos_T *pos = &this->win->w_cursor;
1382
1383 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1384 }
1385 else if (strcmp(name, "height") == 0)
1386 return Py_BuildValue("l", (long)(this->win->w_height));
1387#ifdef FEAT_VERTSPLIT
1388 else if (strcmp(name, "width") == 0)
1389 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1390#endif
1391 else if (strcmp(name,"__members__") == 0)
1392 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1393 else
1394 return Py_FindMethod(WindowMethods, self, name);
1395}
1396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397/* Window list object - Definitions
1398 */
1399
1400typedef struct
1401{
1402 PyObject_HEAD
1403}
1404WinListObject;
1405
1406static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001407 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001409 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1410 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1411 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1412 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1413 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414};
1415
1416static PyTypeObject WinListType = {
1417 PyObject_HEAD_INIT(0)
1418 0,
1419 "window list",
1420 sizeof(WinListObject),
1421 0,
1422
1423 (destructor) 0, /* tp_dealloc, refcount==0 */
1424 (printfunc) 0, /* tp_print, print x */
1425 (getattrfunc) 0, /* tp_getattr, x.attr */
1426 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1427 (cmpfunc) 0, /* tp_compare, x>y */
1428 (reprfunc) 0, /* tp_repr, `x`, print x */
1429
1430 0, /* as number */
1431 &WinListAsSeq, /* as sequence */
1432 0, /* as mapping */
1433
1434 (hashfunc) 0, /* tp_hash, dict(x) */
1435 (ternaryfunc) 0, /* tp_call, x() */
1436 (reprfunc) 0, /* tp_str, str(x) */
1437};
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439/* Current items object - Definitions
1440 */
1441
1442typedef struct
1443{
1444 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001445} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447static PyTypeObject CurrentType = {
1448 PyObject_HEAD_INIT(0)
1449 0,
1450 "current data",
1451 sizeof(CurrentObject),
1452 0,
1453
1454 (destructor) 0, /* tp_dealloc, refcount==0 */
1455 (printfunc) 0, /* tp_print, print x */
1456 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1457 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1458 (cmpfunc) 0, /* tp_compare, x>y */
1459 (reprfunc) 0, /* tp_repr, `x`, print x */
1460
1461 0, /* as number */
1462 0, /* as sequence */
1463 0, /* as mapping */
1464
1465 (hashfunc) 0, /* tp_hash, dict(x) */
1466 (ternaryfunc) 0, /* tp_call, x() */
1467 (reprfunc) 0, /* tp_str, str(x) */
1468};
1469
1470/* Current items object - Implementation
1471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001473CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 if (strcmp(name, "buffer") == 0)
1476 return (PyObject *)BufferNew(curbuf);
1477 else if (strcmp(name, "window") == 0)
1478 return (PyObject *)WindowNew(curwin);
1479 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001480 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 else if (strcmp(name, "range") == 0)
1482 return RangeNew(curbuf, RangeStart, RangeEnd);
1483 else if (strcmp(name,"__members__") == 0)
1484 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1485 else
1486 {
1487 PyErr_SetString(PyExc_AttributeError, name);
1488 return NULL;
1489 }
1490}
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001493CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494{
1495 if (strcmp(name, "line") == 0)
1496 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001497 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 return -1;
1499
1500 return 0;
1501 }
1502 else
1503 {
1504 PyErr_SetString(PyExc_AttributeError, name);
1505 return -1;
1506 }
1507}
1508
1509/* External interface
1510 */
1511
1512 void
1513python_buffer_free(buf_T *buf)
1514{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001515 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001517 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001519 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 }
1521}
1522
1523#if defined(FEAT_WINDOWS) || defined(PROTO)
1524 void
1525python_window_free(win_T *win)
1526{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001527 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001529 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001531 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533}
1534#endif
1535
1536static BufListObject TheBufferList =
1537{
1538 PyObject_HEAD_INIT(&BufListType)
1539};
1540
1541static WinListObject TheWindowList =
1542{
1543 PyObject_HEAD_INIT(&WinListType)
1544};
1545
1546static CurrentObject TheCurrent =
1547{
1548 PyObject_HEAD_INIT(&CurrentType)
1549};
1550
1551 static int
1552PythonMod_Init(void)
1553{
1554 PyObject *mod;
1555 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001556 /* The special value is removed from sys.path in Python_Init(). */
1557 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
1559 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001560 PyType_Ready(&BufferType);
1561 PyType_Ready(&RangeType);
1562 PyType_Ready(&WindowType);
1563 PyType_Ready(&BufListType);
1564 PyType_Ready(&WinListType);
1565 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 /* Set sys.argv[] to avoid a crash in warn(). */
1568 PySys_SetArgv(1, argv);
1569
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001570 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 dict = PyModule_GetDict(mod);
1572
1573 VimError = Py_BuildValue("s", "vim.error");
1574
1575 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001576 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1577 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1578 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 if (PyErr_Occurred())
1581 return -1;
1582
1583 return 0;
1584}
1585
1586/*************************************************************************
1587 * 4. Utility functions for handling the interface between Vim and Python.
1588 */
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590/* Convert a Vim line into a Python string.
1591 * All internal newlines are replaced by null characters.
1592 *
1593 * On errors, the Python exception data is set, and NULL is returned.
1594 */
1595 static PyObject *
1596LineToString(const char *str)
1597{
1598 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001599 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 char *p;
1601
1602 /* Allocate an Python string object, with uninitialised contents. We
1603 * must do it this way, so that we can modify the string in place
1604 * later. See the Python source, Objects/stringobject.c for details.
1605 */
1606 result = PyString_FromStringAndSize(NULL, len);
1607 if (result == NULL)
1608 return NULL;
1609
1610 p = PyString_AsString(result);
1611
1612 while (*str)
1613 {
1614 if (*str == '\n')
1615 *p = '\0';
1616 else
1617 *p = *str;
1618
1619 ++p;
1620 ++str;
1621 }
1622
1623 return result;
1624}
1625
Bram Moolenaardb913952012-06-29 12:54:53 +02001626static void DictionaryDestructor(PyObject *);
1627static PyObject *DictionaryGetattr(PyObject *, char*);
1628
1629static PyMappingMethods DictionaryAsMapping = {
1630 (PyInquiry) DictionaryLength,
1631 (binaryfunc) DictionaryItem,
1632 (objobjargproc) DictionaryAssItem,
1633};
1634
1635static PyTypeObject DictionaryType = {
1636 PyObject_HEAD_INIT(0)
1637 0,
1638 "vimdictionary",
1639 sizeof(DictionaryObject),
1640 0,
1641
1642 (destructor) DictionaryDestructor,
1643 (printfunc) 0,
1644 (getattrfunc) DictionaryGetattr,
1645 (setattrfunc) 0,
1646 (cmpfunc) 0,
1647 (reprfunc) 0,
1648
1649 0, /* as number */
1650 0, /* as sequence */
1651 &DictionaryAsMapping, /* as mapping */
1652
1653 (hashfunc) 0,
1654 (ternaryfunc) 0,
1655 (reprfunc) 0,
1656};
1657
1658 static void
1659DictionaryDestructor(PyObject *self)
1660{
1661 DictionaryObject *this = ((DictionaryObject *) (self));
1662
1663 pyll_remove(&this->ref, &lastdict);
1664 dict_unref(this->dict);
1665
1666 Py_DECREF(self);
1667}
1668
1669 static PyObject *
1670DictionaryGetattr(PyObject *self, char *name)
1671{
1672 return Py_FindMethod(DictionaryMethods, self, name);
1673}
1674
1675static void ListDestructor(PyObject *);
1676static PyObject *ListGetattr(PyObject *, char *);
1677
1678static PySequenceMethods ListAsSeq = {
1679 (PyInquiry) ListLength,
1680 (binaryfunc) 0,
1681 (PyIntArgFunc) 0,
1682 (PyIntArgFunc) ListItem,
1683 (PyIntIntArgFunc) ListSlice,
1684 (PyIntObjArgProc) ListAssItem,
1685 (PyIntIntObjArgProc) ListAssSlice,
1686 (objobjproc) 0,
1687#if PY_MAJOR_VERSION >= 2
1688 (binaryfunc) ListConcatInPlace,
1689 0,
1690#endif
1691};
1692
1693static PyTypeObject ListType = {
1694 PyObject_HEAD_INIT(0)
1695 0,
1696 "vimlist",
1697 sizeof(ListObject),
1698 0,
1699
1700 (destructor) ListDestructor,
1701 (printfunc) 0,
1702 (getattrfunc) ListGetattr,
1703 (setattrfunc) 0,
1704 (cmpfunc) 0,
1705 (reprfunc) 0,
1706
1707 0, /* as number */
1708 &ListAsSeq, /* as sequence */
1709 0, /* as mapping */
1710
1711 (hashfunc) 0,
1712 (ternaryfunc) 0,
1713 (reprfunc) 0,
1714};
1715
1716 static void
1717ListDestructor(PyObject *self)
1718{
1719 ListObject *this = ((ListObject *) (self));
1720
1721 pyll_remove(&this->ref, &lastlist);
1722 list_unref(this->list);
1723
1724 Py_DECREF(self);
1725}
1726
1727 static PyObject *
1728ListGetattr(PyObject *self, char *name)
1729{
1730 return Py_FindMethod(ListMethods, self, name);
1731}
1732
1733static void FunctionDestructor(PyObject *);
1734static PyObject *FunctionGetattr(PyObject *, char *);
1735
1736static PyTypeObject FunctionType = {
1737 PyObject_HEAD_INIT(0)
1738 0,
1739 "vimfunction",
1740 sizeof(FunctionObject),
1741 0,
1742
1743 (destructor) FunctionDestructor,
1744 (printfunc) 0,
1745 (getattrfunc) FunctionGetattr,
1746 (setattrfunc) 0,
1747 (cmpfunc) 0,
1748 (reprfunc) 0,
1749
1750 0, /* as number */
1751 0, /* as sequence */
1752 0, /* as mapping */
1753
1754 (hashfunc) 0,
1755 (ternaryfunc) FunctionCall,
1756 (reprfunc) 0,
1757};
1758
1759 static void
1760FunctionDestructor(PyObject *self)
1761{
1762 FunctionObject *this = (FunctionObject *) (self);
1763
1764 func_unref(this->name);
1765 PyMem_Del(this->name);
1766
1767 Py_DECREF(self);
1768}
1769
1770 static PyObject *
1771FunctionGetattr(PyObject *self, char *name)
1772{
1773 FunctionObject *this = (FunctionObject *)(self);
1774
1775 if (strcmp(name, "name") == 0)
1776 return PyString_FromString((char *)(this->name));
1777 else
1778 return Py_FindMethod(FunctionMethods, self, name);
1779}
1780
1781 void
1782do_pyeval (char_u *str, typval_T *rettv)
1783{
1784 DoPythonCommand(NULL, (char *) str, rettv);
1785 switch(rettv->v_type)
1786 {
1787 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1788 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1789 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
1790 }
1791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793/* Don't generate a prototype for the next function, it generates an error on
1794 * newer Python versions. */
1795#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1796
1797 char *
1798Py_GetProgramName(void)
1799{
1800 return "vim";
1801}
1802#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001803
Bram Moolenaardb913952012-06-29 12:54:53 +02001804 void
1805set_ref_in_python (int copyID)
1806{
1807 set_ref_in_py(copyID);
1808}
1809
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001810 static void
1811init_structs(void)
1812{
1813 vim_memset(&OutputType, 0, sizeof(OutputType));
1814 OutputType.tp_name = "message";
1815 OutputType.tp_basicsize = sizeof(OutputObject);
1816 OutputType.tp_getattr = OutputGetattr;
1817 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001818
1819 vim_memset(&RangeType, 0, sizeof(RangeType));
1820 RangeType.tp_name = "range";
1821 RangeType.tp_basicsize = sizeof(RangeObject);
1822 RangeType.tp_dealloc = RangeDestructor;
1823 RangeType.tp_getattr = RangeGetattr;
1824 RangeType.tp_repr = RangeRepr;
1825 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001826}