blob: 56db2b273c853389de3a7b5cefe73cc7f4194947 [file] [log] [blame]
Bram Moolenaardb913952012-06-29 12:54:53 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * Python extensions by Paul Moore.
11 * Changes for Unix by David Leonard.
12 *
13 * This consists of four parts:
14 * 1. Python interpreter main program
15 * 2. Python output stream: writes output via [e]msg().
16 * 3. Implementation of the Vim module for Python
17 * 4. Utility functions for handling the interface between Vim and Python.
18 */
19
20#include "vim.h"
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#include <limits.h>
23
24/* Python.h defines _POSIX_THREADS itself (if needed) */
25#ifdef _POSIX_THREADS
26# undef _POSIX_THREADS
27#endif
28
Bram Moolenaar860cae12010-06-05 23:22:07 +020029#if defined(_WIN32) && defined(HAVE_FCNTL_H)
Bram Moolenaar071d4272004-06-13 20:20:40 +000030# undef HAVE_FCNTL_H
31#endif
32
33#ifdef _DEBUG
34# undef _DEBUG
35#endif
36
37#ifdef HAVE_STDARG_H
38# undef HAVE_STDARG_H /* Python's config.h defines it as well. */
39#endif
Bram Moolenaarbe2c9ae2009-11-11 14:06:59 +000040#ifdef _POSIX_C_SOURCE
41# undef _POSIX_C_SOURCE /* pyconfig.h defines it as well. */
42#endif
43#ifdef _XOPEN_SOURCE
44# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */
45#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#include <Python.h>
48#if defined(MACOS) && !defined(MACOS_X_UNIX)
49# include "macglue.h"
50# include <CodeFragments.h>
51#endif
52#undef main /* Defined in python.h - aargh */
53#undef HAVE_FCNTL_H /* Clash with os_win32.h */
54
Bram Moolenaare8cdcef2012-09-12 20:21:43 +020055#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
56# define PY_SSIZE_T_CLEAN
57#endif
58
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020059static void init_structs(void);
60
Bram Moolenaardb913952012-06-29 12:54:53 +020061#define PyBytes_FromString PyString_FromString
Bram Moolenaar335e0b62013-04-24 13:47:45 +020062#define PyBytes_Check PyString_Check
Bram Moolenaardb913952012-06-29 12:54:53 +020063
Bram Moolenaar19e60942011-06-19 00:27:51 +020064/* No-op conversion functions, use with care! */
65#define PyString_AsBytes(obj) (obj)
66#define PyString_FreeBytes(obj)
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068#if !defined(FEAT_PYTHON) && defined(PROTO)
69/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000070# define PyObject Py_ssize_t
71# define PyThreadState Py_ssize_t
72# define PyTypeObject Py_ssize_t
73struct PyMethodDef { Py_ssize_t a; };
74# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif
76
Bram Moolenaar2afa3232012-06-29 16:28:28 +020077#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
78# define PY_USE_CAPSULE
79#endif
80
Bram Moolenaar2c45e942008-06-04 11:35:26 +000081#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
82# define PyInt Py_ssize_t
83# define PyInquiry lenfunc
84# define PyIntArgFunc ssizeargfunc
85# define PyIntIntArgFunc ssizessizeargfunc
86# define PyIntObjArgProc ssizeobjargproc
87# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000088# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000089#else
90# define PyInt int
Bram Moolenaar4d1da492013-04-24 13:39:15 +020091# define lenfunc inquiry
Bram Moolenaar2c45e942008-06-04 11:35:26 +000092# define PyInquiry inquiry
93# define PyIntArgFunc intargfunc
94# define PyIntIntArgFunc intintargfunc
95# define PyIntObjArgProc intobjargproc
96# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000097# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000098#endif
99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100/* Parser flags */
101#define single_input 256
102#define file_input 257
103#define eval_input 258
104
105#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
106 /* Python 2.3: can invoke ":python" recursively. */
107# define PY_CAN_RECURSE
108#endif
109
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# if defined(DYNAMIC_PYTHON) || defined(PROTO)
111# ifndef DYNAMIC_PYTHON
112# define HINSTANCE long_u /* for generating prototypes */
113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200115# ifndef WIN3264
116# include <dlfcn.h>
117# define FARPROC void*
118# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100119# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200120# define load_dll(n) dlopen((n), RTLD_LAZY)
121# else
122# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
123# endif
124# define close_dll dlclose
125# define symbol_from_dll dlsym
126# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200127# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200128# define close_dll FreeLibrary
129# define symbol_from_dll GetProcAddress
130# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200131
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000132/* This makes if_python.c compile without warnings against Python 2.5
133 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200134# undef PyRun_SimpleString
Bram Moolenaardb913952012-06-29 12:54:53 +0200135# undef PyRun_String
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200136# undef PyArg_Parse
137# undef PyArg_ParseTuple
138# undef Py_BuildValue
139# undef Py_InitModule4
140# undef Py_InitModule4_64
Bram Moolenaardb913952012-06-29 12:54:53 +0200141# undef PyObject_CallMethod
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143/*
144 * Wrapper defines
145 */
146# define PyArg_Parse dll_PyArg_Parse
147# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200148# define PyMem_Free dll_PyMem_Free
Bram Moolenaardb913952012-06-29 12:54:53 +0200149# define PyMem_Malloc dll_PyMem_Malloc
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define PyDict_SetItemString dll_PyDict_SetItemString
151# define PyErr_BadArgument dll_PyErr_BadArgument
152# define PyErr_Clear dll_PyErr_Clear
Bram Moolenaar4d369872013-02-20 16:09:43 +0100153# define PyErr_PrintEx dll_PyErr_PrintEx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154# define PyErr_NoMemory dll_PyErr_NoMemory
155# define PyErr_Occurred dll_PyErr_Occurred
156# define PyErr_SetNone dll_PyErr_SetNone
157# define PyErr_SetString dll_PyErr_SetString
158# 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 Moolenaar071d4272004-06-13 20:20:40 +0000199# define PyString_AsString dll_PyString_AsString
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200200# define PyString_AsStringAndSize dll_PyString_AsStringAndSize
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201# define PyString_FromString dll_PyString_FromString
202# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
203# define PyString_Size dll_PyString_Size
204# define PyString_Type (*dll_PyString_Type)
Bram Moolenaardb913952012-06-29 12:54:53 +0200205# define PyUnicode_Type (*dll_PyUnicode_Type)
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200206# undef PyUnicode_AsEncodedString
207# define PyUnicode_AsEncodedString py_PyUnicode_AsEncodedString
Bram Moolenaardb913952012-06-29 12:54:53 +0200208# define PyFloat_AsDouble dll_PyFloat_AsDouble
209# define PyFloat_FromDouble dll_PyFloat_FromDouble
210# define PyFloat_Type (*dll_PyFloat_Type)
211# define PyImport_AddModule (*dll_PyImport_AddModule)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212# define PySys_SetObject dll_PySys_SetObject
213# define PySys_SetArgv dll_PySys_SetArgv
214# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100215# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216# define Py_BuildValue dll_Py_BuildValue
217# define Py_FindMethod dll_Py_FindMethod
218# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100219# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000221# define Py_Finalize dll_Py_Finalize
222# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223# define _PyObject_New dll__PyObject_New
Bram Moolenaare7211222012-06-30 13:21:08 +0200224# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
225# define _PyObject_NextNotImplemented (*dll__PyObject_NextNotImplemented)
226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227# define _Py_NoneStruct (*dll__Py_NoneStruct)
Bram Moolenaar66b79852012-09-21 14:00:35 +0200228# define _Py_ZeroStruct (*dll__Py_ZeroStruct)
229# define _Py_TrueStruct (*dll__Py_TrueStruct)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230# define PyObject_Init dll__PyObject_Init
Bram Moolenaardb913952012-06-29 12:54:53 +0200231# define PyObject_GetIter dll_PyObject_GetIter
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
233# define PyType_IsSubtype dll_PyType_IsSubtype
234# endif
235# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
236# define PyObject_Malloc dll_PyObject_Malloc
237# define PyObject_Free dll_PyObject_Free
238# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200239# ifdef PY_USE_CAPSULE
240# define PyCapsule_New dll_PyCapsule_New
241# define PyCapsule_GetPointer dll_PyCapsule_GetPointer
242# else
243# define PyCObject_FromVoidPtr dll_PyCObject_FromVoidPtr
244# define PyCObject_AsVoidPtr dll_PyCObject_AsVoidPtr
245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
248 * Pointers for dynamic link
249 */
250static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
251static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200252static int(*dll_PyMem_Free)(void *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200253static void* (*dll_PyMem_Malloc)(size_t);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
255static int(*dll_PyErr_BadArgument)(void);
256static void(*dll_PyErr_Clear)(void);
Bram Moolenaar4d369872013-02-20 16:09:43 +0100257static void(*dll_PyErr_PrintEx)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258static PyObject*(*dll_PyErr_NoMemory)(void);
259static PyObject*(*dll_PyErr_Occurred)(void);
260static void(*dll_PyErr_SetNone)(PyObject *);
261static void(*dll_PyErr_SetString)(PyObject *, const char *);
262static void(*dll_PyEval_InitThreads)(void);
263static void(*dll_PyEval_RestoreThread)(PyThreadState *);
264static PyThreadState*(*dll_PyEval_SaveThread)(void);
265# ifdef PY_CAN_RECURSE
266static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
267static void (*dll_PyGILState_Release)(PyGILState_STATE);
Bram Moolenaardb913952012-06-29 12:54:53 +0200268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269static long(*dll_PyInt_AsLong)(PyObject *);
270static PyObject*(*dll_PyInt_FromLong)(long);
Bram Moolenaardb913952012-06-29 12:54:53 +0200271static long(*dll_PyLong_AsLong)(PyObject *);
272static PyObject*(*dll_PyLong_FromLong)(long);
Bram Moolenaar66b79852012-09-21 14:00:35 +0200273static PyTypeObject* dll_PyBool_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274static PyTypeObject* dll_PyInt_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200275static PyTypeObject* dll_PyLong_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000276static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000277static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000278static PyObject*(*dll_PyList_New)(PyInt size);
279static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
280static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281static PyTypeObject* dll_PyList_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200282static int (*dll_PySequence_Check)(PyObject *);
283static PyInt(*dll_PySequence_Size)(PyObject *);
284static PyObject*(*dll_PySequence_GetItem)(PyObject *, PyInt);
285static PyInt(*dll_PyTuple_Size)(PyObject *);
286static PyObject*(*dll_PyTuple_GetItem)(PyObject *, PyInt);
287static PyTypeObject* dll_PyTuple_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000289static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200291static int (*dll_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **);
292# ifndef PY_NO_MAPPING_ITEMS
293static PyObject* (*dll_PyMapping_Items)(PyObject *);
294# endif
295static PyObject* (*dll_PyObject_CallMethod)(PyObject *, char *, PyObject *);
296static int (*dll_PyMapping_Check)(PyObject *);
297static PyObject* (*dll_PyIter_Next)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298static PyObject*(*dll_PyModule_GetDict)(PyObject *);
299static int(*dll_PyRun_SimpleString)(char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200300static PyObject *(*dll_PyRun_String)(char *, int, PyObject *, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301static char*(*dll_PyString_AsString)(PyObject *);
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200302static int(*dll_PyString_AsStringAndSize)(PyObject *, char **, int *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000304static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
305static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static PyTypeObject* dll_PyString_Type;
Bram Moolenaardb913952012-06-29 12:54:53 +0200307static PyTypeObject* dll_PyUnicode_Type;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200308static PyObject *(*py_PyUnicode_AsEncodedString)(PyObject *, char *, char *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200309static double(*dll_PyFloat_AsDouble)(PyObject *);
310static PyObject*(*dll_PyFloat_FromDouble)(double);
311static PyTypeObject* dll_PyFloat_Type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312static int(*dll_PySys_SetObject)(char *, PyObject *);
313static int(*dll_PySys_SetArgv)(int, char **);
314static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100315static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316static PyObject*(*dll_Py_BuildValue)(char *, ...);
317static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
318static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaardb913952012-06-29 12:54:53 +0200319static PyObject*(*dll_PyImport_AddModule)(char *);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100320static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000322static void(*dll_Py_Finalize)(void);
323static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
325static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200326static PyObject* (*dll_PyObject_GetIter)(PyObject *);
Bram Moolenaare7211222012-06-30 13:21:08 +0200327# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200328static iternextfunc dll__PyObject_NextNotImplemented;
Bram Moolenaare7211222012-06-30 13:21:08 +0200329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330static PyObject* dll__Py_NoneStruct;
Bram Moolenaar66b79852012-09-21 14:00:35 +0200331static PyObject* _Py_ZeroStruct;
332static PyObject* dll__Py_TrueStruct;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
334static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
335# endif
336# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
337static void* (*dll_PyObject_Malloc)(size_t);
338static void (*dll_PyObject_Free)(void*);
339# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200340# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200341static PyObject* (*dll_PyCapsule_New)(void *, char *, PyCapsule_Destructor);
342static void* (*dll_PyCapsule_GetPointer)(PyObject *, char *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200343# else
Bram Moolenaar221d6872012-06-30 13:34:34 +0200344static PyObject* (*dll_PyCObject_FromVoidPtr)(void *cobj, void (*destr)(void *));
345static void* (*dll_PyCObject_AsVoidPtr)(PyObject *);
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
348static HINSTANCE hinstPython = 0; /* Instance of python.dll */
349
350/* Imported exception objects */
351static PyObject *imp_PyExc_AttributeError;
352static PyObject *imp_PyExc_IndexError;
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200353static PyObject *imp_PyExc_KeyError;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354static PyObject *imp_PyExc_KeyboardInterrupt;
355static PyObject *imp_PyExc_TypeError;
356static PyObject *imp_PyExc_ValueError;
357
358# define PyExc_AttributeError imp_PyExc_AttributeError
359# define PyExc_IndexError imp_PyExc_IndexError
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200360# define PyExc_KeyError imp_PyExc_KeyError
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
362# define PyExc_TypeError imp_PyExc_TypeError
363# define PyExc_ValueError imp_PyExc_ValueError
364
365/*
366 * Table of name to function pointer of python.
367 */
368# define PYTHON_PROC FARPROC
369static struct
370{
371 char *name;
372 PYTHON_PROC *ptr;
373} python_funcname_table[] =
374{
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200375#ifndef PY_SSIZE_T_CLEAN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
377 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaare8cdcef2012-09-12 20:21:43 +0200378 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
379#else
380 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&dll_PyArg_Parse},
381 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
382 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&dll_Py_BuildValue},
383#endif
Bram Moolenaar19e60942011-06-19 00:27:51 +0200384 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaardb913952012-06-29 12:54:53 +0200385 {"PyMem_Malloc", (PYTHON_PROC*)&dll_PyMem_Malloc},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
387 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
388 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
Bram Moolenaar4d369872013-02-20 16:09:43 +0100389 {"PyErr_PrintEx", (PYTHON_PROC*)&dll_PyErr_PrintEx},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
391 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
392 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
393 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
394 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
395 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
396 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
397# ifdef PY_CAN_RECURSE
398 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
399 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
400# endif
401 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
402 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
Bram Moolenaardb913952012-06-29 12:54:53 +0200403 {"PyLong_AsLong", (PYTHON_PROC*)&dll_PyLong_AsLong},
404 {"PyLong_FromLong", (PYTHON_PROC*)&dll_PyLong_FromLong},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200405 {"PyBool_Type", (PYTHON_PROC*)&dll_PyBool_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200407 {"PyLong_Type", (PYTHON_PROC*)&dll_PyLong_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000409 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
411 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
412 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
413 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200414 {"PySequence_GetItem", (PYTHON_PROC*)&dll_PySequence_GetItem},
415 {"PySequence_Size", (PYTHON_PROC*)&dll_PySequence_Size},
416 {"PySequence_Check", (PYTHON_PROC*)&dll_PySequence_Check},
417 {"PyTuple_GetItem", (PYTHON_PROC*)&dll_PyTuple_GetItem},
418 {"PyTuple_Size", (PYTHON_PROC*)&dll_PyTuple_Size},
419 {"PyTuple_Type", (PYTHON_PROC*)&dll_PyTuple_Type},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
421 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200422 {"PyDict_Next", (PYTHON_PROC*)&dll_PyDict_Next},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000423 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaardb913952012-06-29 12:54:53 +0200424# ifndef PY_NO_MAPPING_ITEMS
425 {"PyMapping_Items", (PYTHON_PROC*)&dll_PyMapping_Items},
426# endif
427 {"PyObject_CallMethod", (PYTHON_PROC*)&dll_PyObject_CallMethod},
428 {"PyMapping_Check", (PYTHON_PROC*)&dll_PyMapping_Check},
429 {"PyIter_Next", (PYTHON_PROC*)&dll_PyIter_Next},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
431 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
Bram Moolenaardb913952012-06-29 12:54:53 +0200432 {"PyRun_String", (PYTHON_PROC*)&dll_PyRun_String},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200434 {"PyString_AsStringAndSize", (PYTHON_PROC*)&dll_PyString_AsStringAndSize},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
436 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
437 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
438 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200439 {"PyUnicode_Type", (PYTHON_PROC*)&dll_PyUnicode_Type},
Bram Moolenaardb913952012-06-29 12:54:53 +0200440 {"PyFloat_Type", (PYTHON_PROC*)&dll_PyFloat_Type},
441 {"PyFloat_AsDouble", (PYTHON_PROC*)&dll_PyFloat_AsDouble},
442 {"PyFloat_FromDouble", (PYTHON_PROC*)&dll_PyFloat_FromDouble},
443 {"PyImport_AddModule", (PYTHON_PROC*)&dll_PyImport_AddModule},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
445 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
446 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100447 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200449# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000 \
450 && SIZEOF_SIZE_T != SIZEOF_INT
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000451 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
452# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000454# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100455 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000457 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
458 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
460 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
Bram Moolenaardb913952012-06-29 12:54:53 +0200461 {"PyObject_GetIter", (PYTHON_PROC*)&dll_PyObject_GetIter},
Bram Moolenaare7211222012-06-30 13:21:08 +0200462# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000
Bram Moolenaardb913952012-06-29 12:54:53 +0200463 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&dll__PyObject_NextNotImplemented},
Bram Moolenaare7211222012-06-30 13:21:08 +0200464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
Bram Moolenaar66b79852012-09-21 14:00:35 +0200466 {"_Py_ZeroStruct", (PYTHON_PROC*)&dll__Py_ZeroStruct},
467 {"_Py_TrueStruct", (PYTHON_PROC*)&dll__Py_TrueStruct},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
469 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
470# endif
471# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
472 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
473 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
474# endif
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200475# ifdef PY_USE_CAPSULE
Bram Moolenaardb913952012-06-29 12:54:53 +0200476 {"PyCapsule_New", (PYTHON_PROC*)&dll_PyCapsule_New},
477 {"PyCapsule_GetPointer", (PYTHON_PROC*)&dll_PyCapsule_GetPointer},
Bram Moolenaar2afa3232012-06-29 16:28:28 +0200478# else
479 {"PyCObject_FromVoidPtr", (PYTHON_PROC*)&dll_PyCObject_FromVoidPtr},
480 {"PyCObject_AsVoidPtr", (PYTHON_PROC*)&dll_PyCObject_AsVoidPtr},
481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 {"", NULL},
483};
484
485/*
486 * Free python.dll
487 */
488 static void
489end_dynamic_python(void)
490{
491 if (hinstPython)
492 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200493 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 hinstPython = 0;
495 }
496}
497
498/*
499 * Load library and get all pointers.
500 * Parameter 'libname' provides name of DLL.
501 * Return OK or FAIL.
502 */
503 static int
504python_runtime_link_init(char *libname, int verbose)
505{
506 int i;
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200507 void *ucs_as_encoded_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100509#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200510 /* Can't have Python and Python3 loaded at the same time.
511 * It cause a crash, because RTLD_GLOBAL is needed for
512 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200513 if (python3_loaded())
514 {
Bram Moolenaar9dc93ae2011-08-28 16:00:19 +0200515 if (verbose)
516 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200517 return FAIL;
518 }
519#endif
520
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 if (hinstPython)
522 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200523 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 if (!hinstPython)
525 {
526 if (verbose)
527 EMSG2(_(e_loadlib), libname);
528 return FAIL;
529 }
530
531 for (i = 0; python_funcname_table[i].ptr; ++i)
532 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200533 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 python_funcname_table[i].name)) == NULL)
535 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200536 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 hinstPython = 0;
538 if (verbose)
539 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
540 return FAIL;
541 }
542 }
Bram Moolenaarcc3e85f2012-06-29 19:14:52 +0200543
544 /* Load unicode functions separately as only the ucs2 or the ucs4 functions
545 * will be present in the library. */
546 ucs_as_encoded_string = symbol_from_dll(hinstPython,
547 "PyUnicodeUCS2_AsEncodedString");
548 if (ucs_as_encoded_string == NULL)
549 ucs_as_encoded_string = symbol_from_dll(hinstPython,
550 "PyUnicodeUCS4_AsEncodedString");
551 if (ucs_as_encoded_string != NULL)
552 py_PyUnicode_AsEncodedString = ucs_as_encoded_string;
553 else
554 {
555 close_dll(hinstPython);
556 hinstPython = 0;
557 if (verbose)
558 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*");
559 return FAIL;
560 }
561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 return OK;
563}
564
565/*
566 * If python is enabled (there is installed python on Windows system) return
567 * TRUE, else FALSE.
568 */
569 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000570python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
573}
574
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200575/*
576 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * DLL, as this can cause errors (importing data symbols is not reliable).
578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200580get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581{
582 PyObject *exmod = PyImport_ImportModule("exceptions");
583 PyObject *exdict = PyModule_GetDict(exmod);
584 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
585 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200586 imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
588 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
589 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
590 Py_XINCREF(imp_PyExc_AttributeError);
591 Py_XINCREF(imp_PyExc_IndexError);
Bram Moolenaaraf6abb92013-04-24 13:04:26 +0200592 Py_XINCREF(imp_PyExc_KeyError);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
594 Py_XINCREF(imp_PyExc_TypeError);
595 Py_XINCREF(imp_PyExc_ValueError);
596 Py_XDECREF(exmod);
597}
598#endif /* DYNAMIC_PYTHON */
599
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200600static PyObject *BufferNew (buf_T *);
601static PyObject *WindowNew(win_T *);
Bram Moolenaardb913952012-06-29 12:54:53 +0200602static PyObject *DictionaryNew(dict_T *);
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200603static PyObject *LineToString(const char *);
604
Bram Moolenaardb913952012-06-29 12:54:53 +0200605static int initialised = 0;
606#define PYINITIALISED initialised
607
Bram Moolenaardb913952012-06-29 12:54:53 +0200608#define DICTKEY_GET(err) \
609 if (!PyString_Check(keyObject)) \
610 { \
611 PyErr_SetString(PyExc_TypeError, _("only string keys are allowed")); \
612 return err; \
613 } \
Bram Moolenaarcdab9052012-09-05 19:03:56 +0200614 if (PyString_AsStringAndSize(keyObject, (char **) &key, NULL) == -1) \
615 return err;
616
Bram Moolenaardb913952012-06-29 12:54:53 +0200617#define DICTKEY_UNREF
618#define DICTKEY_DECL
619
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200620#define DESTRUCTOR_FINISH(self) Py_DECREF(self);
621
Bram Moolenaar971db462013-05-12 18:44:48 +0200622#define WIN_PYTHON_REF(win) win->w_python_ref
623#define BUF_PYTHON_REF(buf) buf->b_python_ref
624
Bram Moolenaar4d1da492013-04-24 13:39:15 +0200625static PyObject *OutputGetattr(PyObject *, char *);
626static PyObject *BufferGetattr(PyObject *, char *);
627static PyObject *WindowGetattr(PyObject *, char *);
628static PyObject *RangeGetattr(PyObject *, char *);
629static PyObject *DictionaryGetattr(PyObject *, char*);
630static PyObject *ListGetattr(PyObject *, char *);
631static PyObject *FunctionGetattr(PyObject *, char *);
632
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200633/*
634 * Include the code shared with if_python3.c
635 */
636#include "if_py_both.h"
637
638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639/******************************************************
640 * Internal function prototypes.
641 */
642
Bram Moolenaardb913952012-06-29 12:54:53 +0200643static PyObject *globals;
644
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645static void PythonIO_Flush(void);
646static int PythonIO_Init(void);
647static int PythonMod_Init(void);
648
649/* Utility functions for the vim/python interface
650 * ----------------------------------------------
651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000653static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656/******************************************************
657 * 1. Python interpreter main program.
658 */
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
661typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000664#ifdef PY_CAN_RECURSE
665static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
666#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000667static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670/*
671 * Suspend a thread of the Python interpreter, other threads are allowed to
672 * run.
673 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000674 static void
675Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000677#ifdef PY_CAN_RECURSE
678 PyGILState_Release(pygilstate);
679#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682}
683
684/*
685 * Restore a thread of the Python interpreter, waits for other threads to
686 * block.
687 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000688 static void
689Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691#ifdef PY_CAN_RECURSE
692 pygilstate = PyGILState_Ensure();
693#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 PyEval_RestoreThread(saved_python_thread);
695 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000697}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 void
700python_end()
701{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000702 static int recurse = 0;
703
704 /* If a crash occurs while doing this, don't try again. */
705 if (recurse != 0)
706 return;
707
708 ++recurse;
709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000711 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000712 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000713 Python_RestoreThread(); /* enter python */
714 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000717#else
718 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000719 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000720 Python_RestoreThread(); /* enter python */
721 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000724
725 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726}
727
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200728#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
729 int
730python_loaded()
731{
732 return (hinstPython != 0);
733}
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 static int
737Python_Init(void)
738{
739 if (!initialised)
740 {
741#ifdef DYNAMIC_PYTHON
742 if (!python_enabled(TRUE))
743 {
744 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
745 goto fail;
746 }
747#endif
748
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100749#ifdef PYTHON_HOME
750 Py_SetPythonHome(PYTHON_HOME);
751#endif
752
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200753 init_structs();
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755#if !defined(MACOS) || defined(MACOS_X_UNIX)
756 Py_Initialize();
757#else
758 PyMac_Initialize();
759#endif
Bram Moolenaar02366252013-01-30 11:44:39 +0100760 /* Initialise threads, and below save the state using
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100761 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread
Bram Moolenaar02366252013-01-30 11:44:39 +0100762 * specific state (such as the system trace hook), will be lost
763 * between invocations of Python code. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 PyEval_InitThreads();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765#ifdef DYNAMIC_PYTHON
766 get_exceptions();
767#endif
768
769 if (PythonIO_Init())
770 goto fail;
771
772 if (PythonMod_Init())
773 goto fail;
774
Bram Moolenaardb913952012-06-29 12:54:53 +0200775 globals = PyModule_GetDict(PyImport_AddModule("__main__"));
776
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000777 /* Remove the element from sys.path that was added because of our
778 * argv[0] value in PythonMod_Init(). Previously we used an empty
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200779 * string, but depending on the OS we then get an empty entry or
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000780 * the current directory in sys.path. */
781 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
782
Bram Moolenaar76d711c2013-02-13 14:17:08 +0100783 /* lock is created and acquired in PyEval_InitThreads() and thread
784 * state is created in Py_Initialize()
785 * there _PyGILState_NoteThreadState() also sets gilcounter to 1
786 * (python must have threads enabled!)
787 * so the following does both: unlock GIL and save thread state in TLS
788 * without deleting thread state
789 */
790 PyEval_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 initialised = 1;
793 }
794
795 return 0;
796
797fail:
798 /* We call PythonIO_Flush() here to print any Python errors.
799 * This is OK, as it is possible to call this function even
800 * if PythonIO_Init() has not completed successfully (it will
801 * not do anything in this case).
802 */
803 PythonIO_Flush();
804 return -1;
805}
806
807/*
808 * External interface
809 */
810 static void
Bram Moolenaardb913952012-06-29 12:54:53 +0200811DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000813#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 static int recursive = 0;
815#endif
816#if defined(MACOS) && !defined(MACOS_X_UNIX)
817 GrafPtr oldPort;
818#endif
819#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
820 char *saved_locale;
821#endif
822
823#ifndef PY_CAN_RECURSE
824 if (recursive)
825 {
826 EMSG(_("E659: Cannot invoke Python recursively"));
827 return;
828 }
829 ++recursive;
830#endif
831
832#if defined(MACOS) && !defined(MACOS_X_UNIX)
833 GetPort(&oldPort);
834 /* Check if the Python library is available */
835 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
836 goto theend;
837#endif
838 if (Python_Init())
839 goto theend;
840
Bram Moolenaardb913952012-06-29 12:54:53 +0200841 if (rettv == NULL)
842 {
843 RangeStart = eap->line1;
844 RangeEnd = eap->line2;
845 }
846 else
847 {
848 RangeStart = (PyInt) curwin->w_cursor.lnum;
849 RangeEnd = RangeStart;
850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 Python_Release_Vim(); /* leave vim */
852
853#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
854 /* Python only works properly when the LC_NUMERIC locale is "C". */
855 saved_locale = setlocale(LC_NUMERIC, NULL);
856 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
857 saved_locale = NULL;
858 else
859 {
860 /* Need to make a copy, value may change when setting new locale. */
861 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
862 (void)setlocale(LC_NUMERIC, "C");
863 }
864#endif
865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
Bram Moolenaardb913952012-06-29 12:54:53 +0200868 if (rettv == NULL)
869 PyRun_SimpleString((char *)(cmd));
870 else
871 {
872 PyObject *r;
873
874 r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
875 if (r == NULL)
Bram Moolenaar4d369872013-02-20 16:09:43 +0100876 {
877 if (PyErr_Occurred() && !msg_silent)
878 PyErr_PrintEx(0);
Bram Moolenaardb913952012-06-29 12:54:53 +0200879 EMSG(_("E858: Eval did not return a valid python object"));
Bram Moolenaar4d369872013-02-20 16:09:43 +0100880 }
Bram Moolenaardb913952012-06-29 12:54:53 +0200881 else
882 {
883 if (ConvertFromPyObject(r, rettv) == -1)
884 EMSG(_("E859: Failed to convert returned python object to vim value"));
885 Py_DECREF(r);
886 }
887 PyErr_Clear();
888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
893 if (saved_locale != NULL)
894 {
895 (void)setlocale(LC_NUMERIC, saved_locale);
896 vim_free(saved_locale);
897 }
898#endif
899
900 Python_Lock_Vim(); /* enter vim */
901 PythonIO_Flush();
902#if defined(MACOS) && !defined(MACOS_X_UNIX)
903 SetPort(oldPort);
904#endif
905
906theend:
907#ifndef PY_CAN_RECURSE
908 --recursive;
909#endif
Bram Moolenaardb913952012-06-29 12:54:53 +0200910 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911}
912
913/*
914 * ":python"
915 */
916 void
917ex_python(exarg_T *eap)
918{
919 char_u *script;
920
921 script = script_get(eap, eap->arg);
922 if (!eap->skip)
923 {
924 if (script == NULL)
Bram Moolenaardb913952012-06-29 12:54:53 +0200925 DoPythonCommand(eap, (char *)eap->arg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 else
Bram Moolenaardb913952012-06-29 12:54:53 +0200927 DoPythonCommand(eap, (char *)script, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 }
929 vim_free(script);
930}
931
932#define BUFFER_SIZE 1024
933
934/*
935 * ":pyfile"
936 */
937 void
938ex_pyfile(exarg_T *eap)
939{
940 static char buffer[BUFFER_SIZE];
941 const char *file = (char *)eap->arg;
942 char *p;
943
944 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
945 * stdio file pointer, but Vim and the Python DLL are compiled with
946 * different options under Windows, meaning that stdio pointers aren't
947 * compatible between the two. Yuk.
948 *
949 * Put the string "execfile('file')" into buffer. But, we need to
950 * escape any backslashes or single quotes in the file name, so that
951 * Python won't mangle the file name.
952 */
953 strcpy(buffer, "execfile('");
954 p = buffer + 10; /* size of "execfile('" */
955
956 while (*file && p < buffer + (BUFFER_SIZE - 3))
957 {
958 if (*file == '\\' || *file == '\'')
959 *p++ = '\\';
960 *p++ = *file++;
961 }
962
963 /* If we didn't finish the file name, we hit a buffer overflow */
964 if (*file != '\0')
965 return;
966
967 /* Put in the terminating "')" and a null */
968 *p++ = '\'';
969 *p++ = ')';
970 *p++ = '\0';
971
972 /* Execute the file */
Bram Moolenaardb913952012-06-29 12:54:53 +0200973 DoPythonCommand(eap, buffer, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/******************************************************
977 * 2. Python output stream: writes output via [e]msg().
978 */
979
980/* Implementation functions
981 */
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 static PyObject *
984OutputGetattr(PyObject *self, char *name)
985{
986 if (strcmp(name, "softspace") == 0)
987 return PyInt_FromLong(((OutputObject *)(self))->softspace);
988
989 return Py_FindMethod(OutputMethods, self, name);
990}
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992/***************/
993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 static int
995PythonIO_Init(void)
996{
997 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100998 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001000 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001}
1002
1003/******************************************************
1004 * 3. Implementation of the Vim module for Python
1005 */
1006
Bram Moolenaardb913952012-06-29 12:54:53 +02001007static PyObject *ConvertToPyObject(typval_T *);
1008static int ConvertFromPyObject(PyObject *, typval_T *);
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010/* Window type - Implementation functions
1011 * --------------------------------------
1012 */
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/* Buffer type - Implementation functions
1017 * --------------------------------------
1018 */
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
1021
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001022static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
1023static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025/* Line range type - Implementation functions
1026 * --------------------------------------
1027 */
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
1030
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001031static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
1032static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034/* Current objects type - Implementation functions
1035 * -----------------------------------------------
1036 */
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001039 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001040 (binaryfunc) 0, /* BufferConcat, sq_concat, x+y */
1041 (PyIntArgFunc) 0, /* BufferRepeat, sq_repeat, x*n */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001042 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
1043 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
1044 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001045 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
1046 (objobjproc) 0,
1047#if PY_MAJOR_VERSION >= 2
1048 (binaryfunc) 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 0,
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051};
1052
1053/* Buffer object - Implementation
1054 */
1055
1056 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057BufferGetattr(PyObject *self, char *name)
1058{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001059 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001061 if (CheckBuffer((BufferObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 return NULL;
1063
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001064 r = BufferAttr((BufferObject *)(self), name);
1065 if (r || PyErr_Occurred())
1066 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 else
1068 return Py_FindMethod(BufferMethods, self, name);
1069}
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/******************/
1072
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001073 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001074BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001076 return RBAsItem((BufferObject *)(self), n, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001079 static PyInt
1080BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
Bram Moolenaar8f1723d2013-05-12 20:36:14 +02001082 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1, -1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083}
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085static PySequenceMethods RangeAsSeq = {
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001086 (PyInquiry) RangeLength, /* sq_length, len(x) */
1087 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
1088 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1089 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1090 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1091 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1092 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
1093 (objobjproc) 0,
1094#if PY_MAJOR_VERSION >= 2
1095 (binaryfunc) 0,
1096 0,
1097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098};
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100/* Line range object - Implementation
1101 */
1102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 static PyObject *
1104RangeGetattr(PyObject *self, char *name)
1105{
1106 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001107 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001109 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 else
1111 return Py_FindMethod(RangeMethods, self, name);
1112}
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114/****************/
1115
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001116 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001117RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001119 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 ((RangeObject *)(self))->start,
1121 ((RangeObject *)(self))->end,
1122 &((RangeObject *)(self))->end);
1123}
1124
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001125 static PyInt
1126RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001128 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 ((RangeObject *)(self))->start,
1130 ((RangeObject *)(self))->end,
1131 &((RangeObject *)(self))->end);
1132}
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134/* Window object - Implementation
1135 */
1136
1137 static PyObject *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138WindowGetattr(PyObject *self, char *name)
1139{
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001140 PyObject *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001142 if (CheckWindow((WindowObject *)(self)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 return NULL;
1144
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001145 r = WindowAttr((WindowObject *)(self), name);
1146 if (r || PyErr_Occurred())
1147 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
1149 return Py_FindMethod(WindowMethods, self, name);
1150}
1151
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152/* Window list object - Definitions
1153 */
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001156 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001158 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1159 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1160 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1161 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
Bram Moolenaar4d1da492013-04-24 13:39:15 +02001162 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
1163 (objobjproc) 0,
1164#if PY_MAJOR_VERSION >= 2
1165 (binaryfunc) 0,
1166 0,
1167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168};
1169
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170/* External interface
1171 */
1172
1173 void
1174python_buffer_free(buf_T *buf)
1175{
Bram Moolenaar971db462013-05-12 18:44:48 +02001176 if (BUF_PYTHON_REF(buf) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001178 BufferObject *bp = BUF_PYTHON_REF(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001180 BUF_PYTHON_REF(buf) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 }
1182}
1183
1184#if defined(FEAT_WINDOWS) || defined(PROTO)
1185 void
1186python_window_free(win_T *win)
1187{
Bram Moolenaar971db462013-05-12 18:44:48 +02001188 if (WIN_PYTHON_REF(win) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 {
Bram Moolenaar971db462013-05-12 18:44:48 +02001190 WindowObject *wp = WIN_PYTHON_REF(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaar971db462013-05-12 18:44:48 +02001192 WIN_PYTHON_REF(win) = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194}
1195#endif
1196
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001197static BufMapObject TheBufferMap =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001199 PyObject_HEAD_INIT(&BufMapType)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200};
1201
1202static WinListObject TheWindowList =
1203{
1204 PyObject_HEAD_INIT(&WinListType)
1205};
1206
1207static CurrentObject TheCurrent =
1208{
1209 PyObject_HEAD_INIT(&CurrentType)
1210};
1211
1212 static int
1213PythonMod_Init(void)
1214{
1215 PyObject *mod;
1216 PyObject *dict;
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001217 PyObject *tmp;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001218 /* The special value is removed from sys.path in Python_Init(). */
1219 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221 /* Fixups... */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02001222 PyType_Ready(&IterType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001223 PyType_Ready(&BufferType);
1224 PyType_Ready(&RangeType);
1225 PyType_Ready(&WindowType);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001226 PyType_Ready(&BufMapType);
Bram Moolenaar21377c82011-03-26 13:56:48 +01001227 PyType_Ready(&WinListType);
1228 PyType_Ready(&CurrentType);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001229 PyType_Ready(&OptionsType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 /* Set sys.argv[] to avoid a crash in warn(). */
1232 PySys_SetArgv(1, argv);
1233
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001234 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 dict = PyModule_GetDict(mod);
1236
1237 VimError = Py_BuildValue("s", "vim.error");
1238
1239 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaardfa38d42013-05-15 13:38:47 +02001240 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferMap);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001241 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1242 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar230bb3f2013-04-24 14:07:45 +02001243 tmp = DictionaryNew(&globvardict);
1244 PyDict_SetItemString(dict, "vars", tmp);
1245 Py_DECREF(tmp);
1246 tmp = DictionaryNew(&vimvardict);
1247 PyDict_SetItemString(dict, "vvars", tmp);
1248 Py_DECREF(tmp);
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +02001249 tmp = OptionsNew(SREQ_GLOBAL, NULL, dummy_check, NULL);
1250 PyDict_SetItemString(dict, "options", tmp);
1251 Py_DECREF(tmp);
Bram Moolenaar66b79852012-09-21 14:00:35 +02001252 PyDict_SetItemString(dict, "VAR_LOCKED", PyInt_FromLong(VAR_LOCKED));
1253 PyDict_SetItemString(dict, "VAR_FIXED", PyInt_FromLong(VAR_FIXED));
1254 PyDict_SetItemString(dict, "VAR_SCOPE", PyInt_FromLong(VAR_SCOPE));
1255 PyDict_SetItemString(dict, "VAR_DEF_SCOPE", PyInt_FromLong(VAR_DEF_SCOPE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257 if (PyErr_Occurred())
1258 return -1;
1259
1260 return 0;
1261}
1262
1263/*************************************************************************
1264 * 4. Utility functions for handling the interface between Vim and Python.
1265 */
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267/* Convert a Vim line into a Python string.
1268 * All internal newlines are replaced by null characters.
1269 *
1270 * On errors, the Python exception data is set, and NULL is returned.
1271 */
1272 static PyObject *
1273LineToString(const char *str)
1274{
1275 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001276 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 char *p;
1278
1279 /* Allocate an Python string object, with uninitialised contents. We
1280 * must do it this way, so that we can modify the string in place
1281 * later. See the Python source, Objects/stringobject.c for details.
1282 */
1283 result = PyString_FromStringAndSize(NULL, len);
1284 if (result == NULL)
1285 return NULL;
1286
1287 p = PyString_AsString(result);
1288
1289 while (*str)
1290 {
1291 if (*str == '\n')
1292 *p = '\0';
1293 else
1294 *p = *str;
1295
1296 ++p;
1297 ++str;
1298 }
1299
1300 return result;
1301}
1302
Bram Moolenaardb913952012-06-29 12:54:53 +02001303 static PyObject *
1304DictionaryGetattr(PyObject *self, char *name)
1305{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001306 DictionaryObject *this = ((DictionaryObject *) (self));
1307
1308 if (strcmp(name, "locked") == 0)
1309 return PyInt_FromLong(this->dict->dv_lock);
1310 else if (strcmp(name, "scope") == 0)
1311 return PyInt_FromLong(this->dict->dv_scope);
1312
Bram Moolenaardb913952012-06-29 12:54:53 +02001313 return Py_FindMethod(DictionaryMethods, self, name);
1314}
1315
Bram Moolenaardb913952012-06-29 12:54:53 +02001316static PySequenceMethods ListAsSeq = {
1317 (PyInquiry) ListLength,
1318 (binaryfunc) 0,
1319 (PyIntArgFunc) 0,
1320 (PyIntArgFunc) ListItem,
1321 (PyIntIntArgFunc) ListSlice,
1322 (PyIntObjArgProc) ListAssItem,
1323 (PyIntIntObjArgProc) ListAssSlice,
1324 (objobjproc) 0,
1325#if PY_MAJOR_VERSION >= 2
1326 (binaryfunc) ListConcatInPlace,
1327 0,
1328#endif
1329};
1330
Bram Moolenaardb913952012-06-29 12:54:53 +02001331 static PyObject *
1332ListGetattr(PyObject *self, char *name)
1333{
Bram Moolenaar66b79852012-09-21 14:00:35 +02001334 if (strcmp(name, "locked") == 0)
1335 return PyInt_FromLong(((ListObject *)(self))->list->lv_lock);
1336
Bram Moolenaardb913952012-06-29 12:54:53 +02001337 return Py_FindMethod(ListMethods, self, name);
1338}
1339
Bram Moolenaardb913952012-06-29 12:54:53 +02001340 static PyObject *
1341FunctionGetattr(PyObject *self, char *name)
1342{
1343 FunctionObject *this = (FunctionObject *)(self);
1344
1345 if (strcmp(name, "name") == 0)
1346 return PyString_FromString((char *)(this->name));
1347 else
1348 return Py_FindMethod(FunctionMethods, self, name);
1349}
1350
1351 void
1352do_pyeval (char_u *str, typval_T *rettv)
1353{
1354 DoPythonCommand(NULL, (char *) str, rettv);
1355 switch(rettv->v_type)
1356 {
1357 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
1358 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break;
1359 case VAR_FUNC: func_ref(rettv->vval.v_string); break;
Bram Moolenaar77fceb82012-09-05 18:54:48 +02001360 case VAR_UNKNOWN:
1361 rettv->v_type = VAR_NUMBER;
1362 rettv->vval.v_number = 0;
1363 break;
Bram Moolenaardb913952012-06-29 12:54:53 +02001364 }
1365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367/* Don't generate a prototype for the next function, it generates an error on
1368 * newer Python versions. */
1369#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1370
1371 char *
1372Py_GetProgramName(void)
1373{
1374 return "vim";
1375}
1376#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001377
Bram Moolenaardb913952012-06-29 12:54:53 +02001378 void
1379set_ref_in_python (int copyID)
1380{
1381 set_ref_in_py(copyID);
1382}