blob: 8a459fd43ac53669e70dacb2d10095b456628278 [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
Bram Moolenaar335e0b62013-04-24 13:47:45 +020062#define PyBytes_Check PyString_Check
Bram Moolenaardb913952012-06-29 12:54:53 +020063
Bram Moolenaar19e60942011-06-19 00:27:51 +020064/* No-op conversion functions, use with care! */
65#define PyString_AsBytes(obj) (obj)
66#define PyString_FreeBytes(obj)
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#if !defined(FEAT_PYTHON) && defined(PROTO)
69/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000070# define PyObject Py_ssize_t
71# define PyThreadState Py_ssize_t
72# define PyTypeObject Py_ssize_t
73struct PyMethodDef { Py_ssize_t a; };
74# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif
76
Bram Moolenaar2afa3232012-06-29 16:28:28 +020077#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
78# define PY_USE_CAPSULE
79#endif
80
Bram Moolenaar2c45e942008-06-04 11:35:26 +000081#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
82# define PyInt Py_ssize_t
83# define PyInquiry lenfunc
84# define PyIntArgFunc ssizeargfunc
85# define PyIntIntArgFunc ssizessizeargfunc
86# define PyIntObjArgProc ssizeobjargproc
87# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000088# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000089#else
90# define PyInt int
Bram Moolenaar4d1da492013-04-24 13:39:15 +020091# define lenfunc inquiry
Bram Moolenaar2c45e942008-06-04 11:35:26 +000092# define PyInquiry inquiry
93# define PyIntArgFunc intargfunc
94# define PyIntIntArgFunc intintargfunc
95# define PyIntObjArgProc intobjargproc
96# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000097# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000098#endif
99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100/* Parser flags */
101#define single_input 256
102#define file_input 257
103#define eval_input 258
104
105#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
106 /* Python 2.3: can invoke ":python" recursively. */
107# define PY_CAN_RECURSE
108#endif
109
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# if defined(DYNAMIC_PYTHON) || defined(PROTO)
111# ifndef DYNAMIC_PYTHON
112# define HINSTANCE long_u /* for generating prototypes */
113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200115# ifndef WIN3264
116# include <dlfcn.h>
117# define FARPROC void*
118# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100119# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200120# define load_dll(n) dlopen((n), RTLD_LAZY)
121# else
122# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
123# endif
124# define close_dll dlclose
125# define symbol_from_dll dlsym
126# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200127# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200128# define close_dll FreeLibrary
129# define symbol_from_dll GetProcAddress
130# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200131
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000132/* This makes if_python.c compile without warnings against Python 2.5
133 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200134# undef PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200135# undef PyRun_String
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200136# undef PyArg_Parse
137# undef PyArg_ParseTuple
138# undef Py_BuildValue
139# undef Py_InitModule4
140# undef Py_InitModule4_64
Bram Moolenaardb913952012-06-29 12:54:53 +0200141# undef PyObject_CallMethod
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Wrapper defines
145 */
146# define PyArg_Parse dll_PyArg_Parse
147# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200148# define PyMem_Free dll_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200149# define PyMem_Malloc dll_PyMem_Malloc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define PyDict_SetItemString dll_PyDict_SetItemString
151# define PyErr_BadArgument dll_PyErr_BadArgument
152# define PyErr_Clear dll_PyErr_Clear
Bram Moolenaar4d369872013-02-20 16:09:43 +0100153# define PyErr_PrintEx dll_PyErr_PrintEx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154# define PyErr_NoMemory dll_PyErr_NoMemory
155# define PyErr_Occurred dll_PyErr_Occurred
156# define PyErr_SetNone dll_PyErr_SetNone
157# define PyErr_SetString dll_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200158# define PyErr_SetObject dll_PyErr_SetObject
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159# define PyEval_InitThreads dll_PyEval_InitThreads
160# define PyEval_RestoreThread dll_PyEval_RestoreThread
161# define PyEval_SaveThread dll_PyEval_SaveThread
162# ifdef PY_CAN_RECURSE
163# define PyGILState_Ensure dll_PyGILState_Ensure
164# define PyGILState_Release dll_PyGILState_Release
165# endif
166# define PyInt_AsLong dll_PyInt_AsLong
167# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200168# define PyLong_AsLong dll_PyLong_AsLong
169# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar66b79852012-09-21 14:00:35 +0200170# define PyBool_Type (*dll_PyBool_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200172# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000174# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175# define PyList_New dll_PyList_New
176# define PyList_SetItem dll_PyList_SetItem
177# define PyList_Size dll_PyList_Size
178# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200179# define PySequence_Check dll_PySequence_Check
180# define PySequence_Size dll_PySequence_Size
181# define PySequence_GetItem dll_PySequence_GetItem
182# define PyTuple_Size dll_PyTuple_Size
183# define PyTuple_GetItem dll_PyTuple_GetItem
184# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000186# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200188# define PyDict_Next dll_PyDict_Next
189# ifdef PyMapping_Items
190# define PY_NO_MAPPING_ITEMS
191# else
192# define PyMapping_Items dll_PyMapping_Items
193# endif
194# define PyObject_CallMethod dll_PyObject_CallMethod
195# define PyMapping_Check dll_PyMapping_Check
196# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# define PyModule_GetDict dll_PyModule_GetDict
198# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200199# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200201# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# define PyString_FromString dll_PyString_FromString
203# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
204# define PyString_Size dll_PyString_Size
205# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200206# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200207# undef PyUnicode_AsEncodedString
208# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200209# define PyFloat_AsDouble dll_PyFloat_AsDouble
210# define PyFloat_FromDouble dll_PyFloat_FromDouble
211# define PyFloat_Type (*dll_PyFloat_Type)
212# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213# define PySys_SetObject dll_PySys_SetObject
214# define PySys_SetArgv dll_PySys_SetArgv
215# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100216# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define Py_BuildValue dll_Py_BuildValue
218# define Py_FindMethod dll_Py_FindMethod
219# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100220# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000222# define Py_Finalize dll_Py_Finalize
223# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200225# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
226# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200229# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
230# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200232# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200233# define PyObject_IsTrue dll_PyObject_IsTrue
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
235# define PyType_IsSubtype dll_PyType_IsSubtype
236# endif
237# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
238# define PyObject_Malloc dll_PyObject_Malloc
239# define PyObject_Free dll_PyObject_Free
240# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200241# ifdef PY_USE_CAPSULE
242# define PyCapsule_New dll_PyCapsule_New
243# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
244# else
245# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
246# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248
249/*
250 * Pointers for dynamic link
251 */
252static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
253static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200254static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200255static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
257static int(*dll_PyErr_BadArgument)(void);
258static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100259static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static PyObject*(*dll_PyErr_NoMemory)(void);
261static PyObject*(*dll_PyErr_Occurred)(void);
262static void(*dll_PyErr_SetNone)(PyObject *);
263static void(*dll_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200264static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265static void(*dll_PyEval_InitThreads)(void);
266static void(*dll_PyEval_RestoreThread)(PyThreadState *);
267static PyThreadState*(*dll_PyEval_SaveThread)(void);
268# ifdef PY_CAN_RECURSE
269static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
270static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272static long(*dll_PyInt_AsLong)(PyObject *);
273static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200274static long(*dll_PyLong_AsLong)(PyObject *);
275static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200276static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200278static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000279static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000280static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000281static PyObject*(*dll_PyList_New)(PyInt size);
282static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
283static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200285static int (*dll_PySequence_Check)(PyObject *);
286static PyInt(*dll_PySequence_Size)(PyObject *);
287static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
288static PyInt(*dll_PyTuple_Size)(PyObject *);
289static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
290static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000292static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200294static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
295# ifndef PY_NO_MAPPING_ITEMS
296static PyObject* (*dll_PyMapping_Items)(PyObject *);
297# endif
298static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
299static int (*dll_PyMapping_Check)(PyObject *);
300static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static PyObject*(*dll_PyModule_GetDict)(PyObject *);
302static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200303static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200305static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000307static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
308static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200310static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200311static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200312static double(*dll_PyFloat_AsDouble)(PyObject *);
313static PyObject*(*dll_PyFloat_FromDouble)(double);
314static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static int(*dll_PySys_SetObject)(char *, PyObject *);
316static int(*dll_PySys_SetArgv)(int, char **);
317static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100318static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319static PyObject*(*dll_Py_BuildValue)(char *, ...);
320static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
321static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200322static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100323static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000325static void(*dll_Py_Finalize)(void);
326static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
328static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200329static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200330static int (*dll_PyObject_IsTrue)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200331# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200332static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200335static PyObject* _Py_ZeroStruct;
336static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
338static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
339# endif
340# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
341static void* (*dll_PyObject_Malloc)(size_t);
342static void (*dll_PyObject_Free)(void*);
343# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200344# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200345static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
346static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200347# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200348static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
349static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200350# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352static HINSTANCE hinstPython = 0; /* Instance of python.dll */
353
354/* Imported exception objects */
355static PyObject *imp_PyExc_AttributeError;
356static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200357static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358static PyObject *imp_PyExc_KeyboardInterrupt;
359static PyObject *imp_PyExc_TypeError;
360static PyObject *imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200361static PyObject *imp_PyExc_RuntimeError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363# define PyExc_AttributeError imp_PyExc_AttributeError
364# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200365# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
367# define PyExc_TypeError imp_PyExc_TypeError
368# define PyExc_ValueError imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200369# define PyExc_RuntimeError imp_PyExc_RuntimeError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
371/*
372 * Table of name to function pointer of python.
373 */
374# define PYTHON_PROC FARPROC
375static struct
376{
377 char *name;
378 PYTHON_PROC *ptr;
379} python_funcname_table[] =
380{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200381#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
383 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200384 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
385#else
386 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
387 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
388 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
389#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200390 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200391 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
393 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
394 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100395 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
397 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
398 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
399 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200400 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
402 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
403 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
404# ifdef PY_CAN_RECURSE
405 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
406 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
407# endif
408 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
409 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200410 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
411 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200412 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200414 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000416 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
418 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
419 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
420 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200421 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
422 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
423 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
424 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
425 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
426 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
428 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200429 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000430 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200431# ifndef PY_NO_MAPPING_ITEMS
432 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
433# endif
434 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
435 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
436 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
438 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200439 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200441 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
443 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
444 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
445 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200446 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200447 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
448 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
449 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
450 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
452 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
453 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100454 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200456# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
457 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000458 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
459# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000461# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100462 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000464 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
465 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
467 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200468 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200469 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
Bram Moolenaare7211222012-06-30 13:21:08 +0200470# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200471 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200474 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
475 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
477 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
478# endif
479# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
480 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
481 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
482# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200483# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200484 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
485 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200486# else
487 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
488 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {"", NULL},
491};
492
493/*
494 * Free python.dll
495 */
496 static void
497end_dynamic_python(void)
498{
499 if (hinstPython)
500 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200501 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 hinstPython = 0;
503 }
504}
505
506/*
507 * Load library and get all pointers.
508 * Parameter 'libname' provides name of DLL.
509 * Return OK or FAIL.
510 */
511 static int
512python_runtime_link_init(char *libname, int verbose)
513{
514 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200515 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100517#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200518 /* Can't have Python and Python3 loaded at the same time.
519 * It cause a crash, because RTLD_GLOBAL is needed for
520 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200521 if (python3_loaded())
522 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200523 if (verbose)
524 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200525 return FAIL;
526 }
527#endif
528
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 if (hinstPython)
530 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200531 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (!hinstPython)
533 {
534 if (verbose)
535 EMSG2(_(e_loadlib), libname);
536 return FAIL;
537 }
538
539 for (i = 0; python_funcname_table[i].ptr; ++i)
540 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 python_funcname_table[i].name)) == NULL)
543 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200544 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 hinstPython = 0;
546 if (verbose)
547 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
548 return FAIL;
549 }
550 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200551
552 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
553 * will be present in the library. */
554 ucs_as_encoded_string = symbol_from_dll(hinstPython,
555 "PyUnicodeUCS2_AsEncodedString");
556 if (ucs_as_encoded_string == NULL)
557 ucs_as_encoded_string = symbol_from_dll(hinstPython,
558 "PyUnicodeUCS4_AsEncodedString");
559 if (ucs_as_encoded_string != NULL)
560 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
561 else
562 {
563 close_dll(hinstPython);
564 hinstPython = 0;
565 if (verbose)
566 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
567 return FAIL;
568 }
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 return OK;
571}
572
573/*
574 * If python is enabled (there is installed python on Windows system) return
575 * TRUE, else FALSE.
576 */
577 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000578python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
581}
582
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200583/*
584 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 * DLL, as this can cause errors (importing data symbols is not reliable).
586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200588get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 PyObject *exmod = PyImport_ImportModule("exceptions");
591 PyObject *exdict = PyModule_GetDict(exmod);
592 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
593 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200594 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
596 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
597 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200598 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 Py_XINCREF(imp_PyExc_AttributeError);
600 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200601 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
603 Py_XINCREF(imp_PyExc_TypeError);
604 Py_XINCREF(imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200605 Py_XINCREF(imp_PyExc_RuntimeError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 Py_XDECREF(exmod);
607}
608#endif /* DYNAMIC_PYTHON */
609
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200610static PyObject *BufferNew (buf_T *);
611static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200612static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613static PyObject *LineToString(const char *);
614
Bram Moolenaardb913952012-06-29 12:54:53 +0200615static int initialised = 0;
616#define PYINITIALISED initialised
617
Bram Moolenaardb913952012-06-29 12:54:53 +0200618#define DICTKEY_GET(err) \
619 if (!PyString_Check(keyObject)) \
620 { \
621 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
622 return err; \
623 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200624 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
625 return err;
626
Bram Moolenaardb913952012-06-29 12:54:53 +0200627#define DICTKEY_UNREF
628#define DICTKEY_DECL
629
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200630#define DESTRUCTOR_FINISH(self) Py_DECREF(self);
631
Bram Moolenaar971db462013-05-12 18:44:48 +0200632#define WIN_PYTHON_REF(win) win->w_python_ref
633#define BUF_PYTHON_REF(buf) buf->b_python_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200634#define TAB_PYTHON_REF(tab) tab->tp_python_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200635
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200636static PyObject *OutputGetattr(PyObject *, char *);
637static PyObject *BufferGetattr(PyObject *, char *);
638static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200639static PyObject *TabPageGetattr(PyObject *, char *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200640static PyObject *RangeGetattr(PyObject *, char *);
641static PyObject *DictionaryGetattr(PyObject *, char*);
642static PyObject *ListGetattr(PyObject *, char *);
643static PyObject *FunctionGetattr(PyObject *, char *);
644
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200645/*
646 * Include the code shared with if_python3.c
647 */
648#include "if_py_both.h"
649
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651/******************************************************
652 * Internal function prototypes.
653 */
654
Bram Moolenaardb913952012-06-29 12:54:53 +0200655static PyObject *globals;
656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657static void PythonIO_Flush(void);
658static int PythonIO_Init(void);
659static int PythonMod_Init(void);
660
661/* Utility functions for the vim/python interface
662 * ----------------------------------------------
663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000665static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668/******************************************************
669 * 1. Python interpreter main program.
670 */
671
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
673typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000676#ifdef PY_CAN_RECURSE
677static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
678#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000679static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681
682/*
683 * Suspend a thread of the Python interpreter, other threads are allowed to
684 * run.
685 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000686 static void
687Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000689#ifdef PY_CAN_RECURSE
690 PyGILState_Release(pygilstate);
691#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696/*
697 * Restore a thread of the Python interpreter, waits for other threads to
698 * block.
699 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000700 static void
701Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000703#ifdef PY_CAN_RECURSE
704 pygilstate = PyGILState_Ensure();
705#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 PyEval_RestoreThread(saved_python_thread);
707 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000709}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 void
712python_end()
713{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000714 static int recurse = 0;
715
716 /* If a crash occurs while doing this, don't try again. */
717 if (recurse != 0)
718 return;
719
720 ++recurse;
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000723 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000724 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000725 Python_RestoreThread(); /* enter python */
726 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000729#else
730 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000731 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000732 Python_RestoreThread(); /* enter python */
733 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000736
737 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738}
739
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200740#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
741 int
742python_loaded()
743{
744 return (hinstPython != 0);
745}
746#endif
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static int
749Python_Init(void)
750{
751 if (!initialised)
752 {
753#ifdef DYNAMIC_PYTHON
754 if (!python_enabled(TRUE))
755 {
756 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
757 goto fail;
758 }
759#endif
760
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100761#ifdef PYTHON_HOME
762 Py_SetPythonHome(PYTHON_HOME);
763#endif
764
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200765 init_structs();
766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#if !defined(MACOS) || defined(MACOS_X_UNIX)
768 Py_Initialize();
769#else
770 PyMac_Initialize();
771#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100772 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100773 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100774 * specific state (such as the system trace hook), will be lost
775 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#ifdef DYNAMIC_PYTHON
778 get_exceptions();
779#endif
780
781 if (PythonIO_Init())
782 goto fail;
783
784 if (PythonMod_Init())
785 goto fail;
786
Bram Moolenaardb913952012-06-29 12:54:53 +0200787 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
788
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000789 /* Remove the element from sys.path that was added because of our
790 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200791 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000792 * the current directory in sys.path. */
793 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
794
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100795 /* lock is created and acquired in PyEval_InitThreads() and thread
796 * state is created in Py_Initialize()
797 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
798 * (python must have threads enabled!)
799 * so the following does both: unlock GIL and save thread state in TLS
800 * without deleting thread state
801 */
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200802#ifndef PY_CAN_RECURSE
803 saved_python_thread =
804#endif
805 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 initialised = 1;
808 }
809
810 return 0;
811
812fail:
813 /* We call PythonIO_Flush() here to print any Python errors.
814 * This is OK, as it is possible to call this function even
815 * if PythonIO_Init() has not completed successfully (it will
816 * not do anything in this case).
817 */
818 PythonIO_Flush();
819 return -1;
820}
821
822/*
823 * External interface
824 */
825 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200826DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000828#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 static int recursive = 0;
830#endif
831#if defined(MACOS) && !defined(MACOS_X_UNIX)
832 GrafPtr oldPort;
833#endif
834#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
835 char *saved_locale;
836#endif
837
838#ifndef PY_CAN_RECURSE
839 if (recursive)
840 {
841 EMSG(_("E659: Cannot invoke Python recursively"));
842 return;
843 }
844 ++recursive;
845#endif
846
847#if defined(MACOS) && !defined(MACOS_X_UNIX)
848 GetPort(&oldPort);
849 /* Check if the Python library is available */
850 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
851 goto theend;
852#endif
853 if (Python_Init())
854 goto theend;
855
Bram Moolenaardb913952012-06-29 12:54:53 +0200856 if (rettv == NULL)
857 {
858 RangeStart = eap->line1;
859 RangeEnd = eap->line2;
860 }
861 else
862 {
863 RangeStart = (PyInt) curwin->w_cursor.lnum;
864 RangeEnd = RangeStart;
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 Python_Release_Vim(); /* leave vim */
867
868#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
869 /* Python only works properly when the LC_NUMERIC locale is "C". */
870 saved_locale = setlocale(LC_NUMERIC, NULL);
871 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
872 saved_locale = NULL;
873 else
874 {
875 /* Need to make a copy, value may change when setting new locale. */
876 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
877 (void)setlocale(LC_NUMERIC, "C");
878 }
879#endif
880
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaardb913952012-06-29 12:54:53 +0200883 if (rettv == NULL)
884 PyRun_SimpleString((char *)(cmd));
885 else
886 {
887 PyObject *r;
888
889 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
890 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100891 {
892 if (PyErr_Occurred() && !msg_silent)
893 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200894 EMSG(_("E858: Eval did not return a valid python object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100895 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200896 else
897 {
898 if (ConvertFromPyObject(r, rettv) == -1)
899 EMSG(_("E859: Failed to convert returned python object to vim value"));
900 Py_DECREF(r);
901 }
902 PyErr_Clear();
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
908 if (saved_locale != NULL)
909 {
910 (void)setlocale(LC_NUMERIC, saved_locale);
911 vim_free(saved_locale);
912 }
913#endif
914
915 Python_Lock_Vim(); /* enter vim */
916 PythonIO_Flush();
917#if defined(MACOS) && !defined(MACOS_X_UNIX)
918 SetPort(oldPort);
919#endif
920
921theend:
922#ifndef PY_CAN_RECURSE
923 --recursive;
924#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200925 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
928/*
929 * ":python"
930 */
931 void
932ex_python(exarg_T *eap)
933{
934 char_u *script;
935
936 script = script_get(eap, eap->arg);
937 if (!eap->skip)
938 {
939 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200940 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200942 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944 vim_free(script);
945}
946
947#define BUFFER_SIZE 1024
948
949/*
950 * ":pyfile"
951 */
952 void
953ex_pyfile(exarg_T *eap)
954{
955 static char buffer[BUFFER_SIZE];
956 const char *file = (char *)eap->arg;
957 char *p;
958
959 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
960 * stdio file pointer, but Vim and the Python DLL are compiled with
961 * different options under Windows, meaning that stdio pointers aren't
962 * compatible between the two. Yuk.
963 *
964 * Put the string "execfile('file')" into buffer. But, we need to
965 * escape any backslashes or single quotes in the file name, so that
966 * Python won't mangle the file name.
967 */
968 strcpy(buffer, "execfile('");
969 p = buffer + 10; /* size of "execfile('" */
970
971 while (*file && p < buffer + (BUFFER_SIZE - 3))
972 {
973 if (*file == '\\' || *file == '\'')
974 *p++ = '\\';
975 *p++ = *file++;
976 }
977
978 /* If we didn't finish the file name, we hit a buffer overflow */
979 if (*file != '\0')
980 return;
981
982 /* Put in the terminating "')" and a null */
983 *p++ = '\'';
984 *p++ = ')';
985 *p++ = '\0';
986
987 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200988 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991/******************************************************
992 * 2. Python output stream: writes output via [e]msg().
993 */
994
995/* Implementation functions
996 */
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 static PyObject *
999OutputGetattr(PyObject *self, char *name)
1000{
1001 if (strcmp(name, "softspace") == 0)
1002 return PyInt_FromLong(((OutputObject *)(self))->softspace);
1003
1004 return Py_FindMethod(OutputMethods, self, name);
1005}
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007/***************/
1008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 static int
1010PythonIO_Init(void)
1011{
1012 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001013 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001015 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016}
1017
1018/******************************************************
1019 * 3. Implementation of the Vim module for Python
1020 */
1021
Bram Moolenaardb913952012-06-29 12:54:53 +02001022static PyObject *ConvertToPyObject(typval_T *);
1023static int ConvertFromPyObject(PyObject *, typval_T *);
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025/* Window type - Implementation functions
1026 * --------------------------------------
1027 */
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/* Buffer type - Implementation functions
1032 * --------------------------------------
1033 */
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1036
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001037static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1038static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040/* Line range type - Implementation functions
1041 * --------------------------------------
1042 */
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1045
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001046static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1047static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049/* Current objects type - Implementation functions
1050 * -----------------------------------------------
1051 */
1052
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001054 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001055 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1056 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001057 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1058 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1059 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001060 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1061 (objobjproc) 0,
1062#if PY_MAJOR_VERSION >= 2
1063 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066};
1067
1068/* Buffer object - Implementation
1069 */
1070
1071 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072BufferGetattr(PyObject *self, char *name)
1073{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001074 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001076 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 return NULL;
1078
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001079 r = BufferAttr((BufferObject *)(self), name);
1080 if (r || PyErr_Occurred())
1081 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 return Py_FindMethod(BufferMethods, self, name);
1084}
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086/******************/
1087
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001088 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001089BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001091 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092}
1093
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001094 static PyInt
1095BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001097 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098}
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001101 (PyInquiry) RangeLength, /* sq_length, len(x) */
1102 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1103 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1104 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1105 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1106 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1107 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1108 (objobjproc) 0,
1109#if PY_MAJOR_VERSION >= 2
1110 (binaryfunc) 0,
1111 0,
1112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113};
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115/* Line range object - Implementation
1116 */
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 static PyObject *
1119RangeGetattr(PyObject *self, char *name)
1120{
1121 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001122 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001124 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else
1126 return Py_FindMethod(RangeMethods, self, name);
1127}
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129/****************/
1130
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001131 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001132RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001134 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 ((RangeObject *)(self))->start,
1136 ((RangeObject *)(self))->end,
1137 &((RangeObject *)(self))->end);
1138}
1139
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001140 static PyInt
1141RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001143 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 ((RangeObject *)(self))->start,
1145 ((RangeObject *)(self))->end,
1146 &((RangeObject *)(self))->end);
1147}
1148
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001149/* TabPage object - Implementation
1150 */
1151
1152 static PyObject *
1153TabPageGetattr(PyObject *self, char *name)
1154{
1155 PyObject *r;
1156
1157 if (CheckTabPage((TabPageObject *)(self)))
1158 return NULL;
1159
1160 r = TabPageAttr((TabPageObject *)(self), name);
1161 if (r || PyErr_Occurred())
1162 return r;
1163 else
1164 return Py_FindMethod(TabPageMethods, self, name);
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/* Window object - Implementation
1168 */
1169
1170 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171WindowGetattr(PyObject *self, char *name)
1172{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001173 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001175 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 return NULL;
1177
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001178 r = WindowAttr((WindowObject *)(self), name);
1179 if (r || PyErr_Occurred())
1180 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 else
1182 return Py_FindMethod(WindowMethods, self, name);
1183}
1184
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001185/* Tab page list object - Definitions
1186 */
1187
1188static PySequenceMethods TabListAsSeq = {
1189 (PyInquiry) TabListLength, /* sq_length, len(x) */
1190 (binaryfunc) 0, /* sq_concat, x+y */
1191 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1192 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */
1193 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1194 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1195 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1196 (objobjproc) 0,
1197#if PY_MAJOR_VERSION >= 2
1198 (binaryfunc) 0,
1199 0,
1200#endif
1201};
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203/* Window list object - Definitions
1204 */
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001207 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001209 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1210 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1211 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1212 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001213 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1214 (objobjproc) 0,
1215#if PY_MAJOR_VERSION >= 2
1216 (binaryfunc) 0,
1217 0,
1218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219};
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221/* External interface
1222 */
1223
1224 void
1225python_buffer_free(buf_T *buf)
1226{
Bram Moolenaar971db462013-05-12 18:44:48 +02001227 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001229 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001231 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233}
1234
1235#if defined(FEAT_WINDOWS) || defined(PROTO)
1236 void
1237python_window_free(win_T *win)
1238{
Bram Moolenaar971db462013-05-12 18:44:48 +02001239 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001241 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001243 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001246
1247 void
1248python_tabpage_free(tabpage_T *tab)
1249{
1250 if (TAB_PYTHON_REF(tab) != NULL)
1251 {
1252 TabPageObject *tp = TAB_PYTHON_REF(tab);
1253 tp->tab = INVALID_TABPAGE_VALUE;
1254 TAB_PYTHON_REF(tab) = NULL;
1255 }
1256}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257#endif
1258
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001259static BufMapObject TheBufferMap =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001261 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262};
1263
1264static WinListObject TheWindowList =
1265{
1266 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001267 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268};
1269
1270static CurrentObject TheCurrent =
1271{
1272 PyObject_HEAD_INIT(&CurrentType)
1273};
1274
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001275static TabListObject TheTabPageList =
1276{
1277 PyObject_HEAD_INIT(&TabListType)
1278};
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 static int
1281PythonMod_Init(void)
1282{
1283 PyObject *mod;
1284 PyObject *dict;
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001285 PyObject *tmp;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001286 /* The special value is removed from sys.path in Python_Init(). */
1287 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 /* Fixups... */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001290 PyType_Ready(&IterType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001291 PyType_Ready(&BufferType);
1292 PyType_Ready(&RangeType);
1293 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001294 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001295 PyType_Ready(&BufMapType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001296 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001297 PyType_Ready(&TabListType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001298 PyType_Ready(&CurrentType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001299 PyType_Ready(&OptionsType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 /* Set sys.argv[] to avoid a crash in warn(). */
1302 PySys_SetArgv(1, argv);
1303
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001304 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 dict = PyModule_GetDict(mod);
1306
1307 VimError = Py_BuildValue("s", "vim.error");
1308
1309 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001310 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001311 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1312 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001313 PyDict_SetItemString(dict, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001314 tmp = DictionaryNew(&globvardict);
1315 PyDict_SetItemString(dict, "vars", tmp);
1316 Py_DECREF(tmp);
1317 tmp = DictionaryNew(&vimvardict);
1318 PyDict_SetItemString(dict, "vvars", tmp);
1319 Py_DECREF(tmp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001320 tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
1321 PyDict_SetItemString(dict, "options", tmp);
1322 Py_DECREF(tmp);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001323 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1324 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1325 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1326 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327
1328 if (PyErr_Occurred())
1329 return -1;
1330
1331 return 0;
1332}
1333
1334/*************************************************************************
1335 * 4. Utility functions for handling the interface between Vim and Python.
1336 */
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338/* Convert a Vim line into a Python string.
1339 * All internal newlines are replaced by null characters.
1340 *
1341 * On errors, the Python exception data is set, and NULL is returned.
1342 */
1343 static PyObject *
1344LineToString(const char *str)
1345{
1346 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001347 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 char *p;
1349
1350 /* Allocate an Python string object, with uninitialised contents. We
1351 * must do it this way, so that we can modify the string in place
1352 * later. See the Python source, Objects/stringobject.c for details.
1353 */
1354 result = PyString_FromStringAndSize(NULL, len);
1355 if (result == NULL)
1356 return NULL;
1357
1358 p = PyString_AsString(result);
1359
1360 while (*str)
1361 {
1362 if (*str == '\n')
1363 *p = '\0';
1364 else
1365 *p = *str;
1366
1367 ++p;
1368 ++str;
1369 }
1370
1371 return result;
1372}
1373
Bram Moolenaardb913952012-06-29 12:54:53 +02001374 static PyObject *
1375DictionaryGetattr(PyObject *self, char *name)
1376{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001377 DictionaryObject *this = ((DictionaryObject *) (self));
1378
1379 if (strcmp(name, "locked") == 0)
1380 return PyInt_FromLong(this->dict->dv_lock);
1381 else if (strcmp(name, "scope") == 0)
1382 return PyInt_FromLong(this->dict->dv_scope);
1383
Bram Moolenaardb913952012-06-29 12:54:53 +02001384 return Py_FindMethod(DictionaryMethods, self, name);
1385}
1386
Bram Moolenaardb913952012-06-29 12:54:53 +02001387static PySequenceMethods ListAsSeq = {
1388 (PyInquiry) ListLength,
1389 (binaryfunc) 0,
1390 (PyIntArgFunc) 0,
1391 (PyIntArgFunc) ListItem,
1392 (PyIntIntArgFunc) ListSlice,
1393 (PyIntObjArgProc) ListAssItem,
1394 (PyIntIntObjArgProc) ListAssSlice,
1395 (objobjproc) 0,
1396#if PY_MAJOR_VERSION >= 2
1397 (binaryfunc) ListConcatInPlace,
1398 0,
1399#endif
1400};
1401
Bram Moolenaardb913952012-06-29 12:54:53 +02001402 static PyObject *
1403ListGetattr(PyObject *self, char *name)
1404{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001405 if (strcmp(name, "locked") == 0)
1406 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1407
Bram Moolenaardb913952012-06-29 12:54:53 +02001408 return Py_FindMethod(ListMethods, self, name);
1409}
1410
Bram Moolenaardb913952012-06-29 12:54:53 +02001411 static PyObject *
1412FunctionGetattr(PyObject *self, char *name)
1413{
1414 FunctionObject *this = (FunctionObject *)(self);
1415
1416 if (strcmp(name, "name") == 0)
1417 return PyString_FromString((char *)(this->name));
1418 else
1419 return Py_FindMethod(FunctionMethods, self, name);
1420}
1421
1422 void
1423do_pyeval (char_u *str, typval_T *rettv)
1424{
1425 DoPythonCommand(NULL, (char *) str, rettv);
1426 switch(rettv->v_type)
1427 {
1428 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1429 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1430 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001431 case VAR_UNKNOWN:
1432 rettv->v_type = VAR_NUMBER;
1433 rettv->vval.v_number = 0;
1434 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001435 }
1436}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438/* Don't generate a prototype for the next function, it generates an error on
1439 * newer Python versions. */
1440#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1441
1442 char *
1443Py_GetProgramName(void)
1444{
1445 return "vim";
1446}
1447#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001448
Bram Moolenaardb913952012-06-29 12:54:53 +02001449 void
1450set_ref_in_python (int copyID)
1451{
1452 set_ref_in_py(copyID);
1453}