blob: 7a4fa8ad9a1b15e3785c101d57f9aa7fcf409c69 [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 Moolenaardb913952012-06-29 12:54:53 +020059#define PyBytes_FromString PyString_FromString
Bram Moolenaar335e0b62013-04-24 13:47:45 +020060#define PyBytes_Check PyString_Check
Bram Moolenaardb913952012-06-29 12:54:53 +020061
Bram Moolenaar19e60942011-06-19 00:27:51 +020062/* No-op conversion functions, use with care! */
63#define PyString_AsBytes(obj) (obj)
64#define PyString_FreeBytes(obj)
65
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#if !defined(FEAT_PYTHON) && defined(PROTO)
67/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000068# define PyObject Py_ssize_t
69# define PyThreadState Py_ssize_t
70# define PyTypeObject Py_ssize_t
71struct PyMethodDef { Py_ssize_t a; };
72# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000073#endif
74
Bram Moolenaar2afa3232012-06-29 16:28:28 +020075#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
76# define PY_USE_CAPSULE
77#endif
78
Bram Moolenaar2c45e942008-06-04 11:35:26 +000079#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
80# define PyInt Py_ssize_t
81# define PyInquiry lenfunc
82# define PyIntArgFunc ssizeargfunc
83# define PyIntIntArgFunc ssizessizeargfunc
84# define PyIntObjArgProc ssizeobjargproc
85# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000086# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000087#else
88# define PyInt int
Bram Moolenaar4d1da492013-04-24 13:39:15 +020089# define lenfunc inquiry
Bram Moolenaar2c45e942008-06-04 11:35:26 +000090# define PyInquiry inquiry
91# define PyIntArgFunc intargfunc
92# define PyIntIntArgFunc intintargfunc
93# define PyIntObjArgProc intobjargproc
94# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000095# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000096#endif
97
Bram Moolenaar071d4272004-06-13 20:20:40 +000098/* Parser flags */
99#define single_input 256
100#define file_input 257
101#define eval_input 258
102
103#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
104 /* Python 2.3: can invoke ":python" recursively. */
105# define PY_CAN_RECURSE
106#endif
107
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200108# if defined(DYNAMIC_PYTHON) || defined(PROTO)
109# ifndef DYNAMIC_PYTHON
110# define HINSTANCE long_u /* for generating prototypes */
111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200113# ifndef WIN3264
114# include <dlfcn.h>
115# define FARPROC void*
116# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100117# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# define load_dll(n) dlopen((n), RTLD_LAZY)
119# else
120# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
121# endif
122# define close_dll dlclose
123# define symbol_from_dll dlsym
124# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200125# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200126# define close_dll FreeLibrary
127# define symbol_from_dll GetProcAddress
128# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200129
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000130/* This makes if_python.c compile without warnings against Python 2.5
131 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200132# undef PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200133# undef PyRun_String
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200134# undef PyArg_Parse
135# undef PyArg_ParseTuple
136# undef Py_BuildValue
137# undef Py_InitModule4
138# undef Py_InitModule4_64
Bram Moolenaardb913952012-06-29 12:54:53 +0200139# undef PyObject_CallMethod
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141/*
142 * Wrapper defines
143 */
144# define PyArg_Parse dll_PyArg_Parse
145# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200146# define PyMem_Free dll_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200147# define PyMem_Malloc dll_PyMem_Malloc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define PyDict_SetItemString dll_PyDict_SetItemString
149# define PyErr_BadArgument dll_PyErr_BadArgument
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200150# define PyErr_NewException dll_PyErr_NewException
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define PyErr_Clear dll_PyErr_Clear
Bram Moolenaar4d369872013-02-20 16:09:43 +0100152# define PyErr_PrintEx dll_PyErr_PrintEx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define PyErr_NoMemory dll_PyErr_NoMemory
154# define PyErr_Occurred dll_PyErr_Occurred
155# define PyErr_SetNone dll_PyErr_SetNone
156# define PyErr_SetString dll_PyErr_SetString
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200157# define PyErr_SetObject dll_PyErr_SetObject
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# define PyEval_InitThreads dll_PyEval_InitThreads
159# define PyEval_RestoreThread dll_PyEval_RestoreThread
160# define PyEval_SaveThread dll_PyEval_SaveThread
161# ifdef PY_CAN_RECURSE
162# define PyGILState_Ensure dll_PyGILState_Ensure
163# define PyGILState_Release dll_PyGILState_Release
164# endif
165# define PyInt_AsLong dll_PyInt_AsLong
166# define PyInt_FromLong dll_PyInt_FromLong
Bram Moolenaardb913952012-06-29 12:54:53 +0200167# define PyLong_AsLong dll_PyLong_AsLong
168# define PyLong_FromLong dll_PyLong_FromLong
Bram Moolenaar66b79852012-09-21 14:00:35 +0200169# define PyBool_Type (*dll_PyBool_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170# define PyInt_Type (*dll_PyInt_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200171# define PyLong_Type (*dll_PyLong_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000173# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174# define PyList_New dll_PyList_New
175# define PyList_SetItem dll_PyList_SetItem
176# define PyList_Size dll_PyList_Size
177# define PyList_Type (*dll_PyList_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200178# define PySequence_Check dll_PySequence_Check
179# define PySequence_Size dll_PySequence_Size
180# define PySequence_GetItem dll_PySequence_GetItem
181# define PyTuple_Size dll_PyTuple_Size
182# define PyTuple_GetItem dll_PyTuple_GetItem
183# define PyTuple_Type (*dll_PyTuple_Type)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000185# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186# define PyDict_GetItemString dll_PyDict_GetItemString
Bram Moolenaardb913952012-06-29 12:54:53 +0200187# define PyDict_Next dll_PyDict_Next
188# ifdef PyMapping_Items
189# define PY_NO_MAPPING_ITEMS
190# else
191# define PyMapping_Items dll_PyMapping_Items
192# endif
193# define PyObject_CallMethod dll_PyObject_CallMethod
194# define PyMapping_Check dll_PyMapping_Check
195# define PyIter_Next dll_PyIter_Next
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196# define PyModule_GetDict dll_PyModule_GetDict
197# define PyRun_SimpleString dll_PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200198# define PyRun_String dll_PyRun_String
Bram Moolenaard620aa92013-05-17 16:40:06 +0200199# define PyObject_GetAttrString dll_PyObject_GetAttrString
200# define PyObject_SetAttrString dll_PyObject_SetAttrString
201# define PyObject_CallFunctionObjArgs dll_PyObject_CallFunctionObjArgs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200203# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204# define PyString_FromString dll_PyString_FromString
205# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
206# define PyString_Size dll_PyString_Size
207# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200208# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200209# undef PyUnicode_AsEncodedString
210# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200211# define PyFloat_AsDouble dll_PyFloat_AsDouble
212# define PyFloat_FromDouble dll_PyFloat_FromDouble
213# define PyFloat_Type (*dll_PyFloat_Type)
214# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215# define PySys_SetObject dll_PySys_SetObject
216# define PySys_SetArgv dll_PySys_SetArgv
217# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100218# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219# define Py_BuildValue dll_Py_BuildValue
220# define Py_FindMethod dll_Py_FindMethod
221# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100222# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000224# define Py_Finalize dll_Py_Finalize
225# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200227# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
228# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200231# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
232# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200234# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200235# define PyObject_IsTrue dll_PyObject_IsTrue
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
237# define PyType_IsSubtype dll_PyType_IsSubtype
238# endif
239# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
240# define PyObject_Malloc dll_PyObject_Malloc
241# define PyObject_Free dll_PyObject_Free
242# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200243# ifdef PY_USE_CAPSULE
244# define PyCapsule_New dll_PyCapsule_New
245# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
246# else
247# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
248# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250
251/*
252 * Pointers for dynamic link
253 */
254static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
255static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200256static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200257static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
259static int(*dll_PyErr_BadArgument)(void);
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200260static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100262static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263static PyObject*(*dll_PyErr_NoMemory)(void);
264static PyObject*(*dll_PyErr_Occurred)(void);
265static void(*dll_PyErr_SetNone)(PyObject *);
266static void(*dll_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200267static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268static void(*dll_PyEval_InitThreads)(void);
269static void(*dll_PyEval_RestoreThread)(PyThreadState *);
270static PyThreadState*(*dll_PyEval_SaveThread)(void);
271# ifdef PY_CAN_RECURSE
272static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
273static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static long(*dll_PyInt_AsLong)(PyObject *);
276static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200277static long(*dll_PyLong_AsLong)(PyObject *);
278static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200279static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200281static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000282static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000283static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000284static PyObject*(*dll_PyList_New)(PyInt size);
285static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
286static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200288static int (*dll_PySequence_Check)(PyObject *);
289static PyInt(*dll_PySequence_Size)(PyObject *);
290static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
291static PyInt(*dll_PyTuple_Size)(PyObject *);
292static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
293static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000295static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200297static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
298# ifndef PY_NO_MAPPING_ITEMS
299static PyObject* (*dll_PyMapping_Items)(PyObject *);
300# endif
301static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
302static int (*dll_PyMapping_Check)(PyObject *);
303static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304static PyObject*(*dll_PyModule_GetDict)(PyObject *);
305static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200306static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaard620aa92013-05-17 16:40:06 +0200307static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
308static PyObject* (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
309static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200311static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000313static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
314static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200316static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200317static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200318static double(*dll_PyFloat_AsDouble)(PyObject *);
319static PyObject*(*dll_PyFloat_FromDouble)(double);
320static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321static int(*dll_PySys_SetObject)(char *, PyObject *);
322static int(*dll_PySys_SetArgv)(int, char **);
323static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100324static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static PyObject*(*dll_Py_BuildValue)(char *, ...);
326static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
327static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200328static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100329static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000331static void(*dll_Py_Finalize)(void);
332static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
334static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200335static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200336static int (*dll_PyObject_IsTrue)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200337# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200338static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200341static PyObject* _Py_ZeroStruct;
342static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
344static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
345# endif
346# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
347static void* (*dll_PyObject_Malloc)(size_t);
348static void (*dll_PyObject_Free)(void*);
349# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200350# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200351static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
352static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200353# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200354static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
355static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
358static HINSTANCE hinstPython = 0; /* Instance of python.dll */
359
360/* Imported exception objects */
361static PyObject *imp_PyExc_AttributeError;
362static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200363static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364static PyObject *imp_PyExc_KeyboardInterrupt;
365static PyObject *imp_PyExc_TypeError;
366static PyObject *imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200367static PyObject *imp_PyExc_RuntimeError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369# define PyExc_AttributeError imp_PyExc_AttributeError
370# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200371# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
373# define PyExc_TypeError imp_PyExc_TypeError
374# define PyExc_ValueError imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200375# define PyExc_RuntimeError imp_PyExc_RuntimeError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376
377/*
378 * Table of name to function pointer of python.
379 */
380# define PYTHON_PROC FARPROC
381static struct
382{
383 char *name;
384 PYTHON_PROC *ptr;
385} python_funcname_table[] =
386{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200387#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
389 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200390 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
391#else
392 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
393 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
394 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
395#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200396 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200397 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
399 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200400 {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100402 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
404 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
405 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
406 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200407 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
409 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
410 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
411# ifdef PY_CAN_RECURSE
412 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
413 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
414# endif
415 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
416 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200417 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
418 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200419 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200421 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000423 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
425 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
426 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
427 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200428 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
429 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
430 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
431 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
432 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
433 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
435 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200436 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000437 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200438# ifndef PY_NO_MAPPING_ITEMS
439 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
440# endif
441 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
442 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
443 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
445 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200446 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaard620aa92013-05-17 16:40:06 +0200447 {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
448 {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
449 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200451 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
453 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
454 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
455 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200456 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200457 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
458 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
459 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
460 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
462 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
463 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100464 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200466# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
467 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000468 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
469# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000471# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100472 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000474 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
475 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
477 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200478 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200479 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
Bram Moolenaare7211222012-06-30 13:21:08 +0200480# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200481 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200484 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
485 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
487 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
488# endif
489# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
490 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
491 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
492# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200493# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200494 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
495 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200496# else
497 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
498 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 {"", NULL},
501};
502
503/*
504 * Free python.dll
505 */
506 static void
507end_dynamic_python(void)
508{
509 if (hinstPython)
510 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200511 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 hinstPython = 0;
513 }
514}
515
516/*
517 * Load library and get all pointers.
518 * Parameter 'libname' provides name of DLL.
519 * Return OK or FAIL.
520 */
521 static int
522python_runtime_link_init(char *libname, int verbose)
523{
524 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200525 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100527#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200528 /* Can't have Python and Python3 loaded at the same time.
529 * It cause a crash, because RTLD_GLOBAL is needed for
530 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200531 if (python3_loaded())
532 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200533 if (verbose)
534 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200535 return FAIL;
536 }
537#endif
538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 if (hinstPython)
540 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200541 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (!hinstPython)
543 {
544 if (verbose)
545 EMSG2(_(e_loadlib), libname);
546 return FAIL;
547 }
548
549 for (i = 0; python_funcname_table[i].ptr; ++i)
550 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200551 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 python_funcname_table[i].name)) == NULL)
553 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200554 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 hinstPython = 0;
556 if (verbose)
557 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
558 return FAIL;
559 }
560 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200561
562 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
563 * will be present in the library. */
564 ucs_as_encoded_string = symbol_from_dll(hinstPython,
565 "PyUnicodeUCS2_AsEncodedString");
566 if (ucs_as_encoded_string == NULL)
567 ucs_as_encoded_string = symbol_from_dll(hinstPython,
568 "PyUnicodeUCS4_AsEncodedString");
569 if (ucs_as_encoded_string != NULL)
570 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
571 else
572 {
573 close_dll(hinstPython);
574 hinstPython = 0;
575 if (verbose)
576 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
577 return FAIL;
578 }
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return OK;
581}
582
583/*
584 * If python is enabled (there is installed python on Windows system) return
585 * TRUE, else FALSE.
586 */
587 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000588python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
591}
592
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200593/*
594 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * DLL, as this can cause errors (importing data symbols is not reliable).
596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200598get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 PyObject *exmod = PyImport_ImportModule("exceptions");
601 PyObject *exdict = PyModule_GetDict(exmod);
602 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
603 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200604 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
606 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
607 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200608 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 Py_XINCREF(imp_PyExc_AttributeError);
610 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200611 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
613 Py_XINCREF(imp_PyExc_TypeError);
614 Py_XINCREF(imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200615 Py_XINCREF(imp_PyExc_RuntimeError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 Py_XDECREF(exmod);
617}
618#endif /* DYNAMIC_PYTHON */
619
Bram Moolenaardb913952012-06-29 12:54:53 +0200620static int initialised = 0;
621#define PYINITIALISED initialised
622
Bram Moolenaardb913952012-06-29 12:54:53 +0200623#define DICTKEY_GET(err) \
624 if (!PyString_Check(keyObject)) \
625 { \
626 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
627 return err; \
628 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200629 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
630 return err;
631
Bram Moolenaardb913952012-06-29 12:54:53 +0200632#define DICTKEY_UNREF
633#define DICTKEY_DECL
634
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200635#define DESTRUCTOR_FINISH(self) Py_DECREF(self);
636
Bram Moolenaar971db462013-05-12 18:44:48 +0200637#define WIN_PYTHON_REF(win) win->w_python_ref
638#define BUF_PYTHON_REF(buf) buf->b_python_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200639#define TAB_PYTHON_REF(tab) tab->tp_python_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200640
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200641static PyObject *OutputGetattr(PyObject *, char *);
642static PyObject *BufferGetattr(PyObject *, char *);
643static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200644static PyObject *TabPageGetattr(PyObject *, char *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200645static PyObject *RangeGetattr(PyObject *, char *);
646static PyObject *DictionaryGetattr(PyObject *, char*);
647static PyObject *ListGetattr(PyObject *, char *);
648static PyObject *FunctionGetattr(PyObject *, char *);
649
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200650/*
651 * Include the code shared with if_python3.c
652 */
653#include "if_py_both.h"
654
655
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656/******************************************************
657 * Internal function prototypes.
658 */
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660static int PythonIO_Init(void);
661static int PythonMod_Init(void);
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664/******************************************************
665 * 1. Python interpreter main program.
666 */
667
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
669typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
Bram Moolenaar71700b82013-05-15 17:49:05 +0200672#ifndef PY_CAN_RECURSE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000673static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674
675/*
676 * Suspend a thread of the Python interpreter, other threads are allowed to
677 * run.
678 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000679 static void
680Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 saved_python_thread = PyEval_SaveThread();
683}
684
685/*
686 * Restore a thread of the Python interpreter, waits for other threads to
687 * block.
688 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000689 static void
690Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
692 PyEval_RestoreThread(saved_python_thread);
693 saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000694}
Bram Moolenaar71700b82013-05-15 17:49:05 +0200695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 void
698python_end()
699{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000700 static int recurse = 0;
701
702 /* If a crash occurs while doing this, don't try again. */
703 if (recurse != 0)
704 return;
705
706 ++recurse;
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000709 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000710 {
Bram Moolenaar71700b82013-05-15 17:49:05 +0200711# ifdef PY_CAN_RECURSE
712 PyGILState_Ensure();
713# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000714 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200715# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000716 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000719#else
720 if (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#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000730
731 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732}
733
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200734#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
735 int
736python_loaded()
737{
738 return (hinstPython != 0);
739}
740#endif
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 static int
743Python_Init(void)
744{
745 if (!initialised)
746 {
747#ifdef DYNAMIC_PYTHON
748 if (!python_enabled(TRUE))
749 {
750 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
751 goto fail;
752 }
753#endif
754
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100755#ifdef PYTHON_HOME
756 Py_SetPythonHome(PYTHON_HOME);
757#endif
758
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200759 init_structs();
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761#if !defined(MACOS) || defined(MACOS_X_UNIX)
762 Py_Initialize();
763#else
764 PyMac_Initialize();
765#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100766 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100767 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100768 * specific state (such as the system trace hook), will be lost
769 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef DYNAMIC_PYTHON
772 get_exceptions();
773#endif
774
775 if (PythonIO_Init())
776 goto fail;
777
778 if (PythonMod_Init())
779 goto fail;
780
Bram Moolenaardb913952012-06-29 12:54:53 +0200781 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
782
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000783 /* Remove the element from sys.path that was added because of our
784 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200785 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000786 * the current directory in sys.path. */
787 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
788
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100789 /* lock is created and acquired in PyEval_InitThreads() and thread
790 * state is created in Py_Initialize()
791 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
792 * (python must have threads enabled!)
793 * so the following does both: unlock GIL and save thread state in TLS
794 * without deleting thread state
795 */
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200796#ifndef PY_CAN_RECURSE
797 saved_python_thread =
798#endif
799 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 initialised = 1;
802 }
803
804 return 0;
805
806fail:
807 /* We call PythonIO_Flush() here to print any Python errors.
808 * This is OK, as it is possible to call this function even
809 * if PythonIO_Init() has not completed successfully (it will
810 * not do anything in this case).
811 */
812 PythonIO_Flush();
813 return -1;
814}
815
816/*
817 * External interface
818 */
819 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200820DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000822#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 static int recursive = 0;
824#endif
825#if defined(MACOS) && !defined(MACOS_X_UNIX)
826 GrafPtr oldPort;
827#endif
828#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
829 char *saved_locale;
830#endif
Bram Moolenaar71700b82013-05-15 17:49:05 +0200831#ifdef PY_CAN_RECURSE
832 PyGILState_STATE pygilstate;
833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835#ifndef PY_CAN_RECURSE
836 if (recursive)
837 {
838 EMSG(_("E659: Cannot invoke Python recursively"));
839 return;
840 }
841 ++recursive;
842#endif
843
844#if defined(MACOS) && !defined(MACOS_X_UNIX)
845 GetPort(&oldPort);
846 /* Check if the Python library is available */
847 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
848 goto theend;
849#endif
850 if (Python_Init())
851 goto theend;
852
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200853 init_range(arg);
854
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 Python_Release_Vim(); /* leave vim */
856
857#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
858 /* Python only works properly when the LC_NUMERIC locale is "C". */
859 saved_locale = setlocale(LC_NUMERIC, NULL);
860 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
861 saved_locale = NULL;
862 else
863 {
864 /* Need to make a copy, value may change when setting new locale. */
865 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
866 (void)setlocale(LC_NUMERIC, "C");
867 }
868#endif
869
Bram Moolenaar71700b82013-05-15 17:49:05 +0200870#ifdef PY_CAN_RECURSE
871 pygilstate = PyGILState_Ensure();
872#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200876 run((char *) cmd, arg, &pygilstate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877
Bram Moolenaar71700b82013-05-15 17:49:05 +0200878#ifdef PY_CAN_RECURSE
879 PyGILState_Release(pygilstate);
880#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 Python_SaveThread(); /* leave python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
885 if (saved_locale != NULL)
886 {
887 (void)setlocale(LC_NUMERIC, saved_locale);
888 vim_free(saved_locale);
889 }
890#endif
891
892 Python_Lock_Vim(); /* enter vim */
893 PythonIO_Flush();
894#if defined(MACOS) && !defined(MACOS_X_UNIX)
895 SetPort(oldPort);
896#endif
897
898theend:
899#ifndef PY_CAN_RECURSE
900 --recursive;
901#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200902 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903}
904
905/*
906 * ":python"
907 */
908 void
909ex_python(exarg_T *eap)
910{
911 char_u *script;
912
913 script = script_get(eap, eap->arg);
914 if (!eap->skip)
915 {
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200916 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
917 (rangeinitializer) init_range_cmd,
918 (runner) run_cmd,
919 (void *) eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 vim_free(script);
922}
923
924#define BUFFER_SIZE 1024
925
926/*
927 * ":pyfile"
928 */
929 void
930ex_pyfile(exarg_T *eap)
931{
932 static char buffer[BUFFER_SIZE];
933 const char *file = (char *)eap->arg;
934 char *p;
935
936 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
937 * stdio file pointer, but Vim and the Python DLL are compiled with
938 * different options under Windows, meaning that stdio pointers aren't
939 * compatible between the two. Yuk.
940 *
941 * Put the string "execfile('file')" into buffer. But, we need to
942 * escape any backslashes or single quotes in the file name, so that
943 * Python won't mangle the file name.
944 */
945 strcpy(buffer, "execfile('");
946 p = buffer + 10; /* size of "execfile('" */
947
948 while (*file && p < buffer + (BUFFER_SIZE - 3))
949 {
950 if (*file == '\\' || *file == '\'')
951 *p++ = '\\';
952 *p++ = *file++;
953 }
954
955 /* If we didn't finish the file name, we hit a buffer overflow */
956 if (*file != '\0')
957 return;
958
959 /* Put in the terminating "')" and a null */
960 *p++ = '\'';
961 *p++ = ')';
962 *p++ = '\0';
963
964 /* Execute the file */
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200965 DoPyCommand(buffer,
966 (rangeinitializer) init_range_cmd,
967 (runner) run_cmd,
968 (void *) eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969}
970
Bram Moolenaard620aa92013-05-17 16:40:06 +0200971 void
972ex_pydo(exarg_T *eap)
973{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200974 DoPyCommand((char *)eap->arg,
975 (rangeinitializer) init_range_cmd,
976 (runner)run_do,
977 (void *)eap);
Bram Moolenaard620aa92013-05-17 16:40:06 +0200978}
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980/******************************************************
981 * 2. Python output stream: writes output via [e]msg().
982 */
983
984/* Implementation functions
985 */
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 static PyObject *
988OutputGetattr(PyObject *self, char *name)
989{
990 if (strcmp(name, "softspace") == 0)
991 return PyInt_FromLong(((OutputObject *)(self))->softspace);
992
993 return Py_FindMethod(OutputMethods, self, name);
994}
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/***************/
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 static int
999PythonIO_Init(void)
1000{
1001 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001002 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001004 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007/******************************************************
1008 * 3. Implementation of the Vim module for Python
1009 */
1010
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011/* Window type - Implementation functions
1012 * --------------------------------------
1013 */
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/* Buffer type - Implementation functions
1018 * --------------------------------------
1019 */
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1022
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001023static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1024static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026/* Line range type - Implementation functions
1027 * --------------------------------------
1028 */
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1031
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001032static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1033static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035/* Current objects type - Implementation functions
1036 * -----------------------------------------------
1037 */
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001040 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001041 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1042 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001043 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1044 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1045 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001046 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1047 (objobjproc) 0,
1048#if PY_MAJOR_VERSION >= 2
1049 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052};
1053
1054/* Buffer object - Implementation
1055 */
1056
1057 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058BufferGetattr(PyObject *self, char *name)
1059{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001060 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001062 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 return NULL;
1064
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001065 r = BufferAttr((BufferObject *)(self), name);
1066 if (r || PyErr_Occurred())
1067 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 else
1069 return Py_FindMethod(BufferMethods, self, name);
1070}
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072/******************/
1073
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001074 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001075BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001077 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078}
1079
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001080 static PyInt
1081BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001083 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084}
1085
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001087 (PyInquiry) RangeLength, /* sq_length, len(x) */
1088 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1089 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1090 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1091 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1092 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1093 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1094 (objobjproc) 0,
1095#if PY_MAJOR_VERSION >= 2
1096 (binaryfunc) 0,
1097 0,
1098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099};
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101/* Line range object - Implementation
1102 */
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 static PyObject *
1105RangeGetattr(PyObject *self, char *name)
1106{
1107 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001108 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001110 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 else
1112 return Py_FindMethod(RangeMethods, self, name);
1113}
1114
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115/****************/
1116
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001117 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001118RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001120 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 ((RangeObject *)(self))->start,
1122 ((RangeObject *)(self))->end,
1123 &((RangeObject *)(self))->end);
1124}
1125
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001126 static PyInt
1127RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001129 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 ((RangeObject *)(self))->start,
1131 ((RangeObject *)(self))->end,
1132 &((RangeObject *)(self))->end);
1133}
1134
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001135/* TabPage object - Implementation
1136 */
1137
1138 static PyObject *
1139TabPageGetattr(PyObject *self, char *name)
1140{
1141 PyObject *r;
1142
1143 if (CheckTabPage((TabPageObject *)(self)))
1144 return NULL;
1145
1146 r = TabPageAttr((TabPageObject *)(self), name);
1147 if (r || PyErr_Occurred())
1148 return r;
1149 else
1150 return Py_FindMethod(TabPageMethods, self, name);
1151}
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153/* Window object - Implementation
1154 */
1155
1156 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157WindowGetattr(PyObject *self, char *name)
1158{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001159 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001161 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 return NULL;
1163
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001164 r = WindowAttr((WindowObject *)(self), name);
1165 if (r || PyErr_Occurred())
1166 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 else
1168 return Py_FindMethod(WindowMethods, self, name);
1169}
1170
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001171/* Tab page list object - Definitions
1172 */
1173
1174static PySequenceMethods TabListAsSeq = {
1175 (PyInquiry) TabListLength, /* sq_length, len(x) */
1176 (binaryfunc) 0, /* sq_concat, x+y */
1177 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1178 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */
1179 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1180 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1181 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1182 (objobjproc) 0,
1183#if PY_MAJOR_VERSION >= 2
1184 (binaryfunc) 0,
1185 0,
1186#endif
1187};
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189/* Window list object - Definitions
1190 */
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001193 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001195 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1196 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1197 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1198 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001199 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1200 (objobjproc) 0,
1201#if PY_MAJOR_VERSION >= 2
1202 (binaryfunc) 0,
1203 0,
1204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205};
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207/* External interface
1208 */
1209
1210 void
1211python_buffer_free(buf_T *buf)
1212{
Bram Moolenaar971db462013-05-12 18:44:48 +02001213 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001215 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001217 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219}
1220
1221#if defined(FEAT_WINDOWS) || defined(PROTO)
1222 void
1223python_window_free(win_T *win)
1224{
Bram Moolenaar971db462013-05-12 18:44:48 +02001225 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001227 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001229 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 }
1231}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001232
1233 void
1234python_tabpage_free(tabpage_T *tab)
1235{
1236 if (TAB_PYTHON_REF(tab) != NULL)
1237 {
1238 TabPageObject *tp = TAB_PYTHON_REF(tab);
1239 tp->tab = INVALID_TABPAGE_VALUE;
1240 TAB_PYTHON_REF(tab) = NULL;
1241 }
1242}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001245static BufMapObject TheBufferMap =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001247 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248};
1249
1250static WinListObject TheWindowList =
1251{
1252 PyObject_HEAD_INIT(&WinListType)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001253 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254};
1255
1256static CurrentObject TheCurrent =
1257{
1258 PyObject_HEAD_INIT(&CurrentType)
1259};
1260
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001261static TabListObject TheTabPageList =
1262{
1263 PyObject_HEAD_INIT(&TabListType)
1264};
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 static int
1267PythonMod_Init(void)
1268{
1269 PyObject *mod;
1270 PyObject *dict;
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001271 PyObject *tmp;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001272 /* The special value is removed from sys.path in Python_Init(). */
1273 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274
1275 /* Fixups... */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001276 PyType_Ready(&IterType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001277 PyType_Ready(&BufferType);
1278 PyType_Ready(&RangeType);
1279 PyType_Ready(&WindowType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001280 PyType_Ready(&TabPageType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001281 PyType_Ready(&BufMapType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001282 PyType_Ready(&WinListType);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001283 PyType_Ready(&TabListType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001284 PyType_Ready(&CurrentType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001285 PyType_Ready(&OptionsType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 /* Set sys.argv[] to avoid a crash in warn(). */
1288 PySys_SetArgv(1, argv);
1289
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001290 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 dict = PyModule_GetDict(mod);
1292
Bram Moolenaard5f729c2013-05-15 16:04:40 +02001293 VimError = PyErr_NewException("vim.error", NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001296 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001297 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1298 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001299 PyDict_SetItemString(dict, "tabpages", (PyObject *)(void *)&TheTabPageList);
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001300 tmp = DictionaryNew(&globvardict);
1301 PyDict_SetItemString(dict, "vars", tmp);
1302 Py_DECREF(tmp);
1303 tmp = DictionaryNew(&vimvardict);
1304 PyDict_SetItemString(dict, "vvars", tmp);
1305 Py_DECREF(tmp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001306 tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
1307 PyDict_SetItemString(dict, "options", tmp);
1308 Py_DECREF(tmp);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001309 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1310 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1311 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1312 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314 if (PyErr_Occurred())
1315 return -1;
1316
1317 return 0;
1318}
1319
1320/*************************************************************************
1321 * 4. Utility functions for handling the interface between Vim and Python.
1322 */
1323
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324/* Convert a Vim line into a Python string.
1325 * All internal newlines are replaced by null characters.
1326 *
1327 * On errors, the Python exception data is set, and NULL is returned.
1328 */
1329 static PyObject *
1330LineToString(const char *str)
1331{
1332 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001333 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 char *p;
1335
1336 /* Allocate an Python string object, with uninitialised contents. We
1337 * must do it this way, so that we can modify the string in place
1338 * later. See the Python source, Objects/stringobject.c for details.
1339 */
1340 result = PyString_FromStringAndSize(NULL, len);
1341 if (result == NULL)
1342 return NULL;
1343
1344 p = PyString_AsString(result);
1345
1346 while (*str)
1347 {
1348 if (*str == '\n')
1349 *p = '\0';
1350 else
1351 *p = *str;
1352
1353 ++p;
1354 ++str;
1355 }
1356
1357 return result;
1358}
1359
Bram Moolenaardb913952012-06-29 12:54:53 +02001360 static PyObject *
1361DictionaryGetattr(PyObject *self, char *name)
1362{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001363 DictionaryObject *this = ((DictionaryObject *) (self));
1364
1365 if (strcmp(name, "locked") == 0)
1366 return PyInt_FromLong(this->dict->dv_lock);
1367 else if (strcmp(name, "scope") == 0)
1368 return PyInt_FromLong(this->dict->dv_scope);
1369
Bram Moolenaardb913952012-06-29 12:54:53 +02001370 return Py_FindMethod(DictionaryMethods, self, name);
1371}
1372
Bram Moolenaardb913952012-06-29 12:54:53 +02001373static PySequenceMethods ListAsSeq = {
1374 (PyInquiry) ListLength,
1375 (binaryfunc) 0,
1376 (PyIntArgFunc) 0,
1377 (PyIntArgFunc) ListItem,
1378 (PyIntIntArgFunc) ListSlice,
1379 (PyIntObjArgProc) ListAssItem,
1380 (PyIntIntObjArgProc) ListAssSlice,
1381 (objobjproc) 0,
1382#if PY_MAJOR_VERSION >= 2
1383 (binaryfunc) ListConcatInPlace,
1384 0,
1385#endif
1386};
1387
Bram Moolenaardb913952012-06-29 12:54:53 +02001388 static PyObject *
1389ListGetattr(PyObject *self, char *name)
1390{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001391 if (strcmp(name, "locked") == 0)
1392 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1393
Bram Moolenaardb913952012-06-29 12:54:53 +02001394 return Py_FindMethod(ListMethods, self, name);
1395}
1396
Bram Moolenaardb913952012-06-29 12:54:53 +02001397 static PyObject *
1398FunctionGetattr(PyObject *self, char *name)
1399{
1400 FunctionObject *this = (FunctionObject *)(self);
1401
1402 if (strcmp(name, "name") == 0)
1403 return PyString_FromString((char *)(this->name));
1404 else
1405 return Py_FindMethod(FunctionMethods, self, name);
1406}
1407
1408 void
1409do_pyeval (char_u *str, typval_T *rettv)
1410{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001411 DoPyCommand((char *) str,
1412 (rangeinitializer) init_range_eval,
1413 (runner) run_eval,
1414 (void *) rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001415 switch(rettv->v_type)
1416 {
1417 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1418 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1419 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001420 case VAR_UNKNOWN:
1421 rettv->v_type = VAR_NUMBER;
1422 rettv->vval.v_number = 0;
1423 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001424 }
1425}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427/* Don't generate a prototype for the next function, it generates an error on
1428 * newer Python versions. */
1429#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1430
1431 char *
1432Py_GetProgramName(void)
1433{
1434 return "vim";
1435}
1436#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001437
Bram Moolenaardb913952012-06-29 12:54:53 +02001438 void
1439set_ref_in_python (int copyID)
1440{
1441 set_ref_in_py(copyID);
1442}