blob: 8cc50c3cf9b3f0a15fee892643339c37e4dce8c4 [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 Moolenaar774267b2013-05-21 20:51:59 +0200227# define _PyObject_GC_New dll__PyObject_GC_New
228# define PyObject_GC_Del dll_PyObject_GC_Del
229# define PyObject_GC_UnTrack dll_PyObject_GC_UnTrack
Bram Moolenaare7211222012-06-30 13:21:08 +0200230# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
231# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200234# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
235# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200237# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200238# define PyObject_IsTrue dll_PyObject_IsTrue
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
240# define PyType_IsSubtype dll_PyType_IsSubtype
241# endif
242# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
243# define PyObject_Malloc dll_PyObject_Malloc
244# define PyObject_Free dll_PyObject_Free
245# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200246# ifdef PY_USE_CAPSULE
247# define PyCapsule_New dll_PyCapsule_New
248# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
249# else
250# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
251# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253
254/*
255 * Pointers for dynamic link
256 */
257static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
258static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200259static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200260static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
262static int(*dll_PyErr_BadArgument)(void);
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200263static PyObject *(*dll_PyErr_NewException)(char *, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100265static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266static PyObject*(*dll_PyErr_NoMemory)(void);
267static PyObject*(*dll_PyErr_Occurred)(void);
268static void(*dll_PyErr_SetNone)(PyObject *);
269static void(*dll_PyErr_SetString)(PyObject *, const char *);
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200270static void(*dll_PyErr_SetObject)(PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271static void(*dll_PyEval_InitThreads)(void);
272static void(*dll_PyEval_RestoreThread)(PyThreadState *);
273static PyThreadState*(*dll_PyEval_SaveThread)(void);
274# ifdef PY_CAN_RECURSE
275static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
276static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200277# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278static long(*dll_PyInt_AsLong)(PyObject *);
279static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200280static long(*dll_PyLong_AsLong)(PyObject *);
281static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200282static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200284static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000285static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000286static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000287static PyObject*(*dll_PyList_New)(PyInt size);
288static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
289static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200291static int (*dll_PySequence_Check)(PyObject *);
292static PyInt(*dll_PySequence_Size)(PyObject *);
293static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
294static PyInt(*dll_PyTuple_Size)(PyObject *);
295static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
296static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000298static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200300static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
301# ifndef PY_NO_MAPPING_ITEMS
302static PyObject* (*dll_PyMapping_Items)(PyObject *);
303# endif
304static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
305static int (*dll_PyMapping_Check)(PyObject *);
306static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static PyObject*(*dll_PyModule_GetDict)(PyObject *);
308static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200309static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaard620aa92013-05-17 16:40:06 +0200310static PyObject* (*dll_PyObject_GetAttrString)(PyObject *, const char *);
311static PyObject* (*dll_PyObject_SetAttrString)(PyObject *, const char *, PyObject *);
312static PyObject* (*dll_PyObject_CallFunctionObjArgs)(PyObject *, ...);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200314static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000316static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
317static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200319static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200320static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200321static double(*dll_PyFloat_AsDouble)(PyObject *);
322static PyObject*(*dll_PyFloat_FromDouble)(double);
323static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static int(*dll_PySys_SetObject)(char *, PyObject *);
325static int(*dll_PySys_SetArgv)(int, char **);
326static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100327static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328static PyObject*(*dll_Py_BuildValue)(char *, ...);
329static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
330static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200331static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100332static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000334static void(*dll_Py_Finalize)(void);
335static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
Bram Moolenaar774267b2013-05-21 20:51:59 +0200337static PyObject*(*dll__PyObject_GC_New)(PyTypeObject *);
338static void(*dll_PyObject_GC_Del)(void *);
339static void(*dll_PyObject_GC_UnTrack)(void *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200341static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200342static int (*dll_PyObject_IsTrue)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200343# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200344static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200347static PyObject* _Py_ZeroStruct;
348static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
350static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
351# endif
352# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
353static void* (*dll_PyObject_Malloc)(size_t);
354static void (*dll_PyObject_Free)(void*);
355# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200356# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200357static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
358static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200359# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200360static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
361static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363
364static HINSTANCE hinstPython = 0; /* Instance of python.dll */
365
366/* Imported exception objects */
367static PyObject *imp_PyExc_AttributeError;
368static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200369static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370static PyObject *imp_PyExc_KeyboardInterrupt;
371static PyObject *imp_PyExc_TypeError;
372static PyObject *imp_PyExc_ValueError;
Bram Moolenaar8661b172013-05-15 15:44:28 +0200373static PyObject *imp_PyExc_RuntimeError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375# define PyExc_AttributeError imp_PyExc_AttributeError
376# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200377# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
379# define PyExc_TypeError imp_PyExc_TypeError
380# define PyExc_ValueError imp_PyExc_ValueError
Bram Moolenaar8661b172013-05-15 15:44:28 +0200381# define PyExc_RuntimeError imp_PyExc_RuntimeError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382
383/*
384 * Table of name to function pointer of python.
385 */
386# define PYTHON_PROC FARPROC
387static struct
388{
389 char *name;
390 PYTHON_PROC *ptr;
391} python_funcname_table[] =
392{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200393#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
395 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200396 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
397#else
398 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
399 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
400 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
401#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200402 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200403 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
405 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
Bram Moolenaard5f729c2013-05-15 16:04:40 +0200406 {"PyErr_NewException", (PYTHON_PROC*)&dll_PyErr_NewException},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100408 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
410 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
411 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
412 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
Bram Moolenaar4d188da2013-05-15 15:35:09 +0200413 {"PyErr_SetObject", (PYTHON_PROC*)&dll_PyErr_SetObject},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
415 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
416 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
417# ifdef PY_CAN_RECURSE
418 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
419 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
420# endif
421 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
422 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200423 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
424 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200425 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200427 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000429 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
431 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
432 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
433 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200434 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
435 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
436 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
437 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
438 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
439 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
441 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200442 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000443 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200444# ifndef PY_NO_MAPPING_ITEMS
445 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
446# endif
447 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
448 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
449 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
451 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200452 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaard620aa92013-05-17 16:40:06 +0200453 {"PyObject_GetAttrString", (PYTHON_PROC*)&dll_PyObject_GetAttrString},
454 {"PyObject_SetAttrString", (PYTHON_PROC*)&dll_PyObject_SetAttrString},
455 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&dll_PyObject_CallFunctionObjArgs},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200457 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
459 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
460 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
461 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200462 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200463 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
464 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
465 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
466 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
468 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
469 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100470 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200472# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
473 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000474 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
475# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000477# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100478 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000480 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
481 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
Bram Moolenaar774267b2013-05-21 20:51:59 +0200483 {"_PyObject_GC_New", (PYTHON_PROC*)&dll__PyObject_GC_New},
484 {"PyObject_GC_Del", (PYTHON_PROC*)&dll_PyObject_GC_Del},
485 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&dll_PyObject_GC_UnTrack},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200487 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200488 {"PyObject_IsTrue", (PYTHON_PROC*)&dll_PyObject_IsTrue},
Bram Moolenaare7211222012-06-30 13:21:08 +0200489# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200490 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200493 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
494 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
496 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
497# endif
498# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
499 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
500 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
501# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200502# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200503 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
504 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200505# else
506 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
507 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 {"", NULL},
510};
511
512/*
513 * Free python.dll
514 */
515 static void
516end_dynamic_python(void)
517{
518 if (hinstPython)
519 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200520 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 hinstPython = 0;
522 }
523}
524
525/*
526 * Load library and get all pointers.
527 * Parameter 'libname' provides name of DLL.
528 * Return OK or FAIL.
529 */
530 static int
531python_runtime_link_init(char *libname, int verbose)
532{
533 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200534 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100536#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200537 /* Can't have Python and Python3 loaded at the same time.
538 * It cause a crash, because RTLD_GLOBAL is needed for
539 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200540 if (python3_loaded())
541 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200542 if (verbose)
543 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200544 return FAIL;
545 }
546#endif
547
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 if (hinstPython)
549 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200550 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 if (!hinstPython)
552 {
553 if (verbose)
554 EMSG2(_(e_loadlib), libname);
555 return FAIL;
556 }
557
558 for (i = 0; python_funcname_table[i].ptr; ++i)
559 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200560 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 python_funcname_table[i].name)) == NULL)
562 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200563 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 hinstPython = 0;
565 if (verbose)
566 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
567 return FAIL;
568 }
569 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200570
571 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
572 * will be present in the library. */
573 ucs_as_encoded_string = symbol_from_dll(hinstPython,
574 "PyUnicodeUCS2_AsEncodedString");
575 if (ucs_as_encoded_string == NULL)
576 ucs_as_encoded_string = symbol_from_dll(hinstPython,
577 "PyUnicodeUCS4_AsEncodedString");
578 if (ucs_as_encoded_string != NULL)
579 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
580 else
581 {
582 close_dll(hinstPython);
583 hinstPython = 0;
584 if (verbose)
585 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
586 return FAIL;
587 }
588
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 return OK;
590}
591
592/*
593 * If python is enabled (there is installed python on Windows system) return
594 * TRUE, else FALSE.
595 */
596 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000597python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
600}
601
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200602/*
603 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * DLL, as this can cause errors (importing data symbols is not reliable).
605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200607get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 PyObject *exmod = PyImport_ImportModule("exceptions");
610 PyObject *exdict = PyModule_GetDict(exmod);
611 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
612 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200613 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
615 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
616 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
Bram Moolenaar8661b172013-05-15 15:44:28 +0200617 imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 Py_XINCREF(imp_PyExc_AttributeError);
619 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200620 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
622 Py_XINCREF(imp_PyExc_TypeError);
623 Py_XINCREF(imp_PyExc_ValueError);
Bram Moolenaar8661b172013-05-15 15:44:28 +0200624 Py_XINCREF(imp_PyExc_RuntimeError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 Py_XDECREF(exmod);
626}
627#endif /* DYNAMIC_PYTHON */
628
Bram Moolenaardb913952012-06-29 12:54:53 +0200629static int initialised = 0;
630#define PYINITIALISED initialised
631
Bram Moolenaardb913952012-06-29 12:54:53 +0200632#define DICTKEY_GET(err) \
633 if (!PyString_Check(keyObject)) \
634 { \
635 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
636 return err; \
637 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200638 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
639 return err;
640
Bram Moolenaardb913952012-06-29 12:54:53 +0200641#define DICTKEY_UNREF
642#define DICTKEY_DECL
643
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200644#define DESTRUCTOR_FINISH(self) self->ob_type->tp_free((PyObject*)self);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200645
Bram Moolenaar971db462013-05-12 18:44:48 +0200646#define WIN_PYTHON_REF(win) win->w_python_ref
647#define BUF_PYTHON_REF(buf) buf->b_python_ref
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200648#define TAB_PYTHON_REF(tab) tab->tp_python_ref
Bram Moolenaar971db462013-05-12 18:44:48 +0200649
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200650static PyObject *OutputGetattr(PyObject *, char *);
651static PyObject *BufferGetattr(PyObject *, char *);
652static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar5e538ec2013-05-15 15:12:29 +0200653static PyObject *TabPageGetattr(PyObject *, char *);
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200654static PyObject *RangeGetattr(PyObject *, char *);
655static PyObject *DictionaryGetattr(PyObject *, char*);
656static PyObject *ListGetattr(PyObject *, char *);
657static PyObject *FunctionGetattr(PyObject *, char *);
658
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200659#ifndef Py_VISIT
660# define Py_VISIT(obj) visit(obj, arg)
661#endif
662#ifndef Py_CLEAR
663# define Py_CLEAR(obj) \
664 Py_XDECREF(obj); \
665 obj = NULL;
666#endif
667
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200668/*
669 * Include the code shared with if_python3.c
670 */
671#include "if_py_both.h"
672
673
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674/******************************************************
675 * Internal function prototypes.
676 */
677
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678static int PythonMod_Init(void);
679
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681/******************************************************
682 * 1. Python interpreter main program.
683 */
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
686typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
Bram Moolenaar71700b82013-05-15 17:49:05 +0200689#ifndef PY_CAN_RECURSE
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000690static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692/*
693 * Suspend a thread of the Python interpreter, other threads are allowed to
694 * run.
695 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000696 static void
697Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698{
699 saved_python_thread = PyEval_SaveThread();
700}
701
702/*
703 * Restore a thread of the Python interpreter, waits for other threads to
704 * block.
705 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000706 static void
707Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
709 PyEval_RestoreThread(saved_python_thread);
710 saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000711}
Bram Moolenaar71700b82013-05-15 17:49:05 +0200712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 void
715python_end()
716{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000717 static int recurse = 0;
718
719 /* If a crash occurs while doing this, don't try again. */
720 if (recurse != 0)
721 return;
722
723 ++recurse;
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000726 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000727 {
Bram Moolenaar71700b82013-05-15 17:49:05 +0200728# ifdef PY_CAN_RECURSE
729 PyGILState_Ensure();
730# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000731 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200732# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000733 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000736#else
737 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000738 {
Bram Moolenaar71700b82013-05-15 17:49:05 +0200739# ifdef PY_CAN_RECURSE
740 PyGILState_Ensure();
741# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000742 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200743# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000744 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000747
748 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749}
750
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200751#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
752 int
753python_loaded()
754{
755 return (hinstPython != 0);
756}
757#endif
758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 static int
760Python_Init(void)
761{
762 if (!initialised)
763 {
764#ifdef DYNAMIC_PYTHON
765 if (!python_enabled(TRUE))
766 {
767 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
768 goto fail;
769 }
770#endif
771
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100772#ifdef PYTHON_HOME
773 Py_SetPythonHome(PYTHON_HOME);
774#endif
775
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200776 init_structs();
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#if !defined(MACOS) || defined(MACOS_X_UNIX)
779 Py_Initialize();
780#else
781 PyMac_Initialize();
782#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100783 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100784 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100785 * specific state (such as the system trace hook), will be lost
786 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef DYNAMIC_PYTHON
789 get_exceptions();
790#endif
791
Bram Moolenaar1dc28782013-05-21 19:11:01 +0200792 if (PythonIO_Init_io())
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 goto fail;
794
795 if (PythonMod_Init())
796 goto fail;
797
Bram Moolenaardb913952012-06-29 12:54:53 +0200798 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
799
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000800 /* Remove the element from sys.path that was added because of our
801 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200802 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000803 * the current directory in sys.path. */
804 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
805
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100806 /* lock is created and acquired in PyEval_InitThreads() and thread
807 * state is created in Py_Initialize()
808 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
809 * (python must have threads enabled!)
810 * so the following does both: unlock GIL and save thread state in TLS
811 * without deleting thread state
812 */
Bram Moolenaar03db85b2013-05-15 14:51:35 +0200813#ifndef PY_CAN_RECURSE
814 saved_python_thread =
815#endif
816 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818 initialised = 1;
819 }
820
821 return 0;
822
823fail:
824 /* We call PythonIO_Flush() here to print any Python errors.
825 * This is OK, as it is possible to call this function even
Bram Moolenaar1dc28782013-05-21 19:11:01 +0200826 * if PythonIO_Init_io() has not completed successfully (it will
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 * not do anything in this case).
828 */
829 PythonIO_Flush();
830 return -1;
831}
832
833/*
834 * External interface
835 */
836 static void
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200837DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000839#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 static int recursive = 0;
841#endif
842#if defined(MACOS) && !defined(MACOS_X_UNIX)
843 GrafPtr oldPort;
844#endif
845#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
846 char *saved_locale;
847#endif
Bram Moolenaar71700b82013-05-15 17:49:05 +0200848#ifdef PY_CAN_RECURSE
849 PyGILState_STATE pygilstate;
850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
852#ifndef PY_CAN_RECURSE
853 if (recursive)
854 {
855 EMSG(_("E659: Cannot invoke Python recursively"));
856 return;
857 }
858 ++recursive;
859#endif
860
861#if defined(MACOS) && !defined(MACOS_X_UNIX)
862 GetPort(&oldPort);
863 /* Check if the Python library is available */
864 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
865 goto theend;
866#endif
867 if (Python_Init())
868 goto theend;
869
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200870 init_range(arg);
871
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 Python_Release_Vim(); /* leave vim */
873
874#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
875 /* Python only works properly when the LC_NUMERIC locale is "C". */
876 saved_locale = setlocale(LC_NUMERIC, NULL);
877 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
878 saved_locale = NULL;
879 else
880 {
881 /* Need to make a copy, value may change when setting new locale. */
882 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
883 (void)setlocale(LC_NUMERIC, "C");
884 }
885#endif
886
Bram Moolenaar71700b82013-05-15 17:49:05 +0200887#ifdef PY_CAN_RECURSE
888 pygilstate = PyGILState_Ensure();
889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 Python_RestoreThread(); /* enter python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
Bram Moolenaar2a0f3d32013-05-21 22:23:56 +0200893 run((char *) cmd, arg
894#ifdef PY_CAN_RECURSE
895 , &pygilstate
896#endif
897 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898
Bram Moolenaar71700b82013-05-15 17:49:05 +0200899#ifdef PY_CAN_RECURSE
900 PyGILState_Release(pygilstate);
901#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 Python_SaveThread(); /* leave python */
Bram Moolenaar71700b82013-05-15 17:49:05 +0200903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904
905#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
906 if (saved_locale != NULL)
907 {
908 (void)setlocale(LC_NUMERIC, saved_locale);
909 vim_free(saved_locale);
910 }
911#endif
912
913 Python_Lock_Vim(); /* enter vim */
914 PythonIO_Flush();
915#if defined(MACOS) && !defined(MACOS_X_UNIX)
916 SetPort(oldPort);
917#endif
918
919theend:
920#ifndef PY_CAN_RECURSE
921 --recursive;
922#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200923 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
926/*
927 * ":python"
928 */
929 void
930ex_python(exarg_T *eap)
931{
932 char_u *script;
933
934 script = script_get(eap, eap->arg);
935 if (!eap->skip)
936 {
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200937 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
938 (rangeinitializer) init_range_cmd,
939 (runner) run_cmd,
940 (void *) eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
942 vim_free(script);
943}
944
945#define BUFFER_SIZE 1024
946
947/*
948 * ":pyfile"
949 */
950 void
951ex_pyfile(exarg_T *eap)
952{
953 static char buffer[BUFFER_SIZE];
954 const char *file = (char *)eap->arg;
955 char *p;
956
957 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
958 * stdio file pointer, but Vim and the Python DLL are compiled with
959 * different options under Windows, meaning that stdio pointers aren't
960 * compatible between the two. Yuk.
961 *
962 * Put the string "execfile('file')" into buffer. But, we need to
963 * escape any backslashes or single quotes in the file name, so that
964 * Python won't mangle the file name.
965 */
966 strcpy(buffer, "execfile('");
967 p = buffer + 10; /* size of "execfile('" */
968
969 while (*file && p < buffer + (BUFFER_SIZE - 3))
970 {
971 if (*file == '\\' || *file == '\'')
972 *p++ = '\\';
973 *p++ = *file++;
974 }
975
976 /* If we didn't finish the file name, we hit a buffer overflow */
977 if (*file != '\0')
978 return;
979
980 /* Put in the terminating "')" and a null */
981 *p++ = '\'';
982 *p++ = ')';
983 *p++ = '\0';
984
985 /* Execute the file */
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200986 DoPyCommand(buffer,
987 (rangeinitializer) init_range_cmd,
988 (runner) run_cmd,
989 (void *) eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991
Bram Moolenaard620aa92013-05-17 16:40:06 +0200992 void
993ex_pydo(exarg_T *eap)
994{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +0200995 DoPyCommand((char *)eap->arg,
996 (rangeinitializer) init_range_cmd,
997 (runner)run_do,
998 (void *)eap);
Bram Moolenaard620aa92013-05-17 16:40:06 +0200999}
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/******************************************************
1002 * 2. Python output stream: writes output via [e]msg().
1003 */
1004
1005/* Implementation functions
1006 */
1007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 static PyObject *
1009OutputGetattr(PyObject *self, char *name)
1010{
1011 if (strcmp(name, "softspace") == 0)
1012 return PyInt_FromLong(((OutputObject *)(self))->softspace);
1013
1014 return Py_FindMethod(OutputMethods, self, name);
1015}
1016
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017/******************************************************
1018 * 3. Implementation of the Vim module for Python
1019 */
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021/* Window type - Implementation functions
1022 * --------------------------------------
1023 */
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027/* Buffer type - Implementation functions
1028 * --------------------------------------
1029 */
1030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1032
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001033static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1034static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036/* Line range type - Implementation functions
1037 * --------------------------------------
1038 */
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1041
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001042static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1043static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045/* Current objects type - Implementation functions
1046 * -----------------------------------------------
1047 */
1048
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001050 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001051 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1052 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001053 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1054 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1055 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001056 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1057 (objobjproc) 0,
1058#if PY_MAJOR_VERSION >= 2
1059 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062};
1063
1064/* Buffer object - Implementation
1065 */
1066
1067 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068BufferGetattr(PyObject *self, char *name)
1069{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001070 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001072 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 return NULL;
1074
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001075 r = BufferAttr((BufferObject *)(self), name);
1076 if (r || PyErr_Occurred())
1077 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 else
1079 return Py_FindMethod(BufferMethods, self, name);
1080}
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/******************/
1083
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001084 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001085BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001087 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088}
1089
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001090 static PyInt
1091BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001093 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001097 (PyInquiry) RangeLength, /* sq_length, len(x) */
1098 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1099 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1100 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1101 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1102 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1103 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1104 (objobjproc) 0,
1105#if PY_MAJOR_VERSION >= 2
1106 (binaryfunc) 0,
1107 0,
1108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109};
1110
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111/* Line range object - Implementation
1112 */
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 static PyObject *
1115RangeGetattr(PyObject *self, char *name)
1116{
1117 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001118 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001120 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else
1122 return Py_FindMethod(RangeMethods, self, name);
1123}
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125/****************/
1126
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001127 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001128RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001130 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 ((RangeObject *)(self))->start,
1132 ((RangeObject *)(self))->end,
1133 &((RangeObject *)(self))->end);
1134}
1135
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001136 static PyInt
1137RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001139 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 ((RangeObject *)(self))->start,
1141 ((RangeObject *)(self))->end,
1142 &((RangeObject *)(self))->end);
1143}
1144
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001145/* TabPage object - Implementation
1146 */
1147
1148 static PyObject *
1149TabPageGetattr(PyObject *self, char *name)
1150{
1151 PyObject *r;
1152
1153 if (CheckTabPage((TabPageObject *)(self)))
1154 return NULL;
1155
1156 r = TabPageAttr((TabPageObject *)(self), name);
1157 if (r || PyErr_Occurred())
1158 return r;
1159 else
1160 return Py_FindMethod(TabPageMethods, self, name);
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163/* Window object - Implementation
1164 */
1165
1166 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167WindowGetattr(PyObject *self, char *name)
1168{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001169 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001171 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 return NULL;
1173
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001174 r = WindowAttr((WindowObject *)(self), name);
1175 if (r || PyErr_Occurred())
1176 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 else
1178 return Py_FindMethod(WindowMethods, self, name);
1179}
1180
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001181/* Tab page list object - Definitions
1182 */
1183
1184static PySequenceMethods TabListAsSeq = {
1185 (PyInquiry) TabListLength, /* sq_length, len(x) */
1186 (binaryfunc) 0, /* sq_concat, x+y */
1187 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1188 (PyIntArgFunc) TabListItem, /* sq_item, x[i] */
1189 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1190 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1191 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1192 (objobjproc) 0,
1193#if PY_MAJOR_VERSION >= 2
1194 (binaryfunc) 0,
1195 0,
1196#endif
1197};
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199/* Window list object - Definitions
1200 */
1201
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001203 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001205 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1206 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1207 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1208 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001209 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1210 (objobjproc) 0,
1211#if PY_MAJOR_VERSION >= 2
1212 (binaryfunc) 0,
1213 0,
1214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215};
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217/* External interface
1218 */
1219
1220 void
1221python_buffer_free(buf_T *buf)
1222{
Bram Moolenaar971db462013-05-12 18:44:48 +02001223 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001225 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001227 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229}
1230
1231#if defined(FEAT_WINDOWS) || defined(PROTO)
1232 void
1233python_window_free(win_T *win)
1234{
Bram Moolenaar971db462013-05-12 18:44:48 +02001235 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001237 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001239 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001242
1243 void
1244python_tabpage_free(tabpage_T *tab)
1245{
1246 if (TAB_PYTHON_REF(tab) != NULL)
1247 {
1248 TabPageObject *tp = TAB_PYTHON_REF(tab);
1249 tp->tab = INVALID_TABPAGE_VALUE;
1250 TAB_PYTHON_REF(tab) = NULL;
1251 }
1252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#endif
1254
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001255 static int
1256add_object(PyObject *dict, const char *name, PyObject *object)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257{
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001258 if (PyDict_SetItemString(dict, (char *) name, object))
1259 return -1;
1260 Py_DECREF(object);
1261 return 0;
1262}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02001263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 static int
1265PythonMod_Init(void)
1266{
1267 PyObject *mod;
1268 PyObject *dict;
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001269
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001270 /* The special value is removed from sys.path in Python_Init(). */
1271 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001273 if (init_types())
1274 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 /* Set sys.argv[] to avoid a crash in warn(). */
1277 PySys_SetArgv(1, argv);
1278
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001279 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 dict = PyModule_GetDict(mod);
1281
Bram Moolenaar1dc28782013-05-21 19:11:01 +02001282 return populate_module(dict, add_object);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283}
1284
1285/*************************************************************************
1286 * 4. Utility functions for handling the interface between Vim and Python.
1287 */
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289/* Convert a Vim line into a Python string.
1290 * All internal newlines are replaced by null characters.
1291 *
1292 * On errors, the Python exception data is set, and NULL is returned.
1293 */
1294 static PyObject *
1295LineToString(const char *str)
1296{
1297 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001298 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 char *p;
1300
1301 /* Allocate an Python string object, with uninitialised contents. We
1302 * must do it this way, so that we can modify the string in place
1303 * later. See the Python source, Objects/stringobject.c for details.
1304 */
1305 result = PyString_FromStringAndSize(NULL, len);
1306 if (result == NULL)
1307 return NULL;
1308
1309 p = PyString_AsString(result);
1310
1311 while (*str)
1312 {
1313 if (*str == '\n')
1314 *p = '\0';
1315 else
1316 *p = *str;
1317
1318 ++p;
1319 ++str;
1320 }
1321
1322 return result;
1323}
1324
Bram Moolenaardb913952012-06-29 12:54:53 +02001325 static PyObject *
1326DictionaryGetattr(PyObject *self, char *name)
1327{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001328 DictionaryObject *this = ((DictionaryObject *) (self));
1329
1330 if (strcmp(name, "locked") == 0)
1331 return PyInt_FromLong(this->dict->dv_lock);
1332 else if (strcmp(name, "scope") == 0)
1333 return PyInt_FromLong(this->dict->dv_scope);
1334
Bram Moolenaardb913952012-06-29 12:54:53 +02001335 return Py_FindMethod(DictionaryMethods, self, name);
1336}
1337
Bram Moolenaardb913952012-06-29 12:54:53 +02001338static PySequenceMethods ListAsSeq = {
1339 (PyInquiry) ListLength,
1340 (binaryfunc) 0,
1341 (PyIntArgFunc) 0,
1342 (PyIntArgFunc) ListItem,
1343 (PyIntIntArgFunc) ListSlice,
1344 (PyIntObjArgProc) ListAssItem,
1345 (PyIntIntObjArgProc) ListAssSlice,
1346 (objobjproc) 0,
1347#if PY_MAJOR_VERSION >= 2
1348 (binaryfunc) ListConcatInPlace,
1349 0,
1350#endif
1351};
1352
Bram Moolenaardb913952012-06-29 12:54:53 +02001353 static PyObject *
1354ListGetattr(PyObject *self, char *name)
1355{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001356 if (strcmp(name, "locked") == 0)
1357 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1358
Bram Moolenaardb913952012-06-29 12:54:53 +02001359 return Py_FindMethod(ListMethods, self, name);
1360}
1361
Bram Moolenaardb913952012-06-29 12:54:53 +02001362 static PyObject *
1363FunctionGetattr(PyObject *self, char *name)
1364{
1365 FunctionObject *this = (FunctionObject *)(self);
1366
1367 if (strcmp(name, "name") == 0)
1368 return PyString_FromString((char *)(this->name));
1369 else
1370 return Py_FindMethod(FunctionMethods, self, name);
1371}
1372
1373 void
1374do_pyeval (char_u *str, typval_T *rettv)
1375{
Bram Moolenaarb52f4c02013-05-21 18:19:38 +02001376 DoPyCommand((char *) str,
1377 (rangeinitializer) init_range_eval,
1378 (runner) run_eval,
1379 (void *) rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +02001380 switch(rettv->v_type)
1381 {
1382 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1383 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1384 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001385 case VAR_UNKNOWN:
1386 rettv->v_type = VAR_NUMBER;
1387 rettv->vval.v_number = 0;
1388 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001389 }
1390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392/* Don't generate a prototype for the next function, it generates an error on
1393 * newer Python versions. */
1394#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1395
1396 char *
1397Py_GetProgramName(void)
1398{
1399 return "vim";
1400}
1401#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001402
Bram Moolenaardb913952012-06-29 12:54:53 +02001403 void
1404set_ref_in_python (int copyID)
1405{
1406 set_ref_in_py(copyID);
1407}