blob: 1c192454956c0f02c0ac0ac25fa5933377304f6d [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
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200152# define PyErr_NewException dll_PyErr_NewException
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define PyErr_Clear dll_PyErr_Clear
Bram Moolenaar4d369872013-02-20 16:09:43 +0100154# define PyErr_PrintEx dll_PyErr_PrintEx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155# define PyErr_NoMemory dll_PyErr_NoMemory
156# define PyErr_Occurred dll_PyErr_Occurred
157# define PyErr_SetNone dll_PyErr_SetNone
158# define PyErr_SetString dll_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200159# define PyErr_SetObject dll_PyErr_SetObject
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160# define PyEval_InitThreads dll_PyEval_InitThreads
161# define PyEval_RestoreThread dll_PyEval_RestoreThread
162# define PyEval_SaveThread dll_PyEval_SaveThread
163# ifdef PY_CAN_RECURSE
164# define PyGILState_Ensure dll_PyGILState_Ensure
165# define PyGILState_Release dll_PyGILState_Release
166# endif
167# define PyInt_AsLong dll_PyInt_AsLong
168# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200169# define PyLong_AsLong dll_PyLong_AsLong
170# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar66b79852012-09-21 14:00:35 +0200171# define PyBool_Type (*dll_PyBool_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200173# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000175# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176# define PyList_New dll_PyList_New
177# define PyList_SetItem dll_PyList_SetItem
178# define PyList_Size dll_PyList_Size
179# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200180# define PySequence_Check dll_PySequence_Check
181# define PySequence_Size dll_PySequence_Size
182# define PySequence_GetItem dll_PySequence_GetItem
183# define PyTuple_Size dll_PyTuple_Size
184# define PyTuple_GetItem dll_PyTuple_GetItem
185# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000187# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200189# define PyDict_Next dll_PyDict_Next
190# ifdef PyMapping_Items
191# define PY_NO_MAPPING_ITEMS
192# else
193# define PyMapping_Items dll_PyMapping_Items
194# endif
195# define PyObject_CallMethod dll_PyObject_CallMethod
196# define PyMapping_Check dll_PyMapping_Check
197# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198# define PyModule_GetDict dll_PyModule_GetDict
199# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200200# define PyRun_String dll_PyRun_String
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200202# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203# define PyString_FromString dll_PyString_FromString
204# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
205# define PyString_Size dll_PyString_Size
206# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200207# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200208# undef PyUnicode_AsEncodedString
209# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200210# define PyFloat_AsDouble dll_PyFloat_AsDouble
211# define PyFloat_FromDouble dll_PyFloat_FromDouble
212# define PyFloat_Type (*dll_PyFloat_Type)
213# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214# define PySys_SetObject dll_PySys_SetObject
215# define PySys_SetArgv dll_PySys_SetArgv
216# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100217# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218# define Py_BuildValue dll_Py_BuildValue
219# define Py_FindMethod dll_Py_FindMethod
220# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100221# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000223# define Py_Finalize dll_Py_Finalize
224# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200226# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
227# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200230# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
231# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200233# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200234# define PyObject_IsTrue dll_PyObject_IsTrue
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
236# define PyType_IsSubtype dll_PyType_IsSubtype
237# endif
238# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
239# define PyObject_Malloc dll_PyObject_Malloc
240# define PyObject_Free dll_PyObject_Free
241# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200242# ifdef PY_USE_CAPSULE
243# define PyCapsule_New dll_PyCapsule_New
244# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
245# else
246# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
247# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249
250/*
251 * Pointers for dynamic link
252 */
253static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
254static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200255static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200256static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
258static int(*dll_PyErr_BadArgument)(void);
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200259static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100261static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262static PyObject*(*dll_PyErr_NoMemory)(void);
263static PyObject*(*dll_PyErr_Occurred)(void);
264static void(*dll_PyErr_SetNone)(PyObject *);
265static void(*dll_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200266static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267static void(*dll_PyEval_InitThreads)(void);
268static void(*dll_PyEval_RestoreThread)(PyThreadState *);
269static PyThreadState*(*dll_PyEval_SaveThread)(void);
270# ifdef PY_CAN_RECURSE
271static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
272static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200273# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274static long(*dll_PyInt_AsLong)(PyObject *);
275static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200276static long(*dll_PyLong_AsLong)(PyObject *);
277static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200278static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200280static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000281static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000282static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000283static PyObject*(*dll_PyList_New)(PyInt size);
284static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
285static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200287static int (*dll_PySequence_Check)(PyObject *);
288static PyInt(*dll_PySequence_Size)(PyObject *);
289static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
290static PyInt(*dll_PyTuple_Size)(PyObject *);
291static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
292static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000294static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200296static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
297# ifndef PY_NO_MAPPING_ITEMS
298static PyObject* (*dll_PyMapping_Items)(PyObject *);
299# endif
300static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
301static int (*dll_PyMapping_Check)(PyObject *);
302static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static PyObject*(*dll_PyModule_GetDict)(PyObject *);
304static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200305static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200307static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000309static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
310static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200312static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200313static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200314static double(*dll_PyFloat_AsDouble)(PyObject *);
315static PyObject*(*dll_PyFloat_FromDouble)(double);
316static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static int(*dll_PySys_SetObject)(char *, PyObject *);
318static int(*dll_PySys_SetArgv)(int, char **);
319static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100320static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321static PyObject*(*dll_Py_BuildValue)(char *, ...);
322static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
323static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200324static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100325static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000327static void(*dll_Py_Finalize)(void);
328static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
330static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200331static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200332static int (*dll_PyObject_IsTrue)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200333# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200334static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200337static PyObject* _Py_ZeroStruct;
338static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
340static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
341# endif
342# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
343static void* (*dll_PyObject_Malloc)(size_t);
344static void (*dll_PyObject_Free)(void*);
345# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200346# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200347static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
348static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200349# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200350static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
351static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200352# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353
354static HINSTANCE hinstPython = 0; /* Instance of python.dll */
355
356/* Imported exception objects */
357static PyObject *imp_PyExc_AttributeError;
358static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200359static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360static PyObject *imp_PyExc_KeyboardInterrupt;
361static PyObject *imp_PyExc_TypeError;
362static PyObject *imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200363static PyObject *imp_PyExc_RuntimeError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
365# define PyExc_AttributeError imp_PyExc_AttributeError
366# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200367# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
369# define PyExc_TypeError imp_PyExc_TypeError
370# define PyExc_ValueError imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200371# define PyExc_RuntimeError imp_PyExc_RuntimeError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373/*
374 * Table of name to function pointer of python.
375 */
376# define PYTHON_PROC FARPROC
377static struct
378{
379 char *name;
380 PYTHON_PROC *ptr;
381} python_funcname_table[] =
382{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200383#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
385 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200386 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
387#else
388 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
389 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
390 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
391#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200392 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200393 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
395 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200396 {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100398 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
400 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
401 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
402 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200403 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
405 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
406 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
407# ifdef PY_CAN_RECURSE
408 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
409 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
410# endif
411 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
412 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200413 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
414 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200415 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200417 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000419 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
421 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
422 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
423 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200424 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
425 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
426 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
427 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
428 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
429 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
431 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200432 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000433 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200434# ifndef PY_NO_MAPPING_ITEMS
435 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
436# endif
437 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
438 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
439 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
441 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200442 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200444 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
446 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
447 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
448 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200449 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200450 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
451 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
452 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
453 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
455 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
456 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100457 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200459# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
460 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000461 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
462# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000464# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100465 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000467 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
468 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
470 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200471 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200472 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
Bram Moolenaare7211222012-06-30 13:21:08 +0200473# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200474 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200477 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
478 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
480 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
481# endif
482# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
483 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
484 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
485# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200486# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200487 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
488 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200489# else
490 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
491 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 {"", NULL},
494};
495
496/*
497 * Free python.dll
498 */
499 static void
500end_dynamic_python(void)
501{
502 if (hinstPython)
503 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200504 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 hinstPython = 0;
506 }
507}
508
509/*
510 * Load library and get all pointers.
511 * Parameter 'libname' provides name of DLL.
512 * Return OK or FAIL.
513 */
514 static int
515python_runtime_link_init(char *libname, int verbose)
516{
517 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200518 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100520#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200521 /* Can't have Python and Python3 loaded at the same time.
522 * It cause a crash, because RTLD_GLOBAL is needed for
523 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200524 if (python3_loaded())
525 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200526 if (verbose)
527 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200528 return FAIL;
529 }
530#endif
531
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (hinstPython)
533 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200534 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (!hinstPython)
536 {
537 if (verbose)
538 EMSG2(_(e_loadlib), libname);
539 return FAIL;
540 }
541
542 for (i = 0; python_funcname_table[i].ptr; ++i)
543 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200544 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 python_funcname_table[i].name)) == NULL)
546 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200547 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 hinstPython = 0;
549 if (verbose)
550 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
551 return FAIL;
552 }
553 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200554
555 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
556 * will be present in the library. */
557 ucs_as_encoded_string = symbol_from_dll(hinstPython,
558 "PyUnicodeUCS2_AsEncodedString");
559 if (ucs_as_encoded_string == NULL)
560 ucs_as_encoded_string = symbol_from_dll(hinstPython,
561 "PyUnicodeUCS4_AsEncodedString");
562 if (ucs_as_encoded_string != NULL)
563 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
564 else
565 {
566 close_dll(hinstPython);
567 hinstPython = 0;
568 if (verbose)
569 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
570 return FAIL;
571 }
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return OK;
574}
575
576/*
577 * If python is enabled (there is installed python on Windows system) return
578 * TRUE, else FALSE.
579 */
580 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000581python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
584}
585
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200586/*
587 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 * DLL, as this can cause errors (importing data symbols is not reliable).
589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200591get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 PyObject *exmod = PyImport_ImportModule("exceptions");
594 PyObject *exdict = PyModule_GetDict(exmod);
595 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
596 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200597 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
599 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
600 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200601 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 Py_XINCREF(imp_PyExc_AttributeError);
603 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200604 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
606 Py_XINCREF(imp_PyExc_TypeError);
607 Py_XINCREF(imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200608 Py_XINCREF(imp_PyExc_RuntimeError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 Py_XDECREF(exmod);
610}
611#endif /* DYNAMIC_PYTHON */
612
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200613static PyObject *BufferNew (buf_T *);
614static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200615static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200616static PyObject *LineToString(const char *);
617
Bram Moolenaardb913952012-06-29 12:54:53 +0200618static int initialised = 0;
619#define PYINITIALISED initialised
620
Bram Moolenaardb913952012-06-29 12:54:53 +0200621#define DICTKEY_GET(err) \
622 if (!PyString_Check(keyObject)) \
623 { \
624 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
625 return err; \
626 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200627 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
628 return err;
629
Bram Moolenaardb913952012-06-29 12:54:53 +0200630#define DICTKEY_UNREF
631#define DICTKEY_DECL
632
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200633#define DESTRUCTOR_FINISH(self) Py_DECREF(self);
634
Bram Moolenaar971db462013-05-12 18:44:48 +0200635#define WIN_PYTHON_REF(win) win->w_python_ref
636#define BUF_PYTHON_REF(buf) buf->b_python_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200637#define TAB_PYTHON_REF(tab) tab->tp_python_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200638
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200639static PyObject *OutputGetattr(PyObject *, char *);
640static PyObject *BufferGetattr(PyObject *, char *);
641static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200642static PyObject *TabPageGetattr(PyObject *, char *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200643static PyObject *RangeGetattr(PyObject *, char *);
644static PyObject *DictionaryGetattr(PyObject *, char*);
645static PyObject *ListGetattr(PyObject *, char *);
646static PyObject *FunctionGetattr(PyObject *, char *);
647
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200648/*
649 * Include the code shared with if_python3.c
650 */
651#include "if_py_both.h"
652
653
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654/******************************************************
655 * Internal function prototypes.
656 */
657
Bram Moolenaardb913952012-06-29 12:54:53 +0200658static PyObject *globals;
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660static void PythonIO_Flush(void);
661static int PythonIO_Init(void);
662static int PythonMod_Init(void);
663
664/* Utility functions for the vim/python interface
665 * ----------------------------------------------
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000668static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671/******************************************************
672 * 1. Python interpreter main program.
673 */
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
676typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000679#ifdef PY_CAN_RECURSE
680static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
681#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000682static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685/*
686 * Suspend a thread of the Python interpreter, other threads are allowed to
687 * run.
688 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000689 static void
690Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000692#ifdef PY_CAN_RECURSE
693 PyGILState_Release(pygilstate);
694#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697}
698
699/*
700 * Restore a thread of the Python interpreter, waits for other threads to
701 * block.
702 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000703 static void
704Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000706#ifdef PY_CAN_RECURSE
707 pygilstate = PyGILState_Ensure();
708#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 PyEval_RestoreThread(saved_python_thread);
710 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000712}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 void
715python_end()
716{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000717 static int recurse = 0;
718
719 /* If a crash occurs while doing this, don't try again. */
720 if (recurse != 0)
721 return;
722
723 ++recurse;
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000726 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000727 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000728 Python_RestoreThread(); /* enter python */
729 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000732#else
733 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000735 Python_RestoreThread(); /* enter python */
736 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000739
740 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741}
742
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200743#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
744 int
745python_loaded()
746{
747 return (hinstPython != 0);
748}
749#endif
750
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 static int
752Python_Init(void)
753{
754 if (!initialised)
755 {
756#ifdef DYNAMIC_PYTHON
757 if (!python_enabled(TRUE))
758 {
759 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
760 goto fail;
761 }
762#endif
763
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100764#ifdef PYTHON_HOME
765 Py_SetPythonHome(PYTHON_HOME);
766#endif
767
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200768 init_structs();
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#if !defined(MACOS) || defined(MACOS_X_UNIX)
771 Py_Initialize();
772#else
773 PyMac_Initialize();
774#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100775 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100776 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100777 * specific state (such as the system trace hook), will be lost
778 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef DYNAMIC_PYTHON
781 get_exceptions();
782#endif
783
784 if (PythonIO_Init())
785 goto fail;
786
787 if (PythonMod_Init())
788 goto fail;
789
Bram Moolenaardb913952012-06-29 12:54:53 +0200790 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
791
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000792 /* Remove the element from sys.path that was added because of our
793 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200794 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000795 * the current directory in sys.path. */
796 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
797
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100798 /* lock is created and acquired in PyEval_InitThreads() and thread
799 * state is created in Py_Initialize()
800 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
801 * (python must have threads enabled!)
802 * so the following does both: unlock GIL and save thread state in TLS
803 * without deleting thread state
804 */
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200805#ifndef PY_CAN_RECURSE
806 saved_python_thread =
807#endif
808 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 initialised = 1;
811 }
812
813 return 0;
814
815fail:
816 /* We call PythonIO_Flush() here to print any Python errors.
817 * This is OK, as it is possible to call this function even
818 * if PythonIO_Init() has not completed successfully (it will
819 * not do anything in this case).
820 */
821 PythonIO_Flush();
822 return -1;
823}
824
825/*
826 * External interface
827 */
828 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200829DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000831#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 static int recursive = 0;
833#endif
834#if defined(MACOS) && !defined(MACOS_X_UNIX)
835 GrafPtr oldPort;
836#endif
837#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
838 char *saved_locale;
839#endif
840
841#ifndef PY_CAN_RECURSE
842 if (recursive)
843 {
844 EMSG(_("E659: Cannot invoke Python recursively"));
845 return;
846 }
847 ++recursive;
848#endif
849
850#if defined(MACOS) && !defined(MACOS_X_UNIX)
851 GetPort(&oldPort);
852 /* Check if the Python library is available */
853 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
854 goto theend;
855#endif
856 if (Python_Init())
857 goto theend;
858
Bram Moolenaardb913952012-06-29 12:54:53 +0200859 if (rettv == NULL)
860 {
861 RangeStart = eap->line1;
862 RangeEnd = eap->line2;
863 }
864 else
865 {
866 RangeStart = (PyInt) curwin->w_cursor.lnum;
867 RangeEnd = RangeStart;
868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 Python_Release_Vim(); /* leave vim */
870
871#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
872 /* Python only works properly when the LC_NUMERIC locale is "C". */
873 saved_locale = setlocale(LC_NUMERIC, NULL);
874 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
875 saved_locale = NULL;
876 else
877 {
878 /* Need to make a copy, value may change when setting new locale. */
879 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
880 (void)setlocale(LC_NUMERIC, "C");
881 }
882#endif
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaardb913952012-06-29 12:54:53 +0200886 if (rettv == NULL)
887 PyRun_SimpleString((char *)(cmd));
888 else
889 {
890 PyObject *r;
891
892 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
893 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100894 {
895 if (PyErr_Occurred() && !msg_silent)
896 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200897 EMSG(_("E858: Eval did not return a valid python object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100898 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200899 else
900 {
901 if (ConvertFromPyObject(r, rettv) == -1)
902 EMSG(_("E859: Failed to convert returned python object to vim value"));
903 Py_DECREF(r);
904 }
905 PyErr_Clear();
906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
911 if (saved_locale != NULL)
912 {
913 (void)setlocale(LC_NUMERIC, saved_locale);
914 vim_free(saved_locale);
915 }
916#endif
917
918 Python_Lock_Vim(); /* enter vim */
919 PythonIO_Flush();
920#if defined(MACOS) && !defined(MACOS_X_UNIX)
921 SetPort(oldPort);
922#endif
923
924theend:
925#ifndef PY_CAN_RECURSE
926 --recursive;
927#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200928 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929}
930
931/*
932 * ":python"
933 */
934 void
935ex_python(exarg_T *eap)
936{
937 char_u *script;
938
939 script = script_get(eap, eap->arg);
940 if (!eap->skip)
941 {
942 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200943 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200945 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 vim_free(script);
948}
949
950#define BUFFER_SIZE 1024
951
952/*
953 * ":pyfile"
954 */
955 void
956ex_pyfile(exarg_T *eap)
957{
958 static char buffer[BUFFER_SIZE];
959 const char *file = (char *)eap->arg;
960 char *p;
961
962 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
963 * stdio file pointer, but Vim and the Python DLL are compiled with
964 * different options under Windows, meaning that stdio pointers aren't
965 * compatible between the two. Yuk.
966 *
967 * Put the string "execfile('file')" into buffer. But, we need to
968 * escape any backslashes or single quotes in the file name, so that
969 * Python won't mangle the file name.
970 */
971 strcpy(buffer, "execfile('");
972 p = buffer + 10; /* size of "execfile('" */
973
974 while (*file && p < buffer + (BUFFER_SIZE - 3))
975 {
976 if (*file == '\\' || *file == '\'')
977 *p++ = '\\';
978 *p++ = *file++;
979 }
980
981 /* If we didn't finish the file name, we hit a buffer overflow */
982 if (*file != '\0')
983 return;
984
985 /* Put in the terminating "')" and a null */
986 *p++ = '\'';
987 *p++ = ')';
988 *p++ = '\0';
989
990 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200991 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992}
993
994/******************************************************
995 * 2. Python output stream: writes output via [e]msg().
996 */
997
998/* Implementation functions
999 */
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 static PyObject *
1002OutputGetattr(PyObject *self, char *name)
1003{
1004 if (strcmp(name, "softspace") == 0)
1005 return PyInt_FromLong(((OutputObject *)(self))->softspace);
1006
1007 return Py_FindMethod(OutputMethods, self, name);
1008}
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/***************/
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 static int
1013PythonIO_Init(void)
1014{
1015 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001016 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001018 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019}
1020
1021/******************************************************
1022 * 3. Implementation of the Vim module for Python
1023 */
1024
Bram Moolenaardb913952012-06-29 12:54:53 +02001025static PyObject *ConvertToPyObject(typval_T *);
1026static int ConvertFromPyObject(PyObject *, typval_T *);
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028/* Window type - Implementation functions
1029 * --------------------------------------
1030 */
1031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/* Buffer type - Implementation functions
1035 * --------------------------------------
1036 */
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1039
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001040static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1041static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043/* Line range type - Implementation functions
1044 * --------------------------------------
1045 */
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1048
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001049static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1050static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052/* Current objects type - Implementation functions
1053 * -----------------------------------------------
1054 */
1055
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001057 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001058 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1059 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001060 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1061 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1062 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001063 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1064 (objobjproc) 0,
1065#if PY_MAJOR_VERSION >= 2
1066 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069};
1070
1071/* Buffer object - Implementation
1072 */
1073
1074 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075BufferGetattr(PyObject *self, char *name)
1076{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001077 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001079 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 return NULL;
1081
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001082 r = BufferAttr((BufferObject *)(self), name);
1083 if (r || PyErr_Occurred())
1084 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 else
1086 return Py_FindMethod(BufferMethods, self, name);
1087}
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/******************/
1090
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001091 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001092BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001094 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001097 static PyInt
1098BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001100 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101}
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001104 (PyInquiry) RangeLength, /* sq_length, len(x) */
1105 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1106 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1107 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1108 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1109 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1110 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1111 (objobjproc) 0,
1112#if PY_MAJOR_VERSION >= 2
1113 (binaryfunc) 0,
1114 0,
1115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116};
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118/* Line range object - Implementation
1119 */
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 static PyObject *
1122RangeGetattr(PyObject *self, char *name)
1123{
1124 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001125 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001127 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 else
1129 return Py_FindMethod(RangeMethods, self, name);
1130}
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132/****************/
1133
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001134 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001135RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001137 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 ((RangeObject *)(self))->start,
1139 ((RangeObject *)(self))->end,
1140 &((RangeObject *)(self))->end);
1141}
1142
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001143 static PyInt
1144RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001146 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 ((RangeObject *)(self))->start,
1148 ((RangeObject *)(self))->end,
1149 &((RangeObject *)(self))->end);
1150}
1151
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001152/* TabPage object - Implementation
1153 */
1154
1155 static PyObject *
1156TabPageGetattr(PyObject *self, char *name)
1157{
1158 PyObject *r;
1159
1160 if (CheckTabPage((TabPageObject *)(self)))
1161 return NULL;
1162
1163 r = TabPageAttr((TabPageObject *)(self), name);
1164 if (r || PyErr_Occurred())
1165 return r;
1166 else
1167 return Py_FindMethod(TabPageMethods, self, name);
1168}
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/* Window object - Implementation
1171 */
1172
1173 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174WindowGetattr(PyObject *self, char *name)
1175{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001176 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001178 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return NULL;
1180
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001181 r = WindowAttr((WindowObject *)(self), name);
1182 if (r || PyErr_Occurred())
1183 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 else
1185 return Py_FindMethod(WindowMethods, self, name);
1186}
1187
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001188/* Tab page list object - Definitions
1189 */
1190
1191static PySequenceMethods TabListAsSeq = {
1192 (PyInquiry) TabListLength, /* sq_length, len(x) */
1193 (binaryfunc) 0, /* sq_concat, x+y */
1194 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1195 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */
1196 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1197 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1198 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1199 (objobjproc) 0,
1200#if PY_MAJOR_VERSION >= 2
1201 (binaryfunc) 0,
1202 0,
1203#endif
1204};
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/* Window list object - Definitions
1207 */
1208
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001210 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001212 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1213 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1214 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1215 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001216 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1217 (objobjproc) 0,
1218#if PY_MAJOR_VERSION >= 2
1219 (binaryfunc) 0,
1220 0,
1221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222};
1223
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224/* External interface
1225 */
1226
1227 void
1228python_buffer_free(buf_T *buf)
1229{
Bram Moolenaar971db462013-05-12 18:44:48 +02001230 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001232 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001234 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236}
1237
1238#if defined(FEAT_WINDOWS) || defined(PROTO)
1239 void
1240python_window_free(win_T *win)
1241{
Bram Moolenaar971db462013-05-12 18:44:48 +02001242 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001244 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001246 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001249
1250 void
1251python_tabpage_free(tabpage_T *tab)
1252{
1253 if (TAB_PYTHON_REF(tab) != NULL)
1254 {
1255 TabPageObject *tp = TAB_PYTHON_REF(tab);
1256 tp->tab = INVALID_TABPAGE_VALUE;
1257 TAB_PYTHON_REF(tab) = NULL;
1258 }
1259}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#endif
1261
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001262static BufMapObject TheBufferMap =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001264 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265};
1266
1267static WinListObject TheWindowList =
1268{
1269 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001270 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271};
1272
1273static CurrentObject TheCurrent =
1274{
1275 PyObject_HEAD_INIT(&CurrentType)
1276};
1277
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001278static TabListObject TheTabPageList =
1279{
1280 PyObject_HEAD_INIT(&TabListType)
1281};
1282
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 static int
1284PythonMod_Init(void)
1285{
1286 PyObject *mod;
1287 PyObject *dict;
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001288 PyObject *tmp;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001289 /* The special value is removed from sys.path in Python_Init(). */
1290 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292 /* Fixups... */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001293 PyType_Ready(&IterType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001294 PyType_Ready(&BufferType);
1295 PyType_Ready(&RangeType);
1296 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001297 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001298 PyType_Ready(&BufMapType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001299 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001300 PyType_Ready(&TabListType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001301 PyType_Ready(&CurrentType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001302 PyType_Ready(&OptionsType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 /* Set sys.argv[] to avoid a crash in warn(). */
1305 PySys_SetArgv(1, argv);
1306
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001307 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 dict = PyModule_GetDict(mod);
1309
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001310 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
1312 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001313 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001314 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1315 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001316 PyDict_SetItemString(dict, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001317 tmp = DictionaryNew(&globvardict);
1318 PyDict_SetItemString(dict, "vars", tmp);
1319 Py_DECREF(tmp);
1320 tmp = DictionaryNew(&vimvardict);
1321 PyDict_SetItemString(dict, "vvars", tmp);
1322 Py_DECREF(tmp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001323 tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
1324 PyDict_SetItemString(dict, "options", tmp);
1325 Py_DECREF(tmp);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001326 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1327 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1328 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1329 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 if (PyErr_Occurred())
1332 return -1;
1333
1334 return 0;
1335}
1336
1337/*************************************************************************
1338 * 4. Utility functions for handling the interface between Vim and Python.
1339 */
1340
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341/* Convert a Vim line into a Python string.
1342 * All internal newlines are replaced by null characters.
1343 *
1344 * On errors, the Python exception data is set, and NULL is returned.
1345 */
1346 static PyObject *
1347LineToString(const char *str)
1348{
1349 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001350 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char *p;
1352
1353 /* Allocate an Python string object, with uninitialised contents. We
1354 * must do it this way, so that we can modify the string in place
1355 * later. See the Python source, Objects/stringobject.c for details.
1356 */
1357 result = PyString_FromStringAndSize(NULL, len);
1358 if (result == NULL)
1359 return NULL;
1360
1361 p = PyString_AsString(result);
1362
1363 while (*str)
1364 {
1365 if (*str == '\n')
1366 *p = '\0';
1367 else
1368 *p = *str;
1369
1370 ++p;
1371 ++str;
1372 }
1373
1374 return result;
1375}
1376
Bram Moolenaardb913952012-06-29 12:54:53 +02001377 static PyObject *
1378DictionaryGetattr(PyObject *self, char *name)
1379{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001380 DictionaryObject *this = ((DictionaryObject *) (self));
1381
1382 if (strcmp(name, "locked") == 0)
1383 return PyInt_FromLong(this->dict->dv_lock);
1384 else if (strcmp(name, "scope") == 0)
1385 return PyInt_FromLong(this->dict->dv_scope);
1386
Bram Moolenaardb913952012-06-29 12:54:53 +02001387 return Py_FindMethod(DictionaryMethods, self, name);
1388}
1389
Bram Moolenaardb913952012-06-29 12:54:53 +02001390static PySequenceMethods ListAsSeq = {
1391 (PyInquiry) ListLength,
1392 (binaryfunc) 0,
1393 (PyIntArgFunc) 0,
1394 (PyIntArgFunc) ListItem,
1395 (PyIntIntArgFunc) ListSlice,
1396 (PyIntObjArgProc) ListAssItem,
1397 (PyIntIntObjArgProc) ListAssSlice,
1398 (objobjproc) 0,
1399#if PY_MAJOR_VERSION >= 2
1400 (binaryfunc) ListConcatInPlace,
1401 0,
1402#endif
1403};
1404
Bram Moolenaardb913952012-06-29 12:54:53 +02001405 static PyObject *
1406ListGetattr(PyObject *self, char *name)
1407{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001408 if (strcmp(name, "locked") == 0)
1409 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1410
Bram Moolenaardb913952012-06-29 12:54:53 +02001411 return Py_FindMethod(ListMethods, self, name);
1412}
1413
Bram Moolenaardb913952012-06-29 12:54:53 +02001414 static PyObject *
1415FunctionGetattr(PyObject *self, char *name)
1416{
1417 FunctionObject *this = (FunctionObject *)(self);
1418
1419 if (strcmp(name, "name") == 0)
1420 return PyString_FromString((char *)(this->name));
1421 else
1422 return Py_FindMethod(FunctionMethods, self, name);
1423}
1424
1425 void
1426do_pyeval (char_u *str, typval_T *rettv)
1427{
1428 DoPythonCommand(NULL, (char *) str, rettv);
1429 switch(rettv->v_type)
1430 {
1431 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1432 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1433 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001434 case VAR_UNKNOWN:
1435 rettv->v_type = VAR_NUMBER;
1436 rettv->vval.v_number = 0;
1437 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001438 }
1439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
1441/* Don't generate a prototype for the next function, it generates an error on
1442 * newer Python versions. */
1443#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1444
1445 char *
1446Py_GetProgramName(void)
1447{
1448 return "vim";
1449}
1450#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001451
Bram Moolenaardb913952012-06-29 12:54:53 +02001452 void
1453set_ref_in_python (int copyID)
1454{
1455 set_ref_in_py(copyID);
1456}