blob: f3b7e5b636b2cd23dd4b8db746d52d01b7f1709d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
Bram Moolenaar2c45e942008-06-04 11:35:26 +000047#define PY_SSIZE_T_CLEAN
48
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#include <Python.h>
50#if defined(MACOS) && !defined(MACOS_X_UNIX)
51# include "macglue.h"
52# include <CodeFragments.h>
53#endif
54#undef main /* Defined in python.h - aargh */
55#undef HAVE_FCNTL_H /* Clash with os_win32.h */
56
Bram Moolenaar170bf1a2010-07-24 23:51:45 +020057static void init_structs(void);
58
Bram Moolenaar19e60942011-06-19 00:27:51 +020059/* No-op conversion functions, use with care! */
60#define PyString_AsBytes(obj) (obj)
61#define PyString_FreeBytes(obj)
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#if !defined(FEAT_PYTHON) && defined(PROTO)
64/* Use this to be able to generate prototypes without python being used. */
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000065# define PyObject Py_ssize_t
66# define PyThreadState Py_ssize_t
67# define PyTypeObject Py_ssize_t
68struct PyMethodDef { Py_ssize_t a; };
69# define PySequenceMethods Py_ssize_t
Bram Moolenaar071d4272004-06-13 20:20:40 +000070#endif
71
Bram Moolenaar2c45e942008-06-04 11:35:26 +000072#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02050000
73# define PyInt Py_ssize_t
74# define PyInquiry lenfunc
75# define PyIntArgFunc ssizeargfunc
76# define PyIntIntArgFunc ssizessizeargfunc
77# define PyIntObjArgProc ssizeobjargproc
78# define PyIntIntObjArgProc ssizessizeobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000079# define Py_ssize_t_fmt "n"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000080#else
81# define PyInt int
82# define PyInquiry inquiry
83# define PyIntArgFunc intargfunc
84# define PyIntIntArgFunc intintargfunc
85# define PyIntObjArgProc intobjargproc
86# define PyIntIntObjArgProc intintobjargproc
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +000087# define Py_ssize_t_fmt "i"
Bram Moolenaar2c45e942008-06-04 11:35:26 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090/* Parser flags */
91#define single_input 256
92#define file_input 257
93#define eval_input 258
94
95#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0
96 /* Python 2.3: can invoke ":python" recursively. */
97# define PY_CAN_RECURSE
98#endif
99
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200100# if defined(DYNAMIC_PYTHON) || defined(PROTO)
101# ifndef DYNAMIC_PYTHON
102# define HINSTANCE long_u /* for generating prototypes */
103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200105# ifndef WIN3264
106# include <dlfcn.h>
107# define FARPROC void*
108# define HINSTANCE void*
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100109# if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200110# define load_dll(n) dlopen((n), RTLD_LAZY)
111# else
112# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL)
113# endif
114# define close_dll dlclose
115# define symbol_from_dll dlsym
116# else
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200117# define load_dll vimLoadLib
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200118# define close_dll FreeLibrary
119# define symbol_from_dll GetProcAddress
120# endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200121
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000122/* This makes if_python.c compile without warnings against Python 2.5
123 * on Win32 and Win64. */
Bram Moolenaarb61f95c2010-08-09 22:06:13 +0200124# undef PyRun_SimpleString
125# undef PyArg_Parse
126# undef PyArg_ParseTuple
127# undef Py_BuildValue
128# undef Py_InitModule4
129# undef Py_InitModule4_64
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131/*
132 * Wrapper defines
133 */
134# define PyArg_Parse dll_PyArg_Parse
135# define PyArg_ParseTuple dll_PyArg_ParseTuple
Bram Moolenaar19e60942011-06-19 00:27:51 +0200136# define PyMem_Free dll_PyMem_Free
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137# define PyDict_SetItemString dll_PyDict_SetItemString
138# define PyErr_BadArgument dll_PyErr_BadArgument
139# define PyErr_Clear dll_PyErr_Clear
140# define PyErr_NoMemory dll_PyErr_NoMemory
141# define PyErr_Occurred dll_PyErr_Occurred
142# define PyErr_SetNone dll_PyErr_SetNone
143# define PyErr_SetString dll_PyErr_SetString
144# define PyEval_InitThreads dll_PyEval_InitThreads
145# define PyEval_RestoreThread dll_PyEval_RestoreThread
146# define PyEval_SaveThread dll_PyEval_SaveThread
147# ifdef PY_CAN_RECURSE
148# define PyGILState_Ensure dll_PyGILState_Ensure
149# define PyGILState_Release dll_PyGILState_Release
150# endif
151# define PyInt_AsLong dll_PyInt_AsLong
152# define PyInt_FromLong dll_PyInt_FromLong
153# define PyInt_Type (*dll_PyInt_Type)
154# define PyList_GetItem dll_PyList_GetItem
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000155# define PyList_Append dll_PyList_Append
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156# define PyList_New dll_PyList_New
157# define PyList_SetItem dll_PyList_SetItem
158# define PyList_Size dll_PyList_Size
159# define PyList_Type (*dll_PyList_Type)
160# define PyImport_ImportModule dll_PyImport_ImportModule
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000161# define PyDict_New dll_PyDict_New
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162# define PyDict_GetItemString dll_PyDict_GetItemString
163# define PyModule_GetDict dll_PyModule_GetDict
164# define PyRun_SimpleString dll_PyRun_SimpleString
165# define PyString_AsString dll_PyString_AsString
166# define PyString_FromString dll_PyString_FromString
167# define PyString_FromStringAndSize dll_PyString_FromStringAndSize
168# define PyString_Size dll_PyString_Size
169# define PyString_Type (*dll_PyString_Type)
170# define PySys_SetObject dll_PySys_SetObject
171# define PySys_SetArgv dll_PySys_SetArgv
172# define PyType_Type (*dll_PyType_Type)
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100173# define PyType_Ready (*dll_PyType_Ready)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174# define Py_BuildValue dll_Py_BuildValue
175# define Py_FindMethod dll_Py_FindMethod
176# define Py_InitModule4 dll_Py_InitModule4
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100177# define Py_SetPythonHome dll_Py_SetPythonHome
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178# define Py_Initialize dll_Py_Initialize
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000179# define Py_Finalize dll_Py_Finalize
180# define Py_IsInitialized dll_Py_IsInitialized
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181# define _PyObject_New dll__PyObject_New
182# define _Py_NoneStruct (*dll__Py_NoneStruct)
183# define PyObject_Init dll__PyObject_Init
184# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
185# define PyType_IsSubtype dll_PyType_IsSubtype
186# endif
187# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
188# define PyObject_Malloc dll_PyObject_Malloc
189# define PyObject_Free dll_PyObject_Free
190# endif
191
192/*
193 * Pointers for dynamic link
194 */
195static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
196static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
Bram Moolenaar19e60942011-06-19 00:27:51 +0200197static int(*dll_PyMem_Free)(void *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
199static int(*dll_PyErr_BadArgument)(void);
200static void(*dll_PyErr_Clear)(void);
201static PyObject*(*dll_PyErr_NoMemory)(void);
202static PyObject*(*dll_PyErr_Occurred)(void);
203static void(*dll_PyErr_SetNone)(PyObject *);
204static void(*dll_PyErr_SetString)(PyObject *, const char *);
205static void(*dll_PyEval_InitThreads)(void);
206static void(*dll_PyEval_RestoreThread)(PyThreadState *);
207static PyThreadState*(*dll_PyEval_SaveThread)(void);
208# ifdef PY_CAN_RECURSE
209static PyGILState_STATE (*dll_PyGILState_Ensure)(void);
210static void (*dll_PyGILState_Release)(PyGILState_STATE);
211#endif
212static long(*dll_PyInt_AsLong)(PyObject *);
213static PyObject*(*dll_PyInt_FromLong)(long);
214static PyTypeObject* dll_PyInt_Type;
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000215static PyObject*(*dll_PyList_GetItem)(PyObject *, PyInt);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000216static PyObject*(*dll_PyList_Append)(PyObject *, PyObject *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000217static PyObject*(*dll_PyList_New)(PyInt size);
218static int(*dll_PyList_SetItem)(PyObject *, PyInt, PyObject *);
219static PyInt(*dll_PyList_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220static PyTypeObject* dll_PyList_Type;
221static PyObject*(*dll_PyImport_ImportModule)(const char *);
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000222static PyObject*(*dll_PyDict_New)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static PyObject*(*dll_PyDict_GetItemString)(PyObject *, const char *);
224static PyObject*(*dll_PyModule_GetDict)(PyObject *);
225static int(*dll_PyRun_SimpleString)(char *);
226static char*(*dll_PyString_AsString)(PyObject *);
227static PyObject*(*dll_PyString_FromString)(const char *);
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000228static PyObject*(*dll_PyString_FromStringAndSize)(const char *, PyInt);
229static PyInt(*dll_PyString_Size)(PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230static PyTypeObject* dll_PyString_Type;
231static int(*dll_PySys_SetObject)(char *, PyObject *);
232static int(*dll_PySys_SetArgv)(int, char **);
233static PyTypeObject* dll_PyType_Type;
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100234static int (*dll_PyType_Ready)(PyTypeObject *type);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235static PyObject*(*dll_Py_BuildValue)(char *, ...);
236static PyObject*(*dll_Py_FindMethod)(struct PyMethodDef[], PyObject *, char *);
237static PyObject*(*dll_Py_InitModule4)(char *, struct PyMethodDef *, char *, PyObject *, int);
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100238static void(*dll_Py_SetPythonHome)(char *home);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239static void(*dll_Py_Initialize)(void);
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000240static void(*dll_Py_Finalize)(void);
241static int(*dll_Py_IsInitialized)(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242static PyObject*(*dll__PyObject_New)(PyTypeObject *, PyObject *);
243static PyObject*(*dll__PyObject_Init)(PyObject *, PyTypeObject *);
244static PyObject* dll__Py_NoneStruct;
245# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
246static int (*dll_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *);
247# endif
248# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
249static void* (*dll_PyObject_Malloc)(size_t);
250static void (*dll_PyObject_Free)(void*);
251# endif
252
253static HINSTANCE hinstPython = 0; /* Instance of python.dll */
254
255/* Imported exception objects */
256static PyObject *imp_PyExc_AttributeError;
257static PyObject *imp_PyExc_IndexError;
258static PyObject *imp_PyExc_KeyboardInterrupt;
259static PyObject *imp_PyExc_TypeError;
260static PyObject *imp_PyExc_ValueError;
261
262# define PyExc_AttributeError imp_PyExc_AttributeError
263# define PyExc_IndexError imp_PyExc_IndexError
264# define PyExc_KeyboardInterrupt imp_PyExc_KeyboardInterrupt
265# define PyExc_TypeError imp_PyExc_TypeError
266# define PyExc_ValueError imp_PyExc_ValueError
267
268/*
269 * Table of name to function pointer of python.
270 */
271# define PYTHON_PROC FARPROC
272static struct
273{
274 char *name;
275 PYTHON_PROC *ptr;
276} python_funcname_table[] =
277{
278 {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
279 {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
Bram Moolenaar19e60942011-06-19 00:27:51 +0200280 {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
282 {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
283 {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
284 {"PyErr_NoMemory", (PYTHON_PROC*)&dll_PyErr_NoMemory},
285 {"PyErr_Occurred", (PYTHON_PROC*)&dll_PyErr_Occurred},
286 {"PyErr_SetNone", (PYTHON_PROC*)&dll_PyErr_SetNone},
287 {"PyErr_SetString", (PYTHON_PROC*)&dll_PyErr_SetString},
288 {"PyEval_InitThreads", (PYTHON_PROC*)&dll_PyEval_InitThreads},
289 {"PyEval_RestoreThread", (PYTHON_PROC*)&dll_PyEval_RestoreThread},
290 {"PyEval_SaveThread", (PYTHON_PROC*)&dll_PyEval_SaveThread},
291# ifdef PY_CAN_RECURSE
292 {"PyGILState_Ensure", (PYTHON_PROC*)&dll_PyGILState_Ensure},
293 {"PyGILState_Release", (PYTHON_PROC*)&dll_PyGILState_Release},
294# endif
295 {"PyInt_AsLong", (PYTHON_PROC*)&dll_PyInt_AsLong},
296 {"PyInt_FromLong", (PYTHON_PROC*)&dll_PyInt_FromLong},
297 {"PyInt_Type", (PYTHON_PROC*)&dll_PyInt_Type},
298 {"PyList_GetItem", (PYTHON_PROC*)&dll_PyList_GetItem},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000299 {"PyList_Append", (PYTHON_PROC*)&dll_PyList_Append},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 {"PyList_New", (PYTHON_PROC*)&dll_PyList_New},
301 {"PyList_SetItem", (PYTHON_PROC*)&dll_PyList_SetItem},
302 {"PyList_Size", (PYTHON_PROC*)&dll_PyList_Size},
303 {"PyList_Type", (PYTHON_PROC*)&dll_PyList_Type},
304 {"PyImport_ImportModule", (PYTHON_PROC*)&dll_PyImport_ImportModule},
305 {"PyDict_GetItemString", (PYTHON_PROC*)&dll_PyDict_GetItemString},
Bram Moolenaar0ac93792006-01-21 22:16:51 +0000306 {"PyDict_New", (PYTHON_PROC*)&dll_PyDict_New},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 {"PyModule_GetDict", (PYTHON_PROC*)&dll_PyModule_GetDict},
308 {"PyRun_SimpleString", (PYTHON_PROC*)&dll_PyRun_SimpleString},
309 {"PyString_AsString", (PYTHON_PROC*)&dll_PyString_AsString},
310 {"PyString_FromString", (PYTHON_PROC*)&dll_PyString_FromString},
311 {"PyString_FromStringAndSize", (PYTHON_PROC*)&dll_PyString_FromStringAndSize},
312 {"PyString_Size", (PYTHON_PROC*)&dll_PyString_Size},
313 {"PyString_Type", (PYTHON_PROC*)&dll_PyString_Type},
314 {"PySys_SetObject", (PYTHON_PROC*)&dll_PySys_SetObject},
315 {"PySys_SetArgv", (PYTHON_PROC*)&dll_PySys_SetArgv},
316 {"PyType_Type", (PYTHON_PROC*)&dll_PyType_Type},
Bram Moolenaar30fec7b2011-03-26 18:32:05 +0100317 {"PyType_Ready", (PYTHON_PROC*)&dll_PyType_Ready},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 {"Py_BuildValue", (PYTHON_PROC*)&dll_Py_BuildValue},
319 {"Py_FindMethod", (PYTHON_PROC*)&dll_Py_FindMethod},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000320# if (PY_VERSION_HEX >= 0x02050000) && SIZEOF_SIZE_T != SIZEOF_INT
321 {"Py_InitModule4_64", (PYTHON_PROC*)&dll_Py_InitModule4},
322# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 {"Py_InitModule4", (PYTHON_PROC*)&dll_Py_InitModule4},
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000324# endif
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100325 {"Py_SetPythonHome", (PYTHON_PROC*)&dll_Py_SetPythonHome},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {"Py_Initialize", (PYTHON_PROC*)&dll_Py_Initialize},
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000327 {"Py_Finalize", (PYTHON_PROC*)&dll_Py_Finalize},
328 {"Py_IsInitialized", (PYTHON_PROC*)&dll_Py_IsInitialized},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {"_PyObject_New", (PYTHON_PROC*)&dll__PyObject_New},
330 {"PyObject_Init", (PYTHON_PROC*)&dll__PyObject_Init},
331 {"_Py_NoneStruct", (PYTHON_PROC*)&dll__Py_NoneStruct},
332# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02020000
333 {"PyType_IsSubtype", (PYTHON_PROC*)&dll_PyType_IsSubtype},
334# endif
335# if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02030000
336 {"PyObject_Malloc", (PYTHON_PROC*)&dll_PyObject_Malloc},
337 {"PyObject_Free", (PYTHON_PROC*)&dll_PyObject_Free},
338# endif
339 {"", NULL},
340};
341
342/*
343 * Free python.dll
344 */
345 static void
346end_dynamic_python(void)
347{
348 if (hinstPython)
349 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200350 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 hinstPython = 0;
352 }
353}
354
355/*
356 * Load library and get all pointers.
357 * Parameter 'libname' provides name of DLL.
358 * Return OK or FAIL.
359 */
360 static int
361python_runtime_link_init(char *libname, int verbose)
362{
363 int i;
364
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100365#if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON3)
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200366 /* Can't have Python and Python3 loaded at the same time.
367 * It cause a crash, because RTLD_GLOBAL is needed for
368 * standard C extension libraries of one or both python versions. */
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200369 if (python3_loaded())
370 {
Bram Moolenaarb744b2f2010-08-13 16:22:57 +0200371 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200372 return FAIL;
373 }
374#endif
375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 if (hinstPython)
377 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200378 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 if (!hinstPython)
380 {
381 if (verbose)
382 EMSG2(_(e_loadlib), libname);
383 return FAIL;
384 }
385
386 for (i = 0; python_funcname_table[i].ptr; ++i)
387 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200388 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 python_funcname_table[i].name)) == NULL)
390 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200391 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 hinstPython = 0;
393 if (verbose)
394 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
395 return FAIL;
396 }
397 }
398 return OK;
399}
400
401/*
402 * If python is enabled (there is installed python on Windows system) return
403 * TRUE, else FALSE.
404 */
405 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000406python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407{
408 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
409}
410
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200411/*
412 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 * DLL, as this can cause errors (importing data symbols is not reliable).
414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200416get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417{
418 PyObject *exmod = PyImport_ImportModule("exceptions");
419 PyObject *exdict = PyModule_GetDict(exmod);
420 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
421 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
422 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
423 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
424 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
425 Py_XINCREF(imp_PyExc_AttributeError);
426 Py_XINCREF(imp_PyExc_IndexError);
427 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
428 Py_XINCREF(imp_PyExc_TypeError);
429 Py_XINCREF(imp_PyExc_ValueError);
430 Py_XDECREF(exmod);
431}
432#endif /* DYNAMIC_PYTHON */
433
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200434static PyObject *BufferNew (buf_T *);
435static PyObject *WindowNew(win_T *);
436static PyObject *LineToString(const char *);
437
438static PyTypeObject RangeType;
439
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200440/*
441 * Include the code shared with if_python3.c
442 */
443#include "if_py_both.h"
444
445
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446/******************************************************
447 * Internal function prototypes.
448 */
449
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000450static PyInt RangeStart;
451static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452
453static void PythonIO_Flush(void);
454static int PythonIO_Init(void);
455static int PythonMod_Init(void);
456
457/* Utility functions for the vim/python interface
458 * ----------------------------------------------
459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000461static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464/******************************************************
465 * 1. Python interpreter main program.
466 */
467
468static int initialised = 0;
469
470#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
471typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000474#ifdef PY_CAN_RECURSE
475static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
476#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000477static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480/*
481 * Suspend a thread of the Python interpreter, other threads are allowed to
482 * run.
483 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000484 static void
485Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487#ifdef PY_CAN_RECURSE
488 PyGILState_Release(pygilstate);
489#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492}
493
494/*
495 * Restore a thread of the Python interpreter, waits for other threads to
496 * block.
497 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000498 static void
499Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501#ifdef PY_CAN_RECURSE
502 pygilstate = PyGILState_Ensure();
503#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 PyEval_RestoreThread(saved_python_thread);
505 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 void
510python_end()
511{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000512 static int recurse = 0;
513
514 /* If a crash occurs while doing this, don't try again. */
515 if (recurse != 0)
516 return;
517
518 ++recurse;
519
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000521 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000523 Python_RestoreThread(); /* enter python */
524 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000527#else
528 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000529 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000530 Python_RestoreThread(); /* enter python */
531 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000534
535 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536}
537
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200538#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
539 int
540python_loaded()
541{
542 return (hinstPython != 0);
543}
544#endif
545
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 static int
547Python_Init(void)
548{
549 if (!initialised)
550 {
551#ifdef DYNAMIC_PYTHON
552 if (!python_enabled(TRUE))
553 {
554 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
555 goto fail;
556 }
557#endif
558
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100559#ifdef PYTHON_HOME
560 Py_SetPythonHome(PYTHON_HOME);
561#endif
562
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200563 init_structs();
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565#if !defined(MACOS) || defined(MACOS_X_UNIX)
566 Py_Initialize();
567#else
568 PyMac_Initialize();
569#endif
570 /* initialise threads */
571 PyEval_InitThreads();
572
573#ifdef DYNAMIC_PYTHON
574 get_exceptions();
575#endif
576
577 if (PythonIO_Init())
578 goto fail;
579
580 if (PythonMod_Init())
581 goto fail;
582
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000583 /* Remove the element from sys.path that was added because of our
584 * argv[0] value in PythonMod_Init(). Previously we used an empty
585 * string, but dependinding on the OS we then get an empty entry or
586 * the current directory in sys.path. */
587 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
588
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000589 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592 initialised = 1;
593 }
594
595 return 0;
596
597fail:
598 /* We call PythonIO_Flush() here to print any Python errors.
599 * This is OK, as it is possible to call this function even
600 * if PythonIO_Init() has not completed successfully (it will
601 * not do anything in this case).
602 */
603 PythonIO_Flush();
604 return -1;
605}
606
607/*
608 * External interface
609 */
610 static void
611DoPythonCommand(exarg_T *eap, const char *cmd)
612{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000613#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 static int recursive = 0;
615#endif
616#if defined(MACOS) && !defined(MACOS_X_UNIX)
617 GrafPtr oldPort;
618#endif
619#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
620 char *saved_locale;
621#endif
622
623#ifndef PY_CAN_RECURSE
624 if (recursive)
625 {
626 EMSG(_("E659: Cannot invoke Python recursively"));
627 return;
628 }
629 ++recursive;
630#endif
631
632#if defined(MACOS) && !defined(MACOS_X_UNIX)
633 GetPort(&oldPort);
634 /* Check if the Python library is available */
635 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
636 goto theend;
637#endif
638 if (Python_Init())
639 goto theend;
640
641 RangeStart = eap->line1;
642 RangeEnd = eap->line2;
643 Python_Release_Vim(); /* leave vim */
644
645#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
646 /* Python only works properly when the LC_NUMERIC locale is "C". */
647 saved_locale = setlocale(LC_NUMERIC, NULL);
648 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
649 saved_locale = NULL;
650 else
651 {
652 /* Need to make a copy, value may change when setting new locale. */
653 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
654 (void)setlocale(LC_NUMERIC, "C");
655 }
656#endif
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660 PyRun_SimpleString((char *)(cmd));
661
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
665 if (saved_locale != NULL)
666 {
667 (void)setlocale(LC_NUMERIC, saved_locale);
668 vim_free(saved_locale);
669 }
670#endif
671
672 Python_Lock_Vim(); /* enter vim */
673 PythonIO_Flush();
674#if defined(MACOS) && !defined(MACOS_X_UNIX)
675 SetPort(oldPort);
676#endif
677
678theend:
679#ifndef PY_CAN_RECURSE
680 --recursive;
681#endif
682 return; /* keeps lint happy */
683}
684
685/*
686 * ":python"
687 */
688 void
689ex_python(exarg_T *eap)
690{
691 char_u *script;
692
693 script = script_get(eap, eap->arg);
694 if (!eap->skip)
695 {
696 if (script == NULL)
697 DoPythonCommand(eap, (char *)eap->arg);
698 else
699 DoPythonCommand(eap, (char *)script);
700 }
701 vim_free(script);
702}
703
704#define BUFFER_SIZE 1024
705
706/*
707 * ":pyfile"
708 */
709 void
710ex_pyfile(exarg_T *eap)
711{
712 static char buffer[BUFFER_SIZE];
713 const char *file = (char *)eap->arg;
714 char *p;
715
716 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
717 * stdio file pointer, but Vim and the Python DLL are compiled with
718 * different options under Windows, meaning that stdio pointers aren't
719 * compatible between the two. Yuk.
720 *
721 * Put the string "execfile('file')" into buffer. But, we need to
722 * escape any backslashes or single quotes in the file name, so that
723 * Python won't mangle the file name.
724 */
725 strcpy(buffer, "execfile('");
726 p = buffer + 10; /* size of "execfile('" */
727
728 while (*file && p < buffer + (BUFFER_SIZE - 3))
729 {
730 if (*file == '\\' || *file == '\'')
731 *p++ = '\\';
732 *p++ = *file++;
733 }
734
735 /* If we didn't finish the file name, we hit a buffer overflow */
736 if (*file != '\0')
737 return;
738
739 /* Put in the terminating "')" and a null */
740 *p++ = '\'';
741 *p++ = ')';
742 *p++ = '\0';
743
744 /* Execute the file */
745 DoPythonCommand(eap, buffer);
746}
747
748/******************************************************
749 * 2. Python output stream: writes output via [e]msg().
750 */
751
752/* Implementation functions
753 */
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 static PyObject *
756OutputGetattr(PyObject *self, char *name)
757{
758 if (strcmp(name, "softspace") == 0)
759 return PyInt_FromLong(((OutputObject *)(self))->softspace);
760
761 return Py_FindMethod(OutputMethods, self, name);
762}
763
764 static int
765OutputSetattr(PyObject *self, char *name, PyObject *val)
766{
767 if (val == NULL) {
768 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
769 return -1;
770 }
771
772 if (strcmp(name, "softspace") == 0)
773 {
774 if (!PyInt_Check(val)) {
775 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
776 return -1;
777 }
778
779 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
780 return 0;
781 }
782
783 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
784 return -1;
785}
786
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787/***************/
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 static int
790PythonIO_Init(void)
791{
792 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100793 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200795 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/******************************************************
799 * 3. Implementation of the Vim module for Python
800 */
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802/* Window type - Implementation functions
803 * --------------------------------------
804 */
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808static void WindowDestructor(PyObject *);
809static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/* Buffer type - Implementation functions
812 * --------------------------------------
813 */
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817static void BufferDestructor(PyObject *);
818static PyObject *BufferGetattr(PyObject *, char *);
819static PyObject *BufferRepr(PyObject *);
820
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000821static PyInt BufferLength(PyObject *);
822static PyObject *BufferItem(PyObject *, PyInt);
823static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
824static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
825static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827/* Line range type - Implementation functions
828 * --------------------------------------
829 */
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
832
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000833static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
834static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836/* Current objects type - Implementation functions
837 * -----------------------------------------------
838 */
839
840static PyObject *CurrentGetattr(PyObject *, char *);
841static int CurrentSetattr(PyObject *, char *, PyObject *);
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000844 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000846 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
847 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
848 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
849 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
850 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851};
852
853static PyTypeObject BufferType = {
854 PyObject_HEAD_INIT(0)
855 0,
856 "buffer",
857 sizeof(BufferObject),
858 0,
859
860 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
861 (printfunc) 0, /* tp_print, print x */
862 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
863 (setattrfunc) 0, /* tp_setattr, x.attr=v */
864 (cmpfunc) 0, /* tp_compare, x>y */
865 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
866
867 0, /* as number */
868 &BufferAsSeq, /* as sequence */
869 0, /* as mapping */
870
871 (hashfunc) 0, /* tp_hash, dict(x) */
872 (ternaryfunc) 0, /* tp_call, x() */
873 (reprfunc) 0, /* tp_str, str(x) */
874};
875
876/* Buffer object - Implementation
877 */
878
879 static PyObject *
880BufferNew(buf_T *buf)
881{
882 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000883 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * then we can get at it in buf_freeall() in vim. We then
885 * need to create only ONE Python object per buffer - if
886 * we try to create a second, just INCREF the existing one
887 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +0000888 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 * Question: what to do on a buf_freeall(). We'll probably
890 * have to either delete the Python object (DECREF it to
891 * zero - a bad idea, as it leaves dangling refs!) or
892 * set the buf_T * value to an invalid value (-1?), which
893 * means we need checks in all access functions... Bah.
894 */
895
896 BufferObject *self;
897
Bram Moolenaare344bea2005-09-01 20:46:49 +0000898 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000900 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 Py_INCREF(self);
902 }
903 else
904 {
905 self = PyObject_NEW(BufferObject, &BufferType);
906 if (self == NULL)
907 return NULL;
908 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000909 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 }
911
912 return (PyObject *)(self);
913}
914
915 static void
916BufferDestructor(PyObject *self)
917{
918 BufferObject *this = (BufferObject *)(self);
919
920 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000921 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaar658ada62006-10-03 13:02:36 +0000923 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924}
925
926 static PyObject *
927BufferGetattr(PyObject *self, char *name)
928{
929 BufferObject *this = (BufferObject *)(self);
930
931 if (CheckBuffer(this))
932 return NULL;
933
934 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000935 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000937 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 else if (strcmp(name,"__members__") == 0)
939 return Py_BuildValue("[ss]", "name", "number");
940 else
941 return Py_FindMethod(BufferMethods, self, name);
942}
943
944 static PyObject *
945BufferRepr(PyObject *self)
946{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000947 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 BufferObject *this = (BufferObject *)(self);
949
950 if (this->buf == INVALID_BUFFER_VALUE)
951 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000952 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 return PyString_FromString(repr);
954 }
955 else
956 {
957 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000958 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960 if (name == NULL)
961 name = "";
962 len = strlen(name);
963
964 if (len > 35)
965 name = name + (35 - len);
966
Bram Moolenaar555b2802005-05-19 21:08:39 +0000967 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 return PyString_FromString(repr);
970 }
971}
972
973/******************/
974
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000975 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976BufferLength(PyObject *self)
977{
978 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
979 if (CheckBuffer((BufferObject *)(self)))
980 return -1; /* ??? */
981
982 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
983}
984
985 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000986BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987{
988 return RBItem((BufferObject *)(self), n, 1,
989 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
990}
991
992 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000993BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994{
995 return RBSlice((BufferObject *)(self), lo, hi, 1,
996 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
997}
998
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000999 static PyInt
1000BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001002 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001003 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 NULL);
1005}
1006
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001007 static PyInt
1008BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001010 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001011 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 NULL);
1013}
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001016 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001018 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1019 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1020 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1021 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1022 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023};
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025/* Line range object - Implementation
1026 */
1027
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 static void
1029RangeDestructor(PyObject *self)
1030{
1031 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001032 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035 static PyObject *
1036RangeGetattr(PyObject *self, char *name)
1037{
1038 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001039 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001041 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 else
1043 return Py_FindMethod(RangeMethods, self, name);
1044}
1045
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046/****************/
1047
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001048 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001049RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001051 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 ((RangeObject *)(self))->start,
1053 ((RangeObject *)(self))->end,
1054 &((RangeObject *)(self))->end);
1055}
1056
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001057 static PyInt
1058RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001060 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 ((RangeObject *)(self))->start,
1062 ((RangeObject *)(self))->end,
1063 &((RangeObject *)(self))->end);
1064}
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066/* Buffer list object - Definitions
1067 */
1068
1069typedef struct
1070{
1071 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001072} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
1074static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001075 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001077 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1078 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1079 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1080 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1081 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082};
1083
1084static PyTypeObject BufListType = {
1085 PyObject_HEAD_INIT(0)
1086 0,
1087 "buffer list",
1088 sizeof(BufListObject),
1089 0,
1090
1091 (destructor) 0, /* tp_dealloc, refcount==0 */
1092 (printfunc) 0, /* tp_print, print x */
1093 (getattrfunc) 0, /* tp_getattr, x.attr */
1094 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1095 (cmpfunc) 0, /* tp_compare, x>y */
1096 (reprfunc) 0, /* tp_repr, `x`, print x */
1097
1098 0, /* as number */
1099 &BufListAsSeq, /* as sequence */
1100 0, /* as mapping */
1101
1102 (hashfunc) 0, /* tp_hash, dict(x) */
1103 (ternaryfunc) 0, /* tp_call, x() */
1104 (reprfunc) 0, /* tp_str, str(x) */
1105};
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/* Window object - Definitions
1108 */
1109
1110static struct PyMethodDef WindowMethods[] = {
1111 /* name, function, calling, documentation */
1112 { NULL, NULL, 0, NULL }
1113};
1114
1115static PyTypeObject WindowType = {
1116 PyObject_HEAD_INIT(0)
1117 0,
1118 "window",
1119 sizeof(WindowObject),
1120 0,
1121
1122 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1123 (printfunc) 0, /* tp_print, print x */
1124 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1125 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1126 (cmpfunc) 0, /* tp_compare, x>y */
1127 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1128
1129 0, /* as number */
1130 0, /* as sequence */
1131 0, /* as mapping */
1132
1133 (hashfunc) 0, /* tp_hash, dict(x) */
1134 (ternaryfunc) 0, /* tp_call, x() */
1135 (reprfunc) 0, /* tp_str, str(x) */
1136};
1137
1138/* Window object - Implementation
1139 */
1140
1141 static PyObject *
1142WindowNew(win_T *win)
1143{
1144 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001145 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * then we can get at it in win_free() in vim. We then
1147 * need to create only ONE Python object per window - if
1148 * we try to create a second, just INCREF the existing one
1149 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001150 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * On a win_free() we set the Python object's win_T* field
1152 * to an invalid value. We trap all uses of a window
1153 * object, and reject them if the win_T* field is invalid.
1154 */
1155
1156 WindowObject *self;
1157
Bram Moolenaare344bea2005-09-01 20:46:49 +00001158 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001160 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 Py_INCREF(self);
1162 }
1163 else
1164 {
1165 self = PyObject_NEW(WindowObject, &WindowType);
1166 if (self == NULL)
1167 return NULL;
1168 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001169 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171
1172 return (PyObject *)(self);
1173}
1174
1175 static void
1176WindowDestructor(PyObject *self)
1177{
1178 WindowObject *this = (WindowObject *)(self);
1179
1180 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001181 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
Bram Moolenaar658ada62006-10-03 13:02:36 +00001183 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184}
1185
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 static PyObject *
1187WindowGetattr(PyObject *self, char *name)
1188{
1189 WindowObject *this = (WindowObject *)(self);
1190
1191 if (CheckWindow(this))
1192 return NULL;
1193
1194 if (strcmp(name, "buffer") == 0)
1195 return (PyObject *)BufferNew(this->win->w_buffer);
1196 else if (strcmp(name, "cursor") == 0)
1197 {
1198 pos_T *pos = &this->win->w_cursor;
1199
1200 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1201 }
1202 else if (strcmp(name, "height") == 0)
1203 return Py_BuildValue("l", (long)(this->win->w_height));
1204#ifdef FEAT_VERTSPLIT
1205 else if (strcmp(name, "width") == 0)
1206 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1207#endif
1208 else if (strcmp(name,"__members__") == 0)
1209 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1210 else
1211 return Py_FindMethod(WindowMethods, self, name);
1212}
1213
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214/* Window list object - Definitions
1215 */
1216
1217typedef struct
1218{
1219 PyObject_HEAD
1220}
1221WinListObject;
1222
1223static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001224 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001226 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1227 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1228 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1229 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1230 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231};
1232
1233static PyTypeObject WinListType = {
1234 PyObject_HEAD_INIT(0)
1235 0,
1236 "window list",
1237 sizeof(WinListObject),
1238 0,
1239
1240 (destructor) 0, /* tp_dealloc, refcount==0 */
1241 (printfunc) 0, /* tp_print, print x */
1242 (getattrfunc) 0, /* tp_getattr, x.attr */
1243 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1244 (cmpfunc) 0, /* tp_compare, x>y */
1245 (reprfunc) 0, /* tp_repr, `x`, print x */
1246
1247 0, /* as number */
1248 &WinListAsSeq, /* as sequence */
1249 0, /* as mapping */
1250
1251 (hashfunc) 0, /* tp_hash, dict(x) */
1252 (ternaryfunc) 0, /* tp_call, x() */
1253 (reprfunc) 0, /* tp_str, str(x) */
1254};
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256/* Current items object - Definitions
1257 */
1258
1259typedef struct
1260{
1261 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001262} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
1264static PyTypeObject CurrentType = {
1265 PyObject_HEAD_INIT(0)
1266 0,
1267 "current data",
1268 sizeof(CurrentObject),
1269 0,
1270
1271 (destructor) 0, /* tp_dealloc, refcount==0 */
1272 (printfunc) 0, /* tp_print, print x */
1273 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1274 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1275 (cmpfunc) 0, /* tp_compare, x>y */
1276 (reprfunc) 0, /* tp_repr, `x`, print x */
1277
1278 0, /* as number */
1279 0, /* as sequence */
1280 0, /* as mapping */
1281
1282 (hashfunc) 0, /* tp_hash, dict(x) */
1283 (ternaryfunc) 0, /* tp_call, x() */
1284 (reprfunc) 0, /* tp_str, str(x) */
1285};
1286
1287/* Current items object - Implementation
1288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001290CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 if (strcmp(name, "buffer") == 0)
1293 return (PyObject *)BufferNew(curbuf);
1294 else if (strcmp(name, "window") == 0)
1295 return (PyObject *)WindowNew(curwin);
1296 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001297 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 else if (strcmp(name, "range") == 0)
1299 return RangeNew(curbuf, RangeStart, RangeEnd);
1300 else if (strcmp(name,"__members__") == 0)
1301 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1302 else
1303 {
1304 PyErr_SetString(PyExc_AttributeError, name);
1305 return NULL;
1306 }
1307}
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001310CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 if (strcmp(name, "line") == 0)
1313 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001314 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 return -1;
1316
1317 return 0;
1318 }
1319 else
1320 {
1321 PyErr_SetString(PyExc_AttributeError, name);
1322 return -1;
1323 }
1324}
1325
1326/* External interface
1327 */
1328
1329 void
1330python_buffer_free(buf_T *buf)
1331{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001332 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001334 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001336 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 }
1338}
1339
1340#if defined(FEAT_WINDOWS) || defined(PROTO)
1341 void
1342python_window_free(win_T *win)
1343{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001344 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001346 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001348 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350}
1351#endif
1352
1353static BufListObject TheBufferList =
1354{
1355 PyObject_HEAD_INIT(&BufListType)
1356};
1357
1358static WinListObject TheWindowList =
1359{
1360 PyObject_HEAD_INIT(&WinListType)
1361};
1362
1363static CurrentObject TheCurrent =
1364{
1365 PyObject_HEAD_INIT(&CurrentType)
1366};
1367
1368 static int
1369PythonMod_Init(void)
1370{
1371 PyObject *mod;
1372 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001373 /* The special value is removed from sys.path in Python_Init(). */
1374 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375
1376 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001377 PyType_Ready(&BufferType);
1378 PyType_Ready(&RangeType);
1379 PyType_Ready(&WindowType);
1380 PyType_Ready(&BufListType);
1381 PyType_Ready(&WinListType);
1382 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
1384 /* Set sys.argv[] to avoid a crash in warn(). */
1385 PySys_SetArgv(1, argv);
1386
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001387 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 dict = PyModule_GetDict(mod);
1389
1390 VimError = Py_BuildValue("s", "vim.error");
1391
1392 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001393 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1394 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1395 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396
1397 if (PyErr_Occurred())
1398 return -1;
1399
1400 return 0;
1401}
1402
1403/*************************************************************************
1404 * 4. Utility functions for handling the interface between Vim and Python.
1405 */
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407/* Convert a Vim line into a Python string.
1408 * All internal newlines are replaced by null characters.
1409 *
1410 * On errors, the Python exception data is set, and NULL is returned.
1411 */
1412 static PyObject *
1413LineToString(const char *str)
1414{
1415 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001416 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 char *p;
1418
1419 /* Allocate an Python string object, with uninitialised contents. We
1420 * must do it this way, so that we can modify the string in place
1421 * later. See the Python source, Objects/stringobject.c for details.
1422 */
1423 result = PyString_FromStringAndSize(NULL, len);
1424 if (result == NULL)
1425 return NULL;
1426
1427 p = PyString_AsString(result);
1428
1429 while (*str)
1430 {
1431 if (*str == '\n')
1432 *p = '\0';
1433 else
1434 *p = *str;
1435
1436 ++p;
1437 ++str;
1438 }
1439
1440 return result;
1441}
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
1444/* Don't generate a prototype for the next function, it generates an error on
1445 * newer Python versions. */
1446#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1447
1448 char *
1449Py_GetProgramName(void)
1450{
1451 return "vim";
1452}
1453#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001454
1455 static void
1456init_structs(void)
1457{
1458 vim_memset(&OutputType, 0, sizeof(OutputType));
1459 OutputType.tp_name = "message";
1460 OutputType.tp_basicsize = sizeof(OutputObject);
1461 OutputType.tp_getattr = OutputGetattr;
1462 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001463
1464 vim_memset(&RangeType, 0, sizeof(RangeType));
1465 RangeType.tp_name = "range";
1466 RangeType.tp_basicsize = sizeof(RangeObject);
1467 RangeType.tp_dealloc = RangeDestructor;
1468 RangeType.tp_getattr = RangeGetattr;
1469 RangeType.tp_repr = RangeRepr;
1470 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001471}