blob: d8dff02f794f041b40fe89b2710c10f06a8d85ed [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 Moolenaar9dc93ae2011-08-28 16:00:19 +0200371 if (verbose)
372 EMSG(_("E836: This Vim cannot execute :python after using :py3"));
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200373 return FAIL;
374 }
375#endif
376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 if (hinstPython)
378 return OK;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200379 hinstPython = load_dll(libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 if (!hinstPython)
381 {
382 if (verbose)
383 EMSG2(_(e_loadlib), libname);
384 return FAIL;
385 }
386
387 for (i = 0; python_funcname_table[i].ptr; ++i)
388 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200389 if ((*python_funcname_table[i].ptr = symbol_from_dll(hinstPython,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 python_funcname_table[i].name)) == NULL)
391 {
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200392 close_dll(hinstPython);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 hinstPython = 0;
394 if (verbose)
395 EMSG2(_(e_loadfunc), python_funcname_table[i].name);
396 return FAIL;
397 }
398 }
399 return OK;
400}
401
402/*
403 * If python is enabled (there is installed python on Windows system) return
404 * TRUE, else FALSE.
405 */
406 int
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000407python_enabled(int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408{
409 return python_runtime_link_init(DYNAMIC_PYTHON_DLL, verbose) == OK;
410}
411
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200412/*
413 * Load the standard Python exceptions - don't import the symbols from the
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 * DLL, as this can cause errors (importing data symbols is not reliable).
415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 static void
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200417get_exceptions(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418{
419 PyObject *exmod = PyImport_ImportModule("exceptions");
420 PyObject *exdict = PyModule_GetDict(exmod);
421 imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError");
422 imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError");
423 imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt");
424 imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError");
425 imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError");
426 Py_XINCREF(imp_PyExc_AttributeError);
427 Py_XINCREF(imp_PyExc_IndexError);
428 Py_XINCREF(imp_PyExc_KeyboardInterrupt);
429 Py_XINCREF(imp_PyExc_TypeError);
430 Py_XINCREF(imp_PyExc_ValueError);
431 Py_XDECREF(exmod);
432}
433#endif /* DYNAMIC_PYTHON */
434
Bram Moolenaarca8a4df2010-07-31 19:54:14 +0200435static PyObject *BufferNew (buf_T *);
436static PyObject *WindowNew(win_T *);
437static PyObject *LineToString(const char *);
438
439static PyTypeObject RangeType;
440
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200441/*
442 * Include the code shared with if_python3.c
443 */
444#include "if_py_both.h"
445
446
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447/******************************************************
448 * Internal function prototypes.
449 */
450
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000451static PyInt RangeStart;
452static PyInt RangeEnd;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454static void PythonIO_Flush(void);
455static int PythonIO_Init(void);
456static int PythonMod_Init(void);
457
458/* Utility functions for the vim/python interface
459 * ----------------------------------------------
460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000462static int SetBufferLineList(buf_T *, PyInt, PyInt, PyObject *, PyInt *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464
465/******************************************************
466 * 1. Python interpreter main program.
467 */
468
469static int initialised = 0;
470
471#if PYTHON_API_VERSION < 1007 /* Python 1.4 */
472typedef PyObject PyThreadState;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475#ifdef PY_CAN_RECURSE
476static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
477#else
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000478static PyThreadState *saved_python_thread = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480
481/*
482 * Suspend a thread of the Python interpreter, other threads are allowed to
483 * run.
484 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000485 static void
486Python_SaveThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000488#ifdef PY_CAN_RECURSE
489 PyGILState_Release(pygilstate);
490#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 saved_python_thread = PyEval_SaveThread();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493}
494
495/*
496 * Restore a thread of the Python interpreter, waits for other threads to
497 * block.
498 */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000499 static void
500Python_RestoreThread(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502#ifdef PY_CAN_RECURSE
503 pygilstate = PyGILState_Ensure();
504#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 PyEval_RestoreThread(saved_python_thread);
506 saved_python_thread = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 void
511python_end()
512{
Bram Moolenaara5792f52005-11-23 21:25:05 +0000513 static int recurse = 0;
514
515 /* If a crash occurs while doing this, don't try again. */
516 if (recurse != 0)
517 return;
518
519 ++recurse;
520
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521#ifdef DYNAMIC_PYTHON
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000522 if (hinstPython && Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000523 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000524 Python_RestoreThread(); /* enter python */
525 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 end_dynamic_python();
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +0000528#else
529 if (Py_IsInitialized())
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000530 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000531 Python_RestoreThread(); /* enter python */
532 Py_Finalize();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#endif
Bram Moolenaara5792f52005-11-23 21:25:05 +0000535
536 --recurse;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537}
538
Bram Moolenaar4c3a3262010-07-24 15:42:14 +0200539#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
540 int
541python_loaded()
542{
543 return (hinstPython != 0);
544}
545#endif
546
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 static int
548Python_Init(void)
549{
550 if (!initialised)
551 {
552#ifdef DYNAMIC_PYTHON
553 if (!python_enabled(TRUE))
554 {
555 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
556 goto fail;
557 }
558#endif
559
Bram Moolenaar644d37b2010-11-16 19:26:02 +0100560#ifdef PYTHON_HOME
561 Py_SetPythonHome(PYTHON_HOME);
562#endif
563
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200564 init_structs();
565
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566#if !defined(MACOS) || defined(MACOS_X_UNIX)
567 Py_Initialize();
568#else
569 PyMac_Initialize();
570#endif
571 /* initialise threads */
572 PyEval_InitThreads();
573
574#ifdef DYNAMIC_PYTHON
575 get_exceptions();
576#endif
577
578 if (PythonIO_Init())
579 goto fail;
580
581 if (PythonMod_Init())
582 goto fail;
583
Bram Moolenaar9774ecc2008-11-20 10:04:53 +0000584 /* Remove the element from sys.path that was added because of our
585 * argv[0] value in PythonMod_Init(). Previously we used an empty
586 * string, but dependinding on the OS we then get an empty entry or
587 * the current directory in sys.path. */
588 PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)");
589
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000590 /* the first python thread is vim's, release the lock */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 Python_SaveThread();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592
593 initialised = 1;
594 }
595
596 return 0;
597
598fail:
599 /* We call PythonIO_Flush() here to print any Python errors.
600 * This is OK, as it is possible to call this function even
601 * if PythonIO_Init() has not completed successfully (it will
602 * not do anything in this case).
603 */
604 PythonIO_Flush();
605 return -1;
606}
607
608/*
609 * External interface
610 */
611 static void
612DoPythonCommand(exarg_T *eap, const char *cmd)
613{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614#ifndef PY_CAN_RECURSE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 static int recursive = 0;
616#endif
617#if defined(MACOS) && !defined(MACOS_X_UNIX)
618 GrafPtr oldPort;
619#endif
620#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
621 char *saved_locale;
622#endif
623
624#ifndef PY_CAN_RECURSE
625 if (recursive)
626 {
627 EMSG(_("E659: Cannot invoke Python recursively"));
628 return;
629 }
630 ++recursive;
631#endif
632
633#if defined(MACOS) && !defined(MACOS_X_UNIX)
634 GetPort(&oldPort);
635 /* Check if the Python library is available */
636 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress)
637 goto theend;
638#endif
639 if (Python_Init())
640 goto theend;
641
642 RangeStart = eap->line1;
643 RangeEnd = eap->line2;
644 Python_Release_Vim(); /* leave vim */
645
646#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
647 /* Python only works properly when the LC_NUMERIC locale is "C". */
648 saved_locale = setlocale(LC_NUMERIC, NULL);
649 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0)
650 saved_locale = NULL;
651 else
652 {
653 /* Need to make a copy, value may change when setting new locale. */
654 saved_locale = (char *)vim_strsave((char_u *)saved_locale);
655 (void)setlocale(LC_NUMERIC, "C");
656 }
657#endif
658
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 Python_RestoreThread(); /* enter python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 PyRun_SimpleString((char *)(cmd));
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 Python_SaveThread(); /* leave python */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
666 if (saved_locale != NULL)
667 {
668 (void)setlocale(LC_NUMERIC, saved_locale);
669 vim_free(saved_locale);
670 }
671#endif
672
673 Python_Lock_Vim(); /* enter vim */
674 PythonIO_Flush();
675#if defined(MACOS) && !defined(MACOS_X_UNIX)
676 SetPort(oldPort);
677#endif
678
679theend:
680#ifndef PY_CAN_RECURSE
681 --recursive;
682#endif
683 return; /* keeps lint happy */
684}
685
686/*
687 * ":python"
688 */
689 void
690ex_python(exarg_T *eap)
691{
692 char_u *script;
693
694 script = script_get(eap, eap->arg);
695 if (!eap->skip)
696 {
697 if (script == NULL)
698 DoPythonCommand(eap, (char *)eap->arg);
699 else
700 DoPythonCommand(eap, (char *)script);
701 }
702 vim_free(script);
703}
704
705#define BUFFER_SIZE 1024
706
707/*
708 * ":pyfile"
709 */
710 void
711ex_pyfile(exarg_T *eap)
712{
713 static char buffer[BUFFER_SIZE];
714 const char *file = (char *)eap->arg;
715 char *p;
716
717 /* Have to do it like this. PyRun_SimpleFile requires you to pass a
718 * stdio file pointer, but Vim and the Python DLL are compiled with
719 * different options under Windows, meaning that stdio pointers aren't
720 * compatible between the two. Yuk.
721 *
722 * Put the string "execfile('file')" into buffer. But, we need to
723 * escape any backslashes or single quotes in the file name, so that
724 * Python won't mangle the file name.
725 */
726 strcpy(buffer, "execfile('");
727 p = buffer + 10; /* size of "execfile('" */
728
729 while (*file && p < buffer + (BUFFER_SIZE - 3))
730 {
731 if (*file == '\\' || *file == '\'')
732 *p++ = '\\';
733 *p++ = *file++;
734 }
735
736 /* If we didn't finish the file name, we hit a buffer overflow */
737 if (*file != '\0')
738 return;
739
740 /* Put in the terminating "')" and a null */
741 *p++ = '\'';
742 *p++ = ')';
743 *p++ = '\0';
744
745 /* Execute the file */
746 DoPythonCommand(eap, buffer);
747}
748
749/******************************************************
750 * 2. Python output stream: writes output via [e]msg().
751 */
752
753/* Implementation functions
754 */
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 static PyObject *
757OutputGetattr(PyObject *self, char *name)
758{
759 if (strcmp(name, "softspace") == 0)
760 return PyInt_FromLong(((OutputObject *)(self))->softspace);
761
762 return Py_FindMethod(OutputMethods, self, name);
763}
764
765 static int
766OutputSetattr(PyObject *self, char *name, PyObject *val)
767{
768 if (val == NULL) {
769 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes"));
770 return -1;
771 }
772
773 if (strcmp(name, "softspace") == 0)
774 {
775 if (!PyInt_Check(val)) {
776 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer"));
777 return -1;
778 }
779
780 ((OutputObject *)(self))->softspace = PyInt_AsLong(val);
781 return 0;
782 }
783
784 PyErr_SetString(PyExc_AttributeError, _("invalid attribute"));
785 return -1;
786}
787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788/***************/
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 static int
791PythonIO_Init(void)
792{
793 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +0100794 PyType_Ready(&OutputType);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar170bf1a2010-07-24 23:51:45 +0200796 return PythonIO_Init_io();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797}
798
799/******************************************************
800 * 3. Implementation of the Vim module for Python
801 */
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803/* Window type - Implementation functions
804 * --------------------------------------
805 */
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807#define WindowType_Check(obj) ((obj)->ob_type == &WindowType)
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809static void WindowDestructor(PyObject *);
810static PyObject *WindowGetattr(PyObject *, char *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812/* Buffer type - Implementation functions
813 * --------------------------------------
814 */
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#define BufferType_Check(obj) ((obj)->ob_type == &BufferType)
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818static void BufferDestructor(PyObject *);
819static PyObject *BufferGetattr(PyObject *, char *);
820static PyObject *BufferRepr(PyObject *);
821
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000822static PyInt BufferLength(PyObject *);
823static PyObject *BufferItem(PyObject *, PyInt);
824static PyObject *BufferSlice(PyObject *, PyInt, PyInt);
825static PyInt BufferAssItem(PyObject *, PyInt, PyObject *);
826static PyInt BufferAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828/* Line range type - Implementation functions
829 * --------------------------------------
830 */
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#define RangeType_Check(obj) ((obj)->ob_type == &RangeType)
833
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000834static PyInt RangeAssItem(PyObject *, PyInt, PyObject *);
835static PyInt RangeAssSlice(PyObject *, PyInt, PyInt, PyObject *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837/* Current objects type - Implementation functions
838 * -----------------------------------------------
839 */
840
841static PyObject *CurrentGetattr(PyObject *, char *);
842static int CurrentSetattr(PyObject *, char *, PyObject *);
843
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844static PySequenceMethods BufferAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000845 (PyInquiry) BufferLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 (binaryfunc) 0, /* BufferConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000847 (PyIntArgFunc) 0, /* BufferRepeat, */ /* sq_repeat, x*n */
848 (PyIntArgFunc) BufferItem, /* sq_item, x[i] */
849 (PyIntIntArgFunc) BufferSlice, /* sq_slice, x[i:j] */
850 (PyIntObjArgProc) BufferAssItem, /* sq_ass_item, x[i]=v */
851 (PyIntIntObjArgProc) BufferAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852};
853
854static PyTypeObject BufferType = {
855 PyObject_HEAD_INIT(0)
856 0,
857 "buffer",
858 sizeof(BufferObject),
859 0,
860
861 (destructor) BufferDestructor, /* tp_dealloc, refcount==0 */
862 (printfunc) 0, /* tp_print, print x */
863 (getattrfunc) BufferGetattr, /* tp_getattr, x.attr */
864 (setattrfunc) 0, /* tp_setattr, x.attr=v */
865 (cmpfunc) 0, /* tp_compare, x>y */
866 (reprfunc) BufferRepr, /* tp_repr, `x`, print x */
867
868 0, /* as number */
869 &BufferAsSeq, /* as sequence */
870 0, /* as mapping */
871
872 (hashfunc) 0, /* tp_hash, dict(x) */
873 (ternaryfunc) 0, /* tp_call, x() */
874 (reprfunc) 0, /* tp_str, str(x) */
875};
876
877/* Buffer object - Implementation
878 */
879
880 static PyObject *
881BufferNew(buf_T *buf)
882{
883 /* We need to handle deletion of buffers underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +0000884 * If we add a "b_python_ref" field to the buf_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * then we can get at it in buf_freeall() in vim. We then
886 * need to create only ONE Python object per buffer - if
887 * we try to create a second, just INCREF the existing one
888 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +0000889 * the buffer is stored in "b_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * Question: what to do on a buf_freeall(). We'll probably
891 * have to either delete the Python object (DECREF it to
892 * zero - a bad idea, as it leaves dangling refs!) or
893 * set the buf_T * value to an invalid value (-1?), which
894 * means we need checks in all access functions... Bah.
895 */
896
897 BufferObject *self;
898
Bram Moolenaare344bea2005-09-01 20:46:49 +0000899 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaare344bea2005-09-01 20:46:49 +0000901 self = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 Py_INCREF(self);
903 }
904 else
905 {
906 self = PyObject_NEW(BufferObject, &BufferType);
907 if (self == NULL)
908 return NULL;
909 self->buf = buf;
Bram Moolenaare344bea2005-09-01 20:46:49 +0000910 buf->b_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912
913 return (PyObject *)(self);
914}
915
916 static void
917BufferDestructor(PyObject *self)
918{
919 BufferObject *this = (BufferObject *)(self);
920
921 if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +0000922 this->buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
Bram Moolenaar658ada62006-10-03 13:02:36 +0000924 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925}
926
927 static PyObject *
928BufferGetattr(PyObject *self, char *name)
929{
930 BufferObject *this = (BufferObject *)(self);
931
932 if (CheckBuffer(this))
933 return NULL;
934
935 if (strcmp(name, "name") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000936 return Py_BuildValue("s", this->buf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else if (strcmp(name, "number") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000938 return Py_BuildValue(Py_ssize_t_fmt, this->buf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 else if (strcmp(name,"__members__") == 0)
940 return Py_BuildValue("[ss]", "name", "number");
941 else
942 return Py_FindMethod(BufferMethods, self, name);
943}
944
945 static PyObject *
946BufferRepr(PyObject *self)
947{
Bram Moolenaar555b2802005-05-19 21:08:39 +0000948 static char repr[100];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 BufferObject *this = (BufferObject *)(self);
950
951 if (this->buf == INVALID_BUFFER_VALUE)
952 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000953 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 return PyString_FromString(repr);
955 }
956 else
957 {
958 char *name = (char *)this->buf->b_fname;
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +0000959 PyInt len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 if (name == NULL)
962 name = "";
963 len = strlen(name);
964
965 if (len > 35)
966 name = name + (35 - len);
967
Bram Moolenaar555b2802005-05-19 21:08:39 +0000968 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 return PyString_FromString(repr);
971 }
972}
973
974/******************/
975
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000976 static PyInt
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977BufferLength(PyObject *self)
978{
979 /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
980 if (CheckBuffer((BufferObject *)(self)))
981 return -1; /* ??? */
982
983 return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
984}
985
986 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000987BufferItem(PyObject *self, PyInt n)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988{
989 return RBItem((BufferObject *)(self), n, 1,
990 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
991}
992
993 static PyObject *
Bram Moolenaar2c45e942008-06-04 11:35:26 +0000994BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996 return RBSlice((BufferObject *)(self), lo, hi, 1,
997 (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
998}
999
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001000 static PyInt
1001BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001003 return RBAsItem((BufferObject *)(self), n, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001004 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 NULL);
1006}
1007
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001008 static PyInt
1009BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001011 return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001012 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 NULL);
1014}
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016static PySequenceMethods RangeAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001017 (PyInquiry) RangeLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 (binaryfunc) 0, /* RangeConcat, */ /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001019 (PyIntArgFunc) 0, /* RangeRepeat, */ /* sq_repeat, x*n */
1020 (PyIntArgFunc) RangeItem, /* sq_item, x[i] */
1021 (PyIntIntArgFunc) RangeSlice, /* sq_slice, x[i:j] */
1022 (PyIntObjArgProc) RangeAssItem, /* sq_ass_item, x[i]=v */
1023 (PyIntIntObjArgProc) RangeAssSlice, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024};
1025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026/* Line range object - Implementation
1027 */
1028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 static void
1030RangeDestructor(PyObject *self)
1031{
1032 Py_DECREF(((RangeObject *)(self))->buf);
Bram Moolenaar658ada62006-10-03 13:02:36 +00001033 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036 static PyObject *
1037RangeGetattr(PyObject *self, char *name)
1038{
1039 if (strcmp(name, "start") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001040 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->start - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 else if (strcmp(name, "end") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001042 return Py_BuildValue(Py_ssize_t_fmt, ((RangeObject *)(self))->end - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 else
1044 return Py_FindMethod(RangeMethods, self, name);
1045}
1046
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047/****************/
1048
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001049 static PyInt
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001050RangeAssItem(PyObject *self, PyInt n, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001052 return RBAsItem(((RangeObject *)(self))->buf, n, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 ((RangeObject *)(self))->start,
1054 ((RangeObject *)(self))->end,
1055 &((RangeObject *)(self))->end);
1056}
1057
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001058 static PyInt
1059RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar19e60942011-06-19 00:27:51 +02001061 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 ((RangeObject *)(self))->start,
1063 ((RangeObject *)(self))->end,
1064 &((RangeObject *)(self))->end);
1065}
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067/* Buffer list object - Definitions
1068 */
1069
1070typedef struct
1071{
1072 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001073} BufListObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075static PySequenceMethods BufListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001076 (PyInquiry) BufListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001078 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1079 (PyIntArgFunc) BufListItem, /* sq_item, x[i] */
1080 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1081 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1082 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083};
1084
1085static PyTypeObject BufListType = {
1086 PyObject_HEAD_INIT(0)
1087 0,
1088 "buffer list",
1089 sizeof(BufListObject),
1090 0,
1091
1092 (destructor) 0, /* tp_dealloc, refcount==0 */
1093 (printfunc) 0, /* tp_print, print x */
1094 (getattrfunc) 0, /* tp_getattr, x.attr */
1095 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1096 (cmpfunc) 0, /* tp_compare, x>y */
1097 (reprfunc) 0, /* tp_repr, `x`, print x */
1098
1099 0, /* as number */
1100 &BufListAsSeq, /* as sequence */
1101 0, /* as mapping */
1102
1103 (hashfunc) 0, /* tp_hash, dict(x) */
1104 (ternaryfunc) 0, /* tp_call, x() */
1105 (reprfunc) 0, /* tp_str, str(x) */
1106};
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108/* Window object - Definitions
1109 */
1110
1111static struct PyMethodDef WindowMethods[] = {
1112 /* name, function, calling, documentation */
1113 { NULL, NULL, 0, NULL }
1114};
1115
1116static PyTypeObject WindowType = {
1117 PyObject_HEAD_INIT(0)
1118 0,
1119 "window",
1120 sizeof(WindowObject),
1121 0,
1122
1123 (destructor) WindowDestructor, /* tp_dealloc, refcount==0 */
1124 (printfunc) 0, /* tp_print, print x */
1125 (getattrfunc) WindowGetattr, /* tp_getattr, x.attr */
1126 (setattrfunc) WindowSetattr, /* tp_setattr, x.attr=v */
1127 (cmpfunc) 0, /* tp_compare, x>y */
1128 (reprfunc) WindowRepr, /* tp_repr, `x`, print x */
1129
1130 0, /* as number */
1131 0, /* as sequence */
1132 0, /* as mapping */
1133
1134 (hashfunc) 0, /* tp_hash, dict(x) */
1135 (ternaryfunc) 0, /* tp_call, x() */
1136 (reprfunc) 0, /* tp_str, str(x) */
1137};
1138
1139/* Window object - Implementation
1140 */
1141
1142 static PyObject *
1143WindowNew(win_T *win)
1144{
1145 /* We need to handle deletion of windows underneath us.
Bram Moolenaare344bea2005-09-01 20:46:49 +00001146 * If we add a "w_python_ref" field to the win_T structure,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 * then we can get at it in win_free() in vim. We then
1148 * need to create only ONE Python object per window - if
1149 * we try to create a second, just INCREF the existing one
1150 * and return it. The (single) Python object referring to
Bram Moolenaare344bea2005-09-01 20:46:49 +00001151 * the window is stored in "w_python_ref".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * On a win_free() we set the Python object's win_T* field
1153 * to an invalid value. We trap all uses of a window
1154 * object, and reject them if the win_T* field is invalid.
1155 */
1156
1157 WindowObject *self;
1158
Bram Moolenaare344bea2005-09-01 20:46:49 +00001159 if (win->w_python_ref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001161 self = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 Py_INCREF(self);
1163 }
1164 else
1165 {
1166 self = PyObject_NEW(WindowObject, &WindowType);
1167 if (self == NULL)
1168 return NULL;
1169 self->win = win;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001170 win->w_python_ref = self;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 }
1172
1173 return (PyObject *)(self);
1174}
1175
1176 static void
1177WindowDestructor(PyObject *self)
1178{
1179 WindowObject *this = (WindowObject *)(self);
1180
1181 if (this->win && this->win != INVALID_WINDOW_VALUE)
Bram Moolenaare344bea2005-09-01 20:46:49 +00001182 this->win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaar658ada62006-10-03 13:02:36 +00001184 Py_DECREF(self);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185}
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 static PyObject *
1188WindowGetattr(PyObject *self, char *name)
1189{
1190 WindowObject *this = (WindowObject *)(self);
1191
1192 if (CheckWindow(this))
1193 return NULL;
1194
1195 if (strcmp(name, "buffer") == 0)
1196 return (PyObject *)BufferNew(this->win->w_buffer);
1197 else if (strcmp(name, "cursor") == 0)
1198 {
1199 pos_T *pos = &this->win->w_cursor;
1200
1201 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col));
1202 }
1203 else if (strcmp(name, "height") == 0)
1204 return Py_BuildValue("l", (long)(this->win->w_height));
1205#ifdef FEAT_VERTSPLIT
1206 else if (strcmp(name, "width") == 0)
1207 return Py_BuildValue("l", (long)(W_WIDTH(this->win)));
1208#endif
1209 else if (strcmp(name,"__members__") == 0)
1210 return Py_BuildValue("[sss]", "buffer", "cursor", "height");
1211 else
1212 return Py_FindMethod(WindowMethods, self, name);
1213}
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215/* Window list object - Definitions
1216 */
1217
1218typedef struct
1219{
1220 PyObject_HEAD
1221}
1222WinListObject;
1223
1224static PySequenceMethods WinListAsSeq = {
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001225 (PyInquiry) WinListLength, /* sq_length, len(x) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 (binaryfunc) 0, /* sq_concat, x+y */
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001227 (PyIntArgFunc) 0, /* sq_repeat, x*n */
1228 (PyIntArgFunc) WinListItem, /* sq_item, x[i] */
1229 (PyIntIntArgFunc) 0, /* sq_slice, x[i:j] */
1230 (PyIntObjArgProc) 0, /* sq_ass_item, x[i]=v */
1231 (PyIntIntObjArgProc) 0, /* sq_ass_slice, x[i:j]=v */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232};
1233
1234static PyTypeObject WinListType = {
1235 PyObject_HEAD_INIT(0)
1236 0,
1237 "window list",
1238 sizeof(WinListObject),
1239 0,
1240
1241 (destructor) 0, /* tp_dealloc, refcount==0 */
1242 (printfunc) 0, /* tp_print, print x */
1243 (getattrfunc) 0, /* tp_getattr, x.attr */
1244 (setattrfunc) 0, /* tp_setattr, x.attr=v */
1245 (cmpfunc) 0, /* tp_compare, x>y */
1246 (reprfunc) 0, /* tp_repr, `x`, print x */
1247
1248 0, /* as number */
1249 &WinListAsSeq, /* as sequence */
1250 0, /* as mapping */
1251
1252 (hashfunc) 0, /* tp_hash, dict(x) */
1253 (ternaryfunc) 0, /* tp_call, x() */
1254 (reprfunc) 0, /* tp_str, str(x) */
1255};
1256
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257/* Current items object - Definitions
1258 */
1259
1260typedef struct
1261{
1262 PyObject_HEAD
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001263} CurrentObject;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
1265static PyTypeObject CurrentType = {
1266 PyObject_HEAD_INIT(0)
1267 0,
1268 "current data",
1269 sizeof(CurrentObject),
1270 0,
1271
1272 (destructor) 0, /* tp_dealloc, refcount==0 */
1273 (printfunc) 0, /* tp_print, print x */
1274 (getattrfunc) CurrentGetattr, /* tp_getattr, x.attr */
1275 (setattrfunc) CurrentSetattr, /* tp_setattr, x.attr=v */
1276 (cmpfunc) 0, /* tp_compare, x>y */
1277 (reprfunc) 0, /* tp_repr, `x`, print x */
1278
1279 0, /* as number */
1280 0, /* as sequence */
1281 0, /* as mapping */
1282
1283 (hashfunc) 0, /* tp_hash, dict(x) */
1284 (ternaryfunc) 0, /* tp_call, x() */
1285 (reprfunc) 0, /* tp_str, str(x) */
1286};
1287
1288/* Current items object - Implementation
1289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 static PyObject *
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001291CurrentGetattr(PyObject *self UNUSED, char *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292{
1293 if (strcmp(name, "buffer") == 0)
1294 return (PyObject *)BufferNew(curbuf);
1295 else if (strcmp(name, "window") == 0)
1296 return (PyObject *)WindowNew(curwin);
1297 else if (strcmp(name, "line") == 0)
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001298 return GetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 else if (strcmp(name, "range") == 0)
1300 return RangeNew(curbuf, RangeStart, RangeEnd);
1301 else if (strcmp(name,"__members__") == 0)
1302 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range");
1303 else
1304 {
1305 PyErr_SetString(PyExc_AttributeError, name);
1306 return NULL;
1307 }
1308}
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 static int
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001311CurrentSetattr(PyObject *self UNUSED, char *name, PyObject *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312{
1313 if (strcmp(name, "line") == 0)
1314 {
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001315 if (SetBufferLine(curbuf, (PyInt)curwin->w_cursor.lnum, value, NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 return -1;
1317
1318 return 0;
1319 }
1320 else
1321 {
1322 PyErr_SetString(PyExc_AttributeError, name);
1323 return -1;
1324 }
1325}
1326
1327/* External interface
1328 */
1329
1330 void
1331python_buffer_free(buf_T *buf)
1332{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001333 if (buf->b_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001335 BufferObject *bp = buf->b_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 bp->buf = INVALID_BUFFER_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001337 buf->b_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339}
1340
1341#if defined(FEAT_WINDOWS) || defined(PROTO)
1342 void
1343python_window_free(win_T *win)
1344{
Bram Moolenaare344bea2005-09-01 20:46:49 +00001345 if (win->w_python_ref != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00001347 WindowObject *wp = win->w_python_ref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 wp->win = INVALID_WINDOW_VALUE;
Bram Moolenaare344bea2005-09-01 20:46:49 +00001349 win->w_python_ref = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351}
1352#endif
1353
1354static BufListObject TheBufferList =
1355{
1356 PyObject_HEAD_INIT(&BufListType)
1357};
1358
1359static WinListObject TheWindowList =
1360{
1361 PyObject_HEAD_INIT(&WinListType)
1362};
1363
1364static CurrentObject TheCurrent =
1365{
1366 PyObject_HEAD_INIT(&CurrentType)
1367};
1368
1369 static int
1370PythonMod_Init(void)
1371{
1372 PyObject *mod;
1373 PyObject *dict;
Bram Moolenaar9774ecc2008-11-20 10:04:53 +00001374 /* The special value is removed from sys.path in Python_Init(). */
1375 static char *(argv[2]) = {"/must>not&exist/foo", NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 /* Fixups... */
Bram Moolenaar21377c82011-03-26 13:56:48 +01001378 PyType_Ready(&BufferType);
1379 PyType_Ready(&RangeType);
1380 PyType_Ready(&WindowType);
1381 PyType_Ready(&BufListType);
1382 PyType_Ready(&WinListType);
1383 PyType_Ready(&CurrentType);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 /* Set sys.argv[] to avoid a crash in warn(). */
1386 PySys_SetArgv(1, argv);
1387
Bram Moolenaare7cb9cf2008-06-20 14:32:41 +00001388 mod = Py_InitModule4("vim", VimMethods, (char *)NULL, (PyObject *)NULL, PYTHON_API_VERSION);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 dict = PyModule_GetDict(mod);
1390
1391 VimError = Py_BuildValue("s", "vim.error");
1392
1393 PyDict_SetItemString(dict, "error", VimError);
Bram Moolenaar7df2d662005-01-25 22:18:08 +00001394 PyDict_SetItemString(dict, "buffers", (PyObject *)(void *)&TheBufferList);
1395 PyDict_SetItemString(dict, "current", (PyObject *)(void *)&TheCurrent);
1396 PyDict_SetItemString(dict, "windows", (PyObject *)(void *)&TheWindowList);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397
1398 if (PyErr_Occurred())
1399 return -1;
1400
1401 return 0;
1402}
1403
1404/*************************************************************************
1405 * 4. Utility functions for handling the interface between Vim and Python.
1406 */
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408/* Convert a Vim line into a Python string.
1409 * All internal newlines are replaced by null characters.
1410 *
1411 * On errors, the Python exception data is set, and NULL is returned.
1412 */
1413 static PyObject *
1414LineToString(const char *str)
1415{
1416 PyObject *result;
Bram Moolenaar2c45e942008-06-04 11:35:26 +00001417 PyInt len = strlen(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char *p;
1419
1420 /* Allocate an Python string object, with uninitialised contents. We
1421 * must do it this way, so that we can modify the string in place
1422 * later. See the Python source, Objects/stringobject.c for details.
1423 */
1424 result = PyString_FromStringAndSize(NULL, len);
1425 if (result == NULL)
1426 return NULL;
1427
1428 p = PyString_AsString(result);
1429
1430 while (*str)
1431 {
1432 if (*str == '\n')
1433 *p = '\0';
1434 else
1435 *p = *str;
1436
1437 ++p;
1438 ++str;
1439 }
1440
1441 return result;
1442}
1443
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445/* Don't generate a prototype for the next function, it generates an error on
1446 * newer Python versions. */
1447#if PYTHON_API_VERSION < 1007 /* Python 1.4 */ && !defined(PROTO)
1448
1449 char *
1450Py_GetProgramName(void)
1451{
1452 return "vim";
1453}
1454#endif /* Python 1.4 */
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001455
1456 static void
1457init_structs(void)
1458{
1459 vim_memset(&OutputType, 0, sizeof(OutputType));
1460 OutputType.tp_name = "message";
1461 OutputType.tp_basicsize = sizeof(OutputObject);
1462 OutputType.tp_getattr = OutputGetattr;
1463 OutputType.tp_setattr = OutputSetattr;
Bram Moolenaarca8a4df2010-07-31 19:54:14 +02001464
1465 vim_memset(&RangeType, 0, sizeof(RangeType));
1466 RangeType.tp_name = "range";
1467 RangeType.tp_basicsize = sizeof(RangeObject);
1468 RangeType.tp_dealloc = RangeDestructor;
1469 RangeType.tp_getattr = RangeGetattr;
1470 RangeType.tp_repr = RangeRepr;
1471 RangeType.tp_as_sequence = &RangeAsSeq;
Bram Moolenaar170bf1a2010-07-24 23:51:45 +02001472}