blob: 3c72b260c27df7a72eb11e7b8626eefa18964ca7 [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
47#include <Python.h>
48#if defined(MACOS) && !defined(MACOS_X_UNIX)
49# include "macglue.h"
50# include <CodeFragments.h>
51#endif
52#undef main /* Defined in python.h - aargh */
53#undef HAVE_FCNTL_H /* Clash with os_win32.h */
54
Bram Moolenaare8cdcef2012-09-12 20:21:43 +020055#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
56# define PY_SSIZE_T_CLEAN
57#endif
58
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059static void init_structs(void);
60
Bram Moolenaardb913952012-06-29 12:54:53 +020061#define PyBytes_FromString PyString_FromString
62
Bram Moolenaar19e60942011-06-19 00:27:51 +020063/* No-op conversion functions, use with care! */
64#define PyString_AsBytes(obj) (obj)
65#define PyString_FreeBytes(obj)
66
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#if !defined(FEAT_PYTHON) && defined(PROTO)
68/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000069# define PyObject Py_ssize_t
70# define PyThreadState Py_ssize_t
71# define PyTypeObject Py_ssize_t
72struct PyMethodDef { Py_ssize_t a; };
73# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#endif
75
Bram Moolenaar2afa3232012-06-29 16:28:28 +020076#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
77# define PY_USE_CAPSULE
78#endif
79
Bram Moolenaar2c45e942008-06-04 11:35:26 +000080#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
81# define PyInt Py_ssize_t
82# define PyInquiry lenfunc
83# define PyIntArgFunc ssizeargfunc
84# define PyIntIntArgFunc ssizessizeargfunc
85# define PyIntObjArgProc ssizeobjargproc
86# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000087# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000088#else
89# define PyInt int
90# define PyInquiry inquiry
91# define PyIntArgFunc intargfunc
92# define PyIntIntArgFunc intintargfunc
93# define PyIntObjArgProc intobjargproc
94# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000095# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000096#endif
97
Bram Moolenaar071d4272004-06-13 20:20:40 +000098/* Parser flags */
99#define single_input 256
100#define file_input 257
101#define eval_input 258
102
103#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
104 /* Python 2.3: can invoke ":python" recursively. */
105# define PY_CAN_RECURSE
106#endif
107
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200108# if defined(DYNAMIC_PYTHON) || defined(PROTO)
109# ifndef DYNAMIC_PYTHON
110# define HINSTANCE long_u /* for generating prototypes */
111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200113# ifndef WIN3264
114# include <dlfcn.h>
115# define FARPROC void*
116# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100117# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# define load_dll(n) dlopen((n), RTLD_LAZY)
119# else
120# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
121# endif
122# define close_dll dlclose
123# define symbol_from_dll dlsym
124# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200125# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200126# define close_dll FreeLibrary
127# define symbol_from_dll GetProcAddress
128# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200129
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000130/* This makes if_python.c compile without warnings against Python 2.5
131 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200132# undef PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200133# undef PyRun_String
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200134# undef PyArg_Parse
135# undef PyArg_ParseTuple
136# undef Py_BuildValue
137# undef Py_InitModule4
138# undef Py_InitModule4_64
Bram Moolenaardb913952012-06-29 12:54:53 +0200139# undef PyObject_CallMethod
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141/*
142 * Wrapper defines
143 */
144# define PyArg_Parse dll_PyArg_Parse
145# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200146# define PyMem_Free dll_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200147# define PyMem_Malloc dll_PyMem_Malloc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define PyDict_SetItemString dll_PyDict_SetItemString
149# define PyErr_BadArgument dll_PyErr_BadArgument
150# define PyErr_Clear dll_PyErr_Clear
151# define PyErr_NoMemory dll_PyErr_NoMemory
152# define PyErr_Occurred dll_PyErr_Occurred
153# define PyErr_SetNone dll_PyErr_SetNone
154# define PyErr_SetString dll_PyErr_SetString
155# define PyEval_InitThreads dll_PyEval_InitThreads
156# define PyEval_RestoreThread dll_PyEval_RestoreThread
157# define PyEval_SaveThread dll_PyEval_SaveThread
158# ifdef PY_CAN_RECURSE
159# define PyGILState_Ensure dll_PyGILState_Ensure
160# define PyGILState_Release dll_PyGILState_Release
161# endif
162# define PyInt_AsLong dll_PyInt_AsLong
163# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200164# define PyLong_AsLong dll_PyLong_AsLong
165# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200167# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000169# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define PyList_New dll_PyList_New
171# define PyList_SetItem dll_PyList_SetItem
172# define PyList_Size dll_PyList_Size
173# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200174# define PySequence_Check dll_PySequence_Check
175# define PySequence_Size dll_PySequence_Size
176# define PySequence_GetItem dll_PySequence_GetItem
177# define PyTuple_Size dll_PyTuple_Size
178# define PyTuple_GetItem dll_PyTuple_GetItem
179# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000181# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200183# define PyDict_Next dll_PyDict_Next
184# ifdef PyMapping_Items
185# define PY_NO_MAPPING_ITEMS
186# else
187# define PyMapping_Items dll_PyMapping_Items
188# endif
189# define PyObject_CallMethod dll_PyObject_CallMethod
190# define PyMapping_Check dll_PyMapping_Check
191# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define PyModule_GetDict dll_PyModule_GetDict
193# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200194# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200196# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define PyString_FromString dll_PyString_FromString
198# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
199# define PyString_Size dll_PyString_Size
200# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200201# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200202# undef PyUnicode_AsEncodedString
203# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200204# define PyFloat_AsDouble dll_PyFloat_AsDouble
205# define PyFloat_FromDouble dll_PyFloat_FromDouble
206# define PyFloat_Type (*dll_PyFloat_Type)
207# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208# define PySys_SetObject dll_PySys_SetObject
209# define PySys_SetArgv dll_PySys_SetArgv
210# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100211# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212# define Py_BuildValue dll_Py_BuildValue
213# define Py_FindMethod dll_Py_FindMethod
214# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100215# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000217# define Py_Finalize dll_Py_Finalize
218# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200220# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
221# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# define _Py_NoneStruct (*dll__Py_NoneStruct)
224# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200225# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
227# define PyType_IsSubtype dll_PyType_IsSubtype
228# endif
229# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
230# define PyObject_Malloc dll_PyObject_Malloc
231# define PyObject_Free dll_PyObject_Free
232# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200233# ifdef PY_USE_CAPSULE
234# define PyCapsule_New dll_PyCapsule_New
235# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
236# else
237# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
238# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241/*
242 * Pointers for dynamic link
243 */
244static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
245static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200246static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200247static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
249static int(*dll_PyErr_BadArgument)(void);
250static void(*dll_PyErr_Clear)(void);
251static PyObject*(*dll_PyErr_NoMemory)(void);
252static PyObject*(*dll_PyErr_Occurred)(void);
253static void(*dll_PyErr_SetNone)(PyObject *);
254static void(*dll_PyErr_SetString)(PyObject *, const char *);
255static void(*dll_PyEval_InitThreads)(void);
256static void(*dll_PyEval_RestoreThread)(PyThreadState *);
257static PyThreadState*(*dll_PyEval_SaveThread)(void);
258# ifdef PY_CAN_RECURSE
259static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
260static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262static long(*dll_PyInt_AsLong)(PyObject *);
263static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200264static long(*dll_PyLong_AsLong)(PyObject *);
265static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200267static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000268static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000269static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000270static PyObject*(*dll_PyList_New)(PyInt size);
271static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
272static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200274static int (*dll_PySequence_Check)(PyObject *);
275static PyInt(*dll_PySequence_Size)(PyObject *);
276static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
277static PyInt(*dll_PyTuple_Size)(PyObject *);
278static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
279static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000281static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200283static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
284# ifndef PY_NO_MAPPING_ITEMS
285static PyObject* (*dll_PyMapping_Items)(PyObject *);
286# endif
287static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
288static int (*dll_PyMapping_Check)(PyObject *);
289static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static PyObject*(*dll_PyModule_GetDict)(PyObject *);
291static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200292static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200294static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000296static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
297static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200299static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200300static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200301static double(*dll_PyFloat_AsDouble)(PyObject *);
302static PyObject*(*dll_PyFloat_FromDouble)(double);
303static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static int(*dll_PySys_SetObject)(char *, PyObject *);
305static int(*dll_PySys_SetArgv)(int, char **);
306static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100307static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static PyObject*(*dll_Py_BuildValue)(char *, ...);
309static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
310static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200311static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100312static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000314static void(*dll_Py_Finalize)(void);
315static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
317static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200318static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200319# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200320static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static PyObject* dll__Py_NoneStruct;
323# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
324static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
325# endif
326# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
327static void* (*dll_PyObject_Malloc)(size_t);
328static void (*dll_PyObject_Free)(void*);
329# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200330# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200331static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
332static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200333# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200334static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
335static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337
338static HINSTANCE hinstPython = 0; /* Instance of python.dll */
339
340/* Imported exception objects */
341static PyObject *imp_PyExc_AttributeError;
342static PyObject *imp_PyExc_IndexError;
343static PyObject *imp_PyExc_KeyboardInterrupt;
344static PyObject *imp_PyExc_TypeError;
345static PyObject *imp_PyExc_ValueError;
346
347# define PyExc_AttributeError imp_PyExc_AttributeError
348# define PyExc_IndexError imp_PyExc_IndexError
349# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
350# define PyExc_TypeError imp_PyExc_TypeError
351# define PyExc_ValueError imp_PyExc_ValueError
352
353/*
354 * Table of name to function pointer of python.
355 */
356# define PYTHON_PROC FARPROC
357static struct
358{
359 char *name;
360 PYTHON_PROC *ptr;
361} python_funcname_table[] =
362{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200363#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
365 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200366 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
367#else
368 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
369 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
370 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
371#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200372 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200373 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
375 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
376 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
377 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
378 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
379 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
380 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
381 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
382 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
383 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
384# ifdef PY_CAN_RECURSE
385 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
386 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
387# endif
388 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
389 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200390 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
391 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200393 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000395 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
397 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
398 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
399 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200400 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
401 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
402 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
403 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
404 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
405 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
407 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200408 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000409 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200410# ifndef PY_NO_MAPPING_ITEMS
411 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
412# endif
413 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
414 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
415 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
417 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200418 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200420 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
422 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
423 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
424 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200425 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200426 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
427 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
428 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
429 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
431 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
432 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100433 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200435# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
436 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000437 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
438# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000440# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100441 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000443 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
444 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
446 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200447 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200448# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
452# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
453 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
454# endif
455# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
456 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
457 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
458# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200459# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200460 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
461 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200462# else
463 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
464 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {"", NULL},
467};
468
469/*
470 * Free python.dll
471 */
472 static void
473end_dynamic_python(void)
474{
475 if (hinstPython)
476 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200477 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 hinstPython = 0;
479 }
480}
481
482/*
483 * Load library and get all pointers.
484 * Parameter 'libname' provides name of DLL.
485 * Return OK or FAIL.
486 */
487 static int
488python_runtime_link_init(char *libname, int verbose)
489{
490 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200491 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100493#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200494 /* Can't have Python and Python3 loaded at the same time.
495 * It cause a crash, because RTLD_GLOBAL is needed for
496 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200497 if (python3_loaded())
498 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200499 if (verbose)
500 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200501 return FAIL;
502 }
503#endif
504
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 if (hinstPython)
506 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200507 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (!hinstPython)
509 {
510 if (verbose)
511 EMSG2(_(e_loadlib), libname);
512 return FAIL;
513 }
514
515 for (i = 0; python_funcname_table[i].ptr; ++i)
516 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200517 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 python_funcname_table[i].name)) == NULL)
519 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 hinstPython = 0;
522 if (verbose)
523 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
524 return FAIL;
525 }
526 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200527
528 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
529 * will be present in the library. */
530 ucs_as_encoded_string = symbol_from_dll(hinstPython,
531 "PyUnicodeUCS2_AsEncodedString");
532 if (ucs_as_encoded_string == NULL)
533 ucs_as_encoded_string = symbol_from_dll(hinstPython,
534 "PyUnicodeUCS4_AsEncodedString");
535 if (ucs_as_encoded_string != NULL)
536 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
537 else
538 {
539 close_dll(hinstPython);
540 hinstPython = 0;
541 if (verbose)
542 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
543 return FAIL;
544 }
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 return OK;
547}
548
549/*
550 * If python is enabled (there is installed python on Windows system) return
551 * TRUE, else FALSE.
552 */
553 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000554python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
557}
558
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200559/*
560 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 * DLL, as this can cause errors (importing data symbols is not reliable).
562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200564get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
566 PyObject *exmod = PyImport_ImportModule("exceptions");
567 PyObject *exdict = PyModule_GetDict(exmod);
568 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
569 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
570 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
571 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
572 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
573 Py_XINCREF(imp_PyExc_AttributeError);
574 Py_XINCREF(imp_PyExc_IndexError);
575 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
576 Py_XINCREF(imp_PyExc_TypeError);
577 Py_XINCREF(imp_PyExc_ValueError);
578 Py_XDECREF(exmod);
579}
580#endif /* DYNAMIC_PYTHON */
581
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200582static PyObject *BufferNew (buf_T *);
583static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200584static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200585static PyObject *LineToString(const char *);
586
587static PyTypeObject RangeType;
588
Bram Moolenaardb913952012-06-29 12:54:53 +0200589static int initialised = 0;
590#define PYINITIALISED initialised
591
Bram Moolenaardb913952012-06-29 12:54:53 +0200592#define DICTKEY_GET(err) \
593 if (!PyString_Check(keyObject)) \
594 { \
595 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
596 return err; \
597 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200598 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
599 return err;
600
Bram Moolenaardb913952012-06-29 12:54:53 +0200601#define DICTKEY_UNREF
602#define DICTKEY_DECL
603
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200604/*
605 * Include the code shared with if_python3.c
606 */
607#include "if_py_both.h"
608
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610/******************************************************
611 * Internal function prototypes.
612 */
613
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000614static PyInt RangeStart;
615static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaardb913952012-06-29 12:54:53 +0200617static PyObject *globals;
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619static void PythonIO_Flush(void);
620static int PythonIO_Init(void);
621static int PythonMod_Init(void);
622
623/* Utility functions for the vim/python interface
624 * ----------------------------------------------
625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000627static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630/******************************************************
631 * 1. Python interpreter main program.
632 */
633
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
635typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000638#ifdef PY_CAN_RECURSE
639static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
640#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000641static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644/*
645 * Suspend a thread of the Python interpreter, other threads are allowed to
646 * run.
647 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000648 static void
649Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#ifdef PY_CAN_RECURSE
652 PyGILState_Release(pygilstate);
653#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656}
657
658/*
659 * Restore a thread of the Python interpreter, waits for other threads to
660 * block.
661 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000662 static void
663Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000665#ifdef PY_CAN_RECURSE
666 pygilstate = PyGILState_Ensure();
667#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 PyEval_RestoreThread(saved_python_thread);
669 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000671}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 void
674python_end()
675{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000676 static int recurse = 0;
677
678 /* If a crash occurs while doing this, don't try again. */
679 if (recurse != 0)
680 return;
681
682 ++recurse;
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000685 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000687 Python_RestoreThread(); /* enter python */
688 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000691#else
692 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000694 Python_RestoreThread(); /* enter python */
695 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000698
699 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700}
701
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200702#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
703 int
704python_loaded()
705{
706 return (hinstPython != 0);
707}
708#endif
709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 static int
711Python_Init(void)
712{
713 if (!initialised)
714 {
715#ifdef DYNAMIC_PYTHON
716 if (!python_enabled(TRUE))
717 {
718 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
719 goto fail;
720 }
721#endif
722
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100723#ifdef PYTHON_HOME
724 Py_SetPythonHome(PYTHON_HOME);
725#endif
726
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200727 init_structs();
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#if !defined(MACOS) || defined(MACOS_X_UNIX)
730 Py_Initialize();
731#else
732 PyMac_Initialize();
733#endif
734 /* initialise threads */
735 PyEval_InitThreads();
736
737#ifdef DYNAMIC_PYTHON
738 get_exceptions();
739#endif
740
741 if (PythonIO_Init())
742 goto fail;
743
744 if (PythonMod_Init())
745 goto fail;
746
Bram Moolenaardb913952012-06-29 12:54:53 +0200747 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
748
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000749 /* Remove the element from sys.path that was added because of our
750 * argv[0] value in PythonMod_Init(). Previously we used an empty
751 * string, but dependinding on the OS we then get an empty entry or
752 * the current directory in sys.path. */
753 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
754
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000755 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 initialised = 1;
759 }
760
761 return 0;
762
763fail:
764 /* We call PythonIO_Flush() here to print any Python errors.
765 * This is OK, as it is possible to call this function even
766 * if PythonIO_Init() has not completed successfully (it will
767 * not do anything in this case).
768 */
769 PythonIO_Flush();
770 return -1;
771}
772
773/*
774 * External interface
775 */
776 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200777DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000779#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 static int recursive = 0;
781#endif
782#if defined(MACOS) && !defined(MACOS_X_UNIX)
783 GrafPtr oldPort;
784#endif
785#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
786 char *saved_locale;
787#endif
788
789#ifndef PY_CAN_RECURSE
790 if (recursive)
791 {
792 EMSG(_("E659: Cannot invoke Python recursively"));
793 return;
794 }
795 ++recursive;
796#endif
797
798#if defined(MACOS) && !defined(MACOS_X_UNIX)
799 GetPort(&oldPort);
800 /* Check if the Python library is available */
801 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
802 goto theend;
803#endif
804 if (Python_Init())
805 goto theend;
806
Bram Moolenaardb913952012-06-29 12:54:53 +0200807 if (rettv == NULL)
808 {
809 RangeStart = eap->line1;
810 RangeEnd = eap->line2;
811 }
812 else
813 {
814 RangeStart = (PyInt) curwin->w_cursor.lnum;
815 RangeEnd = RangeStart;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 Python_Release_Vim(); /* leave vim */
818
819#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
820 /* Python only works properly when the LC_NUMERIC locale is "C". */
821 saved_locale = setlocale(LC_NUMERIC, NULL);
822 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
823 saved_locale = NULL;
824 else
825 {
826 /* Need to make a copy, value may change when setting new locale. */
827 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
828 (void)setlocale(LC_NUMERIC, "C");
829 }
830#endif
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaardb913952012-06-29 12:54:53 +0200834 if (rettv == NULL)
835 PyRun_SimpleString((char *)(cmd));
836 else
837 {
838 PyObject *r;
839
840 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
841 if (r == NULL)
842 EMSG(_("E858: Eval did not return a valid python object"));
843 else
844 {
845 if (ConvertFromPyObject(r, rettv) == -1)
846 EMSG(_("E859: Failed to convert returned python object to vim value"));
847 Py_DECREF(r);
848 }
849 PyErr_Clear();
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
855 if (saved_locale != NULL)
856 {
857 (void)setlocale(LC_NUMERIC, saved_locale);
858 vim_free(saved_locale);
859 }
860#endif
861
862 Python_Lock_Vim(); /* enter vim */
863 PythonIO_Flush();
864#if defined(MACOS) && !defined(MACOS_X_UNIX)
865 SetPort(oldPort);
866#endif
867
868theend:
869#ifndef PY_CAN_RECURSE
870 --recursive;
871#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200872 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873}
874
875/*
876 * ":python"
877 */
878 void
879ex_python(exarg_T *eap)
880{
881 char_u *script;
882
883 script = script_get(eap, eap->arg);
884 if (!eap->skip)
885 {
886 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200887 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200889 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891 vim_free(script);
892}
893
894#define BUFFER_SIZE 1024
895
896/*
897 * ":pyfile"
898 */
899 void
900ex_pyfile(exarg_T *eap)
901{
902 static char buffer[BUFFER_SIZE];
903 const char *file = (char *)eap->arg;
904 char *p;
905
906 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
907 * stdio file pointer, but Vim and the Python DLL are compiled with
908 * different options under Windows, meaning that stdio pointers aren't
909 * compatible between the two. Yuk.
910 *
911 * Put the string "execfile('file')" into buffer. But, we need to
912 * escape any backslashes or single quotes in the file name, so that
913 * Python won't mangle the file name.
914 */
915 strcpy(buffer, "execfile('");
916 p = buffer + 10; /* size of "execfile('" */
917
918 while (*file && p < buffer + (BUFFER_SIZE - 3))
919 {
920 if (*file == '\\' || *file == '\'')
921 *p++ = '\\';
922 *p++ = *file++;
923 }
924
925 /* If we didn't finish the file name, we hit a buffer overflow */
926 if (*file != '\0')
927 return;
928
929 /* Put in the terminating "')" and a null */
930 *p++ = '\'';
931 *p++ = ')';
932 *p++ = '\0';
933
934 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200935 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936}
937
938/******************************************************
939 * 2. Python output stream: writes output via [e]msg().
940 */
941
942/* Implementation functions
943 */
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 static PyObject *
946OutputGetattr(PyObject *self, char *name)
947{
948 if (strcmp(name, "softspace") == 0)
949 return PyInt_FromLong(((OutputObject *)(self))->softspace);
950
951 return Py_FindMethod(OutputMethods, self, name);
952}
953
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/***************/
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 static int
957PythonIO_Init(void)
958{
959 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100960 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200962 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/******************************************************
966 * 3. Implementation of the Vim module for Python
967 */
968
Bram Moolenaardb913952012-06-29 12:54:53 +0200969static PyObject *ConvertToPyObject(typval_T *);
970static int ConvertFromPyObject(PyObject *, typval_T *);
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972/* Window type - Implementation functions
973 * --------------------------------------
974 */
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978static void WindowDestructor(PyObject *);
979static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
981/* Buffer type - Implementation functions
982 * --------------------------------------
983 */
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987static void BufferDestructor(PyObject *);
988static PyObject *BufferGetattr(PyObject *, char *);
989static PyObject *BufferRepr(PyObject *);
990
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000991static PyInt BufferLength(PyObject *);
992static PyObject *BufferItem(PyObject *, PyInt);
993static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
994static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
995static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/* Line range type - Implementation functions
998 * --------------------------------------
999 */
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1002
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001003static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1004static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006/* Current objects type - Implementation functions
1007 * -----------------------------------------------
1008 */
1009
1010static PyObject *CurrentGetattr(PyObject *, char *);
1011static int CurrentSetattr(PyObject *, char *, PyObject *);
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001014 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001015 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1016 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001017 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1018 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1019 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
1020 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021};
1022
1023static PyTypeObject BufferType = {
1024 PyObject_HEAD_INIT(0)
1025 0,
1026 "buffer",
1027 sizeof(BufferObject),
1028 0,
1029
1030 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
1031 (printfunc) 0, /* tp_print, print x */
1032 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
1033 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1034 (cmpfunc) 0, /* tp_compare, x>y */
1035 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
1036
1037 0, /* as number */
1038 &BufferAsSeq, /* as sequence */
1039 0, /* as mapping */
1040
1041 (hashfunc) 0, /* tp_hash, dict(x) */
1042 (ternaryfunc) 0, /* tp_call, x() */
1043 (reprfunc) 0, /* tp_str, str(x) */
1044};
1045
1046/* Buffer object - Implementation
1047 */
1048
1049 static PyObject *
1050BufferNew(buf_T *buf)
1051{
1052 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001053 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 * then we can get at it in buf_freeall() in vim. We then
1055 * need to create only ONE Python object per buffer - if
1056 * we try to create a second, just INCREF the existing one
1057 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001058 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 * Question: what to do on a buf_freeall(). We'll probably
1060 * have to either delete the Python object (DECREF it to
1061 * zero - a bad idea, as it leaves dangling refs!) or
1062 * set the buf_T * value to an invalid value (-1?), which
1063 * means we need checks in all access functions... Bah.
1064 */
1065
1066 BufferObject *self;
1067
Bram Moolenaare344bea2005-09-01 20:46:49 +00001068 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001070 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 Py_INCREF(self);
1072 }
1073 else
1074 {
1075 self = PyObject_NEW(BufferObject, &BufferType);
1076 if (self == NULL)
1077 return NULL;
1078 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001079 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081
1082 return (PyObject *)(self);
1083}
1084
1085 static void
1086BufferDestructor(PyObject *self)
1087{
1088 BufferObject *this = (BufferObject *)(self);
1089
1090 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001091 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
Bram Moolenaar658ada62006-10-03 13:02:36 +00001093 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
1096 static PyObject *
1097BufferGetattr(PyObject *self, char *name)
1098{
1099 BufferObject *this = (BufferObject *)(self);
1100
1101 if (CheckBuffer(this))
1102 return NULL;
1103
1104 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001105 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001107 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 else if (strcmp(name,"__members__") == 0)
1109 return Py_BuildValue("[ss]", "name", "number");
1110 else
1111 return Py_FindMethod(BufferMethods, self, name);
1112}
1113
1114 static PyObject *
1115BufferRepr(PyObject *self)
1116{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001117 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 BufferObject *this = (BufferObject *)(self);
1119
1120 if (this->buf == INVALID_BUFFER_VALUE)
1121 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001122 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return PyString_FromString(repr);
1124 }
1125 else
1126 {
1127 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001128 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 if (name == NULL)
1131 name = "";
1132 len = strlen(name);
1133
1134 if (len > 35)
1135 name = name + (35 - len);
1136
Bram Moolenaar555b2802005-05-19 21:08:39 +00001137 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
1139 return PyString_FromString(repr);
1140 }
1141}
1142
1143/******************/
1144
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001145 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146BufferLength(PyObject *self)
1147{
1148 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1149 if (CheckBuffer((BufferObject *)(self)))
1150 return -1; /* ??? */
1151
1152 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1153}
1154
1155 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001156BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 return RBItem((BufferObject *)(self), n, 1,
1159 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1160}
1161
1162 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001163BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165 return RBSlice((BufferObject *)(self), lo, hi, 1,
1166 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1167}
1168
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001169 static PyInt
1170BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001172 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001173 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 NULL);
1175}
1176
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001177 static PyInt
1178BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001180 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001181 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 NULL);
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001186 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001188 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1189 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1190 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1191 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1192 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193};
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195/* Line range object - Implementation
1196 */
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 static void
1199RangeDestructor(PyObject *self)
1200{
1201 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001202 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203}
1204
1205 static PyObject *
1206RangeGetattr(PyObject *self, char *name)
1207{
1208 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001209 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001211 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 else
1213 return Py_FindMethod(RangeMethods, self, name);
1214}
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216/****************/
1217
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001218 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001219RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001221 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 ((RangeObject *)(self))->start,
1223 ((RangeObject *)(self))->end,
1224 &((RangeObject *)(self))->end);
1225}
1226
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001227 static PyInt
1228RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001230 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 ((RangeObject *)(self))->start,
1232 ((RangeObject *)(self))->end,
1233 &((RangeObject *)(self))->end);
1234}
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236/* Buffer list object - Definitions
1237 */
1238
1239typedef struct
1240{
1241 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001242} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001245 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001247 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1248 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1249 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1250 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1251 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252};
1253
1254static PyTypeObject BufListType = {
1255 PyObject_HEAD_INIT(0)
1256 0,
1257 "buffer list",
1258 sizeof(BufListObject),
1259 0,
1260
1261 (destructor) 0, /* tp_dealloc, refcount==0 */
1262 (printfunc) 0, /* tp_print, print x */
1263 (getattrfunc) 0, /* tp_getattr, x.attr */
1264 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1265 (cmpfunc) 0, /* tp_compare, x>y */
1266 (reprfunc) 0, /* tp_repr, `x`, print x */
1267
1268 0, /* as number */
1269 &BufListAsSeq, /* as sequence */
1270 0, /* as mapping */
1271
1272 (hashfunc) 0, /* tp_hash, dict(x) */
1273 (ternaryfunc) 0, /* tp_call, x() */
1274 (reprfunc) 0, /* tp_str, str(x) */
1275};
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277/* Window object - Definitions
1278 */
1279
1280static struct PyMethodDef WindowMethods[] = {
1281 /* name, function, calling, documentation */
1282 { NULL, NULL, 0, NULL }
1283};
1284
1285static PyTypeObject WindowType = {
1286 PyObject_HEAD_INIT(0)
1287 0,
1288 "window",
1289 sizeof(WindowObject),
1290 0,
1291
1292 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1293 (printfunc) 0, /* tp_print, print x */
1294 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1295 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1296 (cmpfunc) 0, /* tp_compare, x>y */
1297 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1298
1299 0, /* as number */
1300 0, /* as sequence */
1301 0, /* as mapping */
1302
1303 (hashfunc) 0, /* tp_hash, dict(x) */
1304 (ternaryfunc) 0, /* tp_call, x() */
1305 (reprfunc) 0, /* tp_str, str(x) */
1306};
1307
1308/* Window object - Implementation
1309 */
1310
1311 static PyObject *
1312WindowNew(win_T *win)
1313{
1314 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001315 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 * then we can get at it in win_free() in vim. We then
1317 * need to create only ONE Python object per window - if
1318 * we try to create a second, just INCREF the existing one
1319 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001320 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 * On a win_free() we set the Python object's win_T* field
1322 * to an invalid value. We trap all uses of a window
1323 * object, and reject them if the win_T* field is invalid.
1324 */
1325
1326 WindowObject *self;
1327
Bram Moolenaare344bea2005-09-01 20:46:49 +00001328 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001330 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 Py_INCREF(self);
1332 }
1333 else
1334 {
1335 self = PyObject_NEW(WindowObject, &WindowType);
1336 if (self == NULL)
1337 return NULL;
1338 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001339 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 }
1341
1342 return (PyObject *)(self);
1343}
1344
1345 static void
1346WindowDestructor(PyObject *self)
1347{
1348 WindowObject *this = (WindowObject *)(self);
1349
1350 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001351 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar658ada62006-10-03 13:02:36 +00001353 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354}
1355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 static PyObject *
1357WindowGetattr(PyObject *self, char *name)
1358{
1359 WindowObject *this = (WindowObject *)(self);
1360
1361 if (CheckWindow(this))
1362 return NULL;
1363
1364 if (strcmp(name, "buffer") == 0)
1365 return (PyObject *)BufferNew(this->win->w_buffer);
1366 else if (strcmp(name, "cursor") == 0)
1367 {
1368 pos_T *pos = &this->win->w_cursor;
1369
1370 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1371 }
1372 else if (strcmp(name, "height") == 0)
1373 return Py_BuildValue("l", (long)(this->win->w_height));
1374#ifdef FEAT_VERTSPLIT
1375 else if (strcmp(name, "width") == 0)
1376 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1377#endif
1378 else if (strcmp(name,"__members__") == 0)
1379 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1380 else
1381 return Py_FindMethod(WindowMethods, self, name);
1382}
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384/* Window list object - Definitions
1385 */
1386
1387typedef struct
1388{
1389 PyObject_HEAD
1390}
1391WinListObject;
1392
1393static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001394 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001396 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1397 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1398 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1399 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1400 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401};
1402
1403static PyTypeObject WinListType = {
1404 PyObject_HEAD_INIT(0)
1405 0,
1406 "window list",
1407 sizeof(WinListObject),
1408 0,
1409
1410 (destructor) 0, /* tp_dealloc, refcount==0 */
1411 (printfunc) 0, /* tp_print, print x */
1412 (getattrfunc) 0, /* tp_getattr, x.attr */
1413 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1414 (cmpfunc) 0, /* tp_compare, x>y */
1415 (reprfunc) 0, /* tp_repr, `x`, print x */
1416
1417 0, /* as number */
1418 &WinListAsSeq, /* as sequence */
1419 0, /* as mapping */
1420
1421 (hashfunc) 0, /* tp_hash, dict(x) */
1422 (ternaryfunc) 0, /* tp_call, x() */
1423 (reprfunc) 0, /* tp_str, str(x) */
1424};
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426/* Current items object - Definitions
1427 */
1428
1429typedef struct
1430{
1431 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001432} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
1434static PyTypeObject CurrentType = {
1435 PyObject_HEAD_INIT(0)
1436 0,
1437 "current data",
1438 sizeof(CurrentObject),
1439 0,
1440
1441 (destructor) 0, /* tp_dealloc, refcount==0 */
1442 (printfunc) 0, /* tp_print, print x */
1443 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1444 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1445 (cmpfunc) 0, /* tp_compare, x>y */
1446 (reprfunc) 0, /* tp_repr, `x`, print x */
1447
1448 0, /* as number */
1449 0, /* as sequence */
1450 0, /* as mapping */
1451
1452 (hashfunc) 0, /* tp_hash, dict(x) */
1453 (ternaryfunc) 0, /* tp_call, x() */
1454 (reprfunc) 0, /* tp_str, str(x) */
1455};
1456
1457/* Current items object - Implementation
1458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001460CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 if (strcmp(name, "buffer") == 0)
1463 return (PyObject *)BufferNew(curbuf);
1464 else if (strcmp(name, "window") == 0)
1465 return (PyObject *)WindowNew(curwin);
1466 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001467 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 else if (strcmp(name, "range") == 0)
1469 return RangeNew(curbuf, RangeStart, RangeEnd);
1470 else if (strcmp(name,"__members__") == 0)
1471 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1472 else
1473 {
1474 PyErr_SetString(PyExc_AttributeError, name);
1475 return NULL;
1476 }
1477}
1478
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001480CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 if (strcmp(name, "line") == 0)
1483 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001484 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 return -1;
1486
1487 return 0;
1488 }
1489 else
1490 {
1491 PyErr_SetString(PyExc_AttributeError, name);
1492 return -1;
1493 }
1494}
1495
1496/* External interface
1497 */
1498
1499 void
1500python_buffer_free(buf_T *buf)
1501{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001502 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001504 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001506 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508}
1509
1510#if defined(FEAT_WINDOWS) || defined(PROTO)
1511 void
1512python_window_free(win_T *win)
1513{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001514 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001516 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001518 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520}
1521#endif
1522
1523static BufListObject TheBufferList =
1524{
1525 PyObject_HEAD_INIT(&BufListType)
1526};
1527
1528static WinListObject TheWindowList =
1529{
1530 PyObject_HEAD_INIT(&WinListType)
1531};
1532
1533static CurrentObject TheCurrent =
1534{
1535 PyObject_HEAD_INIT(&CurrentType)
1536};
1537
1538 static int
1539PythonMod_Init(void)
1540{
1541 PyObject *mod;
1542 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001543 /* The special value is removed from sys.path in Python_Init(). */
1544 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
1546 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001547 PyType_Ready(&BufferType);
1548 PyType_Ready(&RangeType);
1549 PyType_Ready(&WindowType);
1550 PyType_Ready(&BufListType);
1551 PyType_Ready(&WinListType);
1552 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 /* Set sys.argv[] to avoid a crash in warn(). */
1555 PySys_SetArgv(1, argv);
1556
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001557 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 dict = PyModule_GetDict(mod);
1559
1560 VimError = Py_BuildValue("s", "vim.error");
1561
1562 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001563 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1564 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1565 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 if (PyErr_Occurred())
1568 return -1;
1569
1570 return 0;
1571}
1572
1573/*************************************************************************
1574 * 4. Utility functions for handling the interface between Vim and Python.
1575 */
1576
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577/* Convert a Vim line into a Python string.
1578 * All internal newlines are replaced by null characters.
1579 *
1580 * On errors, the Python exception data is set, and NULL is returned.
1581 */
1582 static PyObject *
1583LineToString(const char *str)
1584{
1585 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001586 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 char *p;
1588
1589 /* Allocate an Python string object, with uninitialised contents. We
1590 * must do it this way, so that we can modify the string in place
1591 * later. See the Python source, Objects/stringobject.c for details.
1592 */
1593 result = PyString_FromStringAndSize(NULL, len);
1594 if (result == NULL)
1595 return NULL;
1596
1597 p = PyString_AsString(result);
1598
1599 while (*str)
1600 {
1601 if (*str == '\n')
1602 *p = '\0';
1603 else
1604 *p = *str;
1605
1606 ++p;
1607 ++str;
1608 }
1609
1610 return result;
1611}
1612
Bram Moolenaardb913952012-06-29 12:54:53 +02001613static void DictionaryDestructor(PyObject *);
1614static PyObject *DictionaryGetattr(PyObject *, char*);
1615
1616static PyMappingMethods DictionaryAsMapping = {
1617 (PyInquiry) DictionaryLength,
1618 (binaryfunc) DictionaryItem,
1619 (objobjargproc) DictionaryAssItem,
1620};
1621
1622static PyTypeObject DictionaryType = {
1623 PyObject_HEAD_INIT(0)
1624 0,
1625 "vimdictionary",
1626 sizeof(DictionaryObject),
1627 0,
1628
1629 (destructor) DictionaryDestructor,
1630 (printfunc) 0,
1631 (getattrfunc) DictionaryGetattr,
1632 (setattrfunc) 0,
1633 (cmpfunc) 0,
1634 (reprfunc) 0,
1635
1636 0, /* as number */
1637 0, /* as sequence */
1638 &DictionaryAsMapping, /* as mapping */
1639
1640 (hashfunc) 0,
1641 (ternaryfunc) 0,
1642 (reprfunc) 0,
1643};
1644
1645 static void
1646DictionaryDestructor(PyObject *self)
1647{
1648 DictionaryObject *this = ((DictionaryObject *) (self));
1649
1650 pyll_remove(&this->ref, &lastdict);
1651 dict_unref(this->dict);
1652
1653 Py_DECREF(self);
1654}
1655
1656 static PyObject *
1657DictionaryGetattr(PyObject *self, char *name)
1658{
1659 return Py_FindMethod(DictionaryMethods, self, name);
1660}
1661
1662static void ListDestructor(PyObject *);
1663static PyObject *ListGetattr(PyObject *, char *);
1664
1665static PySequenceMethods ListAsSeq = {
1666 (PyInquiry) ListLength,
1667 (binaryfunc) 0,
1668 (PyIntArgFunc) 0,
1669 (PyIntArgFunc) ListItem,
1670 (PyIntIntArgFunc) ListSlice,
1671 (PyIntObjArgProc) ListAssItem,
1672 (PyIntIntObjArgProc) ListAssSlice,
1673 (objobjproc) 0,
1674#if PY_MAJOR_VERSION >= 2
1675 (binaryfunc) ListConcatInPlace,
1676 0,
1677#endif
1678};
1679
1680static PyTypeObject ListType = {
1681 PyObject_HEAD_INIT(0)
1682 0,
1683 "vimlist",
1684 sizeof(ListObject),
1685 0,
1686
1687 (destructor) ListDestructor,
1688 (printfunc) 0,
1689 (getattrfunc) ListGetattr,
1690 (setattrfunc) 0,
1691 (cmpfunc) 0,
1692 (reprfunc) 0,
1693
1694 0, /* as number */
1695 &ListAsSeq, /* as sequence */
1696 0, /* as mapping */
1697
1698 (hashfunc) 0,
1699 (ternaryfunc) 0,
1700 (reprfunc) 0,
1701};
1702
1703 static void
1704ListDestructor(PyObject *self)
1705{
1706 ListObject *this = ((ListObject *) (self));
1707
1708 pyll_remove(&this->ref, &lastlist);
1709 list_unref(this->list);
1710
1711 Py_DECREF(self);
1712}
1713
1714 static PyObject *
1715ListGetattr(PyObject *self, char *name)
1716{
1717 return Py_FindMethod(ListMethods, self, name);
1718}
1719
1720static void FunctionDestructor(PyObject *);
1721static PyObject *FunctionGetattr(PyObject *, char *);
1722
1723static PyTypeObject FunctionType = {
1724 PyObject_HEAD_INIT(0)
1725 0,
1726 "vimfunction",
1727 sizeof(FunctionObject),
1728 0,
1729
1730 (destructor) FunctionDestructor,
1731 (printfunc) 0,
1732 (getattrfunc) FunctionGetattr,
1733 (setattrfunc) 0,
1734 (cmpfunc) 0,
1735 (reprfunc) 0,
1736
1737 0, /* as number */
1738 0, /* as sequence */
1739 0, /* as mapping */
1740
1741 (hashfunc) 0,
1742 (ternaryfunc) FunctionCall,
1743 (reprfunc) 0,
1744};
1745
1746 static void
1747FunctionDestructor(PyObject *self)
1748{
1749 FunctionObject *this = (FunctionObject *) (self);
1750
1751 func_unref(this->name);
1752 PyMem_Del(this->name);
1753
1754 Py_DECREF(self);
1755}
1756
1757 static PyObject *
1758FunctionGetattr(PyObject *self, char *name)
1759{
1760 FunctionObject *this = (FunctionObject *)(self);
1761
1762 if (strcmp(name, "name") == 0)
1763 return PyString_FromString((char *)(this->name));
1764 else
1765 return Py_FindMethod(FunctionMethods, self, name);
1766}
1767
1768 void
1769do_pyeval (char_u *str, typval_T *rettv)
1770{
1771 DoPythonCommand(NULL, (char *) str, rettv);
1772 switch(rettv->v_type)
1773 {
1774 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1775 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1776 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001777 case VAR_UNKNOWN:
1778 rettv->v_type = VAR_NUMBER;
1779 rettv->vval.v_number = 0;
1780 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001781 }
1782}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784/* Don't generate a prototype for the next function, it generates an error on
1785 * newer Python versions. */
1786#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1787
1788 char *
1789Py_GetProgramName(void)
1790{
1791 return "vim";
1792}
1793#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001794
Bram Moolenaardb913952012-06-29 12:54:53 +02001795 void
1796set_ref_in_python (int copyID)
1797{
1798 set_ref_in_py(copyID);
1799}
1800
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001801 static void
1802init_structs(void)
1803{
1804 vim_memset(&OutputType, 0, sizeof(OutputType));
1805 OutputType.tp_name = "message";
1806 OutputType.tp_basicsize = sizeof(OutputObject);
1807 OutputType.tp_getattr = OutputGetattr;
1808 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001809
1810 vim_memset(&RangeType, 0, sizeof(RangeType));
1811 RangeType.tp_name = "range";
1812 RangeType.tp_basicsize = sizeof(RangeObject);
1813 RangeType.tp_dealloc = RangeDestructor;
1814 RangeType.tp_getattr = RangeGetattr;
1815 RangeType.tp_repr = RangeRepr;
1816 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001817}