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