blob: 5874103f0fe31300ebd0ba5015124a28d89dbf82 [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 Moolenaard620aa92013-05-17 16:40:06 +0200201# define PyObject_GetAttrString dll_PyObject_GetAttrString
202# define PyObject_SetAttrString dll_PyObject_SetAttrString
203# define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200205# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206# define PyString_FromString dll_PyString_FromString
207# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
208# define PyString_Size dll_PyString_Size
209# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200210# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200211# undef PyUnicode_AsEncodedString
212# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200213# define PyFloat_AsDouble dll_PyFloat_AsDouble
214# define PyFloat_FromDouble dll_PyFloat_FromDouble
215# define PyFloat_Type (*dll_PyFloat_Type)
216# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217# define PySys_SetObject dll_PySys_SetObject
218# define PySys_SetArgv dll_PySys_SetArgv
219# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100220# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# define Py_BuildValue dll_Py_BuildValue
222# define Py_FindMethod dll_Py_FindMethod
223# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100224# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000226# define Py_Finalize dll_Py_Finalize
227# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200229# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
230# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200233# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
234# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200236# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200237# define PyObject_IsTrue dll_PyObject_IsTrue
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
239# define PyType_IsSubtype dll_PyType_IsSubtype
240# endif
241# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
242# define PyObject_Malloc dll_PyObject_Malloc
243# define PyObject_Free dll_PyObject_Free
244# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200245# ifdef PY_USE_CAPSULE
246# define PyCapsule_New dll_PyCapsule_New
247# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
248# else
249# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
250# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
253/*
254 * Pointers for dynamic link
255 */
256static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
257static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200258static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200259static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
261static int(*dll_PyErr_BadArgument)(void);
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200262static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100264static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265static PyObject*(*dll_PyErr_NoMemory)(void);
266static PyObject*(*dll_PyErr_Occurred)(void);
267static void(*dll_PyErr_SetNone)(PyObject *);
268static void(*dll_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200269static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270static void(*dll_PyEval_InitThreads)(void);
271static void(*dll_PyEval_RestoreThread)(PyThreadState *);
272static PyThreadState*(*dll_PyEval_SaveThread)(void);
273# ifdef PY_CAN_RECURSE
274static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
275static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277static long(*dll_PyInt_AsLong)(PyObject *);
278static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200279static long(*dll_PyLong_AsLong)(PyObject *);
280static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200281static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200283static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000284static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000285static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000286static PyObject*(*dll_PyList_New)(PyInt size);
287static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
288static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200290static int (*dll_PySequence_Check)(PyObject *);
291static PyInt(*dll_PySequence_Size)(PyObject *);
292static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
293static PyInt(*dll_PyTuple_Size)(PyObject *);
294static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
295static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000297static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200299static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
300# ifndef PY_NO_MAPPING_ITEMS
301static PyObject* (*dll_PyMapping_Items)(PyObject *);
302# endif
303static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
304static int (*dll_PyMapping_Check)(PyObject *);
305static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static PyObject*(*dll_PyModule_GetDict)(PyObject *);
307static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200308static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaard620aa92013-05-17 16:40:06 +0200309static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
310static PyObject* (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
311static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200313static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000315static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
316static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200318static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200319static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200320static double(*dll_PyFloat_AsDouble)(PyObject *);
321static PyObject*(*dll_PyFloat_FromDouble)(double);
322static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static int(*dll_PySys_SetObject)(char *, PyObject *);
324static int(*dll_PySys_SetArgv)(int, char **);
325static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100326static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327static PyObject*(*dll_Py_BuildValue)(char *, ...);
328static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
329static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200330static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100331static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000333static void(*dll_Py_Finalize)(void);
334static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
336static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200337static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200338static int (*dll_PyObject_IsTrue)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200339# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200340static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200343static PyObject* _Py_ZeroStruct;
344static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
346static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
347# endif
348# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
349static void* (*dll_PyObject_Malloc)(size_t);
350static void (*dll_PyObject_Free)(void*);
351# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200352# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200353static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
354static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200355# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200356static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
357static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360static HINSTANCE hinstPython = 0; /* Instance of python.dll */
361
362/* Imported exception objects */
363static PyObject *imp_PyExc_AttributeError;
364static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200365static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366static PyObject *imp_PyExc_KeyboardInterrupt;
367static PyObject *imp_PyExc_TypeError;
368static PyObject *imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200369static PyObject *imp_PyExc_RuntimeError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
371# define PyExc_AttributeError imp_PyExc_AttributeError
372# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200373# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
375# define PyExc_TypeError imp_PyExc_TypeError
376# define PyExc_ValueError imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200377# define PyExc_RuntimeError imp_PyExc_RuntimeError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378
379/*
380 * Table of name to function pointer of python.
381 */
382# define PYTHON_PROC FARPROC
383static struct
384{
385 char *name;
386 PYTHON_PROC *ptr;
387} python_funcname_table[] =
388{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200389#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
391 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200392 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
393#else
394 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
395 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
396 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
397#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200398 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200399 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
401 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200402 {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100404 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
406 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
407 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
408 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200409 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
411 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
412 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
413# ifdef PY_CAN_RECURSE
414 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
415 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
416# endif
417 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
418 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200419 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
420 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200421 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200423 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000425 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
427 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
428 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
429 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200430 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
431 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
432 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
433 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
434 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
435 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
437 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200438 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000439 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200440# ifndef PY_NO_MAPPING_ITEMS
441 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
442# endif
443 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
444 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
445 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
447 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200448 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaard620aa92013-05-17 16:40:06 +0200449 {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
450 {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
451 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200453 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
455 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
456 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
457 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200458 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200459 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
460 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
461 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
462 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
464 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
465 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100466 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200468# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
469 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000470 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000473# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100474 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000476 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
477 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
479 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200480 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200481 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
Bram Moolenaare7211222012-06-30 13:21:08 +0200482# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200483 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200484# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200486 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
487 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
489 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
490# endif
491# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
492 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
493 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
494# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200495# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200496 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
497 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200498# else
499 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
500 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 {"", NULL},
503};
504
505/*
506 * Free python.dll
507 */
508 static void
509end_dynamic_python(void)
510{
511 if (hinstPython)
512 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200513 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 hinstPython = 0;
515 }
516}
517
518/*
519 * Load library and get all pointers.
520 * Parameter 'libname' provides name of DLL.
521 * Return OK or FAIL.
522 */
523 static int
524python_runtime_link_init(char *libname, int verbose)
525{
526 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200527 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100529#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200530 /* Can't have Python and Python3 loaded at the same time.
531 * It cause a crash, because RTLD_GLOBAL is needed for
532 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200533 if (python3_loaded())
534 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200535 if (verbose)
536 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200537 return FAIL;
538 }
539#endif
540
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (hinstPython)
542 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200543 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 if (!hinstPython)
545 {
546 if (verbose)
547 EMSG2(_(e_loadlib), libname);
548 return FAIL;
549 }
550
551 for (i = 0; python_funcname_table[i].ptr; ++i)
552 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200553 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 python_funcname_table[i].name)) == NULL)
555 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200556 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 hinstPython = 0;
558 if (verbose)
559 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
560 return FAIL;
561 }
562 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200563
564 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
565 * will be present in the library. */
566 ucs_as_encoded_string = symbol_from_dll(hinstPython,
567 "PyUnicodeUCS2_AsEncodedString");
568 if (ucs_as_encoded_string == NULL)
569 ucs_as_encoded_string = symbol_from_dll(hinstPython,
570 "PyUnicodeUCS4_AsEncodedString");
571 if (ucs_as_encoded_string != NULL)
572 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
573 else
574 {
575 close_dll(hinstPython);
576 hinstPython = 0;
577 if (verbose)
578 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
579 return FAIL;
580 }
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return OK;
583}
584
585/*
586 * If python is enabled (there is installed python on Windows system) return
587 * TRUE, else FALSE.
588 */
589 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000590python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
593}
594
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200595/*
596 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * DLL, as this can cause errors (importing data symbols is not reliable).
598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 PyObject *exmod = PyImport_ImportModule("exceptions");
603 PyObject *exdict = PyModule_GetDict(exmod);
604 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
605 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200606 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
608 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
609 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200610 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 Py_XINCREF(imp_PyExc_AttributeError);
612 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200613 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
615 Py_XINCREF(imp_PyExc_TypeError);
616 Py_XINCREF(imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200617 Py_XINCREF(imp_PyExc_RuntimeError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 Py_XDECREF(exmod);
619}
620#endif /* DYNAMIC_PYTHON */
621
Bram Moolenaardb913952012-06-29 12:54:53 +0200622static int initialised = 0;
623#define PYINITIALISED initialised
624
Bram Moolenaardb913952012-06-29 12:54:53 +0200625#define DICTKEY_GET(err) \
626 if (!PyString_Check(keyObject)) \
627 { \
628 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
629 return err; \
630 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200631 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
632 return err;
633
Bram Moolenaardb913952012-06-29 12:54:53 +0200634#define DICTKEY_UNREF
635#define DICTKEY_DECL
636
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200637#define DESTRUCTOR_FINISH(self) Py_DECREF(self);
638
Bram Moolenaar971db462013-05-12 18:44:48 +0200639#define WIN_PYTHON_REF(win) win->w_python_ref
640#define BUF_PYTHON_REF(buf) buf->b_python_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200641#define TAB_PYTHON_REF(tab) tab->tp_python_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200642
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200643static PyObject *OutputGetattr(PyObject *, char *);
644static PyObject *BufferGetattr(PyObject *, char *);
645static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200646static PyObject *TabPageGetattr(PyObject *, char *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200647static PyObject *RangeGetattr(PyObject *, char *);
648static PyObject *DictionaryGetattr(PyObject *, char*);
649static PyObject *ListGetattr(PyObject *, char *);
650static PyObject *FunctionGetattr(PyObject *, char *);
651
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200652/*
653 * Include the code shared with if_python3.c
654 */
655#include "if_py_both.h"
656
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658/******************************************************
659 * Internal function prototypes.
660 */
661
Bram Moolenaardb913952012-06-29 12:54:53 +0200662static PyObject *globals;
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664static void PythonIO_Flush(void);
665static int PythonIO_Init(void);
666static int PythonMod_Init(void);
667
668/* Utility functions for the vim/python interface
669 * ----------------------------------------------
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000672static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675/******************************************************
676 * 1. Python interpreter main program.
677 */
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
680typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
Bram Moolenaar71700b82013-05-15 17:49:05 +0200683#ifndef PY_CAN_RECURSE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000684static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686/*
687 * Suspend a thread of the Python interpreter, other threads are allowed to
688 * run.
689 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000690 static void
691Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692{
693 saved_python_thread = PyEval_SaveThread();
694}
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{
703 PyEval_RestoreThread(saved_python_thread);
704 saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000705}
Bram Moolenaar71700b82013-05-15 17:49:05 +0200706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 void
709python_end()
710{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000711 static int recurse = 0;
712
713 /* If a crash occurs while doing this, don't try again. */
714 if (recurse != 0)
715 return;
716
717 ++recurse;
718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000720 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000721 {
Bram Moolenaar71700b82013-05-15 17:49:05 +0200722# ifdef PY_CAN_RECURSE
723 PyGILState_Ensure();
724# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000725 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200726# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000727 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000730#else
731 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000732 {
Bram Moolenaar71700b82013-05-15 17:49:05 +0200733# ifdef PY_CAN_RECURSE
734 PyGILState_Ensure();
735# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000736 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200737# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000738 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000741
742 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743}
744
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200745#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
746 int
747python_loaded()
748{
749 return (hinstPython != 0);
750}
751#endif
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 static int
754Python_Init(void)
755{
756 if (!initialised)
757 {
758#ifdef DYNAMIC_PYTHON
759 if (!python_enabled(TRUE))
760 {
761 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
762 goto fail;
763 }
764#endif
765
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100766#ifdef PYTHON_HOME
767 Py_SetPythonHome(PYTHON_HOME);
768#endif
769
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200770 init_structs();
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#if !defined(MACOS) || defined(MACOS_X_UNIX)
773 Py_Initialize();
774#else
775 PyMac_Initialize();
776#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100777 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100778 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100779 * specific state (such as the system trace hook), will be lost
780 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782#ifdef DYNAMIC_PYTHON
783 get_exceptions();
784#endif
785
786 if (PythonIO_Init())
787 goto fail;
788
789 if (PythonMod_Init())
790 goto fail;
791
Bram Moolenaardb913952012-06-29 12:54:53 +0200792 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
793
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000794 /* Remove the element from sys.path that was added because of our
795 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200796 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000797 * the current directory in sys.path. */
798 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
799
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100800 /* lock is created and acquired in PyEval_InitThreads() and thread
801 * state is created in Py_Initialize()
802 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
803 * (python must have threads enabled!)
804 * so the following does both: unlock GIL and save thread state in TLS
805 * without deleting thread state
806 */
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200807#ifndef PY_CAN_RECURSE
808 saved_python_thread =
809#endif
810 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 initialised = 1;
813 }
814
815 return 0;
816
817fail:
818 /* We call PythonIO_Flush() here to print any Python errors.
819 * This is OK, as it is possible to call this function even
820 * if PythonIO_Init() has not completed successfully (it will
821 * not do anything in this case).
822 */
823 PythonIO_Flush();
824 return -1;
825}
826
827/*
828 * External interface
829 */
830 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200831DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000833#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 static int recursive = 0;
835#endif
836#if defined(MACOS) && !defined(MACOS_X_UNIX)
837 GrafPtr oldPort;
838#endif
839#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
840 char *saved_locale;
841#endif
Bram Moolenaar71700b82013-05-15 17:49:05 +0200842#ifdef PY_CAN_RECURSE
843 PyGILState_STATE pygilstate;
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846#ifndef PY_CAN_RECURSE
847 if (recursive)
848 {
849 EMSG(_("E659: Cannot invoke Python recursively"));
850 return;
851 }
852 ++recursive;
853#endif
854
855#if defined(MACOS) && !defined(MACOS_X_UNIX)
856 GetPort(&oldPort);
857 /* Check if the Python library is available */
858 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
859 goto theend;
860#endif
861 if (Python_Init())
862 goto theend;
863
Bram Moolenaardb913952012-06-29 12:54:53 +0200864 if (rettv == NULL)
865 {
866 RangeStart = eap->line1;
867 RangeEnd = eap->line2;
868 }
869 else
870 {
871 RangeStart = (PyInt) curwin->w_cursor.lnum;
872 RangeEnd = RangeStart;
873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 Python_Release_Vim(); /* leave vim */
875
876#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
877 /* Python only works properly when the LC_NUMERIC locale is "C". */
878 saved_locale = setlocale(LC_NUMERIC, NULL);
879 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
880 saved_locale = NULL;
881 else
882 {
883 /* Need to make a copy, value may change when setting new locale. */
884 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
885 (void)setlocale(LC_NUMERIC, "C");
886 }
887#endif
888
Bram Moolenaar71700b82013-05-15 17:49:05 +0200889#ifdef PY_CAN_RECURSE
890 pygilstate = PyGILState_Ensure();
891#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894
Bram Moolenaardb913952012-06-29 12:54:53 +0200895 if (rettv == NULL)
896 PyRun_SimpleString((char *)(cmd));
897 else
898 {
899 PyObject *r;
900
901 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
902 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100903 {
904 if (PyErr_Occurred() && !msg_silent)
905 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200906 EMSG(_("E858: Eval did not return a valid python object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100907 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200908 else
909 {
910 if (ConvertFromPyObject(r, rettv) == -1)
911 EMSG(_("E859: Failed to convert returned python object to vim value"));
912 Py_DECREF(r);
913 }
914 PyErr_Clear();
915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Bram Moolenaar71700b82013-05-15 17:49:05 +0200917#ifdef PY_CAN_RECURSE
918 PyGILState_Release(pygilstate);
919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 Python_SaveThread(); /* leave python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
923#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
924 if (saved_locale != NULL)
925 {
926 (void)setlocale(LC_NUMERIC, saved_locale);
927 vim_free(saved_locale);
928 }
929#endif
930
931 Python_Lock_Vim(); /* enter vim */
932 PythonIO_Flush();
933#if defined(MACOS) && !defined(MACOS_X_UNIX)
934 SetPort(oldPort);
935#endif
936
937theend:
938#ifndef PY_CAN_RECURSE
939 --recursive;
940#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200941 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
944/*
945 * ":python"
946 */
947 void
948ex_python(exarg_T *eap)
949{
950 char_u *script;
951
952 script = script_get(eap, eap->arg);
953 if (!eap->skip)
954 {
955 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200956 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200958 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960 vim_free(script);
961}
962
963#define BUFFER_SIZE 1024
964
965/*
966 * ":pyfile"
967 */
968 void
969ex_pyfile(exarg_T *eap)
970{
971 static char buffer[BUFFER_SIZE];
972 const char *file = (char *)eap->arg;
973 char *p;
974
975 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
976 * stdio file pointer, but Vim and the Python DLL are compiled with
977 * different options under Windows, meaning that stdio pointers aren't
978 * compatible between the two. Yuk.
979 *
980 * Put the string "execfile('file')" into buffer. But, we need to
981 * escape any backslashes or single quotes in the file name, so that
982 * Python won't mangle the file name.
983 */
984 strcpy(buffer, "execfile('");
985 p = buffer + 10; /* size of "execfile('" */
986
987 while (*file && p < buffer + (BUFFER_SIZE - 3))
988 {
989 if (*file == '\\' || *file == '\'')
990 *p++ = '\\';
991 *p++ = *file++;
992 }
993
994 /* If we didn't finish the file name, we hit a buffer overflow */
995 if (*file != '\0')
996 return;
997
998 /* Put in the terminating "')" and a null */
999 *p++ = '\'';
1000 *p++ = ')';
1001 *p++ = '\0';
1002
1003 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +02001004 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
Bram Moolenaard620aa92013-05-17 16:40:06 +02001007 void
1008ex_pydo(exarg_T *eap)
1009{
1010 linenr_T i;
1011 const char *code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
1012 const char *s = (const char *) eap->arg;
1013 size_t len;
1014 char *code;
1015 int status;
1016 PyObject *pyfunc, *pymain;
1017 PyGILState_STATE pygilstate;
1018
1019 if (Python_Init())
1020 return;
1021
1022 if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
1023 {
1024 EMSG(_("cannot save undo information"));
1025 return;
1026 }
1027 len = strlen(code_hdr) + strlen(s);
1028 code = malloc(len + 1);
1029 STRCPY(code, code_hdr);
1030 STRNCAT(code, s, len + 1);
1031 pygilstate = PyGILState_Ensure();
1032 status = PyRun_SimpleString(code);
1033 vim_free(code);
1034 if (status)
1035 {
1036 EMSG(_("failed to run the code"));
1037 return;
1038 }
1039 status = 0; /* good */
1040 pymain = PyImport_AddModule("__main__");
1041 pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
1042 PyGILState_Release(pygilstate);
1043
1044 for (i = eap->line1; i <= eap->line2; i++)
1045 {
1046 const char *line;
1047 PyObject *pyline, *pylinenr, *pyret;
1048
1049 line = (char *)ml_get(i);
1050 pygilstate = PyGILState_Ensure();
1051 pyline = PyString_FromStringAndSize(line, strlen(line));
1052 pylinenr = PyLong_FromLong(i);
1053 pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
1054 Py_DECREF(pyline);
1055 Py_DECREF(pylinenr);
1056 if (!pyret)
1057 {
1058 PyErr_PrintEx(0);
1059 PythonIO_Flush();
1060 status = 1;
1061 goto out;
1062 }
1063
1064 if (pyret && pyret != Py_None)
1065 {
1066 if (!PyString_Check(pyret))
1067 {
1068 EMSG(_("E863: return value must be an instance of str"));
1069 Py_XDECREF(pyret);
1070 status = 1;
1071 goto out;
1072 }
1073 ml_replace(i, (char_u *) PyString_AsString(pyret), 1);
1074 changed();
1075#ifdef SYNTAX_HL
1076 syn_changed(i); /* recompute syntax hl. for this line */
1077#endif
1078 }
1079 Py_XDECREF(pyret);
1080 PythonIO_Flush();
1081 PyGILState_Release(pygilstate);
1082 }
1083 pygilstate = PyGILState_Ensure();
1084out:
1085 Py_DECREF(pyfunc);
1086 PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
1087 PyGILState_Release(pygilstate);
1088 if (status)
1089 return;
1090 check_cursor();
1091 update_curbuf(NOT_VALID);
1092}
1093
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094/******************************************************
1095 * 2. Python output stream: writes output via [e]msg().
1096 */
1097
1098/* Implementation functions
1099 */
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 static PyObject *
1102OutputGetattr(PyObject *self, char *name)
1103{
1104 if (strcmp(name, "softspace") == 0)
1105 return PyInt_FromLong(((OutputObject *)(self))->softspace);
1106
1107 return Py_FindMethod(OutputMethods, self, name);
1108}
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110/***************/
1111
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 static int
1113PythonIO_Init(void)
1114{
1115 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001116 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001118 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119}
1120
1121/******************************************************
1122 * 3. Implementation of the Vim module for Python
1123 */
1124
Bram Moolenaardb913952012-06-29 12:54:53 +02001125static PyObject *ConvertToPyObject(typval_T *);
1126static int ConvertFromPyObject(PyObject *, typval_T *);
1127
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128/* Window type - Implementation functions
1129 * --------------------------------------
1130 */
1131
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134/* Buffer type - Implementation functions
1135 * --------------------------------------
1136 */
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1139
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001140static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1141static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143/* Line range type - Implementation functions
1144 * --------------------------------------
1145 */
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1148
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001149static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1150static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152/* Current objects type - Implementation functions
1153 * -----------------------------------------------
1154 */
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001157 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001158 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1159 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001160 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1161 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1162 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001163 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1164 (objobjproc) 0,
1165#if PY_MAJOR_VERSION >= 2
1166 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169};
1170
1171/* Buffer object - Implementation
1172 */
1173
1174 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175BufferGetattr(PyObject *self, char *name)
1176{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001177 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001179 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 return NULL;
1181
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001182 r = BufferAttr((BufferObject *)(self), name);
1183 if (r || PyErr_Occurred())
1184 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 else
1186 return Py_FindMethod(BufferMethods, self, name);
1187}
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189/******************/
1190
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001191 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001192BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001194 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195}
1196
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001197 static PyInt
1198BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001200 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201}
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001204 (PyInquiry) RangeLength, /* sq_length, len(x) */
1205 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1206 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1207 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1208 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1209 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1210 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1211 (objobjproc) 0,
1212#if PY_MAJOR_VERSION >= 2
1213 (binaryfunc) 0,
1214 0,
1215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216};
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218/* Line range object - Implementation
1219 */
1220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 static PyObject *
1222RangeGetattr(PyObject *self, char *name)
1223{
1224 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001225 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001227 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 else
1229 return Py_FindMethod(RangeMethods, self, name);
1230}
1231
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232/****************/
1233
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001234 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001235RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001237 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 ((RangeObject *)(self))->start,
1239 ((RangeObject *)(self))->end,
1240 &((RangeObject *)(self))->end);
1241}
1242
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001243 static PyInt
1244RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001246 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 ((RangeObject *)(self))->start,
1248 ((RangeObject *)(self))->end,
1249 &((RangeObject *)(self))->end);
1250}
1251
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001252/* TabPage object - Implementation
1253 */
1254
1255 static PyObject *
1256TabPageGetattr(PyObject *self, char *name)
1257{
1258 PyObject *r;
1259
1260 if (CheckTabPage((TabPageObject *)(self)))
1261 return NULL;
1262
1263 r = TabPageAttr((TabPageObject *)(self), name);
1264 if (r || PyErr_Occurred())
1265 return r;
1266 else
1267 return Py_FindMethod(TabPageMethods, self, name);
1268}
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270/* Window object - Implementation
1271 */
1272
1273 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274WindowGetattr(PyObject *self, char *name)
1275{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001276 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001278 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 return NULL;
1280
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001281 r = WindowAttr((WindowObject *)(self), name);
1282 if (r || PyErr_Occurred())
1283 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
1285 return Py_FindMethod(WindowMethods, self, name);
1286}
1287
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001288/* Tab page list object - Definitions
1289 */
1290
1291static PySequenceMethods TabListAsSeq = {
1292 (PyInquiry) TabListLength, /* sq_length, len(x) */
1293 (binaryfunc) 0, /* sq_concat, x+y */
1294 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1295 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */
1296 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1297 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1298 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1299 (objobjproc) 0,
1300#if PY_MAJOR_VERSION >= 2
1301 (binaryfunc) 0,
1302 0,
1303#endif
1304};
1305
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306/* Window list object - Definitions
1307 */
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001310 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001312 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1313 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1314 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1315 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001316 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1317 (objobjproc) 0,
1318#if PY_MAJOR_VERSION >= 2
1319 (binaryfunc) 0,
1320 0,
1321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322};
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/* External interface
1325 */
1326
1327 void
1328python_buffer_free(buf_T *buf)
1329{
Bram Moolenaar971db462013-05-12 18:44:48 +02001330 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001332 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001334 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336}
1337
1338#if defined(FEAT_WINDOWS) || defined(PROTO)
1339 void
1340python_window_free(win_T *win)
1341{
Bram Moolenaar971db462013-05-12 18:44:48 +02001342 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001344 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001346 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001349
1350 void
1351python_tabpage_free(tabpage_T *tab)
1352{
1353 if (TAB_PYTHON_REF(tab) != NULL)
1354 {
1355 TabPageObject *tp = TAB_PYTHON_REF(tab);
1356 tp->tab = INVALID_TABPAGE_VALUE;
1357 TAB_PYTHON_REF(tab) = NULL;
1358 }
1359}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#endif
1361
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001362static BufMapObject TheBufferMap =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001364 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365};
1366
1367static WinListObject TheWindowList =
1368{
1369 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001370 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371};
1372
1373static CurrentObject TheCurrent =
1374{
1375 PyObject_HEAD_INIT(&CurrentType)
1376};
1377
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001378static TabListObject TheTabPageList =
1379{
1380 PyObject_HEAD_INIT(&TabListType)
1381};
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 static int
1384PythonMod_Init(void)
1385{
1386 PyObject *mod;
1387 PyObject *dict;
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001388 PyObject *tmp;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001389 /* The special value is removed from sys.path in Python_Init(). */
1390 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392 /* Fixups... */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001393 PyType_Ready(&IterType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001394 PyType_Ready(&BufferType);
1395 PyType_Ready(&RangeType);
1396 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001397 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001398 PyType_Ready(&BufMapType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001399 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001400 PyType_Ready(&TabListType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001401 PyType_Ready(&CurrentType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001402 PyType_Ready(&OptionsType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403
1404 /* Set sys.argv[] to avoid a crash in warn(). */
1405 PySys_SetArgv(1, argv);
1406
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001407 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 dict = PyModule_GetDict(mod);
1409
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001410 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001413 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001414 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1415 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001416 PyDict_SetItemString(dict, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001417 tmp = DictionaryNew(&globvardict);
1418 PyDict_SetItemString(dict, "vars", tmp);
1419 Py_DECREF(tmp);
1420 tmp = DictionaryNew(&vimvardict);
1421 PyDict_SetItemString(dict, "vvars", tmp);
1422 Py_DECREF(tmp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001423 tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
1424 PyDict_SetItemString(dict, "options", tmp);
1425 Py_DECREF(tmp);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001426 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1427 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1428 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1429 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 if (PyErr_Occurred())
1432 return -1;
1433
1434 return 0;
1435}
1436
1437/*************************************************************************
1438 * 4. Utility functions for handling the interface between Vim and Python.
1439 */
1440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441/* Convert a Vim line into a Python string.
1442 * All internal newlines are replaced by null characters.
1443 *
1444 * On errors, the Python exception data is set, and NULL is returned.
1445 */
1446 static PyObject *
1447LineToString(const char *str)
1448{
1449 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001450 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 char *p;
1452
1453 /* Allocate an Python string object, with uninitialised contents. We
1454 * must do it this way, so that we can modify the string in place
1455 * later. See the Python source, Objects/stringobject.c for details.
1456 */
1457 result = PyString_FromStringAndSize(NULL, len);
1458 if (result == NULL)
1459 return NULL;
1460
1461 p = PyString_AsString(result);
1462
1463 while (*str)
1464 {
1465 if (*str == '\n')
1466 *p = '\0';
1467 else
1468 *p = *str;
1469
1470 ++p;
1471 ++str;
1472 }
1473
1474 return result;
1475}
1476
Bram Moolenaardb913952012-06-29 12:54:53 +02001477 static PyObject *
1478DictionaryGetattr(PyObject *self, char *name)
1479{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001480 DictionaryObject *this = ((DictionaryObject *) (self));
1481
1482 if (strcmp(name, "locked") == 0)
1483 return PyInt_FromLong(this->dict->dv_lock);
1484 else if (strcmp(name, "scope") == 0)
1485 return PyInt_FromLong(this->dict->dv_scope);
1486
Bram Moolenaardb913952012-06-29 12:54:53 +02001487 return Py_FindMethod(DictionaryMethods, self, name);
1488}
1489
Bram Moolenaardb913952012-06-29 12:54:53 +02001490static PySequenceMethods ListAsSeq = {
1491 (PyInquiry) ListLength,
1492 (binaryfunc) 0,
1493 (PyIntArgFunc) 0,
1494 (PyIntArgFunc) ListItem,
1495 (PyIntIntArgFunc) ListSlice,
1496 (PyIntObjArgProc) ListAssItem,
1497 (PyIntIntObjArgProc) ListAssSlice,
1498 (objobjproc) 0,
1499#if PY_MAJOR_VERSION >= 2
1500 (binaryfunc) ListConcatInPlace,
1501 0,
1502#endif
1503};
1504
Bram Moolenaardb913952012-06-29 12:54:53 +02001505 static PyObject *
1506ListGetattr(PyObject *self, char *name)
1507{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001508 if (strcmp(name, "locked") == 0)
1509 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1510
Bram Moolenaardb913952012-06-29 12:54:53 +02001511 return Py_FindMethod(ListMethods, self, name);
1512}
1513
Bram Moolenaardb913952012-06-29 12:54:53 +02001514 static PyObject *
1515FunctionGetattr(PyObject *self, char *name)
1516{
1517 FunctionObject *this = (FunctionObject *)(self);
1518
1519 if (strcmp(name, "name") == 0)
1520 return PyString_FromString((char *)(this->name));
1521 else
1522 return Py_FindMethod(FunctionMethods, self, name);
1523}
1524
1525 void
1526do_pyeval (char_u *str, typval_T *rettv)
1527{
1528 DoPythonCommand(NULL, (char *) str, rettv);
1529 switch(rettv->v_type)
1530 {
1531 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1532 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1533 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001534 case VAR_UNKNOWN:
1535 rettv->v_type = VAR_NUMBER;
1536 rettv->vval.v_number = 0;
1537 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001538 }
1539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541/* Don't generate a prototype for the next function, it generates an error on
1542 * newer Python versions. */
1543#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1544
1545 char *
1546Py_GetProgramName(void)
1547{
1548 return "vim";
1549}
1550#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001551
Bram Moolenaardb913952012-06-29 12:54:53 +02001552 void
1553set_ref_in_python (int copyID)
1554{
1555 set_ref_in_py(copyID);
1556}