blob: 0f90c9903247fdb1db5b93fa9da9a9da5639e464 [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
Bram Moolenaar4d369872013-02-20 16:09:43 +0100151# define PyErr_PrintEx dll_PyErr_PrintEx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152# define PyErr_NoMemory dll_PyErr_NoMemory
153# define PyErr_Occurred dll_PyErr_Occurred
154# define PyErr_SetNone dll_PyErr_SetNone
155# define PyErr_SetString dll_PyErr_SetString
156# define PyEval_InitThreads dll_PyEval_InitThreads
157# define PyEval_RestoreThread dll_PyEval_RestoreThread
158# define PyEval_SaveThread dll_PyEval_SaveThread
159# ifdef PY_CAN_RECURSE
160# define PyGILState_Ensure dll_PyGILState_Ensure
161# define PyGILState_Release dll_PyGILState_Release
162# endif
163# define PyInt_AsLong dll_PyInt_AsLong
164# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200165# define PyLong_AsLong dll_PyLong_AsLong
166# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar66b79852012-09-21 14:00:35 +0200167# define PyBool_Type (*dll_PyBool_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200169# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000171# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172# define PyList_New dll_PyList_New
173# define PyList_SetItem dll_PyList_SetItem
174# define PyList_Size dll_PyList_Size
175# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200176# define PySequence_Check dll_PySequence_Check
177# define PySequence_Size dll_PySequence_Size
178# define PySequence_GetItem dll_PySequence_GetItem
179# define PyTuple_Size dll_PyTuple_Size
180# define PyTuple_GetItem dll_PyTuple_GetItem
181# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000183# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200185# define PyDict_Next dll_PyDict_Next
186# ifdef PyMapping_Items
187# define PY_NO_MAPPING_ITEMS
188# else
189# define PyMapping_Items dll_PyMapping_Items
190# endif
191# define PyObject_CallMethod dll_PyObject_CallMethod
192# define PyMapping_Check dll_PyMapping_Check
193# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define PyModule_GetDict dll_PyModule_GetDict
195# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200196# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200198# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199# define PyString_FromString dll_PyString_FromString
200# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
201# define PyString_Size dll_PyString_Size
202# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200203# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200204# undef PyUnicode_AsEncodedString
205# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200206# define PyFloat_AsDouble dll_PyFloat_AsDouble
207# define PyFloat_FromDouble dll_PyFloat_FromDouble
208# define PyFloat_Type (*dll_PyFloat_Type)
209# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210# define PySys_SetObject dll_PySys_SetObject
211# define PySys_SetArgv dll_PySys_SetArgv
212# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100213# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define Py_BuildValue dll_Py_BuildValue
215# define Py_FindMethod dll_Py_FindMethod
216# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100217# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000219# define Py_Finalize dll_Py_Finalize
220# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200222# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
223# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200226# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
227# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200229# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
231# define PyType_IsSubtype dll_PyType_IsSubtype
232# endif
233# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
234# define PyObject_Malloc dll_PyObject_Malloc
235# define PyObject_Free dll_PyObject_Free
236# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200237# ifdef PY_USE_CAPSULE
238# define PyCapsule_New dll_PyCapsule_New
239# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
240# else
241# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
242# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245/*
246 * Pointers for dynamic link
247 */
248static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
249static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200250static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200251static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
253static int(*dll_PyErr_BadArgument)(void);
254static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100255static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256static PyObject*(*dll_PyErr_NoMemory)(void);
257static PyObject*(*dll_PyErr_Occurred)(void);
258static void(*dll_PyErr_SetNone)(PyObject *);
259static void(*dll_PyErr_SetString)(PyObject *, const char *);
260static void(*dll_PyEval_InitThreads)(void);
261static void(*dll_PyEval_RestoreThread)(PyThreadState *);
262static PyThreadState*(*dll_PyEval_SaveThread)(void);
263# ifdef PY_CAN_RECURSE
264static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
265static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200266# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267static long(*dll_PyInt_AsLong)(PyObject *);
268static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200269static long(*dll_PyLong_AsLong)(PyObject *);
270static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200271static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200273static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000274static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000275static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000276static PyObject*(*dll_PyList_New)(PyInt size);
277static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
278static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200280static int (*dll_PySequence_Check)(PyObject *);
281static PyInt(*dll_PySequence_Size)(PyObject *);
282static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
283static PyInt(*dll_PyTuple_Size)(PyObject *);
284static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
285static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000287static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200289static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
290# ifndef PY_NO_MAPPING_ITEMS
291static PyObject* (*dll_PyMapping_Items)(PyObject *);
292# endif
293static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
294static int (*dll_PyMapping_Check)(PyObject *);
295static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static PyObject*(*dll_PyModule_GetDict)(PyObject *);
297static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200298static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200300static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000302static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
303static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200305static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200306static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200307static double(*dll_PyFloat_AsDouble)(PyObject *);
308static PyObject*(*dll_PyFloat_FromDouble)(double);
309static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static int(*dll_PySys_SetObject)(char *, PyObject *);
311static int(*dll_PySys_SetArgv)(int, char **);
312static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100313static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static PyObject*(*dll_Py_BuildValue)(char *, ...);
315static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
316static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200317static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100318static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000320static void(*dll_Py_Finalize)(void);
321static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
323static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200324static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200325# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200326static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200329static PyObject* _Py_ZeroStruct;
330static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
332static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
333# endif
334# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
335static void* (*dll_PyObject_Malloc)(size_t);
336static void (*dll_PyObject_Free)(void*);
337# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200338# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200339static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
340static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200341# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200342static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
343static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346static HINSTANCE hinstPython = 0; /* Instance of python.dll */
347
348/* Imported exception objects */
349static PyObject *imp_PyExc_AttributeError;
350static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200351static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352static PyObject *imp_PyExc_KeyboardInterrupt;
353static PyObject *imp_PyExc_TypeError;
354static PyObject *imp_PyExc_ValueError;
355
356# define PyExc_AttributeError imp_PyExc_AttributeError
357# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200358# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
360# define PyExc_TypeError imp_PyExc_TypeError
361# define PyExc_ValueError imp_PyExc_ValueError
362
363/*
364 * Table of name to function pointer of python.
365 */
366# define PYTHON_PROC FARPROC
367static struct
368{
369 char *name;
370 PYTHON_PROC *ptr;
371} python_funcname_table[] =
372{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200373#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
375 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200376 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
377#else
378 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
379 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
380 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
381#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200382 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200383 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
385 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
386 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100387 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
389 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
390 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
391 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
392 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
393 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
394 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
395# ifdef PY_CAN_RECURSE
396 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
397 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
398# endif
399 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
400 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200401 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
402 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200403 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200405 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000407 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
409 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
410 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
411 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200412 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
413 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
414 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
415 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
416 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
417 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
419 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200420 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000421 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200422# ifndef PY_NO_MAPPING_ITEMS
423 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
424# endif
425 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
426 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
427 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
429 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200430 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200432 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
434 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
435 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
436 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200437 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200438 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
439 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
440 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
441 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
443 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
444 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100445 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200447# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
448 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000449 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
450# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000452# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100453 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000455 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
456 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
458 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200459 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200460# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200461 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200464 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
465 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
467 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
468# endif
469# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
470 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
471 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
472# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200473# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200474 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
475 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200476# else
477 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
478 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 {"", NULL},
481};
482
483/*
484 * Free python.dll
485 */
486 static void
487end_dynamic_python(void)
488{
489 if (hinstPython)
490 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200491 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 hinstPython = 0;
493 }
494}
495
496/*
497 * Load library and get all pointers.
498 * Parameter 'libname' provides name of DLL.
499 * Return OK or FAIL.
500 */
501 static int
502python_runtime_link_init(char *libname, int verbose)
503{
504 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200505 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100507#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200508 /* Can't have Python and Python3 loaded at the same time.
509 * It cause a crash, because RTLD_GLOBAL is needed for
510 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200511 if (python3_loaded())
512 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200513 if (verbose)
514 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200515 return FAIL;
516 }
517#endif
518
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (hinstPython)
520 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200521 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (!hinstPython)
523 {
524 if (verbose)
525 EMSG2(_(e_loadlib), libname);
526 return FAIL;
527 }
528
529 for (i = 0; python_funcname_table[i].ptr; ++i)
530 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200531 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 python_funcname_table[i].name)) == NULL)
533 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 hinstPython = 0;
536 if (verbose)
537 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
538 return FAIL;
539 }
540 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200541
542 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
543 * will be present in the library. */
544 ucs_as_encoded_string = symbol_from_dll(hinstPython,
545 "PyUnicodeUCS2_AsEncodedString");
546 if (ucs_as_encoded_string == NULL)
547 ucs_as_encoded_string = symbol_from_dll(hinstPython,
548 "PyUnicodeUCS4_AsEncodedString");
549 if (ucs_as_encoded_string != NULL)
550 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
551 else
552 {
553 close_dll(hinstPython);
554 hinstPython = 0;
555 if (verbose)
556 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
557 return FAIL;
558 }
559
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 return OK;
561}
562
563/*
564 * If python is enabled (there is installed python on Windows system) return
565 * TRUE, else FALSE.
566 */
567 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000568python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
571}
572
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200573/*
574 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 * DLL, as this can cause errors (importing data symbols is not reliable).
576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200578get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 PyObject *exmod = PyImport_ImportModule("exceptions");
581 PyObject *exdict = PyModule_GetDict(exmod);
582 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
583 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200584 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
586 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
587 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
588 Py_XINCREF(imp_PyExc_AttributeError);
589 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200590 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
592 Py_XINCREF(imp_PyExc_TypeError);
593 Py_XINCREF(imp_PyExc_ValueError);
594 Py_XDECREF(exmod);
595}
596#endif /* DYNAMIC_PYTHON */
597
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598static PyObject *BufferNew (buf_T *);
599static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200600static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200601static PyObject *LineToString(const char *);
602
603static PyTypeObject RangeType;
604
Bram Moolenaardb913952012-06-29 12:54:53 +0200605static int initialised = 0;
606#define PYINITIALISED initialised
607
Bram Moolenaardb913952012-06-29 12:54:53 +0200608#define DICTKEY_GET(err) \
609 if (!PyString_Check(keyObject)) \
610 { \
611 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
612 return err; \
613 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200614 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
615 return err;
616
Bram Moolenaardb913952012-06-29 12:54:53 +0200617#define DICTKEY_UNREF
618#define DICTKEY_DECL
619
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200620/*
621 * Include the code shared with if_python3.c
622 */
623#include "if_py_both.h"
624
625
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626/******************************************************
627 * Internal function prototypes.
628 */
629
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000630static PyInt RangeStart;
631static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
Bram Moolenaardb913952012-06-29 12:54:53 +0200633static PyObject *globals;
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635static void PythonIO_Flush(void);
636static int PythonIO_Init(void);
637static int PythonMod_Init(void);
638
639/* Utility functions for the vim/python interface
640 * ----------------------------------------------
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000643static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
646/******************************************************
647 * 1. Python interpreter main program.
648 */
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
651typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654#ifdef PY_CAN_RECURSE
655static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
656#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000657static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660/*
661 * Suspend a thread of the Python interpreter, other threads are allowed to
662 * run.
663 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000664 static void
665Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000667#ifdef PY_CAN_RECURSE
668 PyGILState_Release(pygilstate);
669#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672}
673
674/*
675 * Restore a thread of the Python interpreter, waits for other threads to
676 * block.
677 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000678 static void
679Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681#ifdef PY_CAN_RECURSE
682 pygilstate = PyGILState_Ensure();
683#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 PyEval_RestoreThread(saved_python_thread);
685 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000687}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 void
690python_end()
691{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000692 static int recurse = 0;
693
694 /* If a crash occurs while doing this, don't try again. */
695 if (recurse != 0)
696 return;
697
698 ++recurse;
699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000701 if (hinstPython && 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 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000707#else
708 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000709 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000710 Python_RestoreThread(); /* enter python */
711 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000714
715 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716}
717
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200718#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
719 int
720python_loaded()
721{
722 return (hinstPython != 0);
723}
724#endif
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 static int
727Python_Init(void)
728{
729 if (!initialised)
730 {
731#ifdef DYNAMIC_PYTHON
732 if (!python_enabled(TRUE))
733 {
734 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
735 goto fail;
736 }
737#endif
738
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100739#ifdef PYTHON_HOME
740 Py_SetPythonHome(PYTHON_HOME);
741#endif
742
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200743 init_structs();
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#if !defined(MACOS) || defined(MACOS_X_UNIX)
746 Py_Initialize();
747#else
748 PyMac_Initialize();
749#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100750 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100751 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100752 * specific state (such as the system trace hook), will be lost
753 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#ifdef DYNAMIC_PYTHON
756 get_exceptions();
757#endif
758
759 if (PythonIO_Init())
760 goto fail;
761
762 if (PythonMod_Init())
763 goto fail;
764
Bram Moolenaardb913952012-06-29 12:54:53 +0200765 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
766
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000767 /* Remove the element from sys.path that was added because of our
768 * argv[0] value in PythonMod_Init(). Previously we used an empty
769 * string, but dependinding on the OS we then get an empty entry or
770 * the current directory in sys.path. */
771 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
772
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100773 /* lock is created and acquired in PyEval_InitThreads() and thread
774 * state is created in Py_Initialize()
775 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
776 * (python must have threads enabled!)
777 * so the following does both: unlock GIL and save thread state in TLS
778 * without deleting thread state
779 */
780 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 initialised = 1;
783 }
784
785 return 0;
786
787fail:
788 /* We call PythonIO_Flush() here to print any Python errors.
789 * This is OK, as it is possible to call this function even
790 * if PythonIO_Init() has not completed successfully (it will
791 * not do anything in this case).
792 */
793 PythonIO_Flush();
794 return -1;
795}
796
797/*
798 * External interface
799 */
800 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200801DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000803#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 static int recursive = 0;
805#endif
806#if defined(MACOS) && !defined(MACOS_X_UNIX)
807 GrafPtr oldPort;
808#endif
809#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
810 char *saved_locale;
811#endif
812
813#ifndef PY_CAN_RECURSE
814 if (recursive)
815 {
816 EMSG(_("E659: Cannot invoke Python recursively"));
817 return;
818 }
819 ++recursive;
820#endif
821
822#if defined(MACOS) && !defined(MACOS_X_UNIX)
823 GetPort(&oldPort);
824 /* Check if the Python library is available */
825 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
826 goto theend;
827#endif
828 if (Python_Init())
829 goto theend;
830
Bram Moolenaardb913952012-06-29 12:54:53 +0200831 if (rettv == NULL)
832 {
833 RangeStart = eap->line1;
834 RangeEnd = eap->line2;
835 }
836 else
837 {
838 RangeStart = (PyInt) curwin->w_cursor.lnum;
839 RangeEnd = RangeStart;
840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 Python_Release_Vim(); /* leave vim */
842
843#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
844 /* Python only works properly when the LC_NUMERIC locale is "C". */
845 saved_locale = setlocale(LC_NUMERIC, NULL);
846 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
847 saved_locale = NULL;
848 else
849 {
850 /* Need to make a copy, value may change when setting new locale. */
851 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
852 (void)setlocale(LC_NUMERIC, "C");
853 }
854#endif
855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaardb913952012-06-29 12:54:53 +0200858 if (rettv == NULL)
859 PyRun_SimpleString((char *)(cmd));
860 else
861 {
862 PyObject *r;
863
864 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
865 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100866 {
867 if (PyErr_Occurred() && !msg_silent)
868 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200869 EMSG(_("E858: Eval did not return a valid python object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100870 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200871 else
872 {
873 if (ConvertFromPyObject(r, rettv) == -1)
874 EMSG(_("E859: Failed to convert returned python object to vim value"));
875 Py_DECREF(r);
876 }
877 PyErr_Clear();
878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
883 if (saved_locale != NULL)
884 {
885 (void)setlocale(LC_NUMERIC, saved_locale);
886 vim_free(saved_locale);
887 }
888#endif
889
890 Python_Lock_Vim(); /* enter vim */
891 PythonIO_Flush();
892#if defined(MACOS) && !defined(MACOS_X_UNIX)
893 SetPort(oldPort);
894#endif
895
896theend:
897#ifndef PY_CAN_RECURSE
898 --recursive;
899#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200900 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901}
902
903/*
904 * ":python"
905 */
906 void
907ex_python(exarg_T *eap)
908{
909 char_u *script;
910
911 script = script_get(eap, eap->arg);
912 if (!eap->skip)
913 {
914 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200915 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200917 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919 vim_free(script);
920}
921
922#define BUFFER_SIZE 1024
923
924/*
925 * ":pyfile"
926 */
927 void
928ex_pyfile(exarg_T *eap)
929{
930 static char buffer[BUFFER_SIZE];
931 const char *file = (char *)eap->arg;
932 char *p;
933
934 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
935 * stdio file pointer, but Vim and the Python DLL are compiled with
936 * different options under Windows, meaning that stdio pointers aren't
937 * compatible between the two. Yuk.
938 *
939 * Put the string "execfile('file')" into buffer. But, we need to
940 * escape any backslashes or single quotes in the file name, so that
941 * Python won't mangle the file name.
942 */
943 strcpy(buffer, "execfile('");
944 p = buffer + 10; /* size of "execfile('" */
945
946 while (*file && p < buffer + (BUFFER_SIZE - 3))
947 {
948 if (*file == '\\' || *file == '\'')
949 *p++ = '\\';
950 *p++ = *file++;
951 }
952
953 /* If we didn't finish the file name, we hit a buffer overflow */
954 if (*file != '\0')
955 return;
956
957 /* Put in the terminating "')" and a null */
958 *p++ = '\'';
959 *p++ = ')';
960 *p++ = '\0';
961
962 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200963 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/******************************************************
967 * 2. Python output stream: writes output via [e]msg().
968 */
969
970/* Implementation functions
971 */
972
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 static PyObject *
974OutputGetattr(PyObject *self, char *name)
975{
976 if (strcmp(name, "softspace") == 0)
977 return PyInt_FromLong(((OutputObject *)(self))->softspace);
978
979 return Py_FindMethod(OutputMethods, self, name);
980}
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/***************/
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 static int
985PythonIO_Init(void)
986{
987 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100988 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200990 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991}
992
993/******************************************************
994 * 3. Implementation of the Vim module for Python
995 */
996
Bram Moolenaardb913952012-06-29 12:54:53 +0200997static PyObject *ConvertToPyObject(typval_T *);
998static int ConvertFromPyObject(PyObject *, typval_T *);
999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000/* Window type - Implementation functions
1001 * --------------------------------------
1002 */
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006static void WindowDestructor(PyObject *);
1007static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
1009/* Buffer type - Implementation functions
1010 * --------------------------------------
1011 */
1012
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015static void BufferDestructor(PyObject *);
1016static PyObject *BufferGetattr(PyObject *, char *);
1017static PyObject *BufferRepr(PyObject *);
1018
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001019static PyInt BufferLength(PyObject *);
1020static PyObject *BufferItem(PyObject *, PyInt);
1021static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
1022static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1023static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025/* Line range type - Implementation functions
1026 * --------------------------------------
1027 */
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1030
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001031static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1032static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/* Current objects type - Implementation functions
1035 * -----------------------------------------------
1036 */
1037
1038static PyObject *CurrentGetattr(PyObject *, char *);
1039static int CurrentSetattr(PyObject *, char *, PyObject *);
1040
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001042 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001043 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1044 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001045 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1046 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1047 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
1048 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049};
1050
1051static PyTypeObject BufferType = {
1052 PyObject_HEAD_INIT(0)
1053 0,
1054 "buffer",
1055 sizeof(BufferObject),
1056 0,
1057
1058 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
1059 (printfunc) 0, /* tp_print, print x */
1060 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
1061 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1062 (cmpfunc) 0, /* tp_compare, x>y */
1063 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
1064
1065 0, /* as number */
1066 &BufferAsSeq, /* as sequence */
1067 0, /* as mapping */
1068
1069 (hashfunc) 0, /* tp_hash, dict(x) */
1070 (ternaryfunc) 0, /* tp_call, x() */
1071 (reprfunc) 0, /* tp_str, str(x) */
1072};
1073
1074/* Buffer object - Implementation
1075 */
1076
1077 static PyObject *
1078BufferNew(buf_T *buf)
1079{
1080 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001081 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 * then we can get at it in buf_freeall() in vim. We then
1083 * need to create only ONE Python object per buffer - if
1084 * we try to create a second, just INCREF the existing one
1085 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001086 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 * Question: what to do on a buf_freeall(). We'll probably
1088 * have to either delete the Python object (DECREF it to
1089 * zero - a bad idea, as it leaves dangling refs!) or
1090 * set the buf_T * value to an invalid value (-1?), which
1091 * means we need checks in all access functions... Bah.
1092 */
1093
1094 BufferObject *self;
1095
Bram Moolenaare344bea2005-09-01 20:46:49 +00001096 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001098 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 Py_INCREF(self);
1100 }
1101 else
1102 {
1103 self = PyObject_NEW(BufferObject, &BufferType);
1104 if (self == NULL)
1105 return NULL;
1106 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001107 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109
1110 return (PyObject *)(self);
1111}
1112
1113 static void
1114BufferDestructor(PyObject *self)
1115{
1116 BufferObject *this = (BufferObject *)(self);
1117
1118 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001119 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
Bram Moolenaar658ada62006-10-03 13:02:36 +00001121 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122}
1123
1124 static PyObject *
1125BufferGetattr(PyObject *self, char *name)
1126{
1127 BufferObject *this = (BufferObject *)(self);
1128
1129 if (CheckBuffer(this))
1130 return NULL;
1131
1132 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001133 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001135 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else if (strcmp(name,"__members__") == 0)
1137 return Py_BuildValue("[ss]", "name", "number");
1138 else
1139 return Py_FindMethod(BufferMethods, self, name);
1140}
1141
1142 static PyObject *
1143BufferRepr(PyObject *self)
1144{
Bram Moolenaar555b2802005-05-19 21:08:39 +00001145 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 BufferObject *this = (BufferObject *)(self);
1147
1148 if (this->buf == INVALID_BUFFER_VALUE)
1149 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001150 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return PyString_FromString(repr);
1152 }
1153 else
1154 {
1155 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001156 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
1158 if (name == NULL)
1159 name = "";
1160 len = strlen(name);
1161
1162 if (len > 35)
1163 name = name + (35 - len);
1164
Bram Moolenaar555b2802005-05-19 21:08:39 +00001165 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 return PyString_FromString(repr);
1168 }
1169}
1170
1171/******************/
1172
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001173 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174BufferLength(PyObject *self)
1175{
1176 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
1177 if (CheckBuffer((BufferObject *)(self)))
1178 return -1; /* ??? */
1179
1180 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
1181}
1182
1183 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001184BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185{
1186 return RBItem((BufferObject *)(self), n, 1,
1187 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1188}
1189
1190 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001191BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192{
1193 return RBSlice((BufferObject *)(self), lo, hi, 1,
1194 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
1195}
1196
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001197 static PyInt
1198BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001200 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001201 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 NULL);
1203}
1204
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001205 static PyInt
1206BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001208 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001209 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 NULL);
1211}
1212
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001214 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001216 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1217 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1218 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1219 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1220 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221};
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223/* Line range object - Implementation
1224 */
1225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 static void
1227RangeDestructor(PyObject *self)
1228{
1229 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001230 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231}
1232
1233 static PyObject *
1234RangeGetattr(PyObject *self, char *name)
1235{
1236 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001237 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001239 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 else
1241 return Py_FindMethod(RangeMethods, self, name);
1242}
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244/****************/
1245
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001246 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001247RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001249 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 ((RangeObject *)(self))->start,
1251 ((RangeObject *)(self))->end,
1252 &((RangeObject *)(self))->end);
1253}
1254
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001255 static PyInt
1256RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001258 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 ((RangeObject *)(self))->start,
1260 ((RangeObject *)(self))->end,
1261 &((RangeObject *)(self))->end);
1262}
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264/* Buffer list object - Definitions
1265 */
1266
1267typedef struct
1268{
1269 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001270} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271
1272static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001273 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001275 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1276 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1277 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1278 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1279 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280};
1281
1282static PyTypeObject BufListType = {
1283 PyObject_HEAD_INIT(0)
1284 0,
1285 "buffer list",
1286 sizeof(BufListObject),
1287 0,
1288
1289 (destructor) 0, /* tp_dealloc, refcount==0 */
1290 (printfunc) 0, /* tp_print, print x */
1291 (getattrfunc) 0, /* tp_getattr, x.attr */
1292 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1293 (cmpfunc) 0, /* tp_compare, x>y */
1294 (reprfunc) 0, /* tp_repr, `x`, print x */
1295
1296 0, /* as number */
1297 &BufListAsSeq, /* as sequence */
1298 0, /* as mapping */
1299
1300 (hashfunc) 0, /* tp_hash, dict(x) */
1301 (ternaryfunc) 0, /* tp_call, x() */
1302 (reprfunc) 0, /* tp_str, str(x) */
1303};
1304
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305/* Window object - Definitions
1306 */
1307
1308static struct PyMethodDef WindowMethods[] = {
1309 /* name, function, calling, documentation */
1310 { NULL, NULL, 0, NULL }
1311};
1312
1313static PyTypeObject WindowType = {
1314 PyObject_HEAD_INIT(0)
1315 0,
1316 "window",
1317 sizeof(WindowObject),
1318 0,
1319
1320 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1321 (printfunc) 0, /* tp_print, print x */
1322 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1323 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1324 (cmpfunc) 0, /* tp_compare, x>y */
1325 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1326
1327 0, /* as number */
1328 0, /* as sequence */
1329 0, /* as mapping */
1330
1331 (hashfunc) 0, /* tp_hash, dict(x) */
1332 (ternaryfunc) 0, /* tp_call, x() */
1333 (reprfunc) 0, /* tp_str, str(x) */
1334};
1335
1336/* Window object - Implementation
1337 */
1338
1339 static PyObject *
1340WindowNew(win_T *win)
1341{
1342 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001343 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 * then we can get at it in win_free() in vim. We then
1345 * need to create only ONE Python object per window - if
1346 * we try to create a second, just INCREF the existing one
1347 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001348 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 * On a win_free() we set the Python object's win_T* field
1350 * to an invalid value. We trap all uses of a window
1351 * object, and reject them if the win_T* field is invalid.
1352 */
1353
1354 WindowObject *self;
1355
Bram Moolenaare344bea2005-09-01 20:46:49 +00001356 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001358 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 Py_INCREF(self);
1360 }
1361 else
1362 {
1363 self = PyObject_NEW(WindowObject, &WindowType);
1364 if (self == NULL)
1365 return NULL;
1366 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001367 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369
1370 return (PyObject *)(self);
1371}
1372
1373 static void
1374WindowDestructor(PyObject *self)
1375{
1376 WindowObject *this = (WindowObject *)(self);
1377
1378 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001379 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaar658ada62006-10-03 13:02:36 +00001381 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382}
1383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 static PyObject *
1385WindowGetattr(PyObject *self, char *name)
1386{
1387 WindowObject *this = (WindowObject *)(self);
1388
1389 if (CheckWindow(this))
1390 return NULL;
1391
1392 if (strcmp(name, "buffer") == 0)
1393 return (PyObject *)BufferNew(this->win->w_buffer);
1394 else if (strcmp(name, "cursor") == 0)
1395 {
1396 pos_T *pos = &this->win->w_cursor;
1397
1398 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1399 }
1400 else if (strcmp(name, "height") == 0)
1401 return Py_BuildValue("l", (long)(this->win->w_height));
1402#ifdef FEAT_VERTSPLIT
1403 else if (strcmp(name, "width") == 0)
1404 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1405#endif
1406 else if (strcmp(name,"__members__") == 0)
1407 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1408 else
1409 return Py_FindMethod(WindowMethods, self, name);
1410}
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412/* Window list object - Definitions
1413 */
1414
1415typedef struct
1416{
1417 PyObject_HEAD
1418}
1419WinListObject;
1420
1421static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001422 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001424 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1425 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1426 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1427 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1428 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429};
1430
1431static PyTypeObject WinListType = {
1432 PyObject_HEAD_INIT(0)
1433 0,
1434 "window list",
1435 sizeof(WinListObject),
1436 0,
1437
1438 (destructor) 0, /* tp_dealloc, refcount==0 */
1439 (printfunc) 0, /* tp_print, print x */
1440 (getattrfunc) 0, /* tp_getattr, x.attr */
1441 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1442 (cmpfunc) 0, /* tp_compare, x>y */
1443 (reprfunc) 0, /* tp_repr, `x`, print x */
1444
1445 0, /* as number */
1446 &WinListAsSeq, /* as sequence */
1447 0, /* as mapping */
1448
1449 (hashfunc) 0, /* tp_hash, dict(x) */
1450 (ternaryfunc) 0, /* tp_call, x() */
1451 (reprfunc) 0, /* tp_str, str(x) */
1452};
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454/* Current items object - Definitions
1455 */
1456
1457typedef struct
1458{
1459 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001460} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462static PyTypeObject CurrentType = {
1463 PyObject_HEAD_INIT(0)
1464 0,
1465 "current data",
1466 sizeof(CurrentObject),
1467 0,
1468
1469 (destructor) 0, /* tp_dealloc, refcount==0 */
1470 (printfunc) 0, /* tp_print, print x */
1471 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1472 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1473 (cmpfunc) 0, /* tp_compare, x>y */
1474 (reprfunc) 0, /* tp_repr, `x`, print x */
1475
1476 0, /* as number */
1477 0, /* as sequence */
1478 0, /* as mapping */
1479
1480 (hashfunc) 0, /* tp_hash, dict(x) */
1481 (ternaryfunc) 0, /* tp_call, x() */
1482 (reprfunc) 0, /* tp_str, str(x) */
1483};
1484
1485/* Current items object - Implementation
1486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001488CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 if (strcmp(name, "buffer") == 0)
1491 return (PyObject *)BufferNew(curbuf);
1492 else if (strcmp(name, "window") == 0)
1493 return (PyObject *)WindowNew(curwin);
1494 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001495 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 else if (strcmp(name, "range") == 0)
1497 return RangeNew(curbuf, RangeStart, RangeEnd);
1498 else if (strcmp(name,"__members__") == 0)
1499 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1500 else
1501 {
1502 PyErr_SetString(PyExc_AttributeError, name);
1503 return NULL;
1504 }
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001508CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 if (strcmp(name, "line") == 0)
1511 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001512 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return -1;
1514
1515 return 0;
1516 }
1517 else
1518 {
1519 PyErr_SetString(PyExc_AttributeError, name);
1520 return -1;
1521 }
1522}
1523
1524/* External interface
1525 */
1526
1527 void
1528python_buffer_free(buf_T *buf)
1529{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001530 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001532 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001534 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536}
1537
1538#if defined(FEAT_WINDOWS) || defined(PROTO)
1539 void
1540python_window_free(win_T *win)
1541{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001542 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001544 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001546 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548}
1549#endif
1550
1551static BufListObject TheBufferList =
1552{
1553 PyObject_HEAD_INIT(&BufListType)
1554};
1555
1556static WinListObject TheWindowList =
1557{
1558 PyObject_HEAD_INIT(&WinListType)
1559};
1560
1561static CurrentObject TheCurrent =
1562{
1563 PyObject_HEAD_INIT(&CurrentType)
1564};
1565
1566 static int
1567PythonMod_Init(void)
1568{
1569 PyObject *mod;
1570 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001571 /* The special value is removed from sys.path in Python_Init(). */
1572 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
1574 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001575 PyType_Ready(&BufferType);
1576 PyType_Ready(&RangeType);
1577 PyType_Ready(&WindowType);
1578 PyType_Ready(&BufListType);
1579 PyType_Ready(&WinListType);
1580 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 /* Set sys.argv[] to avoid a crash in warn(). */
1583 PySys_SetArgv(1, argv);
1584
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001585 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 dict = PyModule_GetDict(mod);
1587
1588 VimError = Py_BuildValue("s", "vim.error");
1589
1590 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001591 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1592 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1593 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001594 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1595 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1596 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1597 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 if (PyErr_Occurred())
1600 return -1;
1601
1602 return 0;
1603}
1604
1605/*************************************************************************
1606 * 4. Utility functions for handling the interface between Vim and Python.
1607 */
1608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609/* Convert a Vim line into a Python string.
1610 * All internal newlines are replaced by null characters.
1611 *
1612 * On errors, the Python exception data is set, and NULL is returned.
1613 */
1614 static PyObject *
1615LineToString(const char *str)
1616{
1617 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001618 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 char *p;
1620
1621 /* Allocate an Python string object, with uninitialised contents. We
1622 * must do it this way, so that we can modify the string in place
1623 * later. See the Python source, Objects/stringobject.c for details.
1624 */
1625 result = PyString_FromStringAndSize(NULL, len);
1626 if (result == NULL)
1627 return NULL;
1628
1629 p = PyString_AsString(result);
1630
1631 while (*str)
1632 {
1633 if (*str == '\n')
1634 *p = '\0';
1635 else
1636 *p = *str;
1637
1638 ++p;
1639 ++str;
1640 }
1641
1642 return result;
1643}
1644
Bram Moolenaardb913952012-06-29 12:54:53 +02001645static void DictionaryDestructor(PyObject *);
1646static PyObject *DictionaryGetattr(PyObject *, char*);
1647
1648static PyMappingMethods DictionaryAsMapping = {
1649 (PyInquiry) DictionaryLength,
1650 (binaryfunc) DictionaryItem,
1651 (objobjargproc) DictionaryAssItem,
1652};
1653
1654static PyTypeObject DictionaryType = {
1655 PyObject_HEAD_INIT(0)
1656 0,
1657 "vimdictionary",
1658 sizeof(DictionaryObject),
1659 0,
1660
1661 (destructor) DictionaryDestructor,
1662 (printfunc) 0,
1663 (getattrfunc) DictionaryGetattr,
Bram Moolenaar66b79852012-09-21 14:00:35 +02001664 (setattrfunc) DictionarySetattr,
Bram Moolenaardb913952012-06-29 12:54:53 +02001665 (cmpfunc) 0,
1666 (reprfunc) 0,
1667
1668 0, /* as number */
1669 0, /* as sequence */
1670 &DictionaryAsMapping, /* as mapping */
1671
1672 (hashfunc) 0,
1673 (ternaryfunc) 0,
1674 (reprfunc) 0,
1675};
1676
1677 static void
1678DictionaryDestructor(PyObject *self)
1679{
1680 DictionaryObject *this = ((DictionaryObject *) (self));
1681
1682 pyll_remove(&this->ref, &lastdict);
1683 dict_unref(this->dict);
1684
1685 Py_DECREF(self);
1686}
1687
1688 static PyObject *
1689DictionaryGetattr(PyObject *self, char *name)
1690{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001691 DictionaryObject *this = ((DictionaryObject *) (self));
1692
1693 if (strcmp(name, "locked") == 0)
1694 return PyInt_FromLong(this->dict->dv_lock);
1695 else if (strcmp(name, "scope") == 0)
1696 return PyInt_FromLong(this->dict->dv_scope);
1697
Bram Moolenaardb913952012-06-29 12:54:53 +02001698 return Py_FindMethod(DictionaryMethods, self, name);
1699}
1700
1701static void ListDestructor(PyObject *);
1702static PyObject *ListGetattr(PyObject *, char *);
1703
1704static PySequenceMethods ListAsSeq = {
1705 (PyInquiry) ListLength,
1706 (binaryfunc) 0,
1707 (PyIntArgFunc) 0,
1708 (PyIntArgFunc) ListItem,
1709 (PyIntIntArgFunc) ListSlice,
1710 (PyIntObjArgProc) ListAssItem,
1711 (PyIntIntObjArgProc) ListAssSlice,
1712 (objobjproc) 0,
1713#if PY_MAJOR_VERSION >= 2
1714 (binaryfunc) ListConcatInPlace,
1715 0,
1716#endif
1717};
1718
1719static PyTypeObject ListType = {
1720 PyObject_HEAD_INIT(0)
1721 0,
1722 "vimlist",
1723 sizeof(ListObject),
1724 0,
1725
1726 (destructor) ListDestructor,
1727 (printfunc) 0,
1728 (getattrfunc) ListGetattr,
Bram Moolenaar66b79852012-09-21 14:00:35 +02001729 (setattrfunc) ListSetattr,
Bram Moolenaardb913952012-06-29 12:54:53 +02001730 (cmpfunc) 0,
1731 (reprfunc) 0,
1732
1733 0, /* as number */
1734 &ListAsSeq, /* as sequence */
1735 0, /* as mapping */
1736
1737 (hashfunc) 0,
1738 (ternaryfunc) 0,
1739 (reprfunc) 0,
1740};
1741
1742 static void
1743ListDestructor(PyObject *self)
1744{
1745 ListObject *this = ((ListObject *) (self));
1746
1747 pyll_remove(&this->ref, &lastlist);
1748 list_unref(this->list);
1749
1750 Py_DECREF(self);
1751}
1752
1753 static PyObject *
1754ListGetattr(PyObject *self, char *name)
1755{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001756 if (strcmp(name, "locked") == 0)
1757 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1758
Bram Moolenaardb913952012-06-29 12:54:53 +02001759 return Py_FindMethod(ListMethods, self, name);
1760}
1761
1762static void FunctionDestructor(PyObject *);
1763static PyObject *FunctionGetattr(PyObject *, char *);
1764
1765static PyTypeObject FunctionType = {
1766 PyObject_HEAD_INIT(0)
1767 0,
1768 "vimfunction",
1769 sizeof(FunctionObject),
1770 0,
1771
1772 (destructor) FunctionDestructor,
1773 (printfunc) 0,
1774 (getattrfunc) FunctionGetattr,
1775 (setattrfunc) 0,
1776 (cmpfunc) 0,
1777 (reprfunc) 0,
1778
1779 0, /* as number */
1780 0, /* as sequence */
1781 0, /* as mapping */
1782
1783 (hashfunc) 0,
1784 (ternaryfunc) FunctionCall,
1785 (reprfunc) 0,
1786};
1787
1788 static void
1789FunctionDestructor(PyObject *self)
1790{
1791 FunctionObject *this = (FunctionObject *) (self);
1792
1793 func_unref(this->name);
1794 PyMem_Del(this->name);
1795
1796 Py_DECREF(self);
1797}
1798
1799 static PyObject *
1800FunctionGetattr(PyObject *self, char *name)
1801{
1802 FunctionObject *this = (FunctionObject *)(self);
1803
1804 if (strcmp(name, "name") == 0)
1805 return PyString_FromString((char *)(this->name));
1806 else
1807 return Py_FindMethod(FunctionMethods, self, name);
1808}
1809
1810 void
1811do_pyeval (char_u *str, typval_T *rettv)
1812{
1813 DoPythonCommand(NULL, (char *) str, rettv);
1814 switch(rettv->v_type)
1815 {
1816 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1817 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1818 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001819 case VAR_UNKNOWN:
1820 rettv->v_type = VAR_NUMBER;
1821 rettv->vval.v_number = 0;
1822 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001823 }
1824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826/* Don't generate a prototype for the next function, it generates an error on
1827 * newer Python versions. */
1828#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1829
1830 char *
1831Py_GetProgramName(void)
1832{
1833 return "vim";
1834}
1835#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001836
Bram Moolenaardb913952012-06-29 12:54:53 +02001837 void
1838set_ref_in_python (int copyID)
1839{
1840 set_ref_in_py(copyID);
1841}
1842
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001843 static void
1844init_structs(void)
1845{
1846 vim_memset(&OutputType, 0, sizeof(OutputType));
1847 OutputType.tp_name = "message";
1848 OutputType.tp_basicsize = sizeof(OutputObject);
1849 OutputType.tp_getattr = OutputGetattr;
1850 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001851
1852 vim_memset(&RangeType, 0, sizeof(RangeType));
1853 RangeType.tp_name = "range";
1854 RangeType.tp_basicsize = sizeof(RangeObject);
1855 RangeType.tp_dealloc = RangeDestructor;
1856 RangeType.tp_getattr = RangeGetattr;
1857 RangeType.tp_repr = RangeRepr;
1858 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001859}