blob: d0314e2f535363201932cd61683362cd6eb4bd1d [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 Moolenaar66b79852012-09-21 14:00:35 +0200166# define PyBool_Type (*dll_PyBool_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200168# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000170# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171# define PyList_New dll_PyList_New
172# define PyList_SetItem dll_PyList_SetItem
173# define PyList_Size dll_PyList_Size
174# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200175# define PySequence_Check dll_PySequence_Check
176# define PySequence_Size dll_PySequence_Size
177# define PySequence_GetItem dll_PySequence_GetItem
178# define PyTuple_Size dll_PyTuple_Size
179# define PyTuple_GetItem dll_PyTuple_GetItem
180# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000182# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200184# define PyDict_Next dll_PyDict_Next
185# ifdef PyMapping_Items
186# define PY_NO_MAPPING_ITEMS
187# else
188# define PyMapping_Items dll_PyMapping_Items
189# endif
190# define PyObject_CallMethod dll_PyObject_CallMethod
191# define PyMapping_Check dll_PyMapping_Check
192# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# define PyModule_GetDict dll_PyModule_GetDict
194# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200195# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200197# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198# define PyString_FromString dll_PyString_FromString
199# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
200# define PyString_Size dll_PyString_Size
201# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200202# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200203# undef PyUnicode_AsEncodedString
204# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200205# define PyFloat_AsDouble dll_PyFloat_AsDouble
206# define PyFloat_FromDouble dll_PyFloat_FromDouble
207# define PyFloat_Type (*dll_PyFloat_Type)
208# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209# define PySys_SetObject dll_PySys_SetObject
210# define PySys_SetArgv dll_PySys_SetArgv
211# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100212# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# define Py_BuildValue dll_Py_BuildValue
214# define Py_FindMethod dll_Py_FindMethod
215# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100216# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000218# define Py_Finalize dll_Py_Finalize
219# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200221# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
222# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
223# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200225# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
226# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200228# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
230# define PyType_IsSubtype dll_PyType_IsSubtype
231# endif
232# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
233# define PyObject_Malloc dll_PyObject_Malloc
234# define PyObject_Free dll_PyObject_Free
235# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200236# ifdef PY_USE_CAPSULE
237# define PyCapsule_New dll_PyCapsule_New
238# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
239# else
240# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
241# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243
244/*
245 * Pointers for dynamic link
246 */
247static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
248static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200249static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200250static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
252static int(*dll_PyErr_BadArgument)(void);
253static void(*dll_PyErr_Clear)(void);
254static PyObject*(*dll_PyErr_NoMemory)(void);
255static PyObject*(*dll_PyErr_Occurred)(void);
256static void(*dll_PyErr_SetNone)(PyObject *);
257static void(*dll_PyErr_SetString)(PyObject *, const char *);
258static void(*dll_PyEval_InitThreads)(void);
259static void(*dll_PyEval_RestoreThread)(PyThreadState *);
260static PyThreadState*(*dll_PyEval_SaveThread)(void);
261# ifdef PY_CAN_RECURSE
262static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
263static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265static long(*dll_PyInt_AsLong)(PyObject *);
266static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200267static long(*dll_PyLong_AsLong)(PyObject *);
268static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200269static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200271static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000272static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000273static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000274static PyObject*(*dll_PyList_New)(PyInt size);
275static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
276static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200278static int (*dll_PySequence_Check)(PyObject *);
279static PyInt(*dll_PySequence_Size)(PyObject *);
280static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
281static PyInt(*dll_PyTuple_Size)(PyObject *);
282static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
283static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000285static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200287static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
288# ifndef PY_NO_MAPPING_ITEMS
289static PyObject* (*dll_PyMapping_Items)(PyObject *);
290# endif
291static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
292static int (*dll_PyMapping_Check)(PyObject *);
293static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static PyObject*(*dll_PyModule_GetDict)(PyObject *);
295static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200296static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200298static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000300static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
301static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200303static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200304static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200305static double(*dll_PyFloat_AsDouble)(PyObject *);
306static PyObject*(*dll_PyFloat_FromDouble)(double);
307static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static int(*dll_PySys_SetObject)(char *, PyObject *);
309static int(*dll_PySys_SetArgv)(int, char **);
310static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100311static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static PyObject*(*dll_Py_BuildValue)(char *, ...);
313static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
314static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200315static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100316static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000318static void(*dll_Py_Finalize)(void);
319static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
321static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200322static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200323# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200324static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200327static PyObject* _Py_ZeroStruct;
328static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
330static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
331# endif
332# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
333static void* (*dll_PyObject_Malloc)(size_t);
334static void (*dll_PyObject_Free)(void*);
335# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200336# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200337static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
338static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200339# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200340static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
341static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200342# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
344static HINSTANCE hinstPython = 0; /* Instance of python.dll */
345
346/* Imported exception objects */
347static PyObject *imp_PyExc_AttributeError;
348static PyObject *imp_PyExc_IndexError;
349static PyObject *imp_PyExc_KeyboardInterrupt;
350static PyObject *imp_PyExc_TypeError;
351static PyObject *imp_PyExc_ValueError;
352
353# define PyExc_AttributeError imp_PyExc_AttributeError
354# define PyExc_IndexError imp_PyExc_IndexError
355# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
356# define PyExc_TypeError imp_PyExc_TypeError
357# define PyExc_ValueError imp_PyExc_ValueError
358
359/*
360 * Table of name to function pointer of python.
361 */
362# define PYTHON_PROC FARPROC
363static struct
364{
365 char *name;
366 PYTHON_PROC *ptr;
367} python_funcname_table[] =
368{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200369#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
371 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200372 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
373#else
374 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
375 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
376 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
377#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200378 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200379 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
381 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
382 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
383 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
384 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
385 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
386 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
387 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
388 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
389 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
390# ifdef PY_CAN_RECURSE
391 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
392 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
393# endif
394 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
395 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200396 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
397 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200398 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200400 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000402 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
404 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
405 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
406 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200407 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
408 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
409 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
410 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
411 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
412 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
414 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200415 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000416 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200417# ifndef PY_NO_MAPPING_ITEMS
418 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
419# endif
420 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
421 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
422 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
424 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200425 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200427 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
429 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
430 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
431 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200432 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200433 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
434 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
435 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
436 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
438 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
439 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100440 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200442# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
443 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000444 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
445# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000447# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100448 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000450 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
451 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
453 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200454 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200455# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200456 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200459 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
460 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
462 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
463# endif
464# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
465 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
466 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
467# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200468# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200469 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
470 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200471# else
472 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
473 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {"", NULL},
476};
477
478/*
479 * Free python.dll
480 */
481 static void
482end_dynamic_python(void)
483{
484 if (hinstPython)
485 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200486 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 hinstPython = 0;
488 }
489}
490
491/*
492 * Load library and get all pointers.
493 * Parameter 'libname' provides name of DLL.
494 * Return OK or FAIL.
495 */
496 static int
497python_runtime_link_init(char *libname, int verbose)
498{
499 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200500 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100502#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200503 /* Can't have Python and Python3 loaded at the same time.
504 * It cause a crash, because RTLD_GLOBAL is needed for
505 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200506 if (python3_loaded())
507 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200508 if (verbose)
509 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200510 return FAIL;
511 }
512#endif
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (hinstPython)
515 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200516 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (!hinstPython)
518 {
519 if (verbose)
520 EMSG2(_(e_loadlib), libname);
521 return FAIL;
522 }
523
524 for (i = 0; python_funcname_table[i].ptr; ++i)
525 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200526 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 python_funcname_table[i].name)) == NULL)
528 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200529 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 hinstPython = 0;
531 if (verbose)
532 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
533 return FAIL;
534 }
535 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200536
537 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
538 * will be present in the library. */
539 ucs_as_encoded_string = symbol_from_dll(hinstPython,
540 "PyUnicodeUCS2_AsEncodedString");
541 if (ucs_as_encoded_string == NULL)
542 ucs_as_encoded_string = symbol_from_dll(hinstPython,
543 "PyUnicodeUCS4_AsEncodedString");
544 if (ucs_as_encoded_string != NULL)
545 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
546 else
547 {
548 close_dll(hinstPython);
549 hinstPython = 0;
550 if (verbose)
551 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
552 return FAIL;
553 }
554
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 return OK;
556}
557
558/*
559 * If python is enabled (there is installed python on Windows system) return
560 * TRUE, else FALSE.
561 */
562 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000563python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
566}
567
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200568/*
569 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 * DLL, as this can cause errors (importing data symbols is not reliable).
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200573get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 PyObject *exmod = PyImport_ImportModule("exceptions");
576 PyObject *exdict = PyModule_GetDict(exmod);
577 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
578 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
579 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
580 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
581 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
582 Py_XINCREF(imp_PyExc_AttributeError);
583 Py_XINCREF(imp_PyExc_IndexError);
584 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
585 Py_XINCREF(imp_PyExc_TypeError);
586 Py_XINCREF(imp_PyExc_ValueError);
587 Py_XDECREF(exmod);
588}
589#endif /* DYNAMIC_PYTHON */
590
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200591static PyObject *BufferNew (buf_T *);
592static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200593static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200594static PyObject *LineToString(const char *);
595
596static PyTypeObject RangeType;
597
Bram Moolenaardb913952012-06-29 12:54:53 +0200598static int initialised = 0;
599#define PYINITIALISED initialised
600
Bram Moolenaardb913952012-06-29 12:54:53 +0200601#define DICTKEY_GET(err) \
602 if (!PyString_Check(keyObject)) \
603 { \
604 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
605 return err; \
606 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200607 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
608 return err;
609
Bram Moolenaardb913952012-06-29 12:54:53 +0200610#define DICTKEY_UNREF
611#define DICTKEY_DECL
612
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200613/*
614 * Include the code shared with if_python3.c
615 */
616#include "if_py_both.h"
617
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619/******************************************************
620 * Internal function prototypes.
621 */
622
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000623static PyInt RangeStart;
624static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625
Bram Moolenaardb913952012-06-29 12:54:53 +0200626static PyObject *globals;
627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628static void PythonIO_Flush(void);
629static int PythonIO_Init(void);
630static int PythonMod_Init(void);
631
632/* Utility functions for the vim/python interface
633 * ----------------------------------------------
634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000636static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639/******************************************************
640 * 1. Python interpreter main program.
641 */
642
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
644typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#ifdef PY_CAN_RECURSE
648static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
649#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000650static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
653/*
654 * Suspend a thread of the Python interpreter, other threads are allowed to
655 * run.
656 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000657 static void
658Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660#ifdef PY_CAN_RECURSE
661 PyGILState_Release(pygilstate);
662#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665}
666
667/*
668 * Restore a thread of the Python interpreter, waits for other threads to
669 * block.
670 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000671 static void
672Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000674#ifdef PY_CAN_RECURSE
675 pygilstate = PyGILState_Ensure();
676#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 PyEval_RestoreThread(saved_python_thread);
678 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 void
683python_end()
684{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000685 static int recurse = 0;
686
687 /* If a crash occurs while doing this, don't try again. */
688 if (recurse != 0)
689 return;
690
691 ++recurse;
692
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000694 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000696 Python_RestoreThread(); /* enter python */
697 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000700#else
701 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000702 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000703 Python_RestoreThread(); /* enter python */
704 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000707
708 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200711#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
712 int
713python_loaded()
714{
715 return (hinstPython != 0);
716}
717#endif
718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 static int
720Python_Init(void)
721{
722 if (!initialised)
723 {
724#ifdef DYNAMIC_PYTHON
725 if (!python_enabled(TRUE))
726 {
727 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
728 goto fail;
729 }
730#endif
731
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100732#ifdef PYTHON_HOME
733 Py_SetPythonHome(PYTHON_HOME);
734#endif
735
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200736 init_structs();
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#if !defined(MACOS) || defined(MACOS_X_UNIX)
739 Py_Initialize();
740#else
741 PyMac_Initialize();
742#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100743 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100744 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100745 * specific state (such as the system trace hook), will be lost
746 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748#ifdef DYNAMIC_PYTHON
749 get_exceptions();
750#endif
751
752 if (PythonIO_Init())
753 goto fail;
754
755 if (PythonMod_Init())
756 goto fail;
757
Bram Moolenaardb913952012-06-29 12:54:53 +0200758 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
759
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000760 /* Remove the element from sys.path that was added because of our
761 * argv[0] value in PythonMod_Init(). Previously we used an empty
762 * string, but dependinding on the OS we then get an empty entry or
763 * the current directory in sys.path. */
764 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
765
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100766 /* lock is created and acquired in PyEval_InitThreads() and thread
767 * state is created in Py_Initialize()
768 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
769 * (python must have threads enabled!)
770 * so the following does both: unlock GIL and save thread state in TLS
771 * without deleting thread state
772 */
773 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 initialised = 1;
776 }
777
778 return 0;
779
780fail:
781 /* We call PythonIO_Flush() here to print any Python errors.
782 * This is OK, as it is possible to call this function even
783 * if PythonIO_Init() has not completed successfully (it will
784 * not do anything in this case).
785 */
786 PythonIO_Flush();
787 return -1;
788}
789
790/*
791 * External interface
792 */
793 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200794DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 static int recursive = 0;
798#endif
799#if defined(MACOS) && !defined(MACOS_X_UNIX)
800 GrafPtr oldPort;
801#endif
802#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
803 char *saved_locale;
804#endif
805
806#ifndef PY_CAN_RECURSE
807 if (recursive)
808 {
809 EMSG(_("E659: Cannot invoke Python recursively"));
810 return;
811 }
812 ++recursive;
813#endif
814
815#if defined(MACOS) && !defined(MACOS_X_UNIX)
816 GetPort(&oldPort);
817 /* Check if the Python library is available */
818 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
819 goto theend;
820#endif
821 if (Python_Init())
822 goto theend;
823
Bram Moolenaardb913952012-06-29 12:54:53 +0200824 if (rettv == NULL)
825 {
826 RangeStart = eap->line1;
827 RangeEnd = eap->line2;
828 }
829 else
830 {
831 RangeStart = (PyInt) curwin->w_cursor.lnum;
832 RangeEnd = RangeStart;
833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 Python_Release_Vim(); /* leave vim */
835
836#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
837 /* Python only works properly when the LC_NUMERIC locale is "C". */
838 saved_locale = setlocale(LC_NUMERIC, NULL);
839 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
840 saved_locale = NULL;
841 else
842 {
843 /* Need to make a copy, value may change when setting new locale. */
844 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
845 (void)setlocale(LC_NUMERIC, "C");
846 }
847#endif
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaardb913952012-06-29 12:54:53 +0200851 if (rettv == NULL)
852 PyRun_SimpleString((char *)(cmd));
853 else
854 {
855 PyObject *r;
856
857 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
858 if (r == NULL)
859 EMSG(_("E858: Eval did not return a valid python object"));
860 else
861 {
862 if (ConvertFromPyObject(r, rettv) == -1)
863 EMSG(_("E859: Failed to convert returned python object to vim value"));
864 Py_DECREF(r);
865 }
866 PyErr_Clear();
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
871#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
872 if (saved_locale != NULL)
873 {
874 (void)setlocale(LC_NUMERIC, saved_locale);
875 vim_free(saved_locale);
876 }
877#endif
878
879 Python_Lock_Vim(); /* enter vim */
880 PythonIO_Flush();
881#if defined(MACOS) && !defined(MACOS_X_UNIX)
882 SetPort(oldPort);
883#endif
884
885theend:
886#ifndef PY_CAN_RECURSE
887 --recursive;
888#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200889 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890}
891
892/*
893 * ":python"
894 */
895 void
896ex_python(exarg_T *eap)
897{
898 char_u *script;
899
900 script = script_get(eap, eap->arg);
901 if (!eap->skip)
902 {
903 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200904 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200906 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908 vim_free(script);
909}
910
911#define BUFFER_SIZE 1024
912
913/*
914 * ":pyfile"
915 */
916 void
917ex_pyfile(exarg_T *eap)
918{
919 static char buffer[BUFFER_SIZE];
920 const char *file = (char *)eap->arg;
921 char *p;
922
923 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
924 * stdio file pointer, but Vim and the Python DLL are compiled with
925 * different options under Windows, meaning that stdio pointers aren't
926 * compatible between the two. Yuk.
927 *
928 * Put the string "execfile('file')" into buffer. But, we need to
929 * escape any backslashes or single quotes in the file name, so that
930 * Python won't mangle the file name.
931 */
932 strcpy(buffer, "execfile('");
933 p = buffer + 10; /* size of "execfile('" */
934
935 while (*file && p < buffer + (BUFFER_SIZE - 3))
936 {
937 if (*file == '\\' || *file == '\'')
938 *p++ = '\\';
939 *p++ = *file++;
940 }
941
942 /* If we didn't finish the file name, we hit a buffer overflow */
943 if (*file != '\0')
944 return;
945
946 /* Put in the terminating "')" and a null */
947 *p++ = '\'';
948 *p++ = ')';
949 *p++ = '\0';
950
951 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200952 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/******************************************************
956 * 2. Python output stream: writes output via [e]msg().
957 */
958
959/* Implementation functions
960 */
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 static PyObject *
963OutputGetattr(PyObject *self, char *name)
964{
965 if (strcmp(name, "softspace") == 0)
966 return PyInt_FromLong(((OutputObject *)(self))->softspace);
967
968 return Py_FindMethod(OutputMethods, self, name);
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 Moolenaar66b79852012-09-21 14:00:35 +02001583 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1584 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1585 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1586 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 if (PyErr_Occurred())
1589 return -1;
1590
1591 return 0;
1592}
1593
1594/*************************************************************************
1595 * 4. Utility functions for handling the interface between Vim and Python.
1596 */
1597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/* Convert a Vim line into a Python string.
1599 * All internal newlines are replaced by null characters.
1600 *
1601 * On errors, the Python exception data is set, and NULL is returned.
1602 */
1603 static PyObject *
1604LineToString(const char *str)
1605{
1606 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001607 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 char *p;
1609
1610 /* Allocate an Python string object, with uninitialised contents. We
1611 * must do it this way, so that we can modify the string in place
1612 * later. See the Python source, Objects/stringobject.c for details.
1613 */
1614 result = PyString_FromStringAndSize(NULL, len);
1615 if (result == NULL)
1616 return NULL;
1617
1618 p = PyString_AsString(result);
1619
1620 while (*str)
1621 {
1622 if (*str == '\n')
1623 *p = '\0';
1624 else
1625 *p = *str;
1626
1627 ++p;
1628 ++str;
1629 }
1630
1631 return result;
1632}
1633
Bram Moolenaardb913952012-06-29 12:54:53 +02001634static void DictionaryDestructor(PyObject *);
1635static PyObject *DictionaryGetattr(PyObject *, char*);
1636
1637static PyMappingMethods DictionaryAsMapping = {
1638 (PyInquiry) DictionaryLength,
1639 (binaryfunc) DictionaryItem,
1640 (objobjargproc) DictionaryAssItem,
1641};
1642
1643static PyTypeObject DictionaryType = {
1644 PyObject_HEAD_INIT(0)
1645 0,
1646 "vimdictionary",
1647 sizeof(DictionaryObject),
1648 0,
1649
1650 (destructor) DictionaryDestructor,
1651 (printfunc) 0,
1652 (getattrfunc) DictionaryGetattr,
Bram Moolenaar66b79852012-09-21 14:00:35 +02001653 (setattrfunc) DictionarySetattr,
Bram Moolenaardb913952012-06-29 12:54:53 +02001654 (cmpfunc) 0,
1655 (reprfunc) 0,
1656
1657 0, /* as number */
1658 0, /* as sequence */
1659 &DictionaryAsMapping, /* as mapping */
1660
1661 (hashfunc) 0,
1662 (ternaryfunc) 0,
1663 (reprfunc) 0,
1664};
1665
1666 static void
1667DictionaryDestructor(PyObject *self)
1668{
1669 DictionaryObject *this = ((DictionaryObject *) (self));
1670
1671 pyll_remove(&this->ref, &lastdict);
1672 dict_unref(this->dict);
1673
1674 Py_DECREF(self);
1675}
1676
1677 static PyObject *
1678DictionaryGetattr(PyObject *self, char *name)
1679{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001680 DictionaryObject *this = ((DictionaryObject *) (self));
1681
1682 if (strcmp(name, "locked") == 0)
1683 return PyInt_FromLong(this->dict->dv_lock);
1684 else if (strcmp(name, "scope") == 0)
1685 return PyInt_FromLong(this->dict->dv_scope);
1686
Bram Moolenaardb913952012-06-29 12:54:53 +02001687 return Py_FindMethod(DictionaryMethods, self, name);
1688}
1689
1690static void ListDestructor(PyObject *);
1691static PyObject *ListGetattr(PyObject *, char *);
1692
1693static PySequenceMethods ListAsSeq = {
1694 (PyInquiry) ListLength,
1695 (binaryfunc) 0,
1696 (PyIntArgFunc) 0,
1697 (PyIntArgFunc) ListItem,
1698 (PyIntIntArgFunc) ListSlice,
1699 (PyIntObjArgProc) ListAssItem,
1700 (PyIntIntObjArgProc) ListAssSlice,
1701 (objobjproc) 0,
1702#if PY_MAJOR_VERSION >= 2
1703 (binaryfunc) ListConcatInPlace,
1704 0,
1705#endif
1706};
1707
1708static PyTypeObject ListType = {
1709 PyObject_HEAD_INIT(0)
1710 0,
1711 "vimlist",
1712 sizeof(ListObject),
1713 0,
1714
1715 (destructor) ListDestructor,
1716 (printfunc) 0,
1717 (getattrfunc) ListGetattr,
Bram Moolenaar66b79852012-09-21 14:00:35 +02001718 (setattrfunc) ListSetattr,
Bram Moolenaardb913952012-06-29 12:54:53 +02001719 (cmpfunc) 0,
1720 (reprfunc) 0,
1721
1722 0, /* as number */
1723 &ListAsSeq, /* as sequence */
1724 0, /* as mapping */
1725
1726 (hashfunc) 0,
1727 (ternaryfunc) 0,
1728 (reprfunc) 0,
1729};
1730
1731 static void
1732ListDestructor(PyObject *self)
1733{
1734 ListObject *this = ((ListObject *) (self));
1735
1736 pyll_remove(&this->ref, &lastlist);
1737 list_unref(this->list);
1738
1739 Py_DECREF(self);
1740}
1741
1742 static PyObject *
1743ListGetattr(PyObject *self, char *name)
1744{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001745 if (strcmp(name, "locked") == 0)
1746 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1747
Bram Moolenaardb913952012-06-29 12:54:53 +02001748 return Py_FindMethod(ListMethods, self, name);
1749}
1750
1751static void FunctionDestructor(PyObject *);
1752static PyObject *FunctionGetattr(PyObject *, char *);
1753
1754static PyTypeObject FunctionType = {
1755 PyObject_HEAD_INIT(0)
1756 0,
1757 "vimfunction",
1758 sizeof(FunctionObject),
1759 0,
1760
1761 (destructor) FunctionDestructor,
1762 (printfunc) 0,
1763 (getattrfunc) FunctionGetattr,
1764 (setattrfunc) 0,
1765 (cmpfunc) 0,
1766 (reprfunc) 0,
1767
1768 0, /* as number */
1769 0, /* as sequence */
1770 0, /* as mapping */
1771
1772 (hashfunc) 0,
1773 (ternaryfunc) FunctionCall,
1774 (reprfunc) 0,
1775};
1776
1777 static void
1778FunctionDestructor(PyObject *self)
1779{
1780 FunctionObject *this = (FunctionObject *) (self);
1781
1782 func_unref(this->name);
1783 PyMem_Del(this->name);
1784
1785 Py_DECREF(self);
1786}
1787
1788 static PyObject *
1789FunctionGetattr(PyObject *self, char *name)
1790{
1791 FunctionObject *this = (FunctionObject *)(self);
1792
1793 if (strcmp(name, "name") == 0)
1794 return PyString_FromString((char *)(this->name));
1795 else
1796 return Py_FindMethod(FunctionMethods, self, name);
1797}
1798
1799 void
1800do_pyeval (char_u *str, typval_T *rettv)
1801{
1802 DoPythonCommand(NULL, (char *) str, rettv);
1803 switch(rettv->v_type)
1804 {
1805 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1806 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1807 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001808 case VAR_UNKNOWN:
1809 rettv->v_type = VAR_NUMBER;
1810 rettv->vval.v_number = 0;
1811 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001812 }
1813}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815/* Don't generate a prototype for the next function, it generates an error on
1816 * newer Python versions. */
1817#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1818
1819 char *
1820Py_GetProgramName(void)
1821{
1822 return "vim";
1823}
1824#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001825
Bram Moolenaardb913952012-06-29 12:54:53 +02001826 void
1827set_ref_in_python (int copyID)
1828{
1829 set_ref_in_py(copyID);
1830}
1831
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001832 static void
1833init_structs(void)
1834{
1835 vim_memset(&OutputType, 0, sizeof(OutputType));
1836 OutputType.tp_name = "message";
1837 OutputType.tp_basicsize = sizeof(OutputObject);
1838 OutputType.tp_getattr = OutputGetattr;
1839 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001840
1841 vim_memset(&RangeType, 0, sizeof(RangeType));
1842 RangeType.tp_name = "range";
1843 RangeType.tp_basicsize = sizeof(RangeObject);
1844 RangeType.tp_dealloc = RangeDestructor;
1845 RangeType.tp_getattr = RangeGetattr;
1846 RangeType.tp_repr = RangeRepr;
1847 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001848}