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 /* Note: most of time you can add -DPy_DEBUG to CFLAGS in place of uncommenting 28 */ 29 /* uncomment this if used with the debug version of python, but without its 30 * allocator */ 31 /* #define Py_DEBUG_NO_PYMALLOC */ 32 33 #include "vim.h" 34 35 #include <limits.h> 36 37 /* Python.h defines _POSIX_THREADS itself (if needed) */ 38 #ifdef _POSIX_THREADS 39 # undef _POSIX_THREADS 40 #endif 41 42 #if defined(_WIN32) && defined(HAVE_FCNTL_H) 43 # undef HAVE_FCNTL_H 44 #endif 45 46 #ifdef _DEBUG 47 # undef _DEBUG 48 #endif 49 50 #ifdef F_BLANK 51 # undef F_BLANK 52 #endif 53 54 #ifdef HAVE_STDARG_H 55 # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ 56 #endif 57 #ifdef _POSIX_C_SOURCE /* defined in feature.h */ 58 # undef _POSIX_C_SOURCE 59 #endif 60 #ifdef _XOPEN_SOURCE 61 # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ 62 #endif 63 64 #define PY_SSIZE_T_CLEAN 65 66 #include <Python.h> 67 68 #if defined(MACOS) && !defined(MACOS_X_UNIX) 69 # include "macglue.h" 70 # include <CodeFragments.h> 71 #endif 72 #undef main /* Defined in python.h - aargh */ 73 #undef HAVE_FCNTL_H /* Clash with os_win32.h */ 74 75 /* The "surrogateescape" error handler is new in Python 3.1 */ 76 #if PY_VERSION_HEX >= 0x030100f0 77 # define CODEC_ERROR_HANDLER "surrogateescape" 78 #else 79 # define CODEC_ERROR_HANDLER NULL 80 #endif 81 82 /* Python 3 does not support CObjects, always use Capsules */ 83 #define PY_USE_CAPSULE 84 85 #define PyInt Py_ssize_t 86 #ifndef PyString_Check 87 # define PyString_Check(obj) PyUnicode_Check(obj) 88 #endif 89 #define PyString_FromString(repr) PyUnicode_FromString(repr) 90 #define PyString_FromFormat PyUnicode_FromFormat 91 #ifndef PyInt_Check 92 # define PyInt_Check(obj) PyLong_Check(obj) 93 #endif 94 #define PyInt_FromLong(i) PyLong_FromLong(i) 95 #define PyInt_AsLong(obj) PyLong_AsLong(obj) 96 #define Py_ssize_t_fmt "n" 97 #define Py_bytes_fmt "y" 98 99 #if defined(DYNAMIC_PYTHON3) || defined(PROTO) 100 101 # ifndef WIN3264 102 # include <dlfcn.h> 103 # define FARPROC void* 104 # define HINSTANCE void* 105 # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) 106 # define load_dll(n) dlopen((n), RTLD_LAZY) 107 # else 108 # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) 109 # endif 110 # define close_dll dlclose 111 # define symbol_from_dll dlsym 112 # else 113 # define load_dll vimLoadLib 114 # define close_dll FreeLibrary 115 # define symbol_from_dll GetProcAddress 116 # endif 117 /* 118 * Wrapper defines 119 */ 120 # undef PyArg_Parse 121 # define PyArg_Parse py3_PyArg_Parse 122 # undef PyArg_ParseTuple 123 # define PyArg_ParseTuple py3_PyArg_ParseTuple 124 # define PyMem_Free py3_PyMem_Free 125 # define PyMem_Malloc py3_PyMem_Malloc 126 # define PyDict_SetItemString py3_PyDict_SetItemString 127 # define PyErr_BadArgument py3_PyErr_BadArgument 128 # define PyErr_Clear py3_PyErr_Clear 129 # define PyErr_Format py3_PyErr_Format 130 # define PyErr_PrintEx py3_PyErr_PrintEx 131 # define PyErr_NoMemory py3_PyErr_NoMemory 132 # define PyErr_Occurred py3_PyErr_Occurred 133 # define PyErr_SetNone py3_PyErr_SetNone 134 # define PyErr_SetString py3_PyErr_SetString 135 # define PyErr_SetObject py3_PyErr_SetObject 136 # define PyErr_ExceptionMatches py3_PyErr_ExceptionMatches 137 # define PyEval_InitThreads py3_PyEval_InitThreads 138 # define PyEval_RestoreThread py3_PyEval_RestoreThread 139 # define PyEval_SaveThread py3_PyEval_SaveThread 140 # define PyGILState_Ensure py3_PyGILState_Ensure 141 # define PyGILState_Release py3_PyGILState_Release 142 # define PyLong_AsLong py3_PyLong_AsLong 143 # define PyLong_FromLong py3_PyLong_FromLong 144 # define PyList_GetItem py3_PyList_GetItem 145 # define PyList_Append py3_PyList_Append 146 # define PyList_Insert py3_PyList_Insert 147 # define PyList_New py3_PyList_New 148 # define PyList_SetItem py3_PyList_SetItem 149 # define PyList_Size py3_PyList_Size 150 # define PySequence_Check py3_PySequence_Check 151 # define PySequence_Size py3_PySequence_Size 152 # define PySequence_GetItem py3_PySequence_GetItem 153 # define PySequence_Fast py3_PySequence_Fast 154 # define PyTuple_Size py3_PyTuple_Size 155 # define PyTuple_GetItem py3_PyTuple_GetItem 156 # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx 157 # define PyImport_ImportModule py3_PyImport_ImportModule 158 # define PyObject_Init py3__PyObject_Init 159 # define PyDict_New py3_PyDict_New 160 # define PyDict_GetItemString py3_PyDict_GetItemString 161 # define PyDict_Next py3_PyDict_Next 162 # define PyMapping_Check py3_PyMapping_Check 163 # ifndef PyMapping_Keys 164 # define PyMapping_Keys py3_PyMapping_Keys 165 # endif 166 # define PyIter_Next py3_PyIter_Next 167 # define PyObject_GetIter py3_PyObject_GetIter 168 # define PyObject_Repr py3_PyObject_Repr 169 # define PyObject_GetItem py3_PyObject_GetItem 170 # define PyObject_IsTrue py3_PyObject_IsTrue 171 # define PyModule_GetDict py3_PyModule_GetDict 172 #undef PyRun_SimpleString 173 # define PyRun_SimpleString py3_PyRun_SimpleString 174 #undef PyRun_String 175 # define PyRun_String py3_PyRun_String 176 # define PyObject_GetAttrString py3_PyObject_GetAttrString 177 # define PyObject_HasAttrString py3_PyObject_HasAttrString 178 # define PyObject_SetAttrString py3_PyObject_SetAttrString 179 # define PyObject_CallFunctionObjArgs py3_PyObject_CallFunctionObjArgs 180 # define _PyObject_CallFunction_SizeT py3__PyObject_CallFunction_SizeT 181 # define PyObject_Call py3_PyObject_Call 182 # define PyEval_GetLocals py3_PyEval_GetLocals 183 # define PyEval_GetGlobals py3_PyEval_GetGlobals 184 # define PySys_SetObject py3_PySys_SetObject 185 # define PySys_GetObject py3_PySys_GetObject 186 # define PySys_SetArgv py3_PySys_SetArgv 187 # define PyType_Ready py3_PyType_Ready 188 #undef Py_BuildValue 189 # define Py_BuildValue py3_Py_BuildValue 190 # define Py_SetPythonHome py3_Py_SetPythonHome 191 # define Py_Initialize py3_Py_Initialize 192 # define Py_Finalize py3_Py_Finalize 193 # define Py_IsInitialized py3_Py_IsInitialized 194 # define _Py_NoneStruct (*py3__Py_NoneStruct) 195 # define _Py_FalseStruct (*py3__Py_FalseStruct) 196 # define _Py_TrueStruct (*py3__Py_TrueStruct) 197 # define _PyObject_NextNotImplemented (*py3__PyObject_NextNotImplemented) 198 # define PyModule_AddObject py3_PyModule_AddObject 199 # define PyImport_AppendInittab py3_PyImport_AppendInittab 200 # define PyImport_AddModule py3_PyImport_AddModule 201 # if PY_VERSION_HEX >= 0x030300f0 202 # undef _PyUnicode_AsString 203 # define _PyUnicode_AsString py3_PyUnicode_AsUTF8 204 # else 205 # define _PyUnicode_AsString py3__PyUnicode_AsString 206 # endif 207 # undef PyUnicode_AsEncodedString 208 # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString 209 # undef PyBytes_AsString 210 # define PyBytes_AsString py3_PyBytes_AsString 211 # ifndef PyBytes_AsStringAndSize 212 # define PyBytes_AsStringAndSize py3_PyBytes_AsStringAndSize 213 # endif 214 # undef PyBytes_FromString 215 # define PyBytes_FromString py3_PyBytes_FromString 216 # define PyFloat_FromDouble py3_PyFloat_FromDouble 217 # define PyFloat_AsDouble py3_PyFloat_AsDouble 218 # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr 219 # define PyType_Type (*py3_PyType_Type) 220 # define PySlice_Type (*py3_PySlice_Type) 221 # define PyFloat_Type (*py3_PyFloat_Type) 222 # define PyNumber_Check (*py3_PyNumber_Check) 223 # define PyNumber_Long (*py3_PyNumber_Long) 224 # define PyBool_Type (*py3_PyBool_Type) 225 # define PyErr_NewException py3_PyErr_NewException 226 # ifdef Py_DEBUG 227 # define _Py_NegativeRefcount py3__Py_NegativeRefcount 228 # define _Py_RefTotal (*py3__Py_RefTotal) 229 # define _Py_Dealloc py3__Py_Dealloc 230 # define PyModule_Create2TraceRefs py3_PyModule_Create2TraceRefs 231 # else 232 # define PyModule_Create2 py3_PyModule_Create2 233 # endif 234 # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) 235 # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc 236 # define _PyObject_DebugFree py3__PyObject_DebugFree 237 # else 238 # define PyObject_Malloc py3_PyObject_Malloc 239 # define PyObject_Free py3_PyObject_Free 240 # endif 241 # define _PyObject_GC_New py3__PyObject_GC_New 242 # define PyObject_GC_Del py3_PyObject_GC_Del 243 # define PyObject_GC_UnTrack py3_PyObject_GC_UnTrack 244 # define PyType_GenericAlloc py3_PyType_GenericAlloc 245 # define PyType_GenericNew py3_PyType_GenericNew 246 # undef PyUnicode_FromString 247 # define PyUnicode_FromString py3_PyUnicode_FromString 248 # ifndef PyUnicode_FromFormat 249 # define PyUnicode_FromFormat py3_PyUnicode_FromFormat 250 # else 251 # define Py_UNICODE_USE_UCS_FUNCTIONS 252 # ifdef Py_UNICODE_WIDE 253 # define PyUnicodeUCS4_FromFormat py3_PyUnicodeUCS4_FromFormat 254 # else 255 # define PyUnicodeUCS2_FromFormat py3_PyUnicodeUCS2_FromFormat 256 # endif 257 # endif 258 # undef PyUnicode_Decode 259 # define PyUnicode_Decode py3_PyUnicode_Decode 260 # define PyType_IsSubtype py3_PyType_IsSubtype 261 # define PyCapsule_New py3_PyCapsule_New 262 # define PyCapsule_GetPointer py3_PyCapsule_GetPointer 263 264 # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) 265 # undef PyObject_NEW 266 # define PyObject_NEW(type, typeobj) \ 267 ( (type *) PyObject_Init( \ 268 (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) 269 # endif 270 271 /* 272 * Pointers for dynamic link 273 */ 274 static int (*py3_PySys_SetArgv)(int, wchar_t **); 275 static void (*py3_Py_SetPythonHome)(wchar_t *home); 276 static void (*py3_Py_Initialize)(void); 277 static PyObject* (*py3_PyList_New)(Py_ssize_t size); 278 static PyGILState_STATE (*py3_PyGILState_Ensure)(void); 279 static void (*py3_PyGILState_Release)(PyGILState_STATE); 280 static int (*py3_PySys_SetObject)(char *, PyObject *); 281 static PyObject* (*py3_PySys_GetObject)(char *); 282 static int (*py3_PyList_Append)(PyObject *, PyObject *); 283 static int (*py3_PyList_Insert)(PyObject *, int, PyObject *); 284 static Py_ssize_t (*py3_PyList_Size)(PyObject *); 285 static int (*py3_PySequence_Check)(PyObject *); 286 static Py_ssize_t (*py3_PySequence_Size)(PyObject *); 287 static PyObject* (*py3_PySequence_GetItem)(PyObject *, Py_ssize_t); 288 static PyObject* (*py3_PySequence_Fast)(PyObject *, const char *); 289 static Py_ssize_t (*py3_PyTuple_Size)(PyObject *); 290 static PyObject* (*py3_PyTuple_GetItem)(PyObject *, Py_ssize_t); 291 static int (*py3_PyMapping_Check)(PyObject *); 292 static PyObject* (*py3_PyMapping_Keys)(PyObject *); 293 static int (*py3_PySlice_GetIndicesEx)(PyObject *r, Py_ssize_t length, 294 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength); 295 static PyObject* (*py3_PyErr_NoMemory)(void); 296 static void (*py3_Py_Finalize)(void); 297 static void (*py3_PyErr_SetString)(PyObject *, const char *); 298 static void (*py3_PyErr_SetObject)(PyObject *, PyObject *); 299 static int (*py3_PyErr_ExceptionMatches)(PyObject *); 300 static int (*py3_PyRun_SimpleString)(char *); 301 static PyObject* (*py3_PyRun_String)(char *, int, PyObject *, PyObject *); 302 static PyObject* (*py3_PyObject_GetAttrString)(PyObject *, const char *); 303 static int (*py3_PyObject_HasAttrString)(PyObject *, const char *); 304 static PyObject* (*py3_PyObject_SetAttrString)(PyObject *, const char *, PyObject *); 305 static PyObject* (*py3_PyObject_CallFunctionObjArgs)(PyObject *, ...); 306 static PyObject* (*py3__PyObject_CallFunction_SizeT)(PyObject *, char *, ...); 307 static PyObject* (*py3_PyObject_Call)(PyObject *, PyObject *, PyObject *); 308 static PyObject* (*py3_PyEval_GetGlobals)(); 309 static PyObject* (*py3_PyEval_GetLocals)(); 310 static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); 311 static PyObject* (*py3_PyImport_ImportModule)(const char *); 312 static PyObject* (*py3_PyImport_AddModule)(const char *); 313 static int (*py3_PyErr_BadArgument)(void); 314 static PyObject* (*py3_PyErr_Occurred)(void); 315 static PyObject* (*py3_PyModule_GetDict)(PyObject *); 316 static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); 317 static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); 318 static int (*py3_PyDict_Next)(PyObject *, Py_ssize_t *, PyObject **, PyObject **); 319 static PyObject* (*py3_PyLong_FromLong)(long); 320 static PyObject* (*py3_PyDict_New)(void); 321 static PyObject* (*py3_PyIter_Next)(PyObject *); 322 static PyObject* (*py3_PyObject_GetIter)(PyObject *); 323 static PyObject* (*py3_PyObject_Repr)(PyObject *); 324 static PyObject* (*py3_PyObject_GetItem)(PyObject *, PyObject *); 325 static int (*py3_PyObject_IsTrue)(PyObject *); 326 static PyObject* (*py3_Py_BuildValue)(char *, ...); 327 static int (*py3_PyType_Ready)(PyTypeObject *type); 328 static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); 329 static PyObject* (*py3_PyUnicode_FromString)(const char *u); 330 # ifndef Py_UNICODE_USE_UCS_FUNCTIONS 331 static PyObject* (*py3_PyUnicode_FromFormat)(const char *u, ...); 332 # else 333 # ifdef Py_UNICODE_WIDE 334 static PyObject* (*py3_PyUnicodeUCS4_FromFormat)(const char *u, ...); 335 # else 336 static PyObject* (*py3_PyUnicodeUCS2_FromFormat)(const char *u, ...); 337 # endif 338 # endif 339 static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size, 340 const char *encoding, const char *errors); 341 static long (*py3_PyLong_AsLong)(PyObject *); 342 static void (*py3_PyErr_SetNone)(PyObject *); 343 static void (*py3_PyEval_InitThreads)(void); 344 static void(*py3_PyEval_RestoreThread)(PyThreadState *); 345 static PyThreadState*(*py3_PyEval_SaveThread)(void); 346 static int (*py3_PyArg_Parse)(PyObject *, char *, ...); 347 static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); 348 static int (*py3_PyMem_Free)(void *); 349 static void* (*py3_PyMem_Malloc)(size_t); 350 static int (*py3_Py_IsInitialized)(void); 351 static void (*py3_PyErr_Clear)(void); 352 static PyObject* (*py3_PyErr_Format)(PyObject *, const char *, ...); 353 static void (*py3_PyErr_PrintEx)(int); 354 static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); 355 static iternextfunc py3__PyObject_NextNotImplemented; 356 static PyObject* py3__Py_NoneStruct; 357 static PyObject* py3__Py_FalseStruct; 358 static PyObject* py3__Py_TrueStruct; 359 static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); 360 static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); 361 # if PY_VERSION_HEX >= 0x030300f0 362 static char* (*py3_PyUnicode_AsUTF8)(PyObject *unicode); 363 # else 364 static char* (*py3__PyUnicode_AsString)(PyObject *unicode); 365 # endif 366 static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); 367 static char* (*py3_PyBytes_AsString)(PyObject *bytes); 368 static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length); 369 static PyObject* (*py3_PyBytes_FromString)(char *str); 370 static PyObject* (*py3_PyFloat_FromDouble)(double num); 371 static double (*py3_PyFloat_AsDouble)(PyObject *); 372 static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); 373 static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); 374 static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); 375 static PyTypeObject* py3_PyType_Type; 376 static PyTypeObject* py3_PySlice_Type; 377 static PyTypeObject* py3_PyFloat_Type; 378 static PyTypeObject* py3_PyBool_Type; 379 static int (*py3_PyNumber_Check)(PyObject *); 380 static PyObject* (*py3_PyNumber_Long)(PyObject *); 381 static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict); 382 static PyObject* (*py3_PyCapsule_New)(void *, char *, PyCapsule_Destructor); 383 static void* (*py3_PyCapsule_GetPointer)(PyObject *, char *); 384 # ifdef Py_DEBUG 385 static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); 386 static Py_ssize_t* py3__Py_RefTotal; 387 static void (*py3__Py_Dealloc)(PyObject *obj); 388 static PyObject* (*py3_PyModule_Create2TraceRefs)(struct PyModuleDef* module, int module_api_version); 389 # else 390 static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); 391 # endif 392 # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) 393 static void (*py3__PyObject_DebugFree)(void*); 394 static void* (*py3__PyObject_DebugMalloc)(size_t); 395 # else 396 static void (*py3_PyObject_Free)(void*); 397 static void* (*py3_PyObject_Malloc)(size_t); 398 # endif 399 static PyObject*(*py3__PyObject_GC_New)(PyTypeObject *); 400 static void(*py3_PyObject_GC_Del)(void *); 401 static void(*py3_PyObject_GC_UnTrack)(void *); 402 static int (*py3_PyType_IsSubtype)(PyTypeObject *, PyTypeObject *); 403 404 static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ 405 406 /* Imported exception objects */ 407 static PyObject *p3imp_PyExc_AttributeError; 408 static PyObject *p3imp_PyExc_IndexError; 409 static PyObject *p3imp_PyExc_KeyError; 410 static PyObject *p3imp_PyExc_KeyboardInterrupt; 411 static PyObject *p3imp_PyExc_TypeError; 412 static PyObject *p3imp_PyExc_ValueError; 413 static PyObject *p3imp_PyExc_SystemExit; 414 static PyObject *p3imp_PyExc_RuntimeError; 415 static PyObject *p3imp_PyExc_ImportError; 416 static PyObject *p3imp_PyExc_OverflowError; 417 418 # define PyExc_AttributeError p3imp_PyExc_AttributeError 419 # define PyExc_IndexError p3imp_PyExc_IndexError 420 # define PyExc_KeyError p3imp_PyExc_KeyError 421 # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt 422 # define PyExc_TypeError p3imp_PyExc_TypeError 423 # define PyExc_ValueError p3imp_PyExc_ValueError 424 # define PyExc_SystemExit p3imp_PyExc_SystemExit 425 # define PyExc_RuntimeError p3imp_PyExc_RuntimeError 426 # define PyExc_ImportError p3imp_PyExc_ImportError 427 # define PyExc_OverflowError p3imp_PyExc_OverflowError 428 429 /* 430 * Table of name to function pointer of python. 431 */ 432 # define PYTHON_PROC FARPROC 433 static struct 434 { 435 char *name; 436 PYTHON_PROC *ptr; 437 } py3_funcname_table[] = 438 { 439 {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, 440 {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome}, 441 {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, 442 {"_PyArg_ParseTuple_SizeT", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, 443 {"_Py_BuildValue_SizeT", (PYTHON_PROC*)&py3_Py_BuildValue}, 444 {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free}, 445 {"PyMem_Malloc", (PYTHON_PROC*)&py3_PyMem_Malloc}, 446 {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, 447 {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, 448 {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, 449 {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, 450 {"PySys_GetObject", (PYTHON_PROC*)&py3_PySys_GetObject}, 451 {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, 452 {"PyList_Insert", (PYTHON_PROC*)&py3_PyList_Insert}, 453 {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, 454 {"PySequence_Check", (PYTHON_PROC*)&py3_PySequence_Check}, 455 {"PySequence_Size", (PYTHON_PROC*)&py3_PySequence_Size}, 456 {"PySequence_GetItem", (PYTHON_PROC*)&py3_PySequence_GetItem}, 457 {"PySequence_Fast", (PYTHON_PROC*)&py3_PySequence_Fast}, 458 {"PyTuple_Size", (PYTHON_PROC*)&py3_PyTuple_Size}, 459 {"PyTuple_GetItem", (PYTHON_PROC*)&py3_PyTuple_GetItem}, 460 {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, 461 {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, 462 {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, 463 {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, 464 {"PyErr_SetObject", (PYTHON_PROC*)&py3_PyErr_SetObject}, 465 {"PyErr_ExceptionMatches", (PYTHON_PROC*)&py3_PyErr_ExceptionMatches}, 466 {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, 467 {"PyRun_String", (PYTHON_PROC*)&py3_PyRun_String}, 468 {"PyObject_GetAttrString", (PYTHON_PROC*)&py3_PyObject_GetAttrString}, 469 {"PyObject_HasAttrString", (PYTHON_PROC*)&py3_PyObject_HasAttrString}, 470 {"PyObject_SetAttrString", (PYTHON_PROC*)&py3_PyObject_SetAttrString}, 471 {"PyObject_CallFunctionObjArgs", (PYTHON_PROC*)&py3_PyObject_CallFunctionObjArgs}, 472 {"_PyObject_CallFunction_SizeT", (PYTHON_PROC*)&py3__PyObject_CallFunction_SizeT}, 473 {"PyObject_Call", (PYTHON_PROC*)&py3_PyObject_Call}, 474 {"PyEval_GetGlobals", (PYTHON_PROC*)&py3_PyEval_GetGlobals}, 475 {"PyEval_GetLocals", (PYTHON_PROC*)&py3_PyEval_GetLocals}, 476 {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, 477 {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, 478 {"PyImport_AddModule", (PYTHON_PROC*)&py3_PyImport_AddModule}, 479 {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, 480 {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, 481 {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, 482 {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, 483 {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, 484 {"PyDict_Next", (PYTHON_PROC*)&py3_PyDict_Next}, 485 {"PyMapping_Check", (PYTHON_PROC*)&py3_PyMapping_Check}, 486 {"PyMapping_Keys", (PYTHON_PROC*)&py3_PyMapping_Keys}, 487 {"PyIter_Next", (PYTHON_PROC*)&py3_PyIter_Next}, 488 {"PyObject_GetIter", (PYTHON_PROC*)&py3_PyObject_GetIter}, 489 {"PyObject_Repr", (PYTHON_PROC*)&py3_PyObject_Repr}, 490 {"PyObject_GetItem", (PYTHON_PROC*)&py3_PyObject_GetItem}, 491 {"PyObject_IsTrue", (PYTHON_PROC*)&py3_PyObject_IsTrue}, 492 {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, 493 {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, 494 {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, 495 {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, 496 {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, 497 {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, 498 {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, 499 {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, 500 {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, 501 {"_PyArg_Parse_SizeT", (PYTHON_PROC*)&py3_PyArg_Parse}, 502 {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, 503 {"_PyObject_NextNotImplemented", (PYTHON_PROC*)&py3__PyObject_NextNotImplemented}, 504 {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, 505 {"_Py_FalseStruct", (PYTHON_PROC*)&py3__Py_FalseStruct}, 506 {"_Py_TrueStruct", (PYTHON_PROC*)&py3__Py_TrueStruct}, 507 {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, 508 {"PyErr_Format", (PYTHON_PROC*)&py3_PyErr_Format}, 509 {"PyErr_PrintEx", (PYTHON_PROC*)&py3_PyErr_PrintEx}, 510 {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, 511 {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, 512 {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, 513 # if PY_VERSION_HEX >= 0x030300f0 514 {"PyUnicode_AsUTF8", (PYTHON_PROC*)&py3_PyUnicode_AsUTF8}, 515 # else 516 {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, 517 # endif 518 # ifndef Py_UNICODE_USE_UCS_FUNCTIONS 519 {"PyUnicode_FromFormat", (PYTHON_PROC*)&py3_PyUnicode_FromFormat}, 520 # else 521 # ifdef Py_UNICODE_WIDE 522 {"PyUnicodeUCS4_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS4_FromFormat}, 523 # else 524 {"PyUnicodeUCS2_FromFormat", (PYTHON_PROC*)&py3_PyUnicodeUCS2_FromFormat}, 525 # endif 526 # endif 527 {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString}, 528 {"PyBytes_AsStringAndSize", (PYTHON_PROC*)&py3_PyBytes_AsStringAndSize}, 529 {"PyBytes_FromString", (PYTHON_PROC*)&py3_PyBytes_FromString}, 530 {"PyFloat_FromDouble", (PYTHON_PROC*)&py3_PyFloat_FromDouble}, 531 {"PyFloat_AsDouble", (PYTHON_PROC*)&py3_PyFloat_AsDouble}, 532 {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, 533 {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, 534 {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, 535 {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, 536 {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, 537 {"PyFloat_Type", (PYTHON_PROC*)&py3_PyFloat_Type}, 538 {"PyBool_Type", (PYTHON_PROC*)&py3_PyBool_Type}, 539 {"PyNumber_Check", (PYTHON_PROC*)&py3_PyNumber_Check}, 540 {"PyNumber_Long", (PYTHON_PROC*)&py3_PyNumber_Long}, 541 {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException}, 542 # ifdef Py_DEBUG 543 {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, 544 {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, 545 {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, 546 {"PyModule_Create2TraceRefs", (PYTHON_PROC*)&py3_PyModule_Create2TraceRefs}, 547 # else 548 {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, 549 # endif 550 # if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) 551 {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, 552 {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, 553 # else 554 {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, 555 {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, 556 # endif 557 {"_PyObject_GC_New", (PYTHON_PROC*)&py3__PyObject_GC_New}, 558 {"PyObject_GC_Del", (PYTHON_PROC*)&py3_PyObject_GC_Del}, 559 {"PyObject_GC_UnTrack", (PYTHON_PROC*)&py3_PyObject_GC_UnTrack}, 560 {"PyType_IsSubtype", (PYTHON_PROC*)&py3_PyType_IsSubtype}, 561 {"PyCapsule_New", (PYTHON_PROC*)&py3_PyCapsule_New}, 562 {"PyCapsule_GetPointer", (PYTHON_PROC*)&py3_PyCapsule_GetPointer}, 563 {"", NULL}, 564 }; 565 566 /* 567 * Free python.dll 568 */ 569 static void 570 end_dynamic_python3(void) 571 { 572 if (hinstPy3 != 0) 573 { 574 close_dll(hinstPy3); 575 hinstPy3 = 0; 576 } 577 } 578 579 /* 580 * Load library and get all pointers. 581 * Parameter 'libname' provides name of DLL. 582 * Return OK or FAIL. 583 */ 584 static int 585 py3_runtime_link_init(char *libname, int verbose) 586 { 587 int i; 588 void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string; 589 590 # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON) 591 /* Can't have Python and Python3 loaded at the same time. 592 * It cause a crash, because RTLD_GLOBAL is needed for 593 * standard C extension libraries of one or both python versions. */ 594 if (python_loaded()) 595 { 596 if (verbose) 597 EMSG(_("E837: This Vim cannot execute :py3 after using :python")); 598 return FAIL; 599 } 600 # endif 601 602 if (hinstPy3 != 0) 603 return OK; 604 hinstPy3 = load_dll(libname); 605 606 if (!hinstPy3) 607 { 608 if (verbose) 609 EMSG2(_(e_loadlib), libname); 610 return FAIL; 611 } 612 613 for (i = 0; py3_funcname_table[i].ptr; ++i) 614 { 615 if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, 616 py3_funcname_table[i].name)) == NULL) 617 { 618 close_dll(hinstPy3); 619 hinstPy3 = 0; 620 if (verbose) 621 EMSG2(_(e_loadfunc), py3_funcname_table[i].name); 622 return FAIL; 623 } 624 } 625 626 /* Load unicode functions separately as only the ucs2 or the ucs4 functions 627 * will be present in the library. */ 628 # if PY_VERSION_HEX >= 0x030300f0 629 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicode_FromString"); 630 ucs_decode = symbol_from_dll(hinstPy3, "PyUnicode_Decode"); 631 ucs_as_encoded_string = symbol_from_dll(hinstPy3, 632 "PyUnicode_AsEncodedString"); 633 # else 634 ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); 635 ucs_decode = symbol_from_dll(hinstPy3, 636 "PyUnicodeUCS2_Decode"); 637 ucs_as_encoded_string = symbol_from_dll(hinstPy3, 638 "PyUnicodeUCS2_AsEncodedString"); 639 if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string) 640 { 641 ucs_from_string = symbol_from_dll(hinstPy3, 642 "PyUnicodeUCS4_FromString"); 643 ucs_decode = symbol_from_dll(hinstPy3, 644 "PyUnicodeUCS4_Decode"); 645 ucs_as_encoded_string = symbol_from_dll(hinstPy3, 646 "PyUnicodeUCS4_AsEncodedString"); 647 } 648 # endif 649 if (ucs_from_string && ucs_decode && ucs_as_encoded_string) 650 { 651 py3_PyUnicode_FromString = ucs_from_string; 652 py3_PyUnicode_Decode = ucs_decode; 653 py3_PyUnicode_AsEncodedString = ucs_as_encoded_string; 654 } 655 else 656 { 657 close_dll(hinstPy3); 658 hinstPy3 = 0; 659 if (verbose) 660 EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); 661 return FAIL; 662 } 663 664 return OK; 665 } 666 667 /* 668 * If python is enabled (there is installed python on Windows system) return 669 * TRUE, else FALSE. 670 */ 671 int 672 python3_enabled(int verbose) 673 { 674 return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; 675 } 676 677 /* Load the standard Python exceptions - don't import the symbols from the 678 * DLL, as this can cause errors (importing data symbols is not reliable). 679 */ 680 static void get_py3_exceptions __ARGS((void)); 681 682 static void 683 get_py3_exceptions() 684 { 685 PyObject *exmod = PyImport_ImportModule("builtins"); 686 PyObject *exdict = PyModule_GetDict(exmod); 687 p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); 688 p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); 689 p3imp_PyExc_KeyError = PyDict_GetItemString(exdict, "KeyError"); 690 p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); 691 p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); 692 p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); 693 p3imp_PyExc_SystemExit = PyDict_GetItemString(exdict, "SystemExit"); 694 p3imp_PyExc_RuntimeError = PyDict_GetItemString(exdict, "RuntimeError"); 695 p3imp_PyExc_ImportError = PyDict_GetItemString(exdict, "ImportError"); 696 p3imp_PyExc_OverflowError = PyDict_GetItemString(exdict, "OverflowError"); 697 Py_XINCREF(p3imp_PyExc_AttributeError); 698 Py_XINCREF(p3imp_PyExc_IndexError); 699 Py_XINCREF(p3imp_PyExc_KeyError); 700 Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); 701 Py_XINCREF(p3imp_PyExc_TypeError); 702 Py_XINCREF(p3imp_PyExc_ValueError); 703 Py_XINCREF(p3imp_PyExc_SystemExit); 704 Py_XINCREF(p3imp_PyExc_RuntimeError); 705 Py_XINCREF(p3imp_PyExc_ImportError); 706 Py_XINCREF(p3imp_PyExc_OverflowError); 707 Py_XDECREF(exmod); 708 } 709 #endif /* DYNAMIC_PYTHON3 */ 710 711 static int py3initialised = 0; 712 713 #define PYINITIALISED py3initialised 714 715 #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self) 716 717 #define WIN_PYTHON_REF(win) win->w_python3_ref 718 #define BUF_PYTHON_REF(buf) buf->b_python3_ref 719 #define TAB_PYTHON_REF(tab) tab->tp_python3_ref 720 721 static void 722 call_PyObject_Free(void *p) 723 { 724 #if defined(Py_DEBUG) && !defined(Py_DEBUG_NO_PYMALLOC) 725 _PyObject_DebugFree(p); 726 #else 727 PyObject_Free(p); 728 #endif 729 } 730 731 static PyObject * 732 call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) 733 { 734 return PyType_GenericNew(type,args,kwds); 735 } 736 737 static PyObject * 738 call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) 739 { 740 return PyType_GenericAlloc(type,nitems); 741 } 742 743 static PyObject *OutputGetattro(PyObject *, PyObject *); 744 static int OutputSetattro(PyObject *, PyObject *, PyObject *); 745 static PyObject *BufferGetattro(PyObject *, PyObject *); 746 static int BufferSetattro(PyObject *, PyObject *, PyObject *); 747 static PyObject *TabPageGetattro(PyObject *, PyObject *); 748 static PyObject *WindowGetattro(PyObject *, PyObject *); 749 static int WindowSetattro(PyObject *, PyObject *, PyObject *); 750 static PyObject *RangeGetattro(PyObject *, PyObject *); 751 static PyObject *CurrentGetattro(PyObject *, PyObject *); 752 static int CurrentSetattro(PyObject *, PyObject *, PyObject *); 753 static PyObject *DictionaryGetattro(PyObject *, PyObject *); 754 static int DictionarySetattro(PyObject *, PyObject *, PyObject *); 755 static PyObject *ListGetattro(PyObject *, PyObject *); 756 static int ListSetattro(PyObject *, PyObject *, PyObject *); 757 static PyObject *FunctionGetattro(PyObject *, PyObject *); 758 759 static PyObject *VimPathHook(PyObject *, PyObject *); 760 761 static struct PyModuleDef vimmodule; 762 763 #define PY_CAN_RECURSE 764 765 /* 766 * Include the code shared with if_python.c 767 */ 768 #include "if_py_both.h" 769 770 #define GET_ATTR_STRING(name, nameobj) \ 771 char *name = ""; \ 772 if (PyUnicode_Check(nameobj)) \ 773 name = _PyUnicode_AsString(nameobj) 774 775 #define PY3OBJ_DELETED(obj) (obj->ob_base.ob_refcnt<=0) 776 777 /****************************************************** 778 * Internal function prototypes. 779 */ 780 781 static PyObject *Py3Init_vim(void); 782 783 /****************************************************** 784 * 1. Python interpreter main program. 785 */ 786 787 void 788 python3_end() 789 { 790 static int recurse = 0; 791 792 /* If a crash occurs while doing this, don't try again. */ 793 if (recurse != 0) 794 return; 795 796 ++recurse; 797 798 #ifdef DYNAMIC_PYTHON3 799 if (hinstPy3) 800 #endif 801 if (Py_IsInitialized()) 802 { 803 // acquire lock before finalizing 804 PyGILState_Ensure(); 805 806 Py_Finalize(); 807 } 808 809 #ifdef DYNAMIC_PYTHON3 810 end_dynamic_python3(); 811 #endif 812 813 --recurse; 814 } 815 816 #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO) 817 int 818 python3_loaded() 819 { 820 return (hinstPy3 != 0); 821 } 822 #endif 823 824 static int 825 Python3_Init(void) 826 { 827 if (!py3initialised) 828 { 829 #ifdef DYNAMIC_PYTHON3 830 if (!python3_enabled(TRUE)) 831 { 832 EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); 833 goto fail; 834 } 835 #endif 836 837 init_structs(); 838 839 840 #ifdef PYTHON3_HOME 841 Py_SetPythonHome(PYTHON3_HOME); 842 #endif 843 844 PyImport_AppendInittab("vim", Py3Init_vim); 845 846 #if !defined(MACOS) || defined(MACOS_X_UNIX) 847 Py_Initialize(); 848 #else 849 PyMac_Initialize(); 850 #endif 851 /* Initialise threads, and below save the state using 852 * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread 853 * specific state (such as the system trace hook), will be lost 854 * between invocations of Python code. */ 855 PyEval_InitThreads(); 856 #ifdef DYNAMIC_PYTHON3 857 get_py3_exceptions(); 858 #endif 859 860 if (PythonIO_Init_io()) 861 goto fail; 862 863 globals = PyModule_GetDict(PyImport_AddModule("__main__")); 864 865 /* Remove the element from sys.path that was added because of our 866 * argv[0] value in Py3Init_vim(). Previously we used an empty 867 * string, but depending on the OS we then get an empty entry or 868 * the current directory in sys.path. 869 * Only after vim has been imported, the element does exist in 870 * sys.path. 871 */ 872 PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))"); 873 874 /* lock is created and acquired in PyEval_InitThreads() and thread 875 * state is created in Py_Initialize() 876 * there _PyGILState_NoteThreadState() also sets gilcounter to 1 877 * (python must have threads enabled!) 878 * so the following does both: unlock GIL and save thread state in TLS 879 * without deleting thread state 880 */ 881 PyEval_SaveThread(); 882 883 py3initialised = 1; 884 } 885 886 return 0; 887 888 fail: 889 /* We call PythonIO_Flush() here to print any Python errors. 890 * This is OK, as it is possible to call this function even 891 * if PythonIO_Init_io() has not completed successfully (it will 892 * not do anything in this case). 893 */ 894 PythonIO_Flush(); 895 return -1; 896 } 897 898 /* 899 * External interface 900 */ 901 static void 902 DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) 903 { 904 #if defined(MACOS) && !defined(MACOS_X_UNIX) 905 GrafPtr oldPort; 906 #endif 907 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 908 char *saved_locale; 909 #endif 910 PyObject *cmdstr; 911 PyObject *cmdbytes; 912 PyGILState_STATE pygilstate; 913 914 #if defined(MACOS) && !defined(MACOS_X_UNIX) 915 GetPort(&oldPort); 916 /* Check if the Python library is available */ 917 if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) 918 goto theend; 919 #endif 920 if (Python3_Init()) 921 goto theend; 922 923 init_range(arg); 924 925 Python_Release_Vim(); /* leave vim */ 926 927 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 928 /* Python only works properly when the LC_NUMERIC locale is "C". */ 929 saved_locale = setlocale(LC_NUMERIC, NULL); 930 if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) 931 saved_locale = NULL; 932 else 933 { 934 /* Need to make a copy, value may change when setting new locale. */ 935 saved_locale = (char *)vim_strsave((char_u *)saved_locale); 936 (void)setlocale(LC_NUMERIC, "C"); 937 } 938 #endif 939 940 pygilstate = PyGILState_Ensure(); 941 942 /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause 943 * SyntaxError (unicode error). */ 944 cmdstr = PyUnicode_Decode(cmd, strlen(cmd), 945 (char *)ENC_OPT, CODEC_ERROR_HANDLER); 946 cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER); 947 Py_XDECREF(cmdstr); 948 949 run(PyBytes_AsString(cmdbytes), arg, &pygilstate); 950 Py_XDECREF(cmdbytes); 951 952 PyGILState_Release(pygilstate); 953 954 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 955 if (saved_locale != NULL) 956 { 957 (void)setlocale(LC_NUMERIC, saved_locale); 958 vim_free(saved_locale); 959 } 960 #endif 961 962 Python_Lock_Vim(); /* enter vim */ 963 PythonIO_Flush(); 964 #if defined(MACOS) && !defined(MACOS_X_UNIX) 965 SetPort(oldPort); 966 #endif 967 968 theend: 969 return; /* keeps lint happy */ 970 } 971 972 /* 973 * ":py3" 974 */ 975 void 976 ex_py3(exarg_T *eap) 977 { 978 char_u *script; 979 980 script = script_get(eap, eap->arg); 981 if (!eap->skip) 982 { 983 DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script, 984 (rangeinitializer) init_range_cmd, 985 (runner) run_cmd, 986 (void *) eap); 987 } 988 vim_free(script); 989 } 990 991 #define BUFFER_SIZE 2048 992 993 /* 994 * ":py3file" 995 */ 996 void 997 ex_py3file(exarg_T *eap) 998 { 999 static char buffer[BUFFER_SIZE]; 1000 const char *file; 1001 char *p; 1002 int i; 1003 1004 /* Have to do it like this. PyRun_SimpleFile requires you to pass a 1005 * stdio file pointer, but Vim and the Python DLL are compiled with 1006 * different options under Windows, meaning that stdio pointers aren't 1007 * compatible between the two. Yuk. 1008 * 1009 * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) 1010 * 1011 * Using bytes so that Python can detect the source encoding as it normally 1012 * does. The doc does not say "compile" accept bytes, though. 1013 * 1014 * We need to escape any backslashes or single quotes in the file name, so that 1015 * Python won't mangle the file name. 1016 */ 1017 1018 strcpy(buffer, "exec(compile(open('"); 1019 p = buffer + 19; /* size of "exec(compile(open('" */ 1020 1021 for (i=0; i<2; ++i) 1022 { 1023 file = (char *)eap->arg; 1024 while (*file && p < buffer + (BUFFER_SIZE - 3)) 1025 { 1026 if (*file == '\\' || *file == '\'') 1027 *p++ = '\\'; 1028 *p++ = *file++; 1029 } 1030 /* If we didn't finish the file name, we hit a buffer overflow */ 1031 if (*file != '\0') 1032 return; 1033 if (i==0) 1034 { 1035 strcpy(p,"','rb').read(),'"); 1036 p += 16; 1037 } 1038 else 1039 { 1040 strcpy(p,"','exec'))"); 1041 p += 10; 1042 } 1043 } 1044 1045 1046 /* Execute the file */ 1047 DoPyCommand(buffer, 1048 (rangeinitializer) init_range_cmd, 1049 (runner) run_cmd, 1050 (void *) eap); 1051 } 1052 1053 void 1054 ex_py3do(exarg_T *eap) 1055 { 1056 DoPyCommand((char *)eap->arg, 1057 (rangeinitializer)init_range_cmd, 1058 (runner)run_do, 1059 (void *)eap); 1060 } 1061 1062 /****************************************************** 1063 * 2. Python output stream: writes output via [e]msg(). 1064 */ 1065 1066 /* Implementation functions 1067 */ 1068 1069 static PyObject * 1070 OutputGetattro(PyObject *self, PyObject *nameobj) 1071 { 1072 GET_ATTR_STRING(name, nameobj); 1073 1074 if (strcmp(name, "softspace") == 0) 1075 return PyLong_FromLong(((OutputObject *)(self))->softspace); 1076 1077 return PyObject_GenericGetAttr(self, nameobj); 1078 } 1079 1080 static int 1081 OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1082 { 1083 GET_ATTR_STRING(name, nameobj); 1084 1085 return OutputSetattr((OutputObject *)(self), name, val); 1086 } 1087 1088 /****************************************************** 1089 * 3. Implementation of the Vim module for Python 1090 */ 1091 1092 /* Window type - Implementation functions 1093 * -------------------------------------- 1094 */ 1095 1096 #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) 1097 1098 /* Buffer type - Implementation functions 1099 * -------------------------------------- 1100 */ 1101 1102 #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) 1103 1104 static PyObject* BufferSubscript(PyObject *self, PyObject *idx); 1105 static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject *idx, PyObject *val); 1106 1107 /* Line range type - Implementation functions 1108 * -------------------------------------- 1109 */ 1110 1111 #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) 1112 1113 static PyObject* RangeSubscript(PyObject *self, PyObject *idx); 1114 static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); 1115 static Py_ssize_t RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val); 1116 1117 /* Current objects type - Implementation functions 1118 * ----------------------------------------------- 1119 */ 1120 1121 static PySequenceMethods BufferAsSeq = { 1122 (lenfunc) BufferLength, /* sq_length, len(x) */ 1123 (binaryfunc) 0, /* sq_concat, x+y */ 1124 (ssizeargfunc) 0, /* sq_repeat, x*n */ 1125 (ssizeargfunc) BufferItem, /* sq_item, x[i] */ 1126 0, /* was_sq_slice, x[i:j] */ 1127 0, /* sq_ass_item, x[i]=v */ 1128 0, /* sq_ass_slice, x[i:j]=v */ 1129 0, /* sq_contains */ 1130 0, /* sq_inplace_concat */ 1131 0, /* sq_inplace_repeat */ 1132 }; 1133 1134 static PyMappingMethods BufferAsMapping = { 1135 /* mp_length */ (lenfunc)BufferLength, 1136 /* mp_subscript */ (binaryfunc)BufferSubscript, 1137 /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, 1138 }; 1139 1140 1141 /* Buffer object 1142 */ 1143 1144 static PyObject * 1145 BufferGetattro(PyObject *self, PyObject *nameobj) 1146 { 1147 PyObject *r; 1148 1149 GET_ATTR_STRING(name, nameobj); 1150 1151 if ((r = BufferAttrValid((BufferObject *)(self), name))) 1152 return r; 1153 1154 if (CheckBuffer((BufferObject *)(self))) 1155 return NULL; 1156 1157 r = BufferAttr((BufferObject *)(self), name); 1158 if (r || PyErr_Occurred()) 1159 return r; 1160 else 1161 return PyObject_GenericGetAttr(self, nameobj); 1162 } 1163 1164 static int 1165 BufferSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1166 { 1167 GET_ATTR_STRING(name, nameobj); 1168 1169 return BufferSetattr((BufferObject *)(self), name, val); 1170 } 1171 1172 /******************/ 1173 1174 static PyObject * 1175 BufferSubscript(PyObject *self, PyObject* idx) 1176 { 1177 if (PyLong_Check(idx)) 1178 { 1179 long _idx = PyLong_AsLong(idx); 1180 return BufferItem((BufferObject *)(self), _idx); 1181 } else if (PySlice_Check(idx)) 1182 { 1183 Py_ssize_t start, stop, step, slicelen; 1184 1185 if (CheckBuffer((BufferObject *) self)) 1186 return NULL; 1187 1188 if (PySlice_GetIndicesEx((PyObject *)idx, 1189 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, 1190 &start, &stop, 1191 &step, &slicelen) < 0) 1192 { 1193 return NULL; 1194 } 1195 return BufferSlice((BufferObject *)(self), start, stop); 1196 } 1197 else 1198 { 1199 RAISE_INVALID_INDEX_TYPE(idx); 1200 return NULL; 1201 } 1202 } 1203 1204 static Py_ssize_t 1205 BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) 1206 { 1207 if (PyLong_Check(idx)) 1208 { 1209 long n = PyLong_AsLong(idx); 1210 return RBAsItem((BufferObject *)(self), n, val, 1, 1211 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, 1212 NULL); 1213 } else if (PySlice_Check(idx)) 1214 { 1215 Py_ssize_t start, stop, step, slicelen; 1216 1217 if (CheckBuffer((BufferObject *) self)) 1218 return -1; 1219 1220 if (PySlice_GetIndicesEx((PyObject *)idx, 1221 (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, 1222 &start, &stop, 1223 &step, &slicelen) < 0) 1224 { 1225 return -1; 1226 } 1227 return RBAsSlice((BufferObject *)(self), start, stop, val, 1, 1228 (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, 1229 NULL); 1230 } 1231 else 1232 { 1233 RAISE_INVALID_INDEX_TYPE(idx); 1234 return -1; 1235 } 1236 } 1237 1238 static PySequenceMethods RangeAsSeq = { 1239 (lenfunc) RangeLength, /* sq_length, len(x) */ 1240 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ 1241 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ 1242 (ssizeargfunc) RangeItem, /* sq_item, x[i] */ 1243 0, /* was_sq_slice, x[i:j] */ 1244 (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ 1245 0, /* sq_ass_slice, x[i:j]=v */ 1246 0, /* sq_contains */ 1247 0, /* sq_inplace_concat */ 1248 0, /* sq_inplace_repeat */ 1249 }; 1250 1251 static PyMappingMethods RangeAsMapping = { 1252 /* mp_length */ (lenfunc)RangeLength, 1253 /* mp_subscript */ (binaryfunc)RangeSubscript, 1254 /* mp_ass_subscript */ (objobjargproc)RangeAsSubscript, 1255 }; 1256 1257 /* Line range object - Implementation 1258 */ 1259 1260 static PyObject * 1261 RangeGetattro(PyObject *self, PyObject *nameobj) 1262 { 1263 GET_ATTR_STRING(name, nameobj); 1264 1265 if (strcmp(name, "start") == 0) 1266 return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); 1267 else if (strcmp(name, "end") == 0) 1268 return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); 1269 else 1270 return PyObject_GenericGetAttr(self, nameobj); 1271 } 1272 1273 /****************/ 1274 1275 static Py_ssize_t 1276 RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) 1277 { 1278 return RBAsItem(((RangeObject *)(self))->buf, n, val, 1279 ((RangeObject *)(self))->start, 1280 ((RangeObject *)(self))->end, 1281 &((RangeObject *)(self))->end); 1282 } 1283 1284 static Py_ssize_t 1285 RangeAsSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi, PyObject *val) 1286 { 1287 return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val, 1288 ((RangeObject *)(self))->start, 1289 ((RangeObject *)(self))->end, 1290 &((RangeObject *)(self))->end); 1291 } 1292 1293 static PyObject * 1294 RangeSubscript(PyObject *self, PyObject* idx) 1295 { 1296 if (PyLong_Check(idx)) 1297 { 1298 long _idx = PyLong_AsLong(idx); 1299 return RangeItem((RangeObject *)(self), _idx); 1300 } else if (PySlice_Check(idx)) 1301 { 1302 Py_ssize_t start, stop, step, slicelen; 1303 1304 if (PySlice_GetIndicesEx((PyObject *)idx, 1305 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, 1306 &start, &stop, 1307 &step, &slicelen) < 0) 1308 { 1309 return NULL; 1310 } 1311 return RangeSlice((RangeObject *)(self), start, stop); 1312 } 1313 else 1314 { 1315 RAISE_INVALID_INDEX_TYPE(idx); 1316 return NULL; 1317 } 1318 } 1319 1320 static Py_ssize_t 1321 RangeAsSubscript(PyObject *self, PyObject *idx, PyObject *val) 1322 { 1323 if (PyLong_Check(idx)) 1324 { 1325 long n = PyLong_AsLong(idx); 1326 return RangeAsItem(self, n, val); 1327 } else if (PySlice_Check(idx)) 1328 { 1329 Py_ssize_t start, stop, step, slicelen; 1330 1331 if (PySlice_GetIndicesEx((PyObject *)idx, 1332 ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, 1333 &start, &stop, 1334 &step, &slicelen) < 0) 1335 { 1336 return -1; 1337 } 1338 return RangeAsSlice(self, start, stop, val); 1339 } 1340 else 1341 { 1342 RAISE_INVALID_INDEX_TYPE(idx); 1343 return -1; 1344 } 1345 } 1346 1347 /* TabPage object - Implementation 1348 */ 1349 1350 static PyObject * 1351 TabPageGetattro(PyObject *self, PyObject *nameobj) 1352 { 1353 PyObject *r; 1354 1355 GET_ATTR_STRING(name, nameobj); 1356 1357 if ((r = TabPageAttrValid((TabPageObject *)(self), name))) 1358 return r; 1359 1360 if (CheckTabPage((TabPageObject *)(self))) 1361 return NULL; 1362 1363 r = TabPageAttr((TabPageObject *)(self), name); 1364 if (r || PyErr_Occurred()) 1365 return r; 1366 else 1367 return PyObject_GenericGetAttr(self, nameobj); 1368 } 1369 1370 /* Window object - Implementation 1371 */ 1372 1373 static PyObject * 1374 WindowGetattro(PyObject *self, PyObject *nameobj) 1375 { 1376 PyObject *r; 1377 1378 GET_ATTR_STRING(name, nameobj); 1379 1380 if ((r = WindowAttrValid((WindowObject *)(self), name))) 1381 return r; 1382 1383 if (CheckWindow((WindowObject *)(self))) 1384 return NULL; 1385 1386 r = WindowAttr((WindowObject *)(self), name); 1387 if (r || PyErr_Occurred()) 1388 return r; 1389 else 1390 return PyObject_GenericGetAttr(self, nameobj); 1391 } 1392 1393 static int 1394 WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1395 { 1396 GET_ATTR_STRING(name, nameobj); 1397 1398 return WindowSetattr((WindowObject *)(self), name, val); 1399 } 1400 1401 /* Tab page list object - Definitions 1402 */ 1403 1404 static PySequenceMethods TabListAsSeq = { 1405 (lenfunc) TabListLength, /* sq_length, len(x) */ 1406 (binaryfunc) 0, /* sq_concat, x+y */ 1407 (ssizeargfunc) 0, /* sq_repeat, x*n */ 1408 (ssizeargfunc) TabListItem, /* sq_item, x[i] */ 1409 0, /* sq_slice, x[i:j] */ 1410 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 1411 0, /* sq_ass_slice, x[i:j]=v */ 1412 0, /* sq_contains */ 1413 0, /* sq_inplace_concat */ 1414 0, /* sq_inplace_repeat */ 1415 }; 1416 1417 /* Window list object - Definitions 1418 */ 1419 1420 static PySequenceMethods WinListAsSeq = { 1421 (lenfunc) WinListLength, /* sq_length, len(x) */ 1422 (binaryfunc) 0, /* sq_concat, x+y */ 1423 (ssizeargfunc) 0, /* sq_repeat, x*n */ 1424 (ssizeargfunc) WinListItem, /* sq_item, x[i] */ 1425 0, /* sq_slice, x[i:j] */ 1426 (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 1427 0, /* sq_ass_slice, x[i:j]=v */ 1428 0, /* sq_contains */ 1429 0, /* sq_inplace_concat */ 1430 0, /* sq_inplace_repeat */ 1431 }; 1432 1433 /* Current items object - Implementation 1434 */ 1435 static PyObject * 1436 CurrentGetattro(PyObject *self, PyObject *nameobj) 1437 { 1438 PyObject *r; 1439 GET_ATTR_STRING(name, nameobj); 1440 if (!(r = CurrentGetattr(self, name))) 1441 return PyObject_GenericGetAttr(self, nameobj); 1442 return r; 1443 } 1444 1445 static int 1446 CurrentSetattro(PyObject *self, PyObject *nameobj, PyObject *value) 1447 { 1448 GET_ATTR_STRING(name, nameobj); 1449 return CurrentSetattr(self, name, value); 1450 } 1451 1452 /* Dictionary object - Definitions 1453 */ 1454 1455 static PyObject * 1456 DictionaryGetattro(PyObject *self, PyObject *nameobj) 1457 { 1458 DictionaryObject *this = ((DictionaryObject *) (self)); 1459 1460 GET_ATTR_STRING(name, nameobj); 1461 1462 if (strcmp(name, "locked") == 0) 1463 return PyLong_FromLong(this->dict->dv_lock); 1464 else if (strcmp(name, "scope") == 0) 1465 return PyLong_FromLong(this->dict->dv_scope); 1466 1467 return PyObject_GenericGetAttr(self, nameobj); 1468 } 1469 1470 static int 1471 DictionarySetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1472 { 1473 GET_ATTR_STRING(name, nameobj); 1474 return DictionarySetattr((DictionaryObject *)(self), name, val); 1475 } 1476 1477 /* List object - Definitions 1478 */ 1479 1480 static PySequenceMethods ListAsSeq = { 1481 (lenfunc) ListLength, /* sq_length, len(x) */ 1482 (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ 1483 (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ 1484 (ssizeargfunc) ListItem, /* sq_item, x[i] */ 1485 (void *) 0, /* was_sq_slice, x[i:j] */ 1486 (ssizeobjargproc) ListAssItem, /* sq_as_item, x[i]=v */ 1487 (void *) 0, /* was_sq_ass_slice, x[i:j]=v */ 1488 0, /* sq_contains */ 1489 (binaryfunc) ListConcatInPlace,/* sq_inplace_concat */ 1490 0, /* sq_inplace_repeat */ 1491 }; 1492 1493 static PyObject *ListSubscript(PyObject *, PyObject *); 1494 static Py_ssize_t ListAsSubscript(PyObject *, PyObject *, PyObject *); 1495 1496 static PyMappingMethods ListAsMapping = { 1497 /* mp_length */ (lenfunc) ListLength, 1498 /* mp_subscript */ (binaryfunc) ListSubscript, 1499 /* mp_ass_subscript */ (objobjargproc) ListAsSubscript, 1500 }; 1501 1502 static PyObject * 1503 ListSubscript(PyObject *self, PyObject* idx) 1504 { 1505 if (PyLong_Check(idx)) 1506 { 1507 long _idx = PyLong_AsLong(idx); 1508 return ListItem((ListObject *)(self), _idx); 1509 } 1510 else if (PySlice_Check(idx)) 1511 { 1512 Py_ssize_t start, stop, step, slicelen; 1513 1514 if (PySlice_GetIndicesEx(idx, ListLength((ListObject *)(self)), 1515 &start, &stop, &step, &slicelen) < 0) 1516 return NULL; 1517 return ListSlice((ListObject *)(self), start, stop); 1518 } 1519 else 1520 { 1521 RAISE_INVALID_INDEX_TYPE(idx); 1522 return NULL; 1523 } 1524 } 1525 1526 static Py_ssize_t 1527 ListAsSubscript(PyObject *self, PyObject *idx, PyObject *obj) 1528 { 1529 if (PyLong_Check(idx)) 1530 { 1531 long _idx = PyLong_AsLong(idx); 1532 return ListAssItem((ListObject *)(self), _idx, obj); 1533 } 1534 else if (PySlice_Check(idx)) 1535 { 1536 Py_ssize_t start, stop, step, slicelen; 1537 1538 if (PySlice_GetIndicesEx(idx, ListLength((ListObject *)(self)), 1539 &start, &stop, &step, &slicelen) < 0) 1540 return -1; 1541 return ListAssSlice((ListObject *)(self), start, stop, obj); 1542 } 1543 else 1544 { 1545 RAISE_INVALID_INDEX_TYPE(idx); 1546 return -1; 1547 } 1548 } 1549 1550 static PyObject * 1551 ListGetattro(PyObject *self, PyObject *nameobj) 1552 { 1553 GET_ATTR_STRING(name, nameobj); 1554 1555 if (strcmp(name, "locked") == 0) 1556 return PyLong_FromLong(((ListObject *) (self))->list->lv_lock); 1557 1558 return PyObject_GenericGetAttr(self, nameobj); 1559 } 1560 1561 static int 1562 ListSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1563 { 1564 GET_ATTR_STRING(name, nameobj); 1565 return ListSetattr((ListObject *)(self), name, val); 1566 } 1567 1568 /* Function object - Definitions 1569 */ 1570 1571 static PyObject * 1572 FunctionGetattro(PyObject *self, PyObject *nameobj) 1573 { 1574 FunctionObject *this = (FunctionObject *)(self); 1575 1576 GET_ATTR_STRING(name, nameobj); 1577 1578 if (strcmp(name, "name") == 0) 1579 return PyUnicode_FromString((char *)(this->name)); 1580 1581 return PyObject_GenericGetAttr(self, nameobj); 1582 } 1583 1584 /* External interface 1585 */ 1586 1587 void 1588 python3_buffer_free(buf_T *buf) 1589 { 1590 if (BUF_PYTHON_REF(buf) != NULL) 1591 { 1592 BufferObject *bp = BUF_PYTHON_REF(buf); 1593 bp->buf = INVALID_BUFFER_VALUE; 1594 BUF_PYTHON_REF(buf) = NULL; 1595 } 1596 } 1597 1598 #if defined(FEAT_WINDOWS) || defined(PROTO) 1599 void 1600 python3_window_free(win_T *win) 1601 { 1602 if (WIN_PYTHON_REF(win) != NULL) 1603 { 1604 WindowObject *wp = WIN_PYTHON_REF(win); 1605 wp->win = INVALID_WINDOW_VALUE; 1606 WIN_PYTHON_REF(win) = NULL; 1607 } 1608 } 1609 1610 void 1611 python3_tabpage_free(tabpage_T *tab) 1612 { 1613 if (TAB_PYTHON_REF(tab) != NULL) 1614 { 1615 TabPageObject *tp = TAB_PYTHON_REF(tab); 1616 tp->tab = INVALID_TABPAGE_VALUE; 1617 TAB_PYTHON_REF(tab) = NULL; 1618 } 1619 } 1620 #endif 1621 1622 static PyObject * 1623 Py3Init_vim(void) 1624 { 1625 /* The special value is removed from sys.path in Python3_Init(). */ 1626 static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; 1627 1628 if (init_types()) 1629 return NULL; 1630 1631 /* Set sys.argv[] to avoid a crash in warn(). */ 1632 PySys_SetArgv(1, argv); 1633 1634 if ((vim_module = PyModule_Create(&vimmodule)) == NULL) 1635 return NULL; 1636 1637 if (populate_module(vim_module)) 1638 return NULL; 1639 1640 if (init_sys_path()) 1641 return NULL; 1642 1643 return vim_module; 1644 } 1645 1646 /************************************************************************* 1647 * 4. Utility functions for handling the interface between Vim and Python. 1648 */ 1649 1650 /* Convert a Vim line into a Python string. 1651 * All internal newlines are replaced by null characters. 1652 * 1653 * On errors, the Python exception data is set, and NULL is returned. 1654 */ 1655 static PyObject * 1656 LineToString(const char *str) 1657 { 1658 PyObject *result; 1659 Py_ssize_t len = strlen(str); 1660 char *tmp,*p; 1661 1662 tmp = (char *)alloc((unsigned)(len+1)); 1663 p = tmp; 1664 if (p == NULL) 1665 { 1666 PyErr_NoMemory(); 1667 return NULL; 1668 } 1669 1670 while (*str) 1671 { 1672 if (*str == '\n') 1673 *p = '\0'; 1674 else 1675 *p = *str; 1676 1677 ++p; 1678 ++str; 1679 } 1680 *p = '\0'; 1681 1682 result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER); 1683 1684 vim_free(tmp); 1685 return result; 1686 } 1687 1688 void 1689 do_py3eval (char_u *str, typval_T *rettv) 1690 { 1691 DoPyCommand((char *) str, 1692 (rangeinitializer) init_range_eval, 1693 (runner) run_eval, 1694 (void *) rettv); 1695 switch(rettv->v_type) 1696 { 1697 case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break; 1698 case VAR_LIST: ++rettv->vval.v_list->lv_refcount; break; 1699 case VAR_FUNC: func_ref(rettv->vval.v_string); break; 1700 case VAR_UNKNOWN: 1701 rettv->v_type = VAR_NUMBER; 1702 rettv->vval.v_number = 0; 1703 break; 1704 } 1705 } 1706 1707 void 1708 set_ref_in_python3 (int copyID) 1709 { 1710 set_ref_in_py(copyID); 1711 } 1712