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