1/* 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/* 21 * Roland Puntaier 2009/sept/16: 22 * Adaptations to support both python3.x and python2.x 23 */ 24 25// uncomment this if used with the debug version of python 26// #define Py_DEBUG 27 28#include "vim.h" 29 30#include <limits.h> 31 32/* Python.h defines _POSIX_THREADS itself (if needed) */ 33#ifdef _POSIX_THREADS 34# undef _POSIX_THREADS 35#endif 36 37#if defined(_WIN32) && defined(HAVE_FCNTL_H) 38# undef HAVE_FCNTL_H 39#endif 40 41#ifdef _DEBUG 42# undef _DEBUG 43#endif 44 45#define PY_SSIZE_T_CLEAN 46 47#ifdef F_BLANK 48# undef F_BLANK 49#endif 50 51#ifdef HAVE_STDARG_H 52# undef HAVE_STDARG_H /* Python's config.h defines it as well. */ 53#endif 54#ifdef _POSIX_C_SOURCE /* defined in feature.h */ 55# undef _POSIX_C_SOURCE 56#endif 57#ifdef _XOPEN_SOURCE 58# undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ 59#endif 60 61#include <Python.h> 62#if defined(MACOS) && !defined(MACOS_X_UNIX) 63# include "macglue.h" 64# include <CodeFragments.h> 65#endif 66#undef main /* Defined in python.h - aargh */ 67#undef HAVE_FCNTL_H /* Clash with os_win32.h */ 68 69static void init_structs(void); 70 71#define PyInt Py_ssize_t 72#define PyString_Check(obj) PyUnicode_Check(obj) 73#define PyString_AsString(obj) _PyUnicode_AsString(obj) 74#define PyString_Size(obj) PyUnicode_GET_SIZE(obj) 75#define PyString_FromString(repr) PyUnicode_FromString(repr) 76 77#if defined(DYNAMIC_PYTHON3) 78 79# ifndef WIN3264 80# include <dlfcn.h> 81# define FARPROC void* 82# define HINSTANCE void* 83# ifdef PY_NO_RTLD_GLOBAL 84# define load_dll(n) dlopen((n), RTLD_LAZY) 85# else 86# define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) 87# endif 88# define close_dll dlclose 89# define symbol_from_dll dlsym 90# else 91# define load_dll LoadLibrary 92# define close_dll FreeLibrary 93# define symbol_from_dll GetProcAddress 94# endif 95/* 96 * Wrapper defines 97 */ 98# undef PyArg_Parse 99# define PyArg_Parse py3_PyArg_Parse 100# undef PyArg_ParseTuple 101# define PyArg_ParseTuple py3_PyArg_ParseTuple 102# define PyDict_SetItemString py3_PyDict_SetItemString 103# define PyErr_BadArgument py3_PyErr_BadArgument 104# define PyErr_Clear py3_PyErr_Clear 105# define PyErr_NoMemory py3_PyErr_NoMemory 106# define PyErr_Occurred py3_PyErr_Occurred 107# define PyErr_SetNone py3_PyErr_SetNone 108# define PyErr_SetString py3_PyErr_SetString 109# define PyEval_InitThreads py3_PyEval_InitThreads 110# define PyEval_RestoreThread py3_PyEval_RestoreThread 111# define PyEval_SaveThread py3_PyEval_SaveThread 112# define PyGILState_Ensure py3_PyGILState_Ensure 113# define PyGILState_Release py3_PyGILState_Release 114# define PyLong_AsLong py3_PyLong_AsLong 115# define PyLong_FromLong py3_PyLong_FromLong 116# define PyList_GetItem py3_PyList_GetItem 117# define PyList_Append py3_PyList_Append 118# define PyList_New py3_PyList_New 119# define PyList_SetItem py3_PyList_SetItem 120# define PyList_Size py3_PyList_Size 121# define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx 122# define PyImport_ImportModule py3_PyImport_ImportModule 123# define PyObject_Init py3__PyObject_Init 124# define PyDict_New py3_PyDict_New 125# define PyDict_GetItemString py3_PyDict_GetItemString 126# define PyModule_GetDict py3_PyModule_GetDict 127#undef PyRun_SimpleString 128# define PyRun_SimpleString py3_PyRun_SimpleString 129# define PySys_SetObject py3_PySys_SetObject 130# define PySys_SetArgv py3_PySys_SetArgv 131# define PyType_Type (*py3_PyType_Type) 132# define PyType_Ready py3_PyType_Ready 133#undef Py_BuildValue 134# define Py_BuildValue py3_Py_BuildValue 135# define Py_Initialize py3_Py_Initialize 136# define Py_Finalize py3_Py_Finalize 137# define Py_IsInitialized py3_Py_IsInitialized 138# define _Py_NoneStruct (*py3__Py_NoneStruct) 139# define PyModule_AddObject py3_PyModule_AddObject 140# define PyImport_AppendInittab py3_PyImport_AppendInittab 141# define _PyUnicode_AsString py3__PyUnicode_AsString 142# define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr 143# define PySlice_Type (*py3_PySlice_Type) 144# ifdef Py_DEBUG 145# define _Py_NegativeRefcount py3__Py_NegativeRefcount 146# define _Py_RefTotal (*py3__Py_RefTotal) 147# define _Py_Dealloc py3__Py_Dealloc 148# define _PyObject_DebugMalloc py3__PyObject_DebugMalloc 149# define _PyObject_DebugFree py3__PyObject_DebugFree 150# else 151# define PyObject_Malloc py3_PyObject_Malloc 152# define PyObject_Free py3_PyObject_Free 153# endif 154# define PyType_GenericAlloc py3_PyType_GenericAlloc 155# define PyType_GenericNew py3_PyType_GenericNew 156# define PyModule_Create2 py3_PyModule_Create2 157# undef PyUnicode_FromString 158# define PyUnicode_FromString py3_PyUnicode_FromString 159# undef PyUnicode_FromStringAndSize 160# define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize 161 162# ifdef Py_DEBUG 163# undef PyObject_NEW 164# define PyObject_NEW(type, typeobj) \ 165( (type *) PyObject_Init( \ 166 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) 167# endif 168 169/* 170 * Pointers for dynamic link 171 */ 172static int (*py3_PySys_SetArgv)(int, wchar_t **); 173static void (*py3_Py_Initialize)(void); 174static PyObject* (*py3_PyList_New)(Py_ssize_t size); 175static PyGILState_STATE (*py3_PyGILState_Ensure)(void); 176static void (*py3_PyGILState_Release)(PyGILState_STATE); 177static int (*py3_PySys_SetObject)(char *, PyObject *); 178static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *); 179static Py_ssize_t (*py3_PyList_Size)(PyObject *); 180static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length, 181 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength); 182static PyObject* (*py3_PyErr_NoMemory)(void); 183static void (*py3_Py_Finalize)(void); 184static void (*py3_PyErr_SetString)(PyObject *, const char *); 185static int (*py3_PyRun_SimpleString)(char *); 186static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); 187static PyObject* (*py3_PyImport_ImportModule)(const char *); 188static int (*py3_PyErr_BadArgument)(void); 189static PyTypeObject* py3_PyType_Type; 190static PyObject* (*py3_PyErr_Occurred)(void); 191static PyObject* (*py3_PyModule_GetDict)(PyObject *); 192static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); 193static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); 194static PyObject* (*py3_PyLong_FromLong)(long); 195static PyObject* (*py3_PyDict_New)(void); 196static PyObject* (*py3_Py_BuildValue)(char *, ...); 197static int (*py3_PyType_Ready)(PyTypeObject *type); 198static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); 199static PyObject* (*py3_PyUnicode_FromString)(const char *u); 200static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size); 201static long (*py3_PyLong_AsLong)(PyObject *); 202static void (*py3_PyErr_SetNone)(PyObject *); 203static void (*py3_PyEval_InitThreads)(void); 204static void(*py3_PyEval_RestoreThread)(PyThreadState *); 205static PyThreadState*(*py3_PyEval_SaveThread)(void); 206static int (*py3_PyArg_Parse)(PyObject *, char *, ...); 207static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); 208static int (*py3_Py_IsInitialized)(void); 209static void (*py3_PyErr_Clear)(void); 210static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); 211static PyObject* py3__Py_NoneStruct; 212static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); 213static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); 214static char* (*py3__PyUnicode_AsString)(PyObject *unicode); 215static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); 216static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); 217static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); 218static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); 219static PyTypeObject* py3_PySlice_Type; 220# ifdef Py_DEBUG 221 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); 222 static Py_ssize_t* py3__Py_RefTotal; 223 static void (*py3__Py_Dealloc)(PyObject *obj); 224 static void (*py3__PyObject_DebugFree)(void*); 225 static void* (*py3__PyObject_DebugMalloc)(size_t); 226# else 227 static void (*py3_PyObject_Free)(void*); 228 static void* (*py3_PyObject_Malloc)(size_t); 229# endif 230 231static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ 232 233/* Imported exception objects */ 234static PyObject *p3imp_PyExc_AttributeError; 235static PyObject *p3imp_PyExc_IndexError; 236static PyObject *p3imp_PyExc_KeyboardInterrupt; 237static PyObject *p3imp_PyExc_TypeError; 238static PyObject *p3imp_PyExc_ValueError; 239 240# define PyExc_AttributeError p3imp_PyExc_AttributeError 241# define PyExc_IndexError p3imp_PyExc_IndexError 242# define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt 243# define PyExc_TypeError p3imp_PyExc_TypeError 244# define PyExc_ValueError p3imp_PyExc_ValueError 245 246/* 247 * Table of name to function pointer of python. 248 */ 249# define PYTHON_PROC FARPROC 250static struct 251{ 252 char *name; 253 PYTHON_PROC *ptr; 254} py3_funcname_table[] = 255{ 256 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, 257 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, 258 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, 259 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, 260 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, 261 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, 262 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, 263 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, 264 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, 265 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, 266 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, 267 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, 268 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, 269 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, 270 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, 271 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, 272 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, 273 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, 274 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, 275 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, 276 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, 277 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, 278 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, 279 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, 280 {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue}, 281 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, 282 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, 283 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, 284 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, 285 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, 286 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, 287 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, 288 {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse}, 289 {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, 290 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, 291 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, 292 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, 293 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, 294 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, 295 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, 296 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, 297 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, 298 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, 299 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, 300 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, 301 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, 302# ifdef Py_DEBUG 303 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, 304 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, 305 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, 306 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, 307 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, 308# else 309 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, 310 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, 311# endif 312 {"", NULL}, 313}; 314 315/* 316 * Free python.dll 317 */ 318 static void 319end_dynamic_python3(void) 320{ 321 if (hinstPy3 != 0) 322 { 323 close_dll(hinstPy3); 324 hinstPy3 = 0; 325 } 326} 327 328/* 329 * Load library and get all pointers. 330 * Parameter 'libname' provides name of DLL. 331 * Return OK or FAIL. 332 */ 333 static int 334py3_runtime_link_init(char *libname, int verbose) 335{ 336 int i; 337 void *ucs_from_string, *ucs_from_string_and_size; 338 339# if !defined(PY_NO_RTLD_GLOBAL) && defined(UNIX) && defined(FEAT_PYTHON) 340 /* Can't have Python and Python3 loaded at the same time. 341 * It cause a crash, because RTLD_GLOBAL is needed for 342 * standard C extension libraries of one or both python versions. */ 343 if (python_loaded()) 344 { 345 EMSG(_("E837: This Vim cannot execute :py3 after using :python")); 346 return FAIL; 347 } 348# endif 349 350 if (hinstPy3 != 0) 351 return OK; 352 hinstPy3 = load_dll(libname); 353 354 if (!hinstPy3) 355 { 356 if (verbose) 357 EMSG2(_(e_loadlib), libname); 358 return FAIL; 359 } 360 361 for (i = 0; py3_funcname_table[i].ptr; ++i) 362 { 363 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, 364 py3_funcname_table[i].name)) == NULL) 365 { 366 close_dll(hinstPy3); 367 hinstPy3 = 0; 368 if (verbose) 369 EMSG2(_(e_loadfunc), py3_funcname_table[i].name); 370 return FAIL; 371 } 372 } 373 374 /* Load unicode functions separately as only the ucs2 or the ucs4 functions 375 * will be present in the library. */ 376 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); 377 ucs_from_string_and_size = symbol_from_dll(hinstPy3, 378 "PyUnicodeUCS2_FromStringAndSize"); 379 if (!ucs_from_string || !ucs_from_string_and_size) 380 { 381 ucs_from_string = symbol_from_dll(hinstPy3, 382 "PyUnicodeUCS4_FromString"); 383 ucs_from_string_and_size = symbol_from_dll(hinstPy3, 384 "PyUnicodeUCS4_FromStringAndSize"); 385 } 386 if (ucs_from_string && ucs_from_string_and_size) 387 { 388 py3_PyUnicode_FromString = ucs_from_string; 389 py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size; 390 } 391 else 392 { 393 close_dll(hinstPy3); 394 hinstPy3 = 0; 395 if (verbose) 396 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); 397 return FAIL; 398 } 399 400 return OK; 401} 402 403/* 404 * If python is enabled (there is installed python on Windows system) return 405 * TRUE, else FALSE. 406 */ 407 int 408python3_enabled(int verbose) 409{ 410 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; 411} 412 413/* Load the standard Python exceptions - don't import the symbols from the 414 * DLL, as this can cause errors (importing data symbols is not reliable). 415 */ 416static void get_py3_exceptions __ARGS((void)); 417 418 static void 419get_py3_exceptions() 420{ 421 PyObject *exmod = PyImport_ImportModule("builtins"); 422 PyObject *exdict = PyModule_GetDict(exmod); 423 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); 424 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); 425 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); 426 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); 427 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); 428 Py_XINCREF(p3imp_PyExc_AttributeError); 429 Py_XINCREF(p3imp_PyExc_IndexError); 430 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); 431 Py_XINCREF(p3imp_PyExc_TypeError); 432 Py_XINCREF(p3imp_PyExc_ValueError); 433 Py_XDECREF(exmod); 434} 435#endif /* DYNAMIC_PYTHON3 */ 436 437static PyObject *BufferNew (buf_T *); 438static PyObject *WindowNew(win_T *); 439static PyObject *LineToString(const char *); 440 441static PyTypeObject RangeType; 442 443/* 444 * Include the code shared with if_python.c 445 */ 446#include "if_py_both.h" 447 448 static void 449call_PyObject_Free(void *p) 450{ 451#ifdef Py_DEBUG 452 _PyObject_DebugFree(p); 453#else 454 PyObject_Free(p); 455#endif 456} 457 458 static PyObject * 459call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) 460{ 461 return PyType_GenericNew(type,args,kwds); 462} 463 464 static PyObject * 465call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) 466{ 467 return PyType_GenericAlloc(type,nitems); 468} 469 470/****************************************************** 471 * Internal function prototypes. 472 */ 473 474static Py_ssize_t RangeStart; 475static Py_ssize_t RangeEnd; 476 477static int PythonIO_Init(void); 478static void PythonIO_Fini(void); 479PyMODINIT_FUNC Py3Init_vim(void); 480 481/****************************************************** 482 * 1. Python interpreter main program. 483 */ 484 485static int py3initialised = 0; 486 487static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; 488 489 void 490python3_end() 491{ 492 static int recurse = 0; 493 494 /* If a crash occurs while doing this, don't try again. */ 495 if (recurse != 0) 496 return; 497 498 ++recurse; 499 500#ifdef DYNAMIC_PYTHON3 501 if (hinstPy3) 502#endif 503 if (Py_IsInitialized()) 504 { 505 // acquire lock before finalizing 506 pygilstate = PyGILState_Ensure(); 507 508 PythonIO_Fini(); 509 Py_Finalize(); 510 } 511 512#ifdef DYNAMIC_PYTHON3 513 end_dynamic_python3(); 514#endif 515 516 --recurse; 517} 518 519#if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO) 520 int 521python3_loaded() 522{ 523 return (hinstPy3 != 0); 524} 525#endif 526 527 static int 528Python3_Init(void) 529{ 530 if (!py3initialised) 531 { 532#ifdef DYNAMIC_PYTHON3 533 if (!python3_enabled(TRUE)) 534 { 535 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); 536 goto fail; 537 } 538#endif 539 540 init_structs(); 541 542 /* initialise threads */ 543 PyEval_InitThreads(); 544 545#if !defined(MACOS) || defined(MACOS_X_UNIX) 546 Py_Initialize(); 547#else 548 PyMac_Initialize(); 549#endif 550 551#ifdef DYNAMIC_PYTHON3 552 get_py3_exceptions(); 553#endif 554 555 if (PythonIO_Init()) 556 goto fail; 557 558 PyImport_AppendInittab("vim", Py3Init_vim); 559 560 /* Remove the element from sys.path that was added because of our 561 * argv[0] value in Py3Init_vim(). Previously we used an empty 562 * string, but dependinding on the OS we then get an empty entry or 563 * the current directory in sys.path. */ 564 PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))"); 565 566 // lock is created and acquired in PyEval_InitThreads() and thread 567 // state is created in Py_Initialize() 568 // there _PyGILState_NoteThreadState() also sets gilcounter to 1 569 // (python must have threads enabled!) 570 // so the following does both: unlock GIL and save thread state in TLS 571 // without deleting thread state 572 PyGILState_Release(pygilstate); 573 574 py3initialised = 1; 575 } 576 577 return 0; 578 579fail: 580 /* We call PythonIO_Flush() here to print any Python errors. 581 * This is OK, as it is possible to call this function even 582 * if PythonIO_Init() has not completed successfully (it will 583 * not do anything in this case). 584 */ 585 PythonIO_Flush(); 586 return -1; 587} 588 589/* 590 * External interface 591 */ 592 static void 593DoPy3Command(exarg_T *eap, const char *cmd) 594{ 595#if defined(MACOS) && !defined(MACOS_X_UNIX) 596 GrafPtr oldPort; 597#endif 598#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 599 char *saved_locale; 600#endif 601 602#if defined(MACOS) && !defined(MACOS_X_UNIX) 603 GetPort(&oldPort); 604 /* Check if the Python library is available */ 605 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) 606 goto theend; 607#endif 608 if (Python3_Init()) 609 goto theend; 610 611 RangeStart = eap->line1; 612 RangeEnd = eap->line2; 613 Python_Release_Vim(); /* leave vim */ 614 615#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 616 /* Python only works properly when the LC_NUMERIC locale is "C". */ 617 saved_locale = setlocale(LC_NUMERIC, NULL); 618 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) 619 saved_locale = NULL; 620 else 621 { 622 /* Need to make a copy, value may change when setting new locale. */ 623 saved_locale = (char *)vim_strsave((char_u *)saved_locale); 624 (void)setlocale(LC_NUMERIC, "C"); 625 } 626#endif 627 628 pygilstate = PyGILState_Ensure(); 629 630 PyRun_SimpleString((char *)(cmd)); 631 632 PyGILState_Release(pygilstate); 633 634#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 635 if (saved_locale != NULL) 636 { 637 (void)setlocale(LC_NUMERIC, saved_locale); 638 vim_free(saved_locale); 639 } 640#endif 641 642 Python_Lock_Vim(); /* enter vim */ 643 PythonIO_Flush(); 644#if defined(MACOS) && !defined(MACOS_X_UNIX) 645 SetPort(oldPort); 646#endif 647 648theend: 649 return; /* keeps lint happy */ 650} 651 652/* 653 * ":py3" 654 */ 655 void 656ex_py3(exarg_T *eap) 657{ 658 char_u *script; 659 660 script = script_get(eap, eap->arg); 661 if (!eap->skip) 662 { 663 if (script == NULL) 664 DoPy3Command(eap, (char *)eap->arg); 665 else 666 DoPy3Command(eap, (char *)script); 667 } 668 vim_free(script); 669} 670 671#define BUFFER_SIZE 2048 672 673/* 674 * ":py3file" 675 */ 676 void 677ex_py3file(exarg_T *eap) 678{ 679 static char buffer[BUFFER_SIZE]; 680 const char *file; 681 char *p; 682 int i; 683 684 /* Have to do it like this. PyRun_SimpleFile requires you to pass a 685 * stdio file pointer, but Vim and the Python DLL are compiled with 686 * different options under Windows, meaning that stdio pointers aren't 687 * compatible between the two. Yuk. 688 * 689 * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec')) 690 * 691 * We need to escape any backslashes or single quotes in the file name, so that 692 * Python won't mangle the file name. 693 */ 694 695 strcpy(buffer, "exec(compile(open('"); 696 p = buffer + 19; /* size of "exec(compile(open('" */ 697 698 for (i=0; i<2; ++i) 699 { 700 file = (char *)eap->arg; 701 while (*file && p < buffer + (BUFFER_SIZE - 3)) 702 { 703 if (*file == '\\' || *file == '\'') 704 *p++ = '\\'; 705 *p++ = *file++; 706 } 707 /* If we didn't finish the file name, we hit a buffer overflow */ 708 if (*file != '\0') 709 return; 710 if (i==0) 711 { 712 strcpy(p,"').read(),'"); 713 p += 11; 714 } 715 else 716 { 717 strcpy(p,"','exec'))"); 718 p += 10; 719 } 720 } 721 722 723 /* Execute the file */ 724 DoPy3Command(eap, buffer); 725} 726 727/****************************************************** 728 * 2. Python output stream: writes output via [e]msg(). 729 */ 730 731/* Implementation functions 732 */ 733 734 static PyObject * 735OutputGetattro(PyObject *self, PyObject *nameobj) 736{ 737 char *name = ""; 738 if (PyUnicode_Check(nameobj)) 739 name = _PyUnicode_AsString(nameobj); 740 741 if (strcmp(name, "softspace") == 0) 742 return PyLong_FromLong(((OutputObject *)(self))->softspace); 743 744 return PyObject_GenericGetAttr(self, nameobj); 745} 746 747 static int 748OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 749{ 750 char *name = ""; 751 if (PyUnicode_Check(nameobj)) 752 name = _PyUnicode_AsString(nameobj); 753 754 if (val == NULL) { 755 PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); 756 return -1; 757 } 758 759 if (strcmp(name, "softspace") == 0) 760 { 761 if (!PyLong_Check(val)) { 762 PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); 763 return -1; 764 } 765 766 ((OutputObject *)(self))->softspace = PyLong_AsLong(val); 767 return 0; 768 } 769 770 PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); 771 return -1; 772} 773 774/***************/ 775 776 static int 777PythonIO_Init(void) 778{ 779 PyType_Ready(&OutputType); 780 return PythonIO_Init_io(); 781} 782 783 static void 784PythonIO_Fini(void) 785{ 786 PySys_SetObject("stdout", NULL); 787 PySys_SetObject("stderr", NULL); 788} 789 790/****************************************************** 791 * 3. Implementation of the Vim module for Python 792 */ 793 794/* Window type - Implementation functions 795 * -------------------------------------- 796 */ 797 798#define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) 799 800/* Buffer type - Implementation functions 801 * -------------------------------------- 802 */ 803 804#define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) 805 806static Py_ssize_t BufferLength(PyObject *); 807static PyObject *BufferItem(PyObject *, Py_ssize_t); 808static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *); 809static PyObject* BufferSubscript(PyObject *self, PyObject* idx); 810 811 812/* Line range type - Implementation functions 813 * -------------------------------------- 814 */ 815 816#define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) 817 818static PyObject* RangeSubscript(PyObject *self, PyObject* idx); 819static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); 820 821/* Current objects type - Implementation functions 822 * ----------------------------------------------- 823 */ 824 825static PySequenceMethods BufferAsSeq = { 826 (lenfunc) BufferLength, /* sq_length, len(x) */ 827 (binaryfunc) 0, /* sq_concat, x+y */ 828 (ssizeargfunc) 0, /* sq_repeat, x*n */ 829 (ssizeargfunc) BufferItem, /* sq_item, x[i] */ 830 0, /* was_sq_slice, x[i:j] */ 831 (ssizeobjargproc) BufferAsItem, /* sq_ass_item, x[i]=v */ 832 0, /* sq_ass_slice, x[i:j]=v */ 833 0, /* sq_contains */ 834 0, /* sq_inplace_concat */ 835 0, /* sq_inplace_repeat */ 836}; 837 838PyMappingMethods BufferAsMapping = { 839 /* mp_length */ (lenfunc)BufferLength, 840 /* mp_subscript */ (binaryfunc)BufferSubscript, 841 /* mp_ass_subscript */ (objobjargproc)0, 842}; 843 844 845/* Buffer object - Definitions 846 */ 847 848static PyTypeObject BufferType; 849 850 static PyObject * 851BufferNew(buf_T *buf) 852{ 853 /* We need to handle deletion of buffers underneath us. 854 * If we add a "b_python3_ref" field to the buf_T structure, 855 * then we can get at it in buf_freeall() in vim. We then 856 * need to create only ONE Python object per buffer - if 857 * we try to create a second, just INCREF the existing one 858 * and return it. The (single) Python object referring to 859 * the buffer is stored in "b_python3_ref". 860 * Question: what to do on a buf_freeall(). We'll probably 861 * have to either delete the Python object (DECREF it to 862 * zero - a bad idea, as it leaves dangling refs!) or 863 * set the buf_T * value to an invalid value (-1?), which 864 * means we need checks in all access functions... Bah. 865 */ 866 867 BufferObject *self; 868 869 if (buf->b_python3_ref != NULL) 870 { 871 self = buf->b_python3_ref; 872 Py_INCREF(self); 873 } 874 else 875 { 876 self = PyObject_NEW(BufferObject, &BufferType); 877 buf->b_python3_ref = self; 878 if (self == NULL) 879 return NULL; 880 self->buf = buf; 881 } 882 883 return (PyObject *)(self); 884} 885 886 static void 887BufferDestructor(PyObject *self) 888{ 889 BufferObject *this = (BufferObject *)(self); 890 891 if (this->buf && this->buf != INVALID_BUFFER_VALUE) 892 this->buf->b_python3_ref = NULL; 893} 894 895 static PyObject * 896BufferGetattro(PyObject *self, PyObject*nameobj) 897{ 898 BufferObject *this = (BufferObject *)(self); 899 900 char *name = ""; 901 if (PyUnicode_Check(nameobj)) 902 name = _PyUnicode_AsString(nameobj); 903 904 if (CheckBuffer(this)) 905 return NULL; 906 907 if (strcmp(name, "name") == 0) 908 return Py_BuildValue("s", this->buf->b_ffname); 909 else if (strcmp(name, "number") == 0) 910 return Py_BuildValue("n", this->buf->b_fnum); 911 else if (strcmp(name,"__members__") == 0) 912 return Py_BuildValue("[ss]", "name", "number"); 913 else 914 return PyObject_GenericGetAttr(self, nameobj); 915} 916 917 static PyObject * 918BufferRepr(PyObject *self) 919{ 920 static char repr[100]; 921 BufferObject *this = (BufferObject *)(self); 922 923 if (this->buf == INVALID_BUFFER_VALUE) 924 { 925 vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); 926 return PyUnicode_FromString(repr); 927 } 928 else 929 { 930 char *name = (char *)this->buf->b_fname; 931 Py_ssize_t len; 932 933 if (name == NULL) 934 name = ""; 935 len = strlen(name); 936 937 if (len > 35) 938 name = name + (35 - len); 939 940 vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); 941 942 return PyUnicode_FromString(repr); 943 } 944} 945 946/******************/ 947 948 static Py_ssize_t 949BufferLength(PyObject *self) 950{ 951 if (CheckBuffer((BufferObject *)(self))) 952 return -1; 953 954 return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); 955} 956 957 static PyObject * 958BufferItem(PyObject *self, Py_ssize_t n) 959{ 960 return RBItem((BufferObject *)(self), n, 1, 961 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); 962} 963 964 static PyObject * 965BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) 966{ 967 return RBSlice((BufferObject *)(self), lo, hi, 1, 968 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); 969} 970 971 static Py_ssize_t 972BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val) 973{ 974 return RBAsItem((BufferObject *)(self), n, val, 1, 975 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, 976 NULL); 977} 978 979 980 static PyObject * 981BufferSubscript(PyObject *self, PyObject* idx) 982{ 983 if (PyLong_Check(idx)) { 984 long _idx = PyLong_AsLong(idx); 985 return BufferItem(self,_idx); 986 } else if (PySlice_Check(idx)) { 987 Py_ssize_t start, stop, step, slicelen; 988 989 if (PySlice_GetIndicesEx((PySliceObject *)idx, 990 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, 991 &start, &stop, 992 &step, &slicelen) < 0) { 993 return NULL; 994 } 995 return BufferSlice(self,start,stop+1); 996 } else { 997 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); 998 return NULL; 999 } 1000} 1001 1002static PySequenceMethods RangeAsSeq = { 1003 (lenfunc) RangeLength, /* sq_length, len(x) */ 1004 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ 1005 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ 1006 (ssizeargfunc) RangeItem, /* sq_item, x[i] */ 1007 0, /* was_sq_slice, x[i:j] */ 1008 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ 1009 0, /* sq_ass_slice, x[i:j]=v */ 1010 0, /* sq_contains */ 1011 0, /* sq_inplace_concat */ 1012 0, /* sq_inplace_repeat */ 1013}; 1014 1015PyMappingMethods RangeAsMapping = { 1016 /* mp_length */ (lenfunc)RangeLength, 1017 /* mp_subscript */ (binaryfunc)RangeSubscript, 1018 /* mp_ass_subscript */ (objobjargproc)0, 1019}; 1020 1021/* Line range object - Implementation 1022 */ 1023 1024 static void 1025RangeDestructor(PyObject *self) 1026{ 1027 Py_DECREF(((RangeObject *)(self))->buf); 1028} 1029 1030 static PyObject * 1031RangeGetattro(PyObject *self, PyObject *nameobj) 1032{ 1033 char *name = ""; 1034 if (PyUnicode_Check(nameobj)) 1035 name = _PyUnicode_AsString(nameobj); 1036 1037 if (strcmp(name, "start") == 0) 1038 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); 1039 else if (strcmp(name, "end") == 0) 1040 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); 1041 else 1042 return PyObject_GenericGetAttr(self, nameobj); 1043} 1044 1045/****************/ 1046 1047 static Py_ssize_t 1048RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) 1049{ 1050 return RBAsItem(((RangeObject *)(self))->buf, n, val, 1051 ((RangeObject *)(self))->start, 1052 ((RangeObject *)(self))->end, 1053 &((RangeObject *)(self))->end); 1054} 1055 1056 static PyObject * 1057RangeSubscript(PyObject *self, PyObject* idx) 1058{ 1059 if (PyLong_Check(idx)) { 1060 long _idx = PyLong_AsLong(idx); 1061 return RangeItem(self,_idx); 1062 } else if (PySlice_Check(idx)) { 1063 Py_ssize_t start, stop, step, slicelen; 1064 1065 if (PySlice_GetIndicesEx((PySliceObject *)idx, 1066 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, 1067 &start, &stop, 1068 &step, &slicelen) < 0) { 1069 return NULL; 1070 } 1071 return RangeSlice(self,start,stop+1); 1072 } else { 1073 PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); 1074 return NULL; 1075 } 1076} 1077 1078/* Buffer list object - Definitions 1079 */ 1080 1081typedef struct 1082{ 1083 PyObject_HEAD 1084} BufListObject; 1085 1086static PySequenceMethods BufListAsSeq = { 1087 (lenfunc) BufListLength, /* sq_length, len(x) */ 1088 (binaryfunc) 0, /* sq_concat, x+y */ 1089 (ssizeargfunc) 0, /* sq_repeat, x*n */ 1090 (ssizeargfunc) BufListItem, /* sq_item, x[i] */ 1091 0, /* was_sq_slice, x[i:j] */ 1092 (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */ 1093 0, /* sq_ass_slice, x[i:j]=v */ 1094 0, /* sq_contains */ 1095 0, /* sq_inplace_concat */ 1096 0, /* sq_inplace_repeat */ 1097}; 1098 1099static PyTypeObject BufListType; 1100 1101/* Window object - Definitions 1102 */ 1103 1104static struct PyMethodDef WindowMethods[] = { 1105 /* name, function, calling, documentation */ 1106 { NULL, NULL, 0, NULL } 1107}; 1108 1109static PyTypeObject WindowType; 1110 1111/* Window object - Implementation 1112 */ 1113 1114 static PyObject * 1115WindowNew(win_T *win) 1116{ 1117 /* We need to handle deletion of windows underneath us. 1118 * If we add a "w_python3_ref" field to the win_T structure, 1119 * then we can get at it in win_free() in vim. We then 1120 * need to create only ONE Python object per window - if 1121 * we try to create a second, just INCREF the existing one 1122 * and return it. The (single) Python object referring to 1123 * the window is stored in "w_python3_ref". 1124 * On a win_free() we set the Python object's win_T* field 1125 * to an invalid value. We trap all uses of a window 1126 * object, and reject them if the win_T* field is invalid. 1127 */ 1128 1129 WindowObject *self; 1130 1131 if (win->w_python3_ref) 1132 { 1133 self = win->w_python3_ref; 1134 Py_INCREF(self); 1135 } 1136 else 1137 { 1138 self = PyObject_NEW(WindowObject, &WindowType); 1139 if (self == NULL) 1140 return NULL; 1141 self->win = win; 1142 win->w_python3_ref = self; 1143 } 1144 1145 return (PyObject *)(self); 1146} 1147 1148 static void 1149WindowDestructor(PyObject *self) 1150{ 1151 WindowObject *this = (WindowObject *)(self); 1152 1153 if (this->win && this->win != INVALID_WINDOW_VALUE) 1154 this->win->w_python3_ref = NULL; 1155} 1156 1157 static PyObject * 1158WindowGetattro(PyObject *self, PyObject *nameobj) 1159{ 1160 WindowObject *this = (WindowObject *)(self); 1161 1162 char *name = ""; 1163 if (PyUnicode_Check(nameobj)) 1164 name = _PyUnicode_AsString(nameobj); 1165 1166 1167 if (CheckWindow(this)) 1168 return NULL; 1169 1170 if (strcmp(name, "buffer") == 0) 1171 return (PyObject *)BufferNew(this->win->w_buffer); 1172 else if (strcmp(name, "cursor") == 0) 1173 { 1174 pos_T *pos = &this->win->w_cursor; 1175 1176 return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); 1177 } 1178 else if (strcmp(name, "height") == 0) 1179 return Py_BuildValue("l", (long)(this->win->w_height)); 1180#ifdef FEAT_VERTSPLIT 1181 else if (strcmp(name, "width") == 0) 1182 return Py_BuildValue("l", (long)(W_WIDTH(this->win))); 1183#endif 1184 else if (strcmp(name,"__members__") == 0) 1185 return Py_BuildValue("[sss]", "buffer", "cursor", "height"); 1186 else 1187 return PyObject_GenericGetAttr(self, nameobj); 1188} 1189 1190 static int 1191WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1192{ 1193 char *name = ""; 1194 1195 if (PyUnicode_Check(nameobj)) 1196 name = _PyUnicode_AsString(nameobj); 1197 1198 return WindowSetattr(self, name, val); 1199} 1200 1201/* Window list object - Definitions 1202 */ 1203 1204typedef struct 1205{ 1206 PyObject_HEAD 1207} 1208WinListObject; 1209 1210static PySequenceMethods WinListAsSeq = { 1211 (lenfunc) WinListLength, /* sq_length, len(x) */ 1212 (binaryfunc) 0, /* sq_concat, x+y */ 1213 (ssizeargfunc) 0, /* sq_repeat, x*n */ 1214 (ssizeargfunc) WinListItem, /* sq_item, x[i] */ 1215 0, /* sq_slice, x[i:j] */ 1216 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 1217 0, /* sq_ass_slice, x[i:j]=v */ 1218 0, /* sq_contains */ 1219 0, /* sq_inplace_concat */ 1220 0, /* sq_inplace_repeat */ 1221}; 1222 1223static PyTypeObject WinListType; 1224 1225/* Current items object - Definitions 1226 */ 1227 1228typedef struct 1229{ 1230 PyObject_HEAD 1231} CurrentObject; 1232 1233static PyTypeObject CurrentType; 1234 1235/* Current items object - Implementation 1236 */ 1237 static PyObject * 1238CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) 1239{ 1240 char *name = ""; 1241 if (PyUnicode_Check(nameobj)) 1242 name = _PyUnicode_AsString(nameobj); 1243 1244 if (strcmp(name, "buffer") == 0) 1245 return (PyObject *)BufferNew(curbuf); 1246 else if (strcmp(name, "window") == 0) 1247 return (PyObject *)WindowNew(curwin); 1248 else if (strcmp(name, "line") == 0) 1249 return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum); 1250 else if (strcmp(name, "range") == 0) 1251 return RangeNew(curbuf, RangeStart, RangeEnd); 1252 else if (strcmp(name,"__members__") == 0) 1253 return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); 1254 else 1255 { 1256 PyErr_SetString(PyExc_AttributeError, name); 1257 return NULL; 1258 } 1259} 1260 1261 static int 1262CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) 1263{ 1264 char *name = ""; 1265 if (PyUnicode_Check(nameobj)) 1266 name = _PyUnicode_AsString(nameobj); 1267 1268 if (strcmp(name, "line") == 0) 1269 { 1270 if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL) 1271 return -1; 1272 1273 return 0; 1274 } 1275 else 1276 { 1277 PyErr_SetString(PyExc_AttributeError, name); 1278 return -1; 1279 } 1280} 1281 1282/* External interface 1283 */ 1284 1285 void 1286python3_buffer_free(buf_T *buf) 1287{ 1288 if (buf->b_python3_ref != NULL) 1289 { 1290 BufferObject *bp = buf->b_python3_ref; 1291 bp->buf = INVALID_BUFFER_VALUE; 1292 buf->b_python3_ref = NULL; 1293 } 1294} 1295 1296#if defined(FEAT_WINDOWS) || defined(PROTO) 1297 void 1298python3_window_free(win_T *win) 1299{ 1300 if (win->w_python3_ref != NULL) 1301 { 1302 WindowObject *wp = win->w_python3_ref; 1303 wp->win = INVALID_WINDOW_VALUE; 1304 win->w_python3_ref = NULL; 1305 } 1306} 1307#endif 1308 1309static BufListObject TheBufferList = 1310{ 1311 PyObject_HEAD_INIT(&BufListType) 1312}; 1313 1314static WinListObject TheWindowList = 1315{ 1316 PyObject_HEAD_INIT(&WinListType) 1317}; 1318 1319static CurrentObject TheCurrent = 1320{ 1321 PyObject_HEAD_INIT(&CurrentType) 1322}; 1323 1324PyDoc_STRVAR(vim_module_doc,"vim python interface\n"); 1325 1326static struct PyModuleDef vimmodule; 1327 1328#ifndef PROTO 1329PyMODINIT_FUNC Py3Init_vim(void) 1330{ 1331 PyObject *mod; 1332 /* The special value is removed from sys.path in Python3_Init(). */ 1333 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; 1334 1335 PyType_Ready(&BufferType); 1336 PyType_Ready(&RangeType); 1337 PyType_Ready(&WindowType); 1338 PyType_Ready(&BufListType); 1339 PyType_Ready(&WinListType); 1340 PyType_Ready(&CurrentType); 1341 1342 /* Set sys.argv[] to avoid a crash in warn(). */ 1343 PySys_SetArgv(1, argv); 1344 1345 mod = PyModule_Create(&vimmodule); 1346 1347 VimError = Py_BuildValue("s", "vim.error"); 1348 1349 PyModule_AddObject(mod, "error", VimError); 1350 Py_INCREF((PyObject *)(void *)&TheBufferList); 1351 PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList); 1352 Py_INCREF((PyObject *)(void *)&TheCurrent); 1353 PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); 1354 Py_INCREF((PyObject *)(void *)&TheWindowList); 1355 PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList); 1356 1357 if (PyErr_Occurred()) 1358 return NULL; 1359 1360 return mod; 1361} 1362#endif 1363 1364/************************************************************************* 1365 * 4. Utility functions for handling the interface between Vim and Python. 1366 */ 1367 1368/* Convert a Vim line into a Python string. 1369 * All internal newlines are replaced by null characters. 1370 * 1371 * On errors, the Python exception data is set, and NULL is returned. 1372 */ 1373 static PyObject * 1374LineToString(const char *str) 1375{ 1376 PyObject *result; 1377 Py_ssize_t len = strlen(str); 1378 char *tmp,*p; 1379 1380 tmp = (char *)alloc((unsigned)(len+1)); 1381 p = tmp; 1382 if (p == NULL) 1383 { 1384 PyErr_NoMemory(); 1385 return NULL; 1386 } 1387 1388 while (*str) 1389 { 1390 if (*str == '\n') 1391 *p = '\0'; 1392 else 1393 *p = *str; 1394 1395 ++p; 1396 ++str; 1397 } 1398 *p = '\0'; 1399 1400 result = PyUnicode_FromStringAndSize(tmp, len); 1401 1402 vim_free(tmp); 1403 return result; 1404} 1405 1406 static void 1407init_structs(void) 1408{ 1409 vim_memset(&OutputType, 0, sizeof(OutputType)); 1410 OutputType.tp_name = "vim.message"; 1411 OutputType.tp_basicsize = sizeof(OutputObject); 1412 OutputType.tp_getattro = OutputGetattro; 1413 OutputType.tp_setattro = OutputSetattro; 1414 OutputType.tp_flags = Py_TPFLAGS_DEFAULT; 1415 OutputType.tp_doc = "vim message object"; 1416 OutputType.tp_methods = OutputMethods; 1417 OutputType.tp_alloc = call_PyType_GenericAlloc; 1418 OutputType.tp_new = call_PyType_GenericNew; 1419 OutputType.tp_free = call_PyObject_Free; 1420 1421 vim_memset(&BufferType, 0, sizeof(BufferType)); 1422 BufferType.tp_name = "vim.buffer"; 1423 BufferType.tp_basicsize = sizeof(BufferType); 1424 BufferType.tp_dealloc = BufferDestructor; 1425 BufferType.tp_repr = BufferRepr; 1426 BufferType.tp_as_sequence = &BufferAsSeq; 1427 BufferType.tp_as_mapping = &BufferAsMapping; 1428 BufferType.tp_getattro = BufferGetattro; 1429 BufferType.tp_flags = Py_TPFLAGS_DEFAULT; 1430 BufferType.tp_doc = "vim buffer object"; 1431 BufferType.tp_methods = BufferMethods; 1432 BufferType.tp_alloc = call_PyType_GenericAlloc; 1433 BufferType.tp_new = call_PyType_GenericNew; 1434 BufferType.tp_free = call_PyObject_Free; 1435 1436 vim_memset(&WindowType, 0, sizeof(WindowType)); 1437 WindowType.tp_name = "vim.window"; 1438 WindowType.tp_basicsize = sizeof(WindowObject); 1439 WindowType.tp_dealloc = WindowDestructor; 1440 WindowType.tp_repr = WindowRepr; 1441 WindowType.tp_getattro = WindowGetattro; 1442 WindowType.tp_setattro = WindowSetattro; 1443 WindowType.tp_flags = Py_TPFLAGS_DEFAULT; 1444 WindowType.tp_doc = "vim Window object"; 1445 WindowType.tp_methods = WindowMethods; 1446 WindowType.tp_alloc = call_PyType_GenericAlloc; 1447 WindowType.tp_new = call_PyType_GenericNew; 1448 WindowType.tp_free = call_PyObject_Free; 1449 1450 vim_memset(&BufListType, 0, sizeof(BufListType)); 1451 BufListType.tp_name = "vim.bufferlist"; 1452 BufListType.tp_basicsize = sizeof(BufListObject); 1453 BufListType.tp_as_sequence = &BufListAsSeq; 1454 BufListType.tp_flags = Py_TPFLAGS_DEFAULT; 1455 BufferType.tp_doc = "vim buffer list"; 1456 1457 vim_memset(&WinListType, 0, sizeof(WinListType)); 1458 WinListType.tp_name = "vim.windowlist"; 1459 WinListType.tp_basicsize = sizeof(WinListType); 1460 WinListType.tp_as_sequence = &WinListAsSeq; 1461 WinListType.tp_flags = Py_TPFLAGS_DEFAULT; 1462 WinListType.tp_doc = "vim window list"; 1463 1464 vim_memset(&RangeType, 0, sizeof(RangeType)); 1465 RangeType.tp_name = "vim.range"; 1466 RangeType.tp_basicsize = sizeof(RangeObject); 1467 RangeType.tp_dealloc = RangeDestructor; 1468 RangeType.tp_repr = RangeRepr; 1469 RangeType.tp_as_sequence = &RangeAsSeq; 1470 RangeType.tp_as_mapping = &RangeAsMapping; 1471 RangeType.tp_getattro = RangeGetattro; 1472 RangeType.tp_flags = Py_TPFLAGS_DEFAULT; 1473 RangeType.tp_doc = "vim Range object"; 1474 RangeType.tp_methods = RangeMethods; 1475 RangeType.tp_alloc = call_PyType_GenericAlloc; 1476 RangeType.tp_new = call_PyType_GenericNew; 1477 RangeType.tp_free = call_PyObject_Free; 1478 1479 vim_memset(&CurrentType, 0, sizeof(CurrentType)); 1480 CurrentType.tp_name = "vim.currentdata"; 1481 CurrentType.tp_basicsize = sizeof(CurrentObject); 1482 CurrentType.tp_getattro = CurrentGetattro; 1483 CurrentType.tp_setattro = CurrentSetattro; 1484 CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; 1485 CurrentType.tp_doc = "vim current object"; 1486 1487 vim_memset(&vimmodule, 0, sizeof(vimmodule)); 1488 vimmodule.m_name = "vim"; 1489 vimmodule.m_doc = vim_module_doc; 1490 vimmodule.m_size = -1; 1491 vimmodule.m_methods = VimMethods; 1492} 1493