blob: 385905e24f4838f65e4fb5264736c67d56a01efd [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
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200194# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# define PyString_FromString dll_PyString_FromString
196# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
197# define PyString_Size dll_PyString_Size
198# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200199# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200200# undef PyUnicode_AsEncodedString
201# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200202# define PyFloat_AsDouble dll_PyFloat_AsDouble
203# define PyFloat_FromDouble dll_PyFloat_FromDouble
204# define PyFloat_Type (*dll_PyFloat_Type)
205# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206# define PySys_SetObject dll_PySys_SetObject
207# define PySys_SetArgv dll_PySys_SetArgv
208# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100209# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# define Py_BuildValue dll_Py_BuildValue
211# define Py_FindMethod dll_Py_FindMethod
212# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100213# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000215# define Py_Finalize dll_Py_Finalize
216# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200218# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
219# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define _Py_NoneStruct (*dll__Py_NoneStruct)
222# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200223# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
225# define PyType_IsSubtype dll_PyType_IsSubtype
226# endif
227# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
228# define PyObject_Malloc dll_PyObject_Malloc
229# define PyObject_Free dll_PyObject_Free
230# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200231# ifdef PY_USE_CAPSULE
232# define PyCapsule_New dll_PyCapsule_New
233# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
234# else
235# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
236# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238
239/*
240 * Pointers for dynamic link
241 */
242static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
243static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200244static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200245static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
247static int(*dll_PyErr_BadArgument)(void);
248static void(*dll_PyErr_Clear)(void);
249static PyObject*(*dll_PyErr_NoMemory)(void);
250static PyObject*(*dll_PyErr_Occurred)(void);
251static void(*dll_PyErr_SetNone)(PyObject *);
252static void(*dll_PyErr_SetString)(PyObject *, const char *);
253static void(*dll_PyEval_InitThreads)(void);
254static void(*dll_PyEval_RestoreThread)(PyThreadState *);
255static PyThreadState*(*dll_PyEval_SaveThread)(void);
256# ifdef PY_CAN_RECURSE
257static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
258static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static long(*dll_PyInt_AsLong)(PyObject *);
261static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200262static long(*dll_PyLong_AsLong)(PyObject *);
263static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200265static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000266static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000267static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000268static PyObject*(*dll_PyList_New)(PyInt size);
269static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
270static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200272static int (*dll_PySequence_Check)(PyObject *);
273static PyInt(*dll_PySequence_Size)(PyObject *);
274static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
275static PyInt(*dll_PyTuple_Size)(PyObject *);
276static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
277static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000279static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200281static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
282# ifndef PY_NO_MAPPING_ITEMS
283static PyObject* (*dll_PyMapping_Items)(PyObject *);
284# endif
285static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
286static int (*dll_PyMapping_Check)(PyObject *);
287static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static PyObject*(*dll_PyModule_GetDict)(PyObject *);
289static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200290static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200292static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000294static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
295static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200297static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200298static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200299static double(*dll_PyFloat_AsDouble)(PyObject *);
300static PyObject*(*dll_PyFloat_FromDouble)(double);
301static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302static int(*dll_PySys_SetObject)(char *, PyObject *);
303static int(*dll_PySys_SetArgv)(int, char **);
304static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100305static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static PyObject*(*dll_Py_BuildValue)(char *, ...);
307static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
308static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200309static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100310static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000312static void(*dll_Py_Finalize)(void);
313static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
315static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200316static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200317# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200318static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static PyObject* dll__Py_NoneStruct;
321# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
322static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
323# endif
324# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
325static void* (*dll_PyObject_Malloc)(size_t);
326static void (*dll_PyObject_Free)(void*);
327# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200328# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200329static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
330static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200331# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200332static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
333static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
336static HINSTANCE hinstPython = 0; /* Instance of python.dll */
337
338/* Imported exception objects */
339static PyObject *imp_PyExc_AttributeError;
340static PyObject *imp_PyExc_IndexError;
341static PyObject *imp_PyExc_KeyboardInterrupt;
342static PyObject *imp_PyExc_TypeError;
343static PyObject *imp_PyExc_ValueError;
344
345# define PyExc_AttributeError imp_PyExc_AttributeError
346# define PyExc_IndexError imp_PyExc_IndexError
347# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
348# define PyExc_TypeError imp_PyExc_TypeError
349# define PyExc_ValueError imp_PyExc_ValueError
350
351/*
352 * Table of name to function pointer of python.
353 */
354# define PYTHON_PROC FARPROC
355static struct
356{
357 char *name;
358 PYTHON_PROC *ptr;
359} python_funcname_table[] =
360{
361 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
362 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200363 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200364 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
366 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
367 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
368 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
369 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
370 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
371 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
372 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
373 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
374 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
375# ifdef PY_CAN_RECURSE
376 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
377 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
378# endif
379 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
380 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200381 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
382 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200384 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000386 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
388 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
389 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
390 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200391 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
392 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
393 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
394 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
395 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
396 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
398 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200399 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000400 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200401# ifndef PY_NO_MAPPING_ITEMS
402 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
403# endif
404 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
405 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
406 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
408 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200409 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200411 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
413 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
414 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
415 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200416 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200417 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
418 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
419 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
420 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
422 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
423 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100424 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
426 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200427# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
428 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000429 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
430# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000432# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100433 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000435 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
436 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
438 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200439 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200440# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200441 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
444# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
445 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
446# endif
447# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
448 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
449 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
450# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200451# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
453 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200454# else
455 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
456 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {"", NULL},
459};
460
461/*
462 * Free python.dll
463 */
464 static void
465end_dynamic_python(void)
466{
467 if (hinstPython)
468 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200469 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 hinstPython = 0;
471 }
472}
473
474/*
475 * Load library and get all pointers.
476 * Parameter 'libname' provides name of DLL.
477 * Return OK or FAIL.
478 */
479 static int
480python_runtime_link_init(char *libname, int verbose)
481{
482 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200483 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100485#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200486 /* Can't have Python and Python3 loaded at the same time.
487 * It cause a crash, because RTLD_GLOBAL is needed for
488 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200489 if (python3_loaded())
490 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200491 if (verbose)
492 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200493 return FAIL;
494 }
495#endif
496
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 if (hinstPython)
498 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200499 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 if (!hinstPython)
501 {
502 if (verbose)
503 EMSG2(_(e_loadlib), libname);
504 return FAIL;
505 }
506
507 for (i = 0; python_funcname_table[i].ptr; ++i)
508 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200509 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 python_funcname_table[i].name)) == NULL)
511 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200512 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 hinstPython = 0;
514 if (verbose)
515 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
516 return FAIL;
517 }
518 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200519
520 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
521 * will be present in the library. */
522 ucs_as_encoded_string = symbol_from_dll(hinstPython,
523 "PyUnicodeUCS2_AsEncodedString");
524 if (ucs_as_encoded_string == NULL)
525 ucs_as_encoded_string = symbol_from_dll(hinstPython,
526 "PyUnicodeUCS4_AsEncodedString");
527 if (ucs_as_encoded_string != NULL)
528 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
529 else
530 {
531 close_dll(hinstPython);
532 hinstPython = 0;
533 if (verbose)
534 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
535 return FAIL;
536 }
537
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 return OK;
539}
540
541/*
542 * If python is enabled (there is installed python on Windows system) return
543 * TRUE, else FALSE.
544 */
545 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000546python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
548 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
549}
550
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200551/*
552 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 * DLL, as this can cause errors (importing data symbols is not reliable).
554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200556get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 PyObject *exmod = PyImport_ImportModule("exceptions");
559 PyObject *exdict = PyModule_GetDict(exmod);
560 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
561 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
562 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
563 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
564 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
565 Py_XINCREF(imp_PyExc_AttributeError);
566 Py_XINCREF(imp_PyExc_IndexError);
567 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
568 Py_XINCREF(imp_PyExc_TypeError);
569 Py_XINCREF(imp_PyExc_ValueError);
570 Py_XDECREF(exmod);
571}
572#endif /* DYNAMIC_PYTHON */
573
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200574static PyObject *BufferNew (buf_T *);
575static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200576static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200577static PyObject *LineToString(const char *);
578
579static PyTypeObject RangeType;
580
Bram Moolenaardb913952012-06-29 12:54:53 +0200581static int initialised = 0;
582#define PYINITIALISED initialised
583
Bram Moolenaardb913952012-06-29 12:54:53 +0200584#define DICTKEY_GET(err) \
585 if (!PyString_Check(keyObject)) \
586 { \
587 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
588 return err; \
589 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200590 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
591 return err;
592
Bram Moolenaardb913952012-06-29 12:54:53 +0200593#define DICTKEY_UNREF
594#define DICTKEY_DECL
595
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200596/*
597 * Include the code shared with if_python3.c
598 */
599#include "if_py_both.h"
600
601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602/******************************************************
603 * Internal function prototypes.
604 */
605
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000606static PyInt RangeStart;
607static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaardb913952012-06-29 12:54:53 +0200609static PyObject *globals;
610
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611static void PythonIO_Flush(void);
612static int PythonIO_Init(void);
613static int PythonMod_Init(void);
614
615/* Utility functions for the vim/python interface
616 * ----------------------------------------------
617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000619static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622/******************************************************
623 * 1. Python interpreter main program.
624 */
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
627typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000630#ifdef PY_CAN_RECURSE
631static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
632#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000633static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636/*
637 * Suspend a thread of the Python interpreter, other threads are allowed to
638 * run.
639 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000640 static void
641Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643#ifdef PY_CAN_RECURSE
644 PyGILState_Release(pygilstate);
645#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648}
649
650/*
651 * Restore a thread of the Python interpreter, waits for other threads to
652 * block.
653 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000654 static void
655Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657#ifdef PY_CAN_RECURSE
658 pygilstate = PyGILState_Ensure();
659#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 PyEval_RestoreThread(saved_python_thread);
661 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000663}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 void
666python_end()
667{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000668 static int recurse = 0;
669
670 /* If a crash occurs while doing this, don't try again. */
671 if (recurse != 0)
672 return;
673
674 ++recurse;
675
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000677 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000678 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000679 Python_RestoreThread(); /* enter python */
680 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000683#else
684 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000685 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000686 Python_RestoreThread(); /* enter python */
687 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000690
691 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692}
693
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200694#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
695 int
696python_loaded()
697{
698 return (hinstPython != 0);
699}
700#endif
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 static int
703Python_Init(void)
704{
705 if (!initialised)
706 {
707#ifdef DYNAMIC_PYTHON
708 if (!python_enabled(TRUE))
709 {
710 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
711 goto fail;
712 }
713#endif
714
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100715#ifdef PYTHON_HOME
716 Py_SetPythonHome(PYTHON_HOME);
717#endif
718
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200719 init_structs();
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#if !defined(MACOS) || defined(MACOS_X_UNIX)
722 Py_Initialize();
723#else
724 PyMac_Initialize();
725#endif
726 /* initialise threads */
727 PyEval_InitThreads();
728
729#ifdef DYNAMIC_PYTHON
730 get_exceptions();
731#endif
732
733 if (PythonIO_Init())
734 goto fail;
735
736 if (PythonMod_Init())
737 goto fail;
738
Bram Moolenaardb913952012-06-29 12:54:53 +0200739 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
740
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000741 /* Remove the element from sys.path that was added because of our
742 * argv[0] value in PythonMod_Init(). Previously we used an empty
743 * string, but dependinding on the OS we then get an empty entry or
744 * the current directory in sys.path. */
745 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
746
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000747 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750 initialised = 1;
751 }
752
753 return 0;
754
755fail:
756 /* We call PythonIO_Flush() here to print any Python errors.
757 * This is OK, as it is possible to call this function even
758 * if PythonIO_Init() has not completed successfully (it will
759 * not do anything in this case).
760 */
761 PythonIO_Flush();
762 return -1;
763}
764
765/*
766 * External interface
767 */
768 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200769DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 static int recursive = 0;
773#endif
774#if defined(MACOS) && !defined(MACOS_X_UNIX)
775 GrafPtr oldPort;
776#endif
777#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
778 char *saved_locale;
779#endif
780
781#ifndef PY_CAN_RECURSE
782 if (recursive)
783 {
784 EMSG(_("E659: Cannot invoke Python recursively"));
785 return;
786 }
787 ++recursive;
788#endif
789
790#if defined(MACOS) && !defined(MACOS_X_UNIX)
791 GetPort(&oldPort);
792 /* Check if the Python library is available */
793 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
794 goto theend;
795#endif
796 if (Python_Init())
797 goto theend;
798
Bram Moolenaardb913952012-06-29 12:54:53 +0200799 if (rettv == NULL)
800 {
801 RangeStart = eap->line1;
802 RangeEnd = eap->line2;
803 }
804 else
805 {
806 RangeStart = (PyInt) curwin->w_cursor.lnum;
807 RangeEnd = RangeStart;
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 Python_Release_Vim(); /* leave vim */
810
811#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
812 /* Python only works properly when the LC_NUMERIC locale is "C". */
813 saved_locale = setlocale(LC_NUMERIC, NULL);
814 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
815 saved_locale = NULL;
816 else
817 {
818 /* Need to make a copy, value may change when setting new locale. */
819 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
820 (void)setlocale(LC_NUMERIC, "C");
821 }
822#endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
Bram Moolenaardb913952012-06-29 12:54:53 +0200826 if (rettv == NULL)
827 PyRun_SimpleString((char *)(cmd));
828 else
829 {
830 PyObject *r;
831
832 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
833 if (r == NULL)
834 EMSG(_("E858: Eval did not return a valid python object"));
835 else
836 {
837 if (ConvertFromPyObject(r, rettv) == -1)
838 EMSG(_("E859: Failed to convert returned python object to vim value"));
839 Py_DECREF(r);
840 }
841 PyErr_Clear();
842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
847 if (saved_locale != NULL)
848 {
849 (void)setlocale(LC_NUMERIC, saved_locale);
850 vim_free(saved_locale);
851 }
852#endif
853
854 Python_Lock_Vim(); /* enter vim */
855 PythonIO_Flush();
856#if defined(MACOS) && !defined(MACOS_X_UNIX)
857 SetPort(oldPort);
858#endif
859
860theend:
861#ifndef PY_CAN_RECURSE
862 --recursive;
863#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200864 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865}
866
867/*
868 * ":python"
869 */
870 void
871ex_python(exarg_T *eap)
872{
873 char_u *script;
874
875 script = script_get(eap, eap->arg);
876 if (!eap->skip)
877 {
878 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200879 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200881 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 }
883 vim_free(script);
884}
885
886#define BUFFER_SIZE 1024
887
888/*
889 * ":pyfile"
890 */
891 void
892ex_pyfile(exarg_T *eap)
893{
894 static char buffer[BUFFER_SIZE];
895 const char *file = (char *)eap->arg;
896 char *p;
897
898 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
899 * stdio file pointer, but Vim and the Python DLL are compiled with
900 * different options under Windows, meaning that stdio pointers aren't
901 * compatible between the two. Yuk.
902 *
903 * Put the string "execfile('file')" into buffer. But, we need to
904 * escape any backslashes or single quotes in the file name, so that
905 * Python won't mangle the file name.
906 */
907 strcpy(buffer, "execfile('");
908 p = buffer + 10; /* size of "execfile('" */
909
910 while (*file && p < buffer + (BUFFER_SIZE - 3))
911 {
912 if (*file == '\\' || *file == '\'')
913 *p++ = '\\';
914 *p++ = *file++;
915 }
916
917 /* If we didn't finish the file name, we hit a buffer overflow */
918 if (*file != '\0')
919 return;
920
921 /* Put in the terminating "')" and a null */
922 *p++ = '\'';
923 *p++ = ')';
924 *p++ = '\0';
925
926 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928}
929
930/******************************************************
931 * 2. Python output stream: writes output via [e]msg().
932 */
933
934/* Implementation functions
935 */
936
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 static PyObject *
938OutputGetattr(PyObject *self, char *name)
939{
940 if (strcmp(name, "softspace") == 0)
941 return PyInt_FromLong(((OutputObject *)(self))->softspace);
942
943 return Py_FindMethod(OutputMethods, self, name);
944}
945
946 static int
947OutputSetattr(PyObject *self, char *name, PyObject *val)
948{
Bram Moolenaardb913952012-06-29 12:54:53 +0200949 if (val == NULL)
950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
952 return -1;
953 }
954
955 if (strcmp(name, "softspace") == 0)
956 {
Bram Moolenaardb913952012-06-29 12:54:53 +0200957 if (!PyInt_Check(val))
958 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
960 return -1;
961 }
962
963 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
964 return 0;
965 }
966
967 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
968 return -1;
969}
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/***************/
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 static int
974PythonIO_Init(void)
975{
976 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100977 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200979 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/******************************************************
983 * 3. Implementation of the Vim module for Python
984 */
985
Bram Moolenaardb913952012-06-29 12:54:53 +0200986static PyObject *ConvertToPyObject(typval_T *);
987static int ConvertFromPyObject(PyObject *, typval_T *);
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/* Window type - Implementation functions
990 * --------------------------------------
991 */
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995static void WindowDestructor(PyObject *);
996static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
998/* Buffer type - Implementation functions
999 * --------------------------------------
1000 */
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004static void BufferDestructor(PyObject *);
1005static PyObject *BufferGetattr(PyObject *, char *);
1006static PyObject *BufferRepr(PyObject *);
1007
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001008static PyInt BufferLength(PyObject *);
1009static PyObject *BufferItem(PyObject *, PyInt);
1010static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
1011static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1012static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014/* Line range type - Implementation functions
1015 * --------------------------------------
1016 */
1017
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1019
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001020static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1021static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/* Current objects type - Implementation functions
1024 * -----------------------------------------------
1025 */
1026
1027static PyObject *CurrentGetattr(PyObject *, char *);
1028static int CurrentSetattr(PyObject *, char *, PyObject *);
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001031 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001032 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1033 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001034 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1035 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1036 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
1037 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038};
1039
1040static PyTypeObject BufferType = {
1041 PyObject_HEAD_INIT(0)
1042 0,
1043 "buffer",
1044 sizeof(BufferObject),
1045 0,
1046
1047 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
1048 (printfunc) 0, /* tp_print, print x */
1049 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
1050 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1051 (cmpfunc) 0, /* tp_compare, x>y */
1052 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
1053
1054 0, /* as number */
1055 &BufferAsSeq, /* as sequence */
1056 0, /* as mapping */
1057
1058 (hashfunc) 0, /* tp_hash, dict(x) */
1059 (ternaryfunc) 0, /* tp_call, x() */
1060 (reprfunc) 0, /* tp_str, str(x) */
1061};
1062
1063/* Buffer object - Implementation
1064 */
1065
1066 static PyObject *
1067BufferNew(buf_T *buf)
1068{
1069 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001070 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * then we can get at it in buf_freeall() in vim. We then
1072 * need to create only ONE Python object per buffer - if
1073 * we try to create a second, just INCREF the existing one
1074 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001075 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 * Question: what to do on a buf_freeall(). We'll probably
1077 * have to either delete the Python object (DECREF it to
1078 * zero - a bad idea, as it leaves dangling refs!) or
1079 * set the buf_T * value to an invalid value (-1?), which
1080 * means we need checks in all access functions... Bah.
1081 */
1082
1083 BufferObject *self;
1084
Bram Moolenaare344bea2005-09-01 20:46:49 +00001085 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001087 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 Py_INCREF(self);
1089 }
1090 else
1091 {
1092 self = PyObject_NEW(BufferObject, &BufferType);
1093 if (self == NULL)
1094 return NULL;
1095 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001096 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098
1099 return (PyObject *)(self);
1100}
1101
1102 static void
1103BufferDestructor(PyObject *self)
1104{
1105 BufferObject *this = (BufferObject *)(self);
1106
1107 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001108 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109
Bram Moolenaar658ada62006-10-03 13:02:36 +00001110 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111}
1112
1113 static PyObject *
1114BufferGetattr(PyObject *self, char *name)
1115{
1116 BufferObject *this = (BufferObject *)(self);
1117
1118 if (CheckBuffer(this))
1119 return NULL;
1120
1121 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001122 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001124 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else if (strcmp(name,"__members__") == 0)
1126 return Py_BuildValue("[ss]", "name", "number");
1127 else
1128 return Py_FindMethod(BufferMethods, self, name);
1129}
1130
1131 static PyObject *
1132BufferRepr(PyObject *self)
1133{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001134 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 BufferObject *this = (BufferObject *)(self);
1136
1137 if (this->buf == INVALID_BUFFER_VALUE)
1138 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001139 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 return PyString_FromString(repr);
1141 }
1142 else
1143 {
1144 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001145 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146
1147 if (name == NULL)
1148 name = "";
1149 len = strlen(name);
1150
1151 if (len > 35)
1152 name = name + (35 - len);
1153
Bram Moolenaar555b2802005-05-19 21:08:39 +00001154 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156 return PyString_FromString(repr);
1157 }
1158}
1159
1160/******************/
1161
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001162 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163BufferLength(PyObject *self)
1164{
1165 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1166 if (CheckBuffer((BufferObject *)(self)))
1167 return -1; /* ??? */
1168
1169 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1170}
1171
1172 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001173BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 return RBItem((BufferObject *)(self), n, 1,
1176 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1177}
1178
1179 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001180BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181{
1182 return RBSlice((BufferObject *)(self), lo, hi, 1,
1183 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1184}
1185
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001186 static PyInt
1187BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001189 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001190 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 NULL);
1192}
1193
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001194 static PyInt
1195BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001197 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001198 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 NULL);
1200}
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001203 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001205 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1206 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1207 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1208 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1209 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210};
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212/* Line range object - Implementation
1213 */
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 static void
1216RangeDestructor(PyObject *self)
1217{
1218 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001219 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220}
1221
1222 static PyObject *
1223RangeGetattr(PyObject *self, char *name)
1224{
1225 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001226 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001228 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 else
1230 return Py_FindMethod(RangeMethods, self, name);
1231}
1232
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233/****************/
1234
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001235 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001236RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001238 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 ((RangeObject *)(self))->start,
1240 ((RangeObject *)(self))->end,
1241 &((RangeObject *)(self))->end);
1242}
1243
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001244 static PyInt
1245RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001247 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ((RangeObject *)(self))->start,
1249 ((RangeObject *)(self))->end,
1250 &((RangeObject *)(self))->end);
1251}
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253/* Buffer list object - Definitions
1254 */
1255
1256typedef struct
1257{
1258 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001259} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001262 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001264 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1265 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1266 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1267 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1268 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269};
1270
1271static PyTypeObject BufListType = {
1272 PyObject_HEAD_INIT(0)
1273 0,
1274 "buffer list",
1275 sizeof(BufListObject),
1276 0,
1277
1278 (destructor) 0, /* tp_dealloc, refcount==0 */
1279 (printfunc) 0, /* tp_print, print x */
1280 (getattrfunc) 0, /* tp_getattr, x.attr */
1281 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1282 (cmpfunc) 0, /* tp_compare, x>y */
1283 (reprfunc) 0, /* tp_repr, `x`, print x */
1284
1285 0, /* as number */
1286 &BufListAsSeq, /* as sequence */
1287 0, /* as mapping */
1288
1289 (hashfunc) 0, /* tp_hash, dict(x) */
1290 (ternaryfunc) 0, /* tp_call, x() */
1291 (reprfunc) 0, /* tp_str, str(x) */
1292};
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294/* Window object - Definitions
1295 */
1296
1297static struct PyMethodDef WindowMethods[] = {
1298 /* name, function, calling, documentation */
1299 { NULL, NULL, 0, NULL }
1300};
1301
1302static PyTypeObject WindowType = {
1303 PyObject_HEAD_INIT(0)
1304 0,
1305 "window",
1306 sizeof(WindowObject),
1307 0,
1308
1309 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1310 (printfunc) 0, /* tp_print, print x */
1311 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1312 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1313 (cmpfunc) 0, /* tp_compare, x>y */
1314 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1315
1316 0, /* as number */
1317 0, /* as sequence */
1318 0, /* as mapping */
1319
1320 (hashfunc) 0, /* tp_hash, dict(x) */
1321 (ternaryfunc) 0, /* tp_call, x() */
1322 (reprfunc) 0, /* tp_str, str(x) */
1323};
1324
1325/* Window object - Implementation
1326 */
1327
1328 static PyObject *
1329WindowNew(win_T *win)
1330{
1331 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001332 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * then we can get at it in win_free() in vim. We then
1334 * need to create only ONE Python object per window - if
1335 * we try to create a second, just INCREF the existing one
1336 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001337 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * On a win_free() we set the Python object's win_T* field
1339 * to an invalid value. We trap all uses of a window
1340 * object, and reject them if the win_T* field is invalid.
1341 */
1342
1343 WindowObject *self;
1344
Bram Moolenaare344bea2005-09-01 20:46:49 +00001345 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001347 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 Py_INCREF(self);
1349 }
1350 else
1351 {
1352 self = PyObject_NEW(WindowObject, &WindowType);
1353 if (self == NULL)
1354 return NULL;
1355 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001356 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358
1359 return (PyObject *)(self);
1360}
1361
1362 static void
1363WindowDestructor(PyObject *self)
1364{
1365 WindowObject *this = (WindowObject *)(self);
1366
1367 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001368 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
Bram Moolenaar658ada62006-10-03 13:02:36 +00001370 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371}
1372
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 static PyObject *
1374WindowGetattr(PyObject *self, char *name)
1375{
1376 WindowObject *this = (WindowObject *)(self);
1377
1378 if (CheckWindow(this))
1379 return NULL;
1380
1381 if (strcmp(name, "buffer") == 0)
1382 return (PyObject *)BufferNew(this->win->w_buffer);
1383 else if (strcmp(name, "cursor") == 0)
1384 {
1385 pos_T *pos = &this->win->w_cursor;
1386
1387 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1388 }
1389 else if (strcmp(name, "height") == 0)
1390 return Py_BuildValue("l", (long)(this->win->w_height));
1391#ifdef FEAT_VERTSPLIT
1392 else if (strcmp(name, "width") == 0)
1393 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1394#endif
1395 else if (strcmp(name,"__members__") == 0)
1396 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1397 else
1398 return Py_FindMethod(WindowMethods, self, name);
1399}
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401/* Window list object - Definitions
1402 */
1403
1404typedef struct
1405{
1406 PyObject_HEAD
1407}
1408WinListObject;
1409
1410static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001411 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001413 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1414 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1415 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1416 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1417 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418};
1419
1420static PyTypeObject WinListType = {
1421 PyObject_HEAD_INIT(0)
1422 0,
1423 "window list",
1424 sizeof(WinListObject),
1425 0,
1426
1427 (destructor) 0, /* tp_dealloc, refcount==0 */
1428 (printfunc) 0, /* tp_print, print x */
1429 (getattrfunc) 0, /* tp_getattr, x.attr */
1430 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1431 (cmpfunc) 0, /* tp_compare, x>y */
1432 (reprfunc) 0, /* tp_repr, `x`, print x */
1433
1434 0, /* as number */
1435 &WinListAsSeq, /* as sequence */
1436 0, /* as mapping */
1437
1438 (hashfunc) 0, /* tp_hash, dict(x) */
1439 (ternaryfunc) 0, /* tp_call, x() */
1440 (reprfunc) 0, /* tp_str, str(x) */
1441};
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443/* Current items object - Definitions
1444 */
1445
1446typedef struct
1447{
1448 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001449} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451static PyTypeObject CurrentType = {
1452 PyObject_HEAD_INIT(0)
1453 0,
1454 "current data",
1455 sizeof(CurrentObject),
1456 0,
1457
1458 (destructor) 0, /* tp_dealloc, refcount==0 */
1459 (printfunc) 0, /* tp_print, print x */
1460 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1461 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1462 (cmpfunc) 0, /* tp_compare, x>y */
1463 (reprfunc) 0, /* tp_repr, `x`, print x */
1464
1465 0, /* as number */
1466 0, /* as sequence */
1467 0, /* as mapping */
1468
1469 (hashfunc) 0, /* tp_hash, dict(x) */
1470 (ternaryfunc) 0, /* tp_call, x() */
1471 (reprfunc) 0, /* tp_str, str(x) */
1472};
1473
1474/* Current items object - Implementation
1475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001477CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478{
1479 if (strcmp(name, "buffer") == 0)
1480 return (PyObject *)BufferNew(curbuf);
1481 else if (strcmp(name, "window") == 0)
1482 return (PyObject *)WindowNew(curwin);
1483 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001484 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 else if (strcmp(name, "range") == 0)
1486 return RangeNew(curbuf, RangeStart, RangeEnd);
1487 else if (strcmp(name,"__members__") == 0)
1488 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1489 else
1490 {
1491 PyErr_SetString(PyExc_AttributeError, name);
1492 return NULL;
1493 }
1494}
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001497CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 if (strcmp(name, "line") == 0)
1500 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001501 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 return -1;
1503
1504 return 0;
1505 }
1506 else
1507 {
1508 PyErr_SetString(PyExc_AttributeError, name);
1509 return -1;
1510 }
1511}
1512
1513/* External interface
1514 */
1515
1516 void
1517python_buffer_free(buf_T *buf)
1518{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001519 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001521 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001523 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 }
1525}
1526
1527#if defined(FEAT_WINDOWS) || defined(PROTO)
1528 void
1529python_window_free(win_T *win)
1530{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001531 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001533 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001535 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537}
1538#endif
1539
1540static BufListObject TheBufferList =
1541{
1542 PyObject_HEAD_INIT(&BufListType)
1543};
1544
1545static WinListObject TheWindowList =
1546{
1547 PyObject_HEAD_INIT(&WinListType)
1548};
1549
1550static CurrentObject TheCurrent =
1551{
1552 PyObject_HEAD_INIT(&CurrentType)
1553};
1554
1555 static int
1556PythonMod_Init(void)
1557{
1558 PyObject *mod;
1559 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001560 /* The special value is removed from sys.path in Python_Init(). */
1561 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001564 PyType_Ready(&BufferType);
1565 PyType_Ready(&RangeType);
1566 PyType_Ready(&WindowType);
1567 PyType_Ready(&BufListType);
1568 PyType_Ready(&WinListType);
1569 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 /* Set sys.argv[] to avoid a crash in warn(). */
1572 PySys_SetArgv(1, argv);
1573
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001574 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 dict = PyModule_GetDict(mod);
1576
1577 VimError = Py_BuildValue("s", "vim.error");
1578
1579 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001580 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1581 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1582 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 if (PyErr_Occurred())
1585 return -1;
1586
1587 return 0;
1588}
1589
1590/*************************************************************************
1591 * 4. Utility functions for handling the interface between Vim and Python.
1592 */
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594/* Convert a Vim line into a Python string.
1595 * All internal newlines are replaced by null characters.
1596 *
1597 * On errors, the Python exception data is set, and NULL is returned.
1598 */
1599 static PyObject *
1600LineToString(const char *str)
1601{
1602 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001603 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 char *p;
1605
1606 /* Allocate an Python string object, with uninitialised contents. We
1607 * must do it this way, so that we can modify the string in place
1608 * later. See the Python source, Objects/stringobject.c for details.
1609 */
1610 result = PyString_FromStringAndSize(NULL, len);
1611 if (result == NULL)
1612 return NULL;
1613
1614 p = PyString_AsString(result);
1615
1616 while (*str)
1617 {
1618 if (*str == '\n')
1619 *p = '\0';
1620 else
1621 *p = *str;
1622
1623 ++p;
1624 ++str;
1625 }
1626
1627 return result;
1628}
1629
Bram Moolenaardb913952012-06-29 12:54:53 +02001630static void DictionaryDestructor(PyObject *);
1631static PyObject *DictionaryGetattr(PyObject *, char*);
1632
1633static PyMappingMethods DictionaryAsMapping = {
1634 (PyInquiry) DictionaryLength,
1635 (binaryfunc) DictionaryItem,
1636 (objobjargproc) DictionaryAssItem,
1637};
1638
1639static PyTypeObject DictionaryType = {
1640 PyObject_HEAD_INIT(0)
1641 0,
1642 "vimdictionary",
1643 sizeof(DictionaryObject),
1644 0,
1645
1646 (destructor) DictionaryDestructor,
1647 (printfunc) 0,
1648 (getattrfunc) DictionaryGetattr,
1649 (setattrfunc) 0,
1650 (cmpfunc) 0,
1651 (reprfunc) 0,
1652
1653 0, /* as number */
1654 0, /* as sequence */
1655 &DictionaryAsMapping, /* as mapping */
1656
1657 (hashfunc) 0,
1658 (ternaryfunc) 0,
1659 (reprfunc) 0,
1660};
1661
1662 static void
1663DictionaryDestructor(PyObject *self)
1664{
1665 DictionaryObject *this = ((DictionaryObject *) (self));
1666
1667 pyll_remove(&this->ref, &lastdict);
1668 dict_unref(this->dict);
1669
1670 Py_DECREF(self);
1671}
1672
1673 static PyObject *
1674DictionaryGetattr(PyObject *self, char *name)
1675{
1676 return Py_FindMethod(DictionaryMethods, self, name);
1677}
1678
1679static void ListDestructor(PyObject *);
1680static PyObject *ListGetattr(PyObject *, char *);
1681
1682static PySequenceMethods ListAsSeq = {
1683 (PyInquiry) ListLength,
1684 (binaryfunc) 0,
1685 (PyIntArgFunc) 0,
1686 (PyIntArgFunc) ListItem,
1687 (PyIntIntArgFunc) ListSlice,
1688 (PyIntObjArgProc) ListAssItem,
1689 (PyIntIntObjArgProc) ListAssSlice,
1690 (objobjproc) 0,
1691#if PY_MAJOR_VERSION >= 2
1692 (binaryfunc) ListConcatInPlace,
1693 0,
1694#endif
1695};
1696
1697static PyTypeObject ListType = {
1698 PyObject_HEAD_INIT(0)
1699 0,
1700 "vimlist",
1701 sizeof(ListObject),
1702 0,
1703
1704 (destructor) ListDestructor,
1705 (printfunc) 0,
1706 (getattrfunc) ListGetattr,
1707 (setattrfunc) 0,
1708 (cmpfunc) 0,
1709 (reprfunc) 0,
1710
1711 0, /* as number */
1712 &ListAsSeq, /* as sequence */
1713 0, /* as mapping */
1714
1715 (hashfunc) 0,
1716 (ternaryfunc) 0,
1717 (reprfunc) 0,
1718};
1719
1720 static void
1721ListDestructor(PyObject *self)
1722{
1723 ListObject *this = ((ListObject *) (self));
1724
1725 pyll_remove(&this->ref, &lastlist);
1726 list_unref(this->list);
1727
1728 Py_DECREF(self);
1729}
1730
1731 static PyObject *
1732ListGetattr(PyObject *self, char *name)
1733{
1734 return Py_FindMethod(ListMethods, self, name);
1735}
1736
1737static void FunctionDestructor(PyObject *);
1738static PyObject *FunctionGetattr(PyObject *, char *);
1739
1740static PyTypeObject FunctionType = {
1741 PyObject_HEAD_INIT(0)
1742 0,
1743 "vimfunction",
1744 sizeof(FunctionObject),
1745 0,
1746
1747 (destructor) FunctionDestructor,
1748 (printfunc) 0,
1749 (getattrfunc) FunctionGetattr,
1750 (setattrfunc) 0,
1751 (cmpfunc) 0,
1752 (reprfunc) 0,
1753
1754 0, /* as number */
1755 0, /* as sequence */
1756 0, /* as mapping */
1757
1758 (hashfunc) 0,
1759 (ternaryfunc) FunctionCall,
1760 (reprfunc) 0,
1761};
1762
1763 static void
1764FunctionDestructor(PyObject *self)
1765{
1766 FunctionObject *this = (FunctionObject *) (self);
1767
1768 func_unref(this->name);
1769 PyMem_Del(this->name);
1770
1771 Py_DECREF(self);
1772}
1773
1774 static PyObject *
1775FunctionGetattr(PyObject *self, char *name)
1776{
1777 FunctionObject *this = (FunctionObject *)(self);
1778
1779 if (strcmp(name, "name") == 0)
1780 return PyString_FromString((char *)(this->name));
1781 else
1782 return Py_FindMethod(FunctionMethods, self, name);
1783}
1784
1785 void
1786do_pyeval (char_u *str, typval_T *rettv)
1787{
1788 DoPythonCommand(NULL, (char *) str, rettv);
1789 switch(rettv->v_type)
1790 {
1791 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1792 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1793 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001794 case VAR_UNKNOWN:
1795 rettv->v_type = VAR_NUMBER;
1796 rettv->vval.v_number = 0;
1797 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001798 }
1799}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
1801/* Don't generate a prototype for the next function, it generates an error on
1802 * newer Python versions. */
1803#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1804
1805 char *
1806Py_GetProgramName(void)
1807{
1808 return "vim";
1809}
1810#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001811
Bram Moolenaardb913952012-06-29 12:54:53 +02001812 void
1813set_ref_in_python (int copyID)
1814{
1815 set_ref_in_py(copyID);
1816}
1817
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001818 static void
1819init_structs(void)
1820{
1821 vim_memset(&OutputType, 0, sizeof(OutputType));
1822 OutputType.tp_name = "message";
1823 OutputType.tp_basicsize = sizeof(OutputObject);
1824 OutputType.tp_getattr = OutputGetattr;
1825 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001826
1827 vim_memset(&RangeType, 0, sizeof(RangeType));
1828 RangeType.tp_name = "range";
1829 RangeType.tp_basicsize = sizeof(RangeObject);
1830 RangeType.tp_dealloc = RangeDestructor;
1831 RangeType.tp_getattr = RangeGetattr;
1832 RangeType.tp_repr = RangeRepr;
1833 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001834}