1bd5e15fdSBram Moolenaar /* vi:set ts=8 sts=4 sw=4: 2bd5e15fdSBram Moolenaar * 3bd5e15fdSBram Moolenaar * VIM - Vi IMproved by Bram Moolenaar 4bd5e15fdSBram Moolenaar * 5bd5e15fdSBram Moolenaar * Do ":help uganda" in Vim to read copying and usage conditions. 6bd5e15fdSBram Moolenaar * Do ":help credits" in Vim to see a list of people who contributed. 7bd5e15fdSBram Moolenaar * See README.txt for an overview of the Vim source code. 8bd5e15fdSBram Moolenaar */ 9bd5e15fdSBram Moolenaar /* 10bd5e15fdSBram Moolenaar * Python extensions by Paul Moore. 11bd5e15fdSBram Moolenaar * Changes for Unix by David Leonard. 12bd5e15fdSBram Moolenaar * 13bd5e15fdSBram Moolenaar * This consists of four parts: 14bd5e15fdSBram Moolenaar * 1. Python interpreter main program 15bd5e15fdSBram Moolenaar * 2. Python output stream: writes output via [e]msg(). 16bd5e15fdSBram Moolenaar * 3. Implementation of the Vim module for Python 17bd5e15fdSBram Moolenaar * 4. Utility functions for handling the interface between Vim and Python. 18bd5e15fdSBram Moolenaar */ 19bd5e15fdSBram Moolenaar 20bd5e15fdSBram Moolenaar /* 21bd5e15fdSBram Moolenaar * Roland Puntaier 2009/sept/16: 22bd5e15fdSBram Moolenaar * Adaptations to support both python3.x and python2.x 23bd5e15fdSBram Moolenaar */ 24bd5e15fdSBram Moolenaar 250c1f3f4dSBram Moolenaar /* uncomment this if used with the debug version of python */ 260c1f3f4dSBram Moolenaar /* #define Py_DEBUG */ 27bd5e15fdSBram Moolenaar 28bd5e15fdSBram Moolenaar #include "vim.h" 29bd5e15fdSBram Moolenaar 30bd5e15fdSBram Moolenaar #include <limits.h> 31bd5e15fdSBram Moolenaar 32bd5e15fdSBram Moolenaar /* Python.h defines _POSIX_THREADS itself (if needed) */ 33bd5e15fdSBram Moolenaar #ifdef _POSIX_THREADS 34bd5e15fdSBram Moolenaar # undef _POSIX_THREADS 35bd5e15fdSBram Moolenaar #endif 36bd5e15fdSBram Moolenaar 37bd5e15fdSBram Moolenaar #if defined(_WIN32) && defined(HAVE_FCNTL_H) 38bd5e15fdSBram Moolenaar # undef HAVE_FCNTL_H 39bd5e15fdSBram Moolenaar #endif 40bd5e15fdSBram Moolenaar 41bd5e15fdSBram Moolenaar #ifdef _DEBUG 42bd5e15fdSBram Moolenaar # undef _DEBUG 43bd5e15fdSBram Moolenaar #endif 44bd5e15fdSBram Moolenaar 45bd5e15fdSBram Moolenaar #define PY_SSIZE_T_CLEAN 46bd5e15fdSBram Moolenaar 47bd5e15fdSBram Moolenaar #ifdef F_BLANK 48bd5e15fdSBram Moolenaar # undef F_BLANK 49bd5e15fdSBram Moolenaar #endif 50bd5e15fdSBram Moolenaar 516df6f47dSBram Moolenaar #ifdef HAVE_STDARG_H 526df6f47dSBram Moolenaar # undef HAVE_STDARG_H /* Python's config.h defines it as well. */ 536df6f47dSBram Moolenaar #endif 54bd5e15fdSBram Moolenaar #ifdef _POSIX_C_SOURCE /* defined in feature.h */ 55bd5e15fdSBram Moolenaar # undef _POSIX_C_SOURCE 56bd5e15fdSBram Moolenaar #endif 576df6f47dSBram Moolenaar #ifdef _XOPEN_SOURCE 586df6f47dSBram Moolenaar # undef _XOPEN_SOURCE /* pyconfig.h defines it as well. */ 596df6f47dSBram Moolenaar #endif 60bd5e15fdSBram Moolenaar 61bd5e15fdSBram Moolenaar #include <Python.h> 62bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX) 63bd5e15fdSBram Moolenaar # include "macglue.h" 64bd5e15fdSBram Moolenaar # include <CodeFragments.h> 65bd5e15fdSBram Moolenaar #endif 66bd5e15fdSBram Moolenaar #undef main /* Defined in python.h - aargh */ 67bd5e15fdSBram Moolenaar #undef HAVE_FCNTL_H /* Clash with os_win32.h */ 68bd5e15fdSBram Moolenaar 69bd5e15fdSBram Moolenaar static void init_structs(void); 70bd5e15fdSBram Moolenaar 71*3d64a317SBram Moolenaar /* The "surrogateescape" error handler is new in Python 3.1 */ 72*3d64a317SBram Moolenaar #if PY_VERSION_HEX >= 0x030100f0 73*3d64a317SBram Moolenaar # define CODEC_ERROR_HANDLER "surrogateescape" 74*3d64a317SBram Moolenaar #else 75*3d64a317SBram Moolenaar # define CODEC_ERROR_HANDLER NULL 76*3d64a317SBram Moolenaar #endif 77*3d64a317SBram Moolenaar 78170bf1aeSBram Moolenaar #define PyInt Py_ssize_t 79ca8a4dfeSBram Moolenaar #define PyString_Check(obj) PyUnicode_Check(obj) 80*3d64a317SBram Moolenaar #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER); 8119e60943SBram Moolenaar #define PyString_FreeBytes(obj) Py_XDECREF(bytes) 8219e60943SBram Moolenaar #define PyString_AsString(obj) PyBytes_AsString(obj) 8319e60943SBram Moolenaar #define PyString_Size(obj) PyBytes_GET_SIZE(bytes) 84ca8a4dfeSBram Moolenaar #define PyString_FromString(repr) PyUnicode_FromString(repr) 85170bf1aeSBram Moolenaar 860c1f3f4dSBram Moolenaar #if defined(DYNAMIC_PYTHON3) || defined(PROTO) 87bd5e15fdSBram Moolenaar 88fa5d1e63SBram Moolenaar # ifndef WIN3264 89bd5e15fdSBram Moolenaar # include <dlfcn.h> 90bd5e15fdSBram Moolenaar # define FARPROC void* 91bd5e15fdSBram Moolenaar # define HINSTANCE void* 92644d37b8SBram Moolenaar # if defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL) 93b61f95c3SBram Moolenaar # define load_dll(n) dlopen((n), RTLD_LAZY) 94b61f95c3SBram Moolenaar # else 95fa5d1e63SBram Moolenaar # define load_dll(n) dlopen((n), RTLD_LAZY|RTLD_GLOBAL) 96b61f95c3SBram Moolenaar # endif 97bd5e15fdSBram Moolenaar # define close_dll dlclose 98bd5e15fdSBram Moolenaar # define symbol_from_dll dlsym 99bd5e15fdSBram Moolenaar # else 100ebbcb824SBram Moolenaar # define load_dll vimLoadLib 101bd5e15fdSBram Moolenaar # define close_dll FreeLibrary 102bd5e15fdSBram Moolenaar # define symbol_from_dll GetProcAddress 103bd5e15fdSBram Moolenaar # endif 104bd5e15fdSBram Moolenaar /* 105bd5e15fdSBram Moolenaar * Wrapper defines 106bd5e15fdSBram Moolenaar */ 107bd5e15fdSBram Moolenaar # undef PyArg_Parse 108bd5e15fdSBram Moolenaar # define PyArg_Parse py3_PyArg_Parse 109bd5e15fdSBram Moolenaar # undef PyArg_ParseTuple 110bd5e15fdSBram Moolenaar # define PyArg_ParseTuple py3_PyArg_ParseTuple 11119e60943SBram Moolenaar # define PyMem_Free py3_PyMem_Free 112bd5e15fdSBram Moolenaar # define PyDict_SetItemString py3_PyDict_SetItemString 113bd5e15fdSBram Moolenaar # define PyErr_BadArgument py3_PyErr_BadArgument 114bd5e15fdSBram Moolenaar # define PyErr_Clear py3_PyErr_Clear 115bd5e15fdSBram Moolenaar # define PyErr_NoMemory py3_PyErr_NoMemory 116bd5e15fdSBram Moolenaar # define PyErr_Occurred py3_PyErr_Occurred 117bd5e15fdSBram Moolenaar # define PyErr_SetNone py3_PyErr_SetNone 118bd5e15fdSBram Moolenaar # define PyErr_SetString py3_PyErr_SetString 119bd5e15fdSBram Moolenaar # define PyEval_InitThreads py3_PyEval_InitThreads 120bd5e15fdSBram Moolenaar # define PyEval_RestoreThread py3_PyEval_RestoreThread 121bd5e15fdSBram Moolenaar # define PyEval_SaveThread py3_PyEval_SaveThread 122bd5e15fdSBram Moolenaar # define PyGILState_Ensure py3_PyGILState_Ensure 123bd5e15fdSBram Moolenaar # define PyGILState_Release py3_PyGILState_Release 124bd5e15fdSBram Moolenaar # define PyLong_AsLong py3_PyLong_AsLong 125bd5e15fdSBram Moolenaar # define PyLong_FromLong py3_PyLong_FromLong 126bd5e15fdSBram Moolenaar # define PyList_GetItem py3_PyList_GetItem 127bd5e15fdSBram Moolenaar # define PyList_Append py3_PyList_Append 128bd5e15fdSBram Moolenaar # define PyList_New py3_PyList_New 129bd5e15fdSBram Moolenaar # define PyList_SetItem py3_PyList_SetItem 130bd5e15fdSBram Moolenaar # define PyList_Size py3_PyList_Size 131bd5e15fdSBram Moolenaar # define PySlice_GetIndicesEx py3_PySlice_GetIndicesEx 132bd5e15fdSBram Moolenaar # define PyImport_ImportModule py3_PyImport_ImportModule 133bd5e15fdSBram Moolenaar # define PyObject_Init py3__PyObject_Init 134bd5e15fdSBram Moolenaar # define PyDict_New py3_PyDict_New 135bd5e15fdSBram Moolenaar # define PyDict_GetItemString py3_PyDict_GetItemString 136bd5e15fdSBram Moolenaar # define PyModule_GetDict py3_PyModule_GetDict 137bd5e15fdSBram Moolenaar #undef PyRun_SimpleString 138bd5e15fdSBram Moolenaar # define PyRun_SimpleString py3_PyRun_SimpleString 139bd5e15fdSBram Moolenaar # define PySys_SetObject py3_PySys_SetObject 140bd5e15fdSBram Moolenaar # define PySys_SetArgv py3_PySys_SetArgv 141bd5e15fdSBram Moolenaar # define PyType_Type (*py3_PyType_Type) 142bd5e15fdSBram Moolenaar # define PyType_Ready py3_PyType_Ready 143bd5e15fdSBram Moolenaar #undef Py_BuildValue 144bd5e15fdSBram Moolenaar # define Py_BuildValue py3_Py_BuildValue 145644d37b8SBram Moolenaar # define Py_SetPythonHome py3_Py_SetPythonHome 146bd5e15fdSBram Moolenaar # define Py_Initialize py3_Py_Initialize 147bd5e15fdSBram Moolenaar # define Py_Finalize py3_Py_Finalize 148bd5e15fdSBram Moolenaar # define Py_IsInitialized py3_Py_IsInitialized 149bd5e15fdSBram Moolenaar # define _Py_NoneStruct (*py3__Py_NoneStruct) 150bd5e15fdSBram Moolenaar # define PyModule_AddObject py3_PyModule_AddObject 151bd5e15fdSBram Moolenaar # define PyImport_AppendInittab py3_PyImport_AppendInittab 152bd5e15fdSBram Moolenaar # define _PyUnicode_AsString py3__PyUnicode_AsString 15319e60943SBram Moolenaar # undef PyUnicode_AsEncodedString 15419e60943SBram Moolenaar # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString 15519e60943SBram Moolenaar # undef PyBytes_AsString 15619e60943SBram Moolenaar # define PyBytes_AsString py3_PyBytes_AsString 157bd5e15fdSBram Moolenaar # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr 158bd5e15fdSBram Moolenaar # define PySlice_Type (*py3_PySlice_Type) 15919e60943SBram Moolenaar # define PyErr_NewException py3_PyErr_NewException 160bd5e15fdSBram Moolenaar # ifdef Py_DEBUG 161bd5e15fdSBram Moolenaar # define _Py_NegativeRefcount py3__Py_NegativeRefcount 162bd5e15fdSBram Moolenaar # define _Py_RefTotal (*py3__Py_RefTotal) 163bd5e15fdSBram Moolenaar # define _Py_Dealloc py3__Py_Dealloc 164bd5e15fdSBram Moolenaar # define _PyObject_DebugMalloc py3__PyObject_DebugMalloc 165bd5e15fdSBram Moolenaar # define _PyObject_DebugFree py3__PyObject_DebugFree 166bd5e15fdSBram Moolenaar # else 167bd5e15fdSBram Moolenaar # define PyObject_Malloc py3_PyObject_Malloc 168bd5e15fdSBram Moolenaar # define PyObject_Free py3_PyObject_Free 169bd5e15fdSBram Moolenaar # endif 170bd5e15fdSBram Moolenaar # define PyType_GenericAlloc py3_PyType_GenericAlloc 171bd5e15fdSBram Moolenaar # define PyType_GenericNew py3_PyType_GenericNew 172bd5e15fdSBram Moolenaar # define PyModule_Create2 py3_PyModule_Create2 173bd5e15fdSBram Moolenaar # undef PyUnicode_FromString 174bd5e15fdSBram Moolenaar # define PyUnicode_FromString py3_PyUnicode_FromString 17519e60943SBram Moolenaar # undef PyUnicode_Decode 17619e60943SBram Moolenaar # define PyUnicode_Decode py3_PyUnicode_Decode 177bd5e15fdSBram Moolenaar 178bd5e15fdSBram Moolenaar # ifdef Py_DEBUG 179bd5e15fdSBram Moolenaar # undef PyObject_NEW 180bd5e15fdSBram Moolenaar # define PyObject_NEW(type, typeobj) \ 181bd5e15fdSBram Moolenaar ( (type *) PyObject_Init( \ 182bd5e15fdSBram Moolenaar (PyObject *) _PyObject_DebugMalloc( _PyObject_SIZE(typeobj) ), (typeobj)) ) 183bd5e15fdSBram Moolenaar # endif 184b61f95c3SBram Moolenaar 185bd5e15fdSBram Moolenaar /* 186bd5e15fdSBram Moolenaar * Pointers for dynamic link 187bd5e15fdSBram Moolenaar */ 188bd5e15fdSBram Moolenaar static int (*py3_PySys_SetArgv)(int, wchar_t **); 189644d37b8SBram Moolenaar static void (*py3_Py_SetPythonHome)(wchar_t *home); 190bd5e15fdSBram Moolenaar static void (*py3_Py_Initialize)(void); 191bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_New)(Py_ssize_t size); 192bd5e15fdSBram Moolenaar static PyGILState_STATE (*py3_PyGILState_Ensure)(void); 193bd5e15fdSBram Moolenaar static void (*py3_PyGILState_Release)(PyGILState_STATE); 194bd5e15fdSBram Moolenaar static int (*py3_PySys_SetObject)(char *, PyObject *); 195bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_Append)(PyObject *, PyObject *); 196bd5e15fdSBram Moolenaar static Py_ssize_t (*py3_PyList_Size)(PyObject *); 197bd5e15fdSBram Moolenaar static int (*py3_PySlice_GetIndicesEx)(PySliceObject *r, Py_ssize_t length, 198bd5e15fdSBram Moolenaar Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength); 199bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_NoMemory)(void); 200bd5e15fdSBram Moolenaar static void (*py3_Py_Finalize)(void); 201bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetString)(PyObject *, const char *); 202bd5e15fdSBram Moolenaar static int (*py3_PyRun_SimpleString)(char *); 203bd5e15fdSBram Moolenaar static PyObject* (*py3_PyList_GetItem)(PyObject *, Py_ssize_t); 204bd5e15fdSBram Moolenaar static PyObject* (*py3_PyImport_ImportModule)(const char *); 205bd5e15fdSBram Moolenaar static int (*py3_PyErr_BadArgument)(void); 206bd5e15fdSBram Moolenaar static PyTypeObject* py3_PyType_Type; 207bd5e15fdSBram Moolenaar static PyObject* (*py3_PyErr_Occurred)(void); 208bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_GetDict)(PyObject *); 209bd5e15fdSBram Moolenaar static int (*py3_PyList_SetItem)(PyObject *, Py_ssize_t, PyObject *); 210bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_GetItemString)(PyObject *, const char *); 211bd5e15fdSBram Moolenaar static PyObject* (*py3_PyLong_FromLong)(long); 212bd5e15fdSBram Moolenaar static PyObject* (*py3_PyDict_New)(void); 213bd5e15fdSBram Moolenaar static PyObject* (*py3_Py_BuildValue)(char *, ...); 214bd5e15fdSBram Moolenaar static int (*py3_PyType_Ready)(PyTypeObject *type); 215bd5e15fdSBram Moolenaar static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item); 216bd5e15fdSBram Moolenaar static PyObject* (*py3_PyUnicode_FromString)(const char *u); 21719e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size, 21819e60943SBram Moolenaar const char *encoding, const char *errors); 219bd5e15fdSBram Moolenaar static long (*py3_PyLong_AsLong)(PyObject *); 220bd5e15fdSBram Moolenaar static void (*py3_PyErr_SetNone)(PyObject *); 221bd5e15fdSBram Moolenaar static void (*py3_PyEval_InitThreads)(void); 222bd5e15fdSBram Moolenaar static void(*py3_PyEval_RestoreThread)(PyThreadState *); 223bd5e15fdSBram Moolenaar static PyThreadState*(*py3_PyEval_SaveThread)(void); 224bd5e15fdSBram Moolenaar static int (*py3_PyArg_Parse)(PyObject *, char *, ...); 225bd5e15fdSBram Moolenaar static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...); 22619e60943SBram Moolenaar static int (*py3_PyMem_Free)(void *); 227bd5e15fdSBram Moolenaar static int (*py3_Py_IsInitialized)(void); 228bd5e15fdSBram Moolenaar static void (*py3_PyErr_Clear)(void); 229bd5e15fdSBram Moolenaar static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *); 230bd5e15fdSBram Moolenaar static PyObject* py3__Py_NoneStruct; 231bd5e15fdSBram Moolenaar static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o); 232bd5e15fdSBram Moolenaar static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void)); 233bd5e15fdSBram Moolenaar static char* (*py3__PyUnicode_AsString)(PyObject *unicode); 23419e60943SBram Moolenaar static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); 23519e60943SBram Moolenaar static char* (*py3_PyBytes_AsString)(PyObject *bytes); 236bd5e15fdSBram Moolenaar static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name); 237bd5e15fdSBram Moolenaar static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version); 238bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems); 239bd5e15fdSBram Moolenaar static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds); 240bd5e15fdSBram Moolenaar static PyTypeObject* py3_PySlice_Type; 24119e60943SBram Moolenaar static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict); 242bd5e15fdSBram Moolenaar # ifdef Py_DEBUG 243bd5e15fdSBram Moolenaar static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op); 244bd5e15fdSBram Moolenaar static Py_ssize_t* py3__Py_RefTotal; 245bd5e15fdSBram Moolenaar static void (*py3__Py_Dealloc)(PyObject *obj); 246bd5e15fdSBram Moolenaar static void (*py3__PyObject_DebugFree)(void*); 247bd5e15fdSBram Moolenaar static void* (*py3__PyObject_DebugMalloc)(size_t); 248bd5e15fdSBram Moolenaar # else 249bd5e15fdSBram Moolenaar static void (*py3_PyObject_Free)(void*); 250bd5e15fdSBram Moolenaar static void* (*py3_PyObject_Malloc)(size_t); 251bd5e15fdSBram Moolenaar # endif 252bd5e15fdSBram Moolenaar 253bd5e15fdSBram Moolenaar static HINSTANCE hinstPy3 = 0; /* Instance of python.dll */ 254bd5e15fdSBram Moolenaar 255bd5e15fdSBram Moolenaar /* Imported exception objects */ 256bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_AttributeError; 257bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_IndexError; 258bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_KeyboardInterrupt; 259bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_TypeError; 260bd5e15fdSBram Moolenaar static PyObject *p3imp_PyExc_ValueError; 261bd5e15fdSBram Moolenaar 262bd5e15fdSBram Moolenaar # define PyExc_AttributeError p3imp_PyExc_AttributeError 263bd5e15fdSBram Moolenaar # define PyExc_IndexError p3imp_PyExc_IndexError 264bd5e15fdSBram Moolenaar # define PyExc_KeyboardInterrupt p3imp_PyExc_KeyboardInterrupt 265bd5e15fdSBram Moolenaar # define PyExc_TypeError p3imp_PyExc_TypeError 266bd5e15fdSBram Moolenaar # define PyExc_ValueError p3imp_PyExc_ValueError 267bd5e15fdSBram Moolenaar 268bd5e15fdSBram Moolenaar /* 269bd5e15fdSBram Moolenaar * Table of name to function pointer of python. 270bd5e15fdSBram Moolenaar */ 271bd5e15fdSBram Moolenaar # define PYTHON_PROC FARPROC 272bd5e15fdSBram Moolenaar static struct 273bd5e15fdSBram Moolenaar { 274bd5e15fdSBram Moolenaar char *name; 275bd5e15fdSBram Moolenaar PYTHON_PROC *ptr; 276bd5e15fdSBram Moolenaar } py3_funcname_table[] = 277bd5e15fdSBram Moolenaar { 278bd5e15fdSBram Moolenaar {"PySys_SetArgv", (PYTHON_PROC*)&py3_PySys_SetArgv}, 279644d37b8SBram Moolenaar {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome}, 280bd5e15fdSBram Moolenaar {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize}, 281bd5e15fdSBram Moolenaar {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple}, 28219e60943SBram Moolenaar {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free}, 283bd5e15fdSBram Moolenaar {"PyList_New", (PYTHON_PROC*)&py3_PyList_New}, 284bd5e15fdSBram Moolenaar {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure}, 285bd5e15fdSBram Moolenaar {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release}, 286bd5e15fdSBram Moolenaar {"PySys_SetObject", (PYTHON_PROC*)&py3_PySys_SetObject}, 287bd5e15fdSBram Moolenaar {"PyList_Append", (PYTHON_PROC*)&py3_PyList_Append}, 288bd5e15fdSBram Moolenaar {"PyList_Size", (PYTHON_PROC*)&py3_PyList_Size}, 289bd5e15fdSBram Moolenaar {"PySlice_GetIndicesEx", (PYTHON_PROC*)&py3_PySlice_GetIndicesEx}, 290bd5e15fdSBram Moolenaar {"PyErr_NoMemory", (PYTHON_PROC*)&py3_PyErr_NoMemory}, 291bd5e15fdSBram Moolenaar {"Py_Finalize", (PYTHON_PROC*)&py3_Py_Finalize}, 292bd5e15fdSBram Moolenaar {"PyErr_SetString", (PYTHON_PROC*)&py3_PyErr_SetString}, 293bd5e15fdSBram Moolenaar {"PyRun_SimpleString", (PYTHON_PROC*)&py3_PyRun_SimpleString}, 294bd5e15fdSBram Moolenaar {"PyList_GetItem", (PYTHON_PROC*)&py3_PyList_GetItem}, 295bd5e15fdSBram Moolenaar {"PyImport_ImportModule", (PYTHON_PROC*)&py3_PyImport_ImportModule}, 296bd5e15fdSBram Moolenaar {"PyErr_BadArgument", (PYTHON_PROC*)&py3_PyErr_BadArgument}, 297bd5e15fdSBram Moolenaar {"PyType_Type", (PYTHON_PROC*)&py3_PyType_Type}, 298bd5e15fdSBram Moolenaar {"PyErr_Occurred", (PYTHON_PROC*)&py3_PyErr_Occurred}, 299bd5e15fdSBram Moolenaar {"PyModule_GetDict", (PYTHON_PROC*)&py3_PyModule_GetDict}, 300bd5e15fdSBram Moolenaar {"PyList_SetItem", (PYTHON_PROC*)&py3_PyList_SetItem}, 301bd5e15fdSBram Moolenaar {"PyDict_GetItemString", (PYTHON_PROC*)&py3_PyDict_GetItemString}, 302bd5e15fdSBram Moolenaar {"PyLong_FromLong", (PYTHON_PROC*)&py3_PyLong_FromLong}, 303bd5e15fdSBram Moolenaar {"PyDict_New", (PYTHON_PROC*)&py3_PyDict_New}, 304bd5e15fdSBram Moolenaar {"Py_BuildValue", (PYTHON_PROC*)&py3_Py_BuildValue}, 305bd5e15fdSBram Moolenaar {"PyType_Ready", (PYTHON_PROC*)&py3_PyType_Ready}, 306bd5e15fdSBram Moolenaar {"PyDict_SetItemString", (PYTHON_PROC*)&py3_PyDict_SetItemString}, 307bd5e15fdSBram Moolenaar {"PyLong_AsLong", (PYTHON_PROC*)&py3_PyLong_AsLong}, 308bd5e15fdSBram Moolenaar {"PyErr_SetNone", (PYTHON_PROC*)&py3_PyErr_SetNone}, 309bd5e15fdSBram Moolenaar {"PyEval_InitThreads", (PYTHON_PROC*)&py3_PyEval_InitThreads}, 310bd5e15fdSBram Moolenaar {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread}, 311bd5e15fdSBram Moolenaar {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread}, 312bd5e15fdSBram Moolenaar {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse}, 313bd5e15fdSBram Moolenaar {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized}, 314bd5e15fdSBram Moolenaar {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct}, 315bd5e15fdSBram Moolenaar {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear}, 316bd5e15fdSBram Moolenaar {"PyObject_Init", (PYTHON_PROC*)&py3__PyObject_Init}, 317bd5e15fdSBram Moolenaar {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject}, 318bd5e15fdSBram Moolenaar {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab}, 319bd5e15fdSBram Moolenaar {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString}, 32019e60943SBram Moolenaar {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString}, 321bd5e15fdSBram Moolenaar {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr}, 322bd5e15fdSBram Moolenaar {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2}, 323bd5e15fdSBram Moolenaar {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc}, 324bd5e15fdSBram Moolenaar {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew}, 325bd5e15fdSBram Moolenaar {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type}, 32619e60943SBram Moolenaar {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException}, 327bd5e15fdSBram Moolenaar # ifdef Py_DEBUG 328bd5e15fdSBram Moolenaar {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount}, 329bd5e15fdSBram Moolenaar {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal}, 330bd5e15fdSBram Moolenaar {"_Py_Dealloc", (PYTHON_PROC*)&py3__Py_Dealloc}, 331bd5e15fdSBram Moolenaar {"_PyObject_DebugFree", (PYTHON_PROC*)&py3__PyObject_DebugFree}, 332bd5e15fdSBram Moolenaar {"_PyObject_DebugMalloc", (PYTHON_PROC*)&py3__PyObject_DebugMalloc}, 333bd5e15fdSBram Moolenaar # else 334bd5e15fdSBram Moolenaar {"PyObject_Malloc", (PYTHON_PROC*)&py3_PyObject_Malloc}, 335bd5e15fdSBram Moolenaar {"PyObject_Free", (PYTHON_PROC*)&py3_PyObject_Free}, 336bd5e15fdSBram Moolenaar # endif 337bd5e15fdSBram Moolenaar {"", NULL}, 338bd5e15fdSBram Moolenaar }; 339bd5e15fdSBram Moolenaar 340bd5e15fdSBram Moolenaar /* 341bd5e15fdSBram Moolenaar * Free python.dll 342bd5e15fdSBram Moolenaar */ 343170bf1aeSBram Moolenaar static void 344170bf1aeSBram Moolenaar end_dynamic_python3(void) 345bd5e15fdSBram Moolenaar { 3464c3a326cSBram Moolenaar if (hinstPy3 != 0) 347bd5e15fdSBram Moolenaar { 348bd5e15fdSBram Moolenaar close_dll(hinstPy3); 349bd5e15fdSBram Moolenaar hinstPy3 = 0; 350bd5e15fdSBram Moolenaar } 351bd5e15fdSBram Moolenaar } 352bd5e15fdSBram Moolenaar 353bd5e15fdSBram Moolenaar /* 354bd5e15fdSBram Moolenaar * Load library and get all pointers. 355bd5e15fdSBram Moolenaar * Parameter 'libname' provides name of DLL. 356bd5e15fdSBram Moolenaar * Return OK or FAIL. 357bd5e15fdSBram Moolenaar */ 358170bf1aeSBram Moolenaar static int 359170bf1aeSBram Moolenaar py3_runtime_link_init(char *libname, int verbose) 360bd5e15fdSBram Moolenaar { 361bd5e15fdSBram Moolenaar int i; 36219e60943SBram Moolenaar void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string; 363bd5e15fdSBram Moolenaar 364644d37b8SBram Moolenaar # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON) 365b744b2faSBram Moolenaar /* Can't have Python and Python3 loaded at the same time. 366b744b2faSBram Moolenaar * It cause a crash, because RTLD_GLOBAL is needed for 367b744b2faSBram Moolenaar * standard C extension libraries of one or both python versions. */ 3684c3a326cSBram Moolenaar if (python_loaded()) 3694c3a326cSBram Moolenaar { 370b744b2faSBram Moolenaar EMSG(_("E837: This Vim cannot execute :py3 after using :python")); 3714c3a326cSBram Moolenaar return FAIL; 3724c3a326cSBram Moolenaar } 3734c3a326cSBram Moolenaar # endif 3744c3a326cSBram Moolenaar 3754c3a326cSBram Moolenaar if (hinstPy3 != 0) 376bd5e15fdSBram Moolenaar return OK; 377bd5e15fdSBram Moolenaar hinstPy3 = load_dll(libname); 378bd5e15fdSBram Moolenaar 379bd5e15fdSBram Moolenaar if (!hinstPy3) 380bd5e15fdSBram Moolenaar { 381bd5e15fdSBram Moolenaar if (verbose) 382bd5e15fdSBram Moolenaar EMSG2(_(e_loadlib), libname); 383bd5e15fdSBram Moolenaar return FAIL; 384bd5e15fdSBram Moolenaar } 385bd5e15fdSBram Moolenaar 386bd5e15fdSBram Moolenaar for (i = 0; py3_funcname_table[i].ptr; ++i) 387bd5e15fdSBram Moolenaar { 388bd5e15fdSBram Moolenaar if ((*py3_funcname_table[i].ptr = symbol_from_dll(hinstPy3, 389bd5e15fdSBram Moolenaar py3_funcname_table[i].name)) == NULL) 390bd5e15fdSBram Moolenaar { 391bd5e15fdSBram Moolenaar close_dll(hinstPy3); 392bd5e15fdSBram Moolenaar hinstPy3 = 0; 393bd5e15fdSBram Moolenaar if (verbose) 394bd5e15fdSBram Moolenaar EMSG2(_(e_loadfunc), py3_funcname_table[i].name); 395bd5e15fdSBram Moolenaar return FAIL; 396bd5e15fdSBram Moolenaar } 397bd5e15fdSBram Moolenaar } 398bd5e15fdSBram Moolenaar 39969154f22SBram Moolenaar /* Load unicode functions separately as only the ucs2 or the ucs4 functions 40069154f22SBram Moolenaar * will be present in the library. */ 401bd5e15fdSBram Moolenaar ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString"); 40219e60943SBram Moolenaar ucs_decode = symbol_from_dll(hinstPy3, 40319e60943SBram Moolenaar "PyUnicodeUCS2_Decode"); 40419e60943SBram Moolenaar ucs_as_encoded_string = symbol_from_dll(hinstPy3, 40519e60943SBram Moolenaar "PyUnicodeUCS2_AsEncodedString"); 40619e60943SBram Moolenaar if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string) 407bd5e15fdSBram Moolenaar { 408bd5e15fdSBram Moolenaar ucs_from_string = symbol_from_dll(hinstPy3, 409bd5e15fdSBram Moolenaar "PyUnicodeUCS4_FromString"); 41019e60943SBram Moolenaar ucs_decode = symbol_from_dll(hinstPy3, 41119e60943SBram Moolenaar "PyUnicodeUCS4_Decode"); 41219e60943SBram Moolenaar ucs_as_encoded_string = symbol_from_dll(hinstPy3, 41319e60943SBram Moolenaar "PyUnicodeUCS4_AsEncodedString"); 414bd5e15fdSBram Moolenaar } 41519e60943SBram Moolenaar if (ucs_from_string && ucs_decode && ucs_as_encoded_string) 416bd5e15fdSBram Moolenaar { 417bd5e15fdSBram Moolenaar py3_PyUnicode_FromString = ucs_from_string; 41819e60943SBram Moolenaar py3_PyUnicode_Decode = ucs_decode; 41919e60943SBram Moolenaar py3_PyUnicode_AsEncodedString = ucs_as_encoded_string; 420bd5e15fdSBram Moolenaar } 421bd5e15fdSBram Moolenaar else 422bd5e15fdSBram Moolenaar { 423bd5e15fdSBram Moolenaar close_dll(hinstPy3); 424bd5e15fdSBram Moolenaar hinstPy3 = 0; 425bd5e15fdSBram Moolenaar if (verbose) 426bd5e15fdSBram Moolenaar EMSG2(_(e_loadfunc), "PyUnicode_UCSX_*"); 427bd5e15fdSBram Moolenaar return FAIL; 428bd5e15fdSBram Moolenaar } 429bd5e15fdSBram Moolenaar 430bd5e15fdSBram Moolenaar return OK; 431bd5e15fdSBram Moolenaar } 432bd5e15fdSBram Moolenaar 433bd5e15fdSBram Moolenaar /* 434bd5e15fdSBram Moolenaar * If python is enabled (there is installed python on Windows system) return 435bd5e15fdSBram Moolenaar * TRUE, else FALSE. 436bd5e15fdSBram Moolenaar */ 437170bf1aeSBram Moolenaar int 438170bf1aeSBram Moolenaar python3_enabled(int verbose) 439bd5e15fdSBram Moolenaar { 440bd5e15fdSBram Moolenaar return py3_runtime_link_init(DYNAMIC_PYTHON3_DLL, verbose) == OK; 441bd5e15fdSBram Moolenaar } 442bd5e15fdSBram Moolenaar 443bd5e15fdSBram Moolenaar /* Load the standard Python exceptions - don't import the symbols from the 444bd5e15fdSBram Moolenaar * DLL, as this can cause errors (importing data symbols is not reliable). 445bd5e15fdSBram Moolenaar */ 446bd5e15fdSBram Moolenaar static void get_py3_exceptions __ARGS((void)); 447bd5e15fdSBram Moolenaar 448170bf1aeSBram Moolenaar static void 449170bf1aeSBram Moolenaar get_py3_exceptions() 450bd5e15fdSBram Moolenaar { 451bd5e15fdSBram Moolenaar PyObject *exmod = PyImport_ImportModule("builtins"); 452bd5e15fdSBram Moolenaar PyObject *exdict = PyModule_GetDict(exmod); 453bd5e15fdSBram Moolenaar p3imp_PyExc_AttributeError = PyDict_GetItemString(exdict, "AttributeError"); 454bd5e15fdSBram Moolenaar p3imp_PyExc_IndexError = PyDict_GetItemString(exdict, "IndexError"); 455bd5e15fdSBram Moolenaar p3imp_PyExc_KeyboardInterrupt = PyDict_GetItemString(exdict, "KeyboardInterrupt"); 456bd5e15fdSBram Moolenaar p3imp_PyExc_TypeError = PyDict_GetItemString(exdict, "TypeError"); 457bd5e15fdSBram Moolenaar p3imp_PyExc_ValueError = PyDict_GetItemString(exdict, "ValueError"); 458bd5e15fdSBram Moolenaar Py_XINCREF(p3imp_PyExc_AttributeError); 459bd5e15fdSBram Moolenaar Py_XINCREF(p3imp_PyExc_IndexError); 460bd5e15fdSBram Moolenaar Py_XINCREF(p3imp_PyExc_KeyboardInterrupt); 461bd5e15fdSBram Moolenaar Py_XINCREF(p3imp_PyExc_TypeError); 462bd5e15fdSBram Moolenaar Py_XINCREF(p3imp_PyExc_ValueError); 463bd5e15fdSBram Moolenaar Py_XDECREF(exmod); 464bd5e15fdSBram Moolenaar } 465bd5e15fdSBram Moolenaar #endif /* DYNAMIC_PYTHON3 */ 466bd5e15fdSBram Moolenaar 467ca8a4dfeSBram Moolenaar static PyObject *BufferNew (buf_T *); 468ca8a4dfeSBram Moolenaar static PyObject *WindowNew(win_T *); 469ca8a4dfeSBram Moolenaar static PyObject *LineToString(const char *); 470ca8a4dfeSBram Moolenaar 471ca8a4dfeSBram Moolenaar static PyTypeObject RangeType; 472ca8a4dfeSBram Moolenaar 473170bf1aeSBram Moolenaar /* 474170bf1aeSBram Moolenaar * Include the code shared with if_python.c 475170bf1aeSBram Moolenaar */ 476170bf1aeSBram Moolenaar #include "if_py_both.h" 477170bf1aeSBram Moolenaar 478170bf1aeSBram Moolenaar static void 479170bf1aeSBram Moolenaar call_PyObject_Free(void *p) 480bd5e15fdSBram Moolenaar { 481bd5e15fdSBram Moolenaar #ifdef Py_DEBUG 482bd5e15fdSBram Moolenaar _PyObject_DebugFree(p); 483bd5e15fdSBram Moolenaar #else 484bd5e15fdSBram Moolenaar PyObject_Free(p); 485bd5e15fdSBram Moolenaar #endif 486bd5e15fdSBram Moolenaar } 487170bf1aeSBram Moolenaar 488170bf1aeSBram Moolenaar static PyObject * 489170bf1aeSBram Moolenaar call_PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) 490bd5e15fdSBram Moolenaar { 491bd5e15fdSBram Moolenaar return PyType_GenericNew(type,args,kwds); 492bd5e15fdSBram Moolenaar } 493170bf1aeSBram Moolenaar 494170bf1aeSBram Moolenaar static PyObject * 495170bf1aeSBram Moolenaar call_PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems) 496bd5e15fdSBram Moolenaar { 497bd5e15fdSBram Moolenaar return PyType_GenericAlloc(type,nitems); 498bd5e15fdSBram Moolenaar } 499bd5e15fdSBram Moolenaar 500bd5e15fdSBram Moolenaar /****************************************************** 501bd5e15fdSBram Moolenaar * Internal function prototypes. 502bd5e15fdSBram Moolenaar */ 503bd5e15fdSBram Moolenaar 504bd5e15fdSBram Moolenaar static Py_ssize_t RangeStart; 505bd5e15fdSBram Moolenaar static Py_ssize_t RangeEnd; 506bd5e15fdSBram Moolenaar 507bd5e15fdSBram Moolenaar static int PythonIO_Init(void); 508bd5e15fdSBram Moolenaar static void PythonIO_Fini(void); 50969154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void); 510bd5e15fdSBram Moolenaar 511bd5e15fdSBram Moolenaar /****************************************************** 512bd5e15fdSBram Moolenaar * 1. Python interpreter main program. 513bd5e15fdSBram Moolenaar */ 514bd5e15fdSBram Moolenaar 515bd5e15fdSBram Moolenaar static int py3initialised = 0; 516bd5e15fdSBram Moolenaar 517bd5e15fdSBram Moolenaar static PyGILState_STATE pygilstate = PyGILState_UNLOCKED; 518bd5e15fdSBram Moolenaar 519170bf1aeSBram Moolenaar void 520170bf1aeSBram Moolenaar python3_end() 521bd5e15fdSBram Moolenaar { 522bd5e15fdSBram Moolenaar static int recurse = 0; 523bd5e15fdSBram Moolenaar 524bd5e15fdSBram Moolenaar /* If a crash occurs while doing this, don't try again. */ 525bd5e15fdSBram Moolenaar if (recurse != 0) 526bd5e15fdSBram Moolenaar return; 527bd5e15fdSBram Moolenaar 528bd5e15fdSBram Moolenaar ++recurse; 529bd5e15fdSBram Moolenaar 530bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3 531bd5e15fdSBram Moolenaar if (hinstPy3) 532bd5e15fdSBram Moolenaar #endif 533bd5e15fdSBram Moolenaar if (Py_IsInitialized()) 534bd5e15fdSBram Moolenaar { 535bd5e15fdSBram Moolenaar // acquire lock before finalizing 536bd5e15fdSBram Moolenaar pygilstate = PyGILState_Ensure(); 537bd5e15fdSBram Moolenaar 538bd5e15fdSBram Moolenaar PythonIO_Fini(); 539bd5e15fdSBram Moolenaar Py_Finalize(); 540bd5e15fdSBram Moolenaar } 541bd5e15fdSBram Moolenaar 542bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3 543bd5e15fdSBram Moolenaar end_dynamic_python3(); 544bd5e15fdSBram Moolenaar #endif 545bd5e15fdSBram Moolenaar 546bd5e15fdSBram Moolenaar --recurse; 547bd5e15fdSBram Moolenaar } 548bd5e15fdSBram Moolenaar 5494c3a326cSBram Moolenaar #if (defined(DYNAMIC_PYTHON) && defined(FEAT_PYTHON)) || defined(PROTO) 5504c3a326cSBram Moolenaar int 5514c3a326cSBram Moolenaar python3_loaded() 5524c3a326cSBram Moolenaar { 5534c3a326cSBram Moolenaar return (hinstPy3 != 0); 5544c3a326cSBram Moolenaar } 5554c3a326cSBram Moolenaar #endif 5564c3a326cSBram Moolenaar 557170bf1aeSBram Moolenaar static int 558170bf1aeSBram Moolenaar Python3_Init(void) 559bd5e15fdSBram Moolenaar { 560bd5e15fdSBram Moolenaar if (!py3initialised) 561bd5e15fdSBram Moolenaar { 562bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3 563bd5e15fdSBram Moolenaar if (!python3_enabled(TRUE)) 564bd5e15fdSBram Moolenaar { 565bd5e15fdSBram Moolenaar EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); 566bd5e15fdSBram Moolenaar goto fail; 567bd5e15fdSBram Moolenaar } 568bd5e15fdSBram Moolenaar #endif 569bd5e15fdSBram Moolenaar 570bd5e15fdSBram Moolenaar init_structs(); 571bd5e15fdSBram Moolenaar 572644d37b8SBram Moolenaar 573644d37b8SBram Moolenaar #ifdef PYTHON3_HOME 574644d37b8SBram Moolenaar Py_SetPythonHome(PYTHON3_HOME); 575644d37b8SBram Moolenaar #endif 576644d37b8SBram Moolenaar 577bd5e15fdSBram Moolenaar #if !defined(MACOS) || defined(MACOS_X_UNIX) 578bd5e15fdSBram Moolenaar Py_Initialize(); 579bd5e15fdSBram Moolenaar #else 580bd5e15fdSBram Moolenaar PyMac_Initialize(); 581bd5e15fdSBram Moolenaar #endif 582456f2bb2SBram Moolenaar /* initialise threads, must be after Py_Initialize() */ 583456f2bb2SBram Moolenaar PyEval_InitThreads(); 584bd5e15fdSBram Moolenaar 585bd5e15fdSBram Moolenaar #ifdef DYNAMIC_PYTHON3 586bd5e15fdSBram Moolenaar get_py3_exceptions(); 587bd5e15fdSBram Moolenaar #endif 588bd5e15fdSBram Moolenaar 589bd5e15fdSBram Moolenaar if (PythonIO_Init()) 590bd5e15fdSBram Moolenaar goto fail; 591bd5e15fdSBram Moolenaar 592bd5e15fdSBram Moolenaar PyImport_AppendInittab("vim", Py3Init_vim); 593bd5e15fdSBram Moolenaar 594bd5e15fdSBram Moolenaar /* Remove the element from sys.path that was added because of our 595bd5e15fdSBram Moolenaar * argv[0] value in Py3Init_vim(). Previously we used an empty 596bd5e15fdSBram Moolenaar * string, but dependinding on the OS we then get an empty entry or 59719e60943SBram Moolenaar * the current directory in sys.path. 59819e60943SBram Moolenaar * Only after vim has been imported, the element does exist in 59919e60943SBram Moolenaar * sys.path. 60019e60943SBram Moolenaar */ 60119e60943SBram Moolenaar PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))"); 602bd5e15fdSBram Moolenaar 603bd5e15fdSBram Moolenaar // lock is created and acquired in PyEval_InitThreads() and thread 604bd5e15fdSBram Moolenaar // state is created in Py_Initialize() 605bd5e15fdSBram Moolenaar // there _PyGILState_NoteThreadState() also sets gilcounter to 1 606bd5e15fdSBram Moolenaar // (python must have threads enabled!) 607bd5e15fdSBram Moolenaar // so the following does both: unlock GIL and save thread state in TLS 608bd5e15fdSBram Moolenaar // without deleting thread state 609bd5e15fdSBram Moolenaar PyGILState_Release(pygilstate); 610bd5e15fdSBram Moolenaar 611bd5e15fdSBram Moolenaar py3initialised = 1; 612bd5e15fdSBram Moolenaar } 613bd5e15fdSBram Moolenaar 614bd5e15fdSBram Moolenaar return 0; 615bd5e15fdSBram Moolenaar 616bd5e15fdSBram Moolenaar fail: 617bd5e15fdSBram Moolenaar /* We call PythonIO_Flush() here to print any Python errors. 618bd5e15fdSBram Moolenaar * This is OK, as it is possible to call this function even 619bd5e15fdSBram Moolenaar * if PythonIO_Init() has not completed successfully (it will 620bd5e15fdSBram Moolenaar * not do anything in this case). 621bd5e15fdSBram Moolenaar */ 622bd5e15fdSBram Moolenaar PythonIO_Flush(); 623bd5e15fdSBram Moolenaar return -1; 624bd5e15fdSBram Moolenaar } 625bd5e15fdSBram Moolenaar 626bd5e15fdSBram Moolenaar /* 627bd5e15fdSBram Moolenaar * External interface 628bd5e15fdSBram Moolenaar */ 629170bf1aeSBram Moolenaar static void 630170bf1aeSBram Moolenaar DoPy3Command(exarg_T *eap, const char *cmd) 631bd5e15fdSBram Moolenaar { 632bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX) 633bd5e15fdSBram Moolenaar GrafPtr oldPort; 634bd5e15fdSBram Moolenaar #endif 635bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 636bd5e15fdSBram Moolenaar char *saved_locale; 637bd5e15fdSBram Moolenaar #endif 63819e60943SBram Moolenaar PyObject *cmdstr; 63919e60943SBram Moolenaar PyObject *cmdbytes; 640bd5e15fdSBram Moolenaar 641bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX) 642bd5e15fdSBram Moolenaar GetPort(&oldPort); 643bd5e15fdSBram Moolenaar /* Check if the Python library is available */ 644bd5e15fdSBram Moolenaar if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) 645bd5e15fdSBram Moolenaar goto theend; 646bd5e15fdSBram Moolenaar #endif 647bd5e15fdSBram Moolenaar if (Python3_Init()) 648bd5e15fdSBram Moolenaar goto theend; 649bd5e15fdSBram Moolenaar 650bd5e15fdSBram Moolenaar RangeStart = eap->line1; 651bd5e15fdSBram Moolenaar RangeEnd = eap->line2; 652bd5e15fdSBram Moolenaar Python_Release_Vim(); /* leave vim */ 653bd5e15fdSBram Moolenaar 654bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 655bd5e15fdSBram Moolenaar /* Python only works properly when the LC_NUMERIC locale is "C". */ 656bd5e15fdSBram Moolenaar saved_locale = setlocale(LC_NUMERIC, NULL); 657bd5e15fdSBram Moolenaar if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) 658bd5e15fdSBram Moolenaar saved_locale = NULL; 659bd5e15fdSBram Moolenaar else 660bd5e15fdSBram Moolenaar { 661bd5e15fdSBram Moolenaar /* Need to make a copy, value may change when setting new locale. */ 662bd5e15fdSBram Moolenaar saved_locale = (char *)vim_strsave((char_u *)saved_locale); 663bd5e15fdSBram Moolenaar (void)setlocale(LC_NUMERIC, "C"); 664bd5e15fdSBram Moolenaar } 665bd5e15fdSBram Moolenaar #endif 666bd5e15fdSBram Moolenaar 667bd5e15fdSBram Moolenaar pygilstate = PyGILState_Ensure(); 668bd5e15fdSBram Moolenaar 66919e60943SBram Moolenaar /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause 67019e60943SBram Moolenaar * SyntaxError (unicode error). */ 671*3d64a317SBram Moolenaar cmdstr = PyUnicode_Decode(cmd, strlen(cmd), 672*3d64a317SBram Moolenaar (char *)ENC_OPT, CODEC_ERROR_HANDLER); 673*3d64a317SBram Moolenaar cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER); 67419e60943SBram Moolenaar Py_XDECREF(cmdstr); 67519e60943SBram Moolenaar PyRun_SimpleString(PyBytes_AsString(cmdbytes)); 67619e60943SBram Moolenaar Py_XDECREF(cmdbytes); 677bd5e15fdSBram Moolenaar 678bd5e15fdSBram Moolenaar PyGILState_Release(pygilstate); 679bd5e15fdSBram Moolenaar 680bd5e15fdSBram Moolenaar #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 681bd5e15fdSBram Moolenaar if (saved_locale != NULL) 682bd5e15fdSBram Moolenaar { 683bd5e15fdSBram Moolenaar (void)setlocale(LC_NUMERIC, saved_locale); 684bd5e15fdSBram Moolenaar vim_free(saved_locale); 685bd5e15fdSBram Moolenaar } 686bd5e15fdSBram Moolenaar #endif 687bd5e15fdSBram Moolenaar 688bd5e15fdSBram Moolenaar Python_Lock_Vim(); /* enter vim */ 689bd5e15fdSBram Moolenaar PythonIO_Flush(); 690bd5e15fdSBram Moolenaar #if defined(MACOS) && !defined(MACOS_X_UNIX) 691bd5e15fdSBram Moolenaar SetPort(oldPort); 692bd5e15fdSBram Moolenaar #endif 693bd5e15fdSBram Moolenaar 694bd5e15fdSBram Moolenaar theend: 695bd5e15fdSBram Moolenaar return; /* keeps lint happy */ 696bd5e15fdSBram Moolenaar } 697bd5e15fdSBram Moolenaar 698bd5e15fdSBram Moolenaar /* 699368373e9SBram Moolenaar * ":py3" 700bd5e15fdSBram Moolenaar */ 701170bf1aeSBram Moolenaar void 702170bf1aeSBram Moolenaar ex_py3(exarg_T *eap) 703bd5e15fdSBram Moolenaar { 704bd5e15fdSBram Moolenaar char_u *script; 705bd5e15fdSBram Moolenaar 706bd5e15fdSBram Moolenaar script = script_get(eap, eap->arg); 707bd5e15fdSBram Moolenaar if (!eap->skip) 708bd5e15fdSBram Moolenaar { 709bd5e15fdSBram Moolenaar if (script == NULL) 710bd5e15fdSBram Moolenaar DoPy3Command(eap, (char *)eap->arg); 711bd5e15fdSBram Moolenaar else 712bd5e15fdSBram Moolenaar DoPy3Command(eap, (char *)script); 713bd5e15fdSBram Moolenaar } 714bd5e15fdSBram Moolenaar vim_free(script); 715bd5e15fdSBram Moolenaar } 716bd5e15fdSBram Moolenaar 717bd5e15fdSBram Moolenaar #define BUFFER_SIZE 2048 718bd5e15fdSBram Moolenaar 719bd5e15fdSBram Moolenaar /* 7206df6f47dSBram Moolenaar * ":py3file" 721bd5e15fdSBram Moolenaar */ 722bd5e15fdSBram Moolenaar void 723bd5e15fdSBram Moolenaar ex_py3file(exarg_T *eap) 724bd5e15fdSBram Moolenaar { 725bd5e15fdSBram Moolenaar static char buffer[BUFFER_SIZE]; 726bd5e15fdSBram Moolenaar const char *file; 727bd5e15fdSBram Moolenaar char *p; 728bd5e15fdSBram Moolenaar int i; 729bd5e15fdSBram Moolenaar 730bd5e15fdSBram Moolenaar /* Have to do it like this. PyRun_SimpleFile requires you to pass a 731bd5e15fdSBram Moolenaar * stdio file pointer, but Vim and the Python DLL are compiled with 732bd5e15fdSBram Moolenaar * different options under Windows, meaning that stdio pointers aren't 733bd5e15fdSBram Moolenaar * compatible between the two. Yuk. 734bd5e15fdSBram Moolenaar * 73519e60943SBram Moolenaar * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec')) 73619e60943SBram Moolenaar * 73719e60943SBram Moolenaar * Using bytes so that Python can detect the source encoding as it normally 73819e60943SBram Moolenaar * does. The doc does not say "compile" accept bytes, though. 739bd5e15fdSBram Moolenaar * 740bd5e15fdSBram Moolenaar * We need to escape any backslashes or single quotes in the file name, so that 741bd5e15fdSBram Moolenaar * Python won't mangle the file name. 742bd5e15fdSBram Moolenaar */ 743bd5e15fdSBram Moolenaar 744bd5e15fdSBram Moolenaar strcpy(buffer, "exec(compile(open('"); 745bd5e15fdSBram Moolenaar p = buffer + 19; /* size of "exec(compile(open('" */ 746bd5e15fdSBram Moolenaar 747bd5e15fdSBram Moolenaar for (i=0; i<2; ++i) 748bd5e15fdSBram Moolenaar { 749bd5e15fdSBram Moolenaar file = (char *)eap->arg; 750bd5e15fdSBram Moolenaar while (*file && p < buffer + (BUFFER_SIZE - 3)) 751bd5e15fdSBram Moolenaar { 752bd5e15fdSBram Moolenaar if (*file == '\\' || *file == '\'') 753bd5e15fdSBram Moolenaar *p++ = '\\'; 754bd5e15fdSBram Moolenaar *p++ = *file++; 755bd5e15fdSBram Moolenaar } 756bd5e15fdSBram Moolenaar /* If we didn't finish the file name, we hit a buffer overflow */ 757bd5e15fdSBram Moolenaar if (*file != '\0') 758bd5e15fdSBram Moolenaar return; 759bd5e15fdSBram Moolenaar if (i==0) 760bd5e15fdSBram Moolenaar { 76119e60943SBram Moolenaar strcpy(p,"','rb').read(),'"); 76219e60943SBram Moolenaar p += 16; 763bd5e15fdSBram Moolenaar } 764bd5e15fdSBram Moolenaar else 765bd5e15fdSBram Moolenaar { 766bd5e15fdSBram Moolenaar strcpy(p,"','exec'))"); 767bd5e15fdSBram Moolenaar p += 10; 768bd5e15fdSBram Moolenaar } 769bd5e15fdSBram Moolenaar } 770bd5e15fdSBram Moolenaar 771bd5e15fdSBram Moolenaar 772bd5e15fdSBram Moolenaar /* Execute the file */ 773bd5e15fdSBram Moolenaar DoPy3Command(eap, buffer); 774bd5e15fdSBram Moolenaar } 775bd5e15fdSBram Moolenaar 776bd5e15fdSBram Moolenaar /****************************************************** 777bd5e15fdSBram Moolenaar * 2. Python output stream: writes output via [e]msg(). 778bd5e15fdSBram Moolenaar */ 779bd5e15fdSBram Moolenaar 780bd5e15fdSBram Moolenaar /* Implementation functions 781bd5e15fdSBram Moolenaar */ 782bd5e15fdSBram Moolenaar 783170bf1aeSBram Moolenaar static PyObject * 784170bf1aeSBram Moolenaar OutputGetattro(PyObject *self, PyObject *nameobj) 785bd5e15fdSBram Moolenaar { 786bd5e15fdSBram Moolenaar char *name = ""; 787bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 788bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 789bd5e15fdSBram Moolenaar 790bd5e15fdSBram Moolenaar if (strcmp(name, "softspace") == 0) 791bd5e15fdSBram Moolenaar return PyLong_FromLong(((OutputObject *)(self))->softspace); 792bd5e15fdSBram Moolenaar 793bd5e15fdSBram Moolenaar return PyObject_GenericGetAttr(self, nameobj); 794bd5e15fdSBram Moolenaar } 795bd5e15fdSBram Moolenaar 796170bf1aeSBram Moolenaar static int 797170bf1aeSBram Moolenaar OutputSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 798bd5e15fdSBram Moolenaar { 799bd5e15fdSBram Moolenaar char *name = ""; 800bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 801bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 802bd5e15fdSBram Moolenaar 803bd5e15fdSBram Moolenaar if (val == NULL) { 804bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_AttributeError, _("can't delete OutputObject attributes")); 805bd5e15fdSBram Moolenaar return -1; 806bd5e15fdSBram Moolenaar } 807bd5e15fdSBram Moolenaar 808bd5e15fdSBram Moolenaar if (strcmp(name, "softspace") == 0) 809bd5e15fdSBram Moolenaar { 810bd5e15fdSBram Moolenaar if (!PyLong_Check(val)) { 811bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_TypeError, _("softspace must be an integer")); 812bd5e15fdSBram Moolenaar return -1; 813bd5e15fdSBram Moolenaar } 814bd5e15fdSBram Moolenaar 815bd5e15fdSBram Moolenaar ((OutputObject *)(self))->softspace = PyLong_AsLong(val); 816bd5e15fdSBram Moolenaar return 0; 817bd5e15fdSBram Moolenaar } 818bd5e15fdSBram Moolenaar 819bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_AttributeError, _("invalid attribute")); 820bd5e15fdSBram Moolenaar return -1; 821bd5e15fdSBram Moolenaar } 822bd5e15fdSBram Moolenaar 823bd5e15fdSBram Moolenaar /***************/ 824bd5e15fdSBram Moolenaar 825170bf1aeSBram Moolenaar static int 826170bf1aeSBram Moolenaar PythonIO_Init(void) 827bd5e15fdSBram Moolenaar { 828bd5e15fdSBram Moolenaar PyType_Ready(&OutputType); 829170bf1aeSBram Moolenaar return PythonIO_Init_io(); 830bd5e15fdSBram Moolenaar } 831bd5e15fdSBram Moolenaar 832170bf1aeSBram Moolenaar static void 833170bf1aeSBram Moolenaar PythonIO_Fini(void) 834bd5e15fdSBram Moolenaar { 835bd5e15fdSBram Moolenaar PySys_SetObject("stdout", NULL); 836bd5e15fdSBram Moolenaar PySys_SetObject("stderr", NULL); 837bd5e15fdSBram Moolenaar } 838bd5e15fdSBram Moolenaar 839bd5e15fdSBram Moolenaar /****************************************************** 840bd5e15fdSBram Moolenaar * 3. Implementation of the Vim module for Python 841bd5e15fdSBram Moolenaar */ 842bd5e15fdSBram Moolenaar 843bd5e15fdSBram Moolenaar /* Window type - Implementation functions 844bd5e15fdSBram Moolenaar * -------------------------------------- 845bd5e15fdSBram Moolenaar */ 846bd5e15fdSBram Moolenaar 847bd5e15fdSBram Moolenaar #define WindowType_Check(obj) ((obj)->ob_base.ob_type == &WindowType) 848bd5e15fdSBram Moolenaar 849bd5e15fdSBram Moolenaar /* Buffer type - Implementation functions 850bd5e15fdSBram Moolenaar * -------------------------------------- 851bd5e15fdSBram Moolenaar */ 852bd5e15fdSBram Moolenaar 853bd5e15fdSBram Moolenaar #define BufferType_Check(obj) ((obj)->ob_base.ob_type == &BufferType) 854bd5e15fdSBram Moolenaar 855bd5e15fdSBram Moolenaar static Py_ssize_t BufferLength(PyObject *); 856bd5e15fdSBram Moolenaar static PyObject *BufferItem(PyObject *, Py_ssize_t); 857bd5e15fdSBram Moolenaar static PyObject* BufferSubscript(PyObject *self, PyObject* idx); 85819e60943SBram Moolenaar static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val); 859bd5e15fdSBram Moolenaar 860bd5e15fdSBram Moolenaar 861bd5e15fdSBram Moolenaar /* Line range type - Implementation functions 862bd5e15fdSBram Moolenaar * -------------------------------------- 863bd5e15fdSBram Moolenaar */ 864bd5e15fdSBram Moolenaar 865bd5e15fdSBram Moolenaar #define RangeType_Check(obj) ((obj)->ob_base.ob_type == &RangeType) 866bd5e15fdSBram Moolenaar 867bd5e15fdSBram Moolenaar static PyObject* RangeSubscript(PyObject *self, PyObject* idx); 868bd5e15fdSBram Moolenaar static Py_ssize_t RangeAsItem(PyObject *, Py_ssize_t, PyObject *); 869bd5e15fdSBram Moolenaar 870bd5e15fdSBram Moolenaar /* Current objects type - Implementation functions 871bd5e15fdSBram Moolenaar * ----------------------------------------------- 872bd5e15fdSBram Moolenaar */ 873bd5e15fdSBram Moolenaar 874bd5e15fdSBram Moolenaar static PySequenceMethods BufferAsSeq = { 875bd5e15fdSBram Moolenaar (lenfunc) BufferLength, /* sq_length, len(x) */ 876bd5e15fdSBram Moolenaar (binaryfunc) 0, /* sq_concat, x+y */ 877bd5e15fdSBram Moolenaar (ssizeargfunc) 0, /* sq_repeat, x*n */ 878bd5e15fdSBram Moolenaar (ssizeargfunc) BufferItem, /* sq_item, x[i] */ 879bd5e15fdSBram Moolenaar 0, /* was_sq_slice, x[i:j] */ 88019e60943SBram Moolenaar 0, /* sq_ass_item, x[i]=v */ 881bd5e15fdSBram Moolenaar 0, /* sq_ass_slice, x[i:j]=v */ 882bd5e15fdSBram Moolenaar 0, /* sq_contains */ 883bd5e15fdSBram Moolenaar 0, /* sq_inplace_concat */ 884bd5e15fdSBram Moolenaar 0, /* sq_inplace_repeat */ 885bd5e15fdSBram Moolenaar }; 886bd5e15fdSBram Moolenaar 887bd5e15fdSBram Moolenaar PyMappingMethods BufferAsMapping = { 888bd5e15fdSBram Moolenaar /* mp_length */ (lenfunc)BufferLength, 889bd5e15fdSBram Moolenaar /* mp_subscript */ (binaryfunc)BufferSubscript, 89019e60943SBram Moolenaar /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript, 891bd5e15fdSBram Moolenaar }; 892bd5e15fdSBram Moolenaar 893bd5e15fdSBram Moolenaar 894bd5e15fdSBram Moolenaar /* Buffer object - Definitions 895bd5e15fdSBram Moolenaar */ 896bd5e15fdSBram Moolenaar 897bd5e15fdSBram Moolenaar static PyTypeObject BufferType; 898bd5e15fdSBram Moolenaar 899170bf1aeSBram Moolenaar static PyObject * 900170bf1aeSBram Moolenaar BufferNew(buf_T *buf) 901bd5e15fdSBram Moolenaar { 902bd5e15fdSBram Moolenaar /* We need to handle deletion of buffers underneath us. 903bd5e15fdSBram Moolenaar * If we add a "b_python3_ref" field to the buf_T structure, 904bd5e15fdSBram Moolenaar * then we can get at it in buf_freeall() in vim. We then 905bd5e15fdSBram Moolenaar * need to create only ONE Python object per buffer - if 906bd5e15fdSBram Moolenaar * we try to create a second, just INCREF the existing one 907bd5e15fdSBram Moolenaar * and return it. The (single) Python object referring to 908bd5e15fdSBram Moolenaar * the buffer is stored in "b_python3_ref". 909bd5e15fdSBram Moolenaar * Question: what to do on a buf_freeall(). We'll probably 910bd5e15fdSBram Moolenaar * have to either delete the Python object (DECREF it to 911bd5e15fdSBram Moolenaar * zero - a bad idea, as it leaves dangling refs!) or 912bd5e15fdSBram Moolenaar * set the buf_T * value to an invalid value (-1?), which 913bd5e15fdSBram Moolenaar * means we need checks in all access functions... Bah. 914bd5e15fdSBram Moolenaar */ 915bd5e15fdSBram Moolenaar 916bd5e15fdSBram Moolenaar BufferObject *self; 917bd5e15fdSBram Moolenaar 918bd5e15fdSBram Moolenaar if (buf->b_python3_ref != NULL) 919bd5e15fdSBram Moolenaar { 920bd5e15fdSBram Moolenaar self = buf->b_python3_ref; 921bd5e15fdSBram Moolenaar Py_INCREF(self); 922bd5e15fdSBram Moolenaar } 923bd5e15fdSBram Moolenaar else 924bd5e15fdSBram Moolenaar { 925bd5e15fdSBram Moolenaar self = PyObject_NEW(BufferObject, &BufferType); 926bd5e15fdSBram Moolenaar buf->b_python3_ref = self; 927bd5e15fdSBram Moolenaar if (self == NULL) 928bd5e15fdSBram Moolenaar return NULL; 929bd5e15fdSBram Moolenaar self->buf = buf; 930bd5e15fdSBram Moolenaar } 931bd5e15fdSBram Moolenaar 932bd5e15fdSBram Moolenaar return (PyObject *)(self); 933bd5e15fdSBram Moolenaar } 934bd5e15fdSBram Moolenaar 935170bf1aeSBram Moolenaar static void 936170bf1aeSBram Moolenaar BufferDestructor(PyObject *self) 937bd5e15fdSBram Moolenaar { 938bd5e15fdSBram Moolenaar BufferObject *this = (BufferObject *)(self); 939bd5e15fdSBram Moolenaar 940bd5e15fdSBram Moolenaar if (this->buf && this->buf != INVALID_BUFFER_VALUE) 941bd5e15fdSBram Moolenaar this->buf->b_python3_ref = NULL; 94219e60943SBram Moolenaar 94319e60943SBram Moolenaar Py_TYPE(self)->tp_free((PyObject*)self); 944bd5e15fdSBram Moolenaar } 945bd5e15fdSBram Moolenaar 946170bf1aeSBram Moolenaar static PyObject * 947170bf1aeSBram Moolenaar BufferGetattro(PyObject *self, PyObject*nameobj) 948bd5e15fdSBram Moolenaar { 949bd5e15fdSBram Moolenaar BufferObject *this = (BufferObject *)(self); 950bd5e15fdSBram Moolenaar 951bd5e15fdSBram Moolenaar char *name = ""; 952bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 953bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 954bd5e15fdSBram Moolenaar 955bd5e15fdSBram Moolenaar if (CheckBuffer(this)) 956bd5e15fdSBram Moolenaar return NULL; 957bd5e15fdSBram Moolenaar 958bd5e15fdSBram Moolenaar if (strcmp(name, "name") == 0) 959bd5e15fdSBram Moolenaar return Py_BuildValue("s", this->buf->b_ffname); 960bd5e15fdSBram Moolenaar else if (strcmp(name, "number") == 0) 961bd5e15fdSBram Moolenaar return Py_BuildValue("n", this->buf->b_fnum); 962bd5e15fdSBram Moolenaar else if (strcmp(name,"__members__") == 0) 963bd5e15fdSBram Moolenaar return Py_BuildValue("[ss]", "name", "number"); 964bd5e15fdSBram Moolenaar else 965bd5e15fdSBram Moolenaar return PyObject_GenericGetAttr(self, nameobj); 966bd5e15fdSBram Moolenaar } 967bd5e15fdSBram Moolenaar 968170bf1aeSBram Moolenaar static PyObject * 969170bf1aeSBram Moolenaar BufferRepr(PyObject *self) 970bd5e15fdSBram Moolenaar { 971bd5e15fdSBram Moolenaar static char repr[100]; 972bd5e15fdSBram Moolenaar BufferObject *this = (BufferObject *)(self); 973bd5e15fdSBram Moolenaar 974bd5e15fdSBram Moolenaar if (this->buf == INVALID_BUFFER_VALUE) 975bd5e15fdSBram Moolenaar { 976bd5e15fdSBram Moolenaar vim_snprintf(repr, 100, _("<buffer object (deleted) at %p>"), (self)); 977bd5e15fdSBram Moolenaar return PyUnicode_FromString(repr); 978bd5e15fdSBram Moolenaar } 979bd5e15fdSBram Moolenaar else 980bd5e15fdSBram Moolenaar { 981bd5e15fdSBram Moolenaar char *name = (char *)this->buf->b_fname; 982bd5e15fdSBram Moolenaar Py_ssize_t len; 983bd5e15fdSBram Moolenaar 984bd5e15fdSBram Moolenaar if (name == NULL) 985bd5e15fdSBram Moolenaar name = ""; 986bd5e15fdSBram Moolenaar len = strlen(name); 987bd5e15fdSBram Moolenaar 988bd5e15fdSBram Moolenaar if (len > 35) 989bd5e15fdSBram Moolenaar name = name + (35 - len); 990bd5e15fdSBram Moolenaar 991bd5e15fdSBram Moolenaar vim_snprintf(repr, 100, "<buffer %s%s>", len > 35 ? "..." : "", name); 992bd5e15fdSBram Moolenaar 993bd5e15fdSBram Moolenaar return PyUnicode_FromString(repr); 994bd5e15fdSBram Moolenaar } 995bd5e15fdSBram Moolenaar } 996bd5e15fdSBram Moolenaar 997bd5e15fdSBram Moolenaar /******************/ 998bd5e15fdSBram Moolenaar 999170bf1aeSBram Moolenaar static Py_ssize_t 1000170bf1aeSBram Moolenaar BufferLength(PyObject *self) 1001bd5e15fdSBram Moolenaar { 1002bd5e15fdSBram Moolenaar if (CheckBuffer((BufferObject *)(self))) 1003bd5e15fdSBram Moolenaar return -1; 1004bd5e15fdSBram Moolenaar 1005bd5e15fdSBram Moolenaar return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count); 1006bd5e15fdSBram Moolenaar } 1007bd5e15fdSBram Moolenaar 1008170bf1aeSBram Moolenaar static PyObject * 1009170bf1aeSBram Moolenaar BufferItem(PyObject *self, Py_ssize_t n) 1010bd5e15fdSBram Moolenaar { 1011bd5e15fdSBram Moolenaar return RBItem((BufferObject *)(self), n, 1, 1012bd5e15fdSBram Moolenaar (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); 1013bd5e15fdSBram Moolenaar } 1014bd5e15fdSBram Moolenaar 1015170bf1aeSBram Moolenaar static PyObject * 1016170bf1aeSBram Moolenaar BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi) 1017170bf1aeSBram Moolenaar { 1018170bf1aeSBram Moolenaar return RBSlice((BufferObject *)(self), lo, hi, 1, 1019170bf1aeSBram Moolenaar (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count); 1020170bf1aeSBram Moolenaar } 1021170bf1aeSBram Moolenaar 1022170bf1aeSBram Moolenaar static PyObject * 1023170bf1aeSBram Moolenaar BufferSubscript(PyObject *self, PyObject* idx) 1024bd5e15fdSBram Moolenaar { 1025bd5e15fdSBram Moolenaar if (PyLong_Check(idx)) { 1026bd5e15fdSBram Moolenaar long _idx = PyLong_AsLong(idx); 1027bd5e15fdSBram Moolenaar return BufferItem(self,_idx); 1028bd5e15fdSBram Moolenaar } else if (PySlice_Check(idx)) { 1029bd5e15fdSBram Moolenaar Py_ssize_t start, stop, step, slicelen; 1030bd5e15fdSBram Moolenaar 1031bd5e15fdSBram Moolenaar if (PySlice_GetIndicesEx((PySliceObject *)idx, 1032bd5e15fdSBram Moolenaar (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, 1033bd5e15fdSBram Moolenaar &start, &stop, 1034bd5e15fdSBram Moolenaar &step, &slicelen) < 0) { 1035bd5e15fdSBram Moolenaar return NULL; 1036bd5e15fdSBram Moolenaar } 103719e60943SBram Moolenaar return BufferSlice(self,start,stop); 1038bd5e15fdSBram Moolenaar } else { 1039bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); 1040bd5e15fdSBram Moolenaar return NULL; 1041bd5e15fdSBram Moolenaar } 1042bd5e15fdSBram Moolenaar } 1043bd5e15fdSBram Moolenaar 104419e60943SBram Moolenaar static Py_ssize_t 104519e60943SBram Moolenaar BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val) 104619e60943SBram Moolenaar { 104719e60943SBram Moolenaar if (PyLong_Check(idx)) { 104819e60943SBram Moolenaar long n = PyLong_AsLong(idx); 104919e60943SBram Moolenaar return RBAsItem((BufferObject *)(self), n, val, 1, 105019e60943SBram Moolenaar (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count, 105119e60943SBram Moolenaar NULL); 105219e60943SBram Moolenaar } else if (PySlice_Check(idx)) { 105319e60943SBram Moolenaar Py_ssize_t start, stop, step, slicelen; 105419e60943SBram Moolenaar 105519e60943SBram Moolenaar if (PySlice_GetIndicesEx((PySliceObject *)idx, 105619e60943SBram Moolenaar (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1, 105719e60943SBram Moolenaar &start, &stop, 105819e60943SBram Moolenaar &step, &slicelen) < 0) { 105919e60943SBram Moolenaar return -1; 106019e60943SBram Moolenaar } 106119e60943SBram Moolenaar return RBAsSlice((BufferObject *)(self), start, stop, val, 1, 106219e60943SBram Moolenaar (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count, 106319e60943SBram Moolenaar NULL); 106419e60943SBram Moolenaar } else { 106519e60943SBram Moolenaar PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); 106619e60943SBram Moolenaar return -1; 106719e60943SBram Moolenaar } 106819e60943SBram Moolenaar } 106919e60943SBram Moolenaar 1070bd5e15fdSBram Moolenaar static PySequenceMethods RangeAsSeq = { 1071bd5e15fdSBram Moolenaar (lenfunc) RangeLength, /* sq_length, len(x) */ 107255d5c034SBram Moolenaar (binaryfunc) 0, /* RangeConcat, sq_concat, x+y */ 107355d5c034SBram Moolenaar (ssizeargfunc) 0, /* RangeRepeat, sq_repeat, x*n */ 1074bd5e15fdSBram Moolenaar (ssizeargfunc) RangeItem, /* sq_item, x[i] */ 1075bd5e15fdSBram Moolenaar 0, /* was_sq_slice, x[i:j] */ 1076bd5e15fdSBram Moolenaar (ssizeobjargproc) RangeAsItem, /* sq_as_item, x[i]=v */ 1077bd5e15fdSBram Moolenaar 0, /* sq_ass_slice, x[i:j]=v */ 1078bd5e15fdSBram Moolenaar 0, /* sq_contains */ 1079bd5e15fdSBram Moolenaar 0, /* sq_inplace_concat */ 1080bd5e15fdSBram Moolenaar 0, /* sq_inplace_repeat */ 1081bd5e15fdSBram Moolenaar }; 1082bd5e15fdSBram Moolenaar 1083bd5e15fdSBram Moolenaar PyMappingMethods RangeAsMapping = { 1084bd5e15fdSBram Moolenaar /* mp_length */ (lenfunc)RangeLength, 1085bd5e15fdSBram Moolenaar /* mp_subscript */ (binaryfunc)RangeSubscript, 1086bd5e15fdSBram Moolenaar /* mp_ass_subscript */ (objobjargproc)0, 1087bd5e15fdSBram Moolenaar }; 1088bd5e15fdSBram Moolenaar 1089bd5e15fdSBram Moolenaar /* Line range object - Implementation 1090bd5e15fdSBram Moolenaar */ 1091bd5e15fdSBram Moolenaar 1092170bf1aeSBram Moolenaar static void 1093170bf1aeSBram Moolenaar RangeDestructor(PyObject *self) 1094bd5e15fdSBram Moolenaar { 1095bd5e15fdSBram Moolenaar Py_DECREF(((RangeObject *)(self))->buf); 109619e60943SBram Moolenaar Py_TYPE(self)->tp_free((PyObject*)self); 1097bd5e15fdSBram Moolenaar } 1098bd5e15fdSBram Moolenaar 1099170bf1aeSBram Moolenaar static PyObject * 1100170bf1aeSBram Moolenaar RangeGetattro(PyObject *self, PyObject *nameobj) 1101bd5e15fdSBram Moolenaar { 1102bd5e15fdSBram Moolenaar char *name = ""; 1103bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 1104bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 1105bd5e15fdSBram Moolenaar 1106bd5e15fdSBram Moolenaar if (strcmp(name, "start") == 0) 1107bd5e15fdSBram Moolenaar return Py_BuildValue("n", ((RangeObject *)(self))->start - 1); 1108bd5e15fdSBram Moolenaar else if (strcmp(name, "end") == 0) 1109bd5e15fdSBram Moolenaar return Py_BuildValue("n", ((RangeObject *)(self))->end - 1); 1110bd5e15fdSBram Moolenaar else 1111bd5e15fdSBram Moolenaar return PyObject_GenericGetAttr(self, nameobj); 1112bd5e15fdSBram Moolenaar } 1113bd5e15fdSBram Moolenaar 1114bd5e15fdSBram Moolenaar /****************/ 1115bd5e15fdSBram Moolenaar 1116170bf1aeSBram Moolenaar static Py_ssize_t 1117170bf1aeSBram Moolenaar RangeAsItem(PyObject *self, Py_ssize_t n, PyObject *val) 1118bd5e15fdSBram Moolenaar { 1119bd5e15fdSBram Moolenaar return RBAsItem(((RangeObject *)(self))->buf, n, val, 1120bd5e15fdSBram Moolenaar ((RangeObject *)(self))->start, 1121bd5e15fdSBram Moolenaar ((RangeObject *)(self))->end, 1122bd5e15fdSBram Moolenaar &((RangeObject *)(self))->end); 1123bd5e15fdSBram Moolenaar } 1124bd5e15fdSBram Moolenaar 1125170bf1aeSBram Moolenaar static PyObject * 1126170bf1aeSBram Moolenaar RangeSubscript(PyObject *self, PyObject* idx) 1127bd5e15fdSBram Moolenaar { 1128bd5e15fdSBram Moolenaar if (PyLong_Check(idx)) { 1129bd5e15fdSBram Moolenaar long _idx = PyLong_AsLong(idx); 1130bd5e15fdSBram Moolenaar return RangeItem(self,_idx); 1131bd5e15fdSBram Moolenaar } else if (PySlice_Check(idx)) { 1132bd5e15fdSBram Moolenaar Py_ssize_t start, stop, step, slicelen; 1133bd5e15fdSBram Moolenaar 1134bd5e15fdSBram Moolenaar if (PySlice_GetIndicesEx((PySliceObject *)idx, 1135bd5e15fdSBram Moolenaar ((RangeObject *)(self))->end-((RangeObject *)(self))->start+1, 1136bd5e15fdSBram Moolenaar &start, &stop, 1137bd5e15fdSBram Moolenaar &step, &slicelen) < 0) { 1138bd5e15fdSBram Moolenaar return NULL; 1139bd5e15fdSBram Moolenaar } 1140bd5e15fdSBram Moolenaar return RangeSlice(self,start,stop+1); 1141bd5e15fdSBram Moolenaar } else { 1142bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_IndexError, "Index must be int or slice"); 1143bd5e15fdSBram Moolenaar return NULL; 1144bd5e15fdSBram Moolenaar } 1145bd5e15fdSBram Moolenaar } 1146bd5e15fdSBram Moolenaar 1147bd5e15fdSBram Moolenaar /* Buffer list object - Definitions 1148bd5e15fdSBram Moolenaar */ 1149bd5e15fdSBram Moolenaar 1150bd5e15fdSBram Moolenaar typedef struct 1151bd5e15fdSBram Moolenaar { 1152bd5e15fdSBram Moolenaar PyObject_HEAD 1153ca8a4dfeSBram Moolenaar } BufListObject; 1154bd5e15fdSBram Moolenaar 1155bd5e15fdSBram Moolenaar static PySequenceMethods BufListAsSeq = { 1156bd5e15fdSBram Moolenaar (lenfunc) BufListLength, /* sq_length, len(x) */ 1157bd5e15fdSBram Moolenaar (binaryfunc) 0, /* sq_concat, x+y */ 1158bd5e15fdSBram Moolenaar (ssizeargfunc) 0, /* sq_repeat, x*n */ 1159bd5e15fdSBram Moolenaar (ssizeargfunc) BufListItem, /* sq_item, x[i] */ 1160bd5e15fdSBram Moolenaar 0, /* was_sq_slice, x[i:j] */ 1161bd5e15fdSBram Moolenaar (ssizeobjargproc) 0, /* sq_as_item, x[i]=v */ 1162bd5e15fdSBram Moolenaar 0, /* sq_ass_slice, x[i:j]=v */ 1163bd5e15fdSBram Moolenaar 0, /* sq_contains */ 1164bd5e15fdSBram Moolenaar 0, /* sq_inplace_concat */ 1165bd5e15fdSBram Moolenaar 0, /* sq_inplace_repeat */ 1166bd5e15fdSBram Moolenaar }; 1167bd5e15fdSBram Moolenaar 1168bd5e15fdSBram Moolenaar static PyTypeObject BufListType; 1169bd5e15fdSBram Moolenaar 1170bd5e15fdSBram Moolenaar /* Window object - Definitions 1171bd5e15fdSBram Moolenaar */ 1172bd5e15fdSBram Moolenaar 1173bd5e15fdSBram Moolenaar static struct PyMethodDef WindowMethods[] = { 1174bd5e15fdSBram Moolenaar /* name, function, calling, documentation */ 1175bd5e15fdSBram Moolenaar { NULL, NULL, 0, NULL } 1176bd5e15fdSBram Moolenaar }; 1177bd5e15fdSBram Moolenaar 117855d5c034SBram Moolenaar static PyTypeObject WindowType; 1179bd5e15fdSBram Moolenaar 1180bd5e15fdSBram Moolenaar /* Window object - Implementation 1181bd5e15fdSBram Moolenaar */ 1182bd5e15fdSBram Moolenaar 1183170bf1aeSBram Moolenaar static PyObject * 1184170bf1aeSBram Moolenaar WindowNew(win_T *win) 1185bd5e15fdSBram Moolenaar { 1186bd5e15fdSBram Moolenaar /* We need to handle deletion of windows underneath us. 1187bd5e15fdSBram Moolenaar * If we add a "w_python3_ref" field to the win_T structure, 1188bd5e15fdSBram Moolenaar * then we can get at it in win_free() in vim. We then 1189bd5e15fdSBram Moolenaar * need to create only ONE Python object per window - if 1190bd5e15fdSBram Moolenaar * we try to create a second, just INCREF the existing one 1191bd5e15fdSBram Moolenaar * and return it. The (single) Python object referring to 1192bd5e15fdSBram Moolenaar * the window is stored in "w_python3_ref". 1193bd5e15fdSBram Moolenaar * On a win_free() we set the Python object's win_T* field 1194bd5e15fdSBram Moolenaar * to an invalid value. We trap all uses of a window 1195bd5e15fdSBram Moolenaar * object, and reject them if the win_T* field is invalid. 1196bd5e15fdSBram Moolenaar */ 1197bd5e15fdSBram Moolenaar 1198bd5e15fdSBram Moolenaar WindowObject *self; 1199bd5e15fdSBram Moolenaar 1200bd5e15fdSBram Moolenaar if (win->w_python3_ref) 1201bd5e15fdSBram Moolenaar { 1202bd5e15fdSBram Moolenaar self = win->w_python3_ref; 1203bd5e15fdSBram Moolenaar Py_INCREF(self); 1204bd5e15fdSBram Moolenaar } 1205bd5e15fdSBram Moolenaar else 1206bd5e15fdSBram Moolenaar { 1207bd5e15fdSBram Moolenaar self = PyObject_NEW(WindowObject, &WindowType); 1208bd5e15fdSBram Moolenaar if (self == NULL) 1209bd5e15fdSBram Moolenaar return NULL; 1210bd5e15fdSBram Moolenaar self->win = win; 1211bd5e15fdSBram Moolenaar win->w_python3_ref = self; 1212bd5e15fdSBram Moolenaar } 1213bd5e15fdSBram Moolenaar 1214bd5e15fdSBram Moolenaar return (PyObject *)(self); 1215bd5e15fdSBram Moolenaar } 1216bd5e15fdSBram Moolenaar 1217170bf1aeSBram Moolenaar static void 1218170bf1aeSBram Moolenaar WindowDestructor(PyObject *self) 1219bd5e15fdSBram Moolenaar { 1220bd5e15fdSBram Moolenaar WindowObject *this = (WindowObject *)(self); 1221bd5e15fdSBram Moolenaar 1222bd5e15fdSBram Moolenaar if (this->win && this->win != INVALID_WINDOW_VALUE) 1223bd5e15fdSBram Moolenaar this->win->w_python3_ref = NULL; 122419e60943SBram Moolenaar 122519e60943SBram Moolenaar Py_TYPE(self)->tp_free((PyObject*)self); 1226bd5e15fdSBram Moolenaar } 1227bd5e15fdSBram Moolenaar 1228170bf1aeSBram Moolenaar static PyObject * 1229170bf1aeSBram Moolenaar WindowGetattro(PyObject *self, PyObject *nameobj) 1230bd5e15fdSBram Moolenaar { 1231bd5e15fdSBram Moolenaar WindowObject *this = (WindowObject *)(self); 1232bd5e15fdSBram Moolenaar 1233bd5e15fdSBram Moolenaar char *name = ""; 1234bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 1235bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 1236bd5e15fdSBram Moolenaar 1237bd5e15fdSBram Moolenaar 1238bd5e15fdSBram Moolenaar if (CheckWindow(this)) 1239bd5e15fdSBram Moolenaar return NULL; 1240bd5e15fdSBram Moolenaar 1241bd5e15fdSBram Moolenaar if (strcmp(name, "buffer") == 0) 1242bd5e15fdSBram Moolenaar return (PyObject *)BufferNew(this->win->w_buffer); 1243bd5e15fdSBram Moolenaar else if (strcmp(name, "cursor") == 0) 1244bd5e15fdSBram Moolenaar { 1245bd5e15fdSBram Moolenaar pos_T *pos = &this->win->w_cursor; 1246bd5e15fdSBram Moolenaar 1247bd5e15fdSBram Moolenaar return Py_BuildValue("(ll)", (long)(pos->lnum), (long)(pos->col)); 1248bd5e15fdSBram Moolenaar } 1249bd5e15fdSBram Moolenaar else if (strcmp(name, "height") == 0) 1250bd5e15fdSBram Moolenaar return Py_BuildValue("l", (long)(this->win->w_height)); 1251bd5e15fdSBram Moolenaar #ifdef FEAT_VERTSPLIT 1252bd5e15fdSBram Moolenaar else if (strcmp(name, "width") == 0) 1253bd5e15fdSBram Moolenaar return Py_BuildValue("l", (long)(W_WIDTH(this->win))); 1254bd5e15fdSBram Moolenaar #endif 1255bd5e15fdSBram Moolenaar else if (strcmp(name,"__members__") == 0) 1256bd5e15fdSBram Moolenaar return Py_BuildValue("[sss]", "buffer", "cursor", "height"); 1257bd5e15fdSBram Moolenaar else 1258bd5e15fdSBram Moolenaar return PyObject_GenericGetAttr(self, nameobj); 1259bd5e15fdSBram Moolenaar } 1260bd5e15fdSBram Moolenaar 1261170bf1aeSBram Moolenaar static int 1262170bf1aeSBram Moolenaar WindowSetattro(PyObject *self, PyObject *nameobj, PyObject *val) 1263bd5e15fdSBram Moolenaar { 1264bd5e15fdSBram Moolenaar char *name = ""; 1265ca8a4dfeSBram Moolenaar 1266bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 1267bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 1268bd5e15fdSBram Moolenaar 1269ca8a4dfeSBram Moolenaar return WindowSetattr(self, name, val); 1270bd5e15fdSBram Moolenaar } 1271bd5e15fdSBram Moolenaar 1272bd5e15fdSBram Moolenaar /* Window list object - Definitions 1273bd5e15fdSBram Moolenaar */ 1274bd5e15fdSBram Moolenaar 1275bd5e15fdSBram Moolenaar typedef struct 1276bd5e15fdSBram Moolenaar { 1277bd5e15fdSBram Moolenaar PyObject_HEAD 1278bd5e15fdSBram Moolenaar } 1279bd5e15fdSBram Moolenaar WinListObject; 1280bd5e15fdSBram Moolenaar 1281bd5e15fdSBram Moolenaar static PySequenceMethods WinListAsSeq = { 1282bd5e15fdSBram Moolenaar (lenfunc) WinListLength, /* sq_length, len(x) */ 1283bd5e15fdSBram Moolenaar (binaryfunc) 0, /* sq_concat, x+y */ 1284bd5e15fdSBram Moolenaar (ssizeargfunc) 0, /* sq_repeat, x*n */ 1285bd5e15fdSBram Moolenaar (ssizeargfunc) WinListItem, /* sq_item, x[i] */ 1286bd5e15fdSBram Moolenaar 0, /* sq_slice, x[i:j] */ 1287bd5e15fdSBram Moolenaar (ssizeobjargproc)0, /* sq_as_item, x[i]=v */ 1288bd5e15fdSBram Moolenaar 0, /* sq_ass_slice, x[i:j]=v */ 1289bd5e15fdSBram Moolenaar 0, /* sq_contains */ 1290bd5e15fdSBram Moolenaar 0, /* sq_inplace_concat */ 1291bd5e15fdSBram Moolenaar 0, /* sq_inplace_repeat */ 1292bd5e15fdSBram Moolenaar }; 1293bd5e15fdSBram Moolenaar 1294bd5e15fdSBram Moolenaar static PyTypeObject WinListType; 1295bd5e15fdSBram Moolenaar 1296bd5e15fdSBram Moolenaar /* Current items object - Definitions 1297bd5e15fdSBram Moolenaar */ 1298bd5e15fdSBram Moolenaar 1299bd5e15fdSBram Moolenaar typedef struct 1300bd5e15fdSBram Moolenaar { 1301bd5e15fdSBram Moolenaar PyObject_HEAD 1302ca8a4dfeSBram Moolenaar } CurrentObject; 1303bd5e15fdSBram Moolenaar 1304bd5e15fdSBram Moolenaar static PyTypeObject CurrentType; 1305bd5e15fdSBram Moolenaar 1306bd5e15fdSBram Moolenaar /* Current items object - Implementation 1307bd5e15fdSBram Moolenaar */ 1308170bf1aeSBram Moolenaar static PyObject * 1309170bf1aeSBram Moolenaar CurrentGetattro(PyObject *self UNUSED, PyObject *nameobj) 1310bd5e15fdSBram Moolenaar { 1311bd5e15fdSBram Moolenaar char *name = ""; 1312bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 1313bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 1314bd5e15fdSBram Moolenaar 1315bd5e15fdSBram Moolenaar if (strcmp(name, "buffer") == 0) 1316bd5e15fdSBram Moolenaar return (PyObject *)BufferNew(curbuf); 1317bd5e15fdSBram Moolenaar else if (strcmp(name, "window") == 0) 1318bd5e15fdSBram Moolenaar return (PyObject *)WindowNew(curwin); 1319bd5e15fdSBram Moolenaar else if (strcmp(name, "line") == 0) 1320bd5e15fdSBram Moolenaar return GetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum); 1321bd5e15fdSBram Moolenaar else if (strcmp(name, "range") == 0) 1322bd5e15fdSBram Moolenaar return RangeNew(curbuf, RangeStart, RangeEnd); 1323bd5e15fdSBram Moolenaar else if (strcmp(name,"__members__") == 0) 1324bd5e15fdSBram Moolenaar return Py_BuildValue("[ssss]", "buffer", "window", "line", "range"); 1325bd5e15fdSBram Moolenaar else 1326bd5e15fdSBram Moolenaar { 1327bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_AttributeError, name); 1328bd5e15fdSBram Moolenaar return NULL; 1329bd5e15fdSBram Moolenaar } 1330bd5e15fdSBram Moolenaar } 1331bd5e15fdSBram Moolenaar 1332170bf1aeSBram Moolenaar static int 1333170bf1aeSBram Moolenaar CurrentSetattro(PyObject *self UNUSED, PyObject *nameobj, PyObject *value) 1334bd5e15fdSBram Moolenaar { 1335bd5e15fdSBram Moolenaar char *name = ""; 1336bd5e15fdSBram Moolenaar if (PyUnicode_Check(nameobj)) 1337bd5e15fdSBram Moolenaar name = _PyUnicode_AsString(nameobj); 1338bd5e15fdSBram Moolenaar 1339bd5e15fdSBram Moolenaar if (strcmp(name, "line") == 0) 1340bd5e15fdSBram Moolenaar { 1341bd5e15fdSBram Moolenaar if (SetBufferLine(curbuf, (Py_ssize_t)curwin->w_cursor.lnum, value, NULL) == FAIL) 1342bd5e15fdSBram Moolenaar return -1; 1343bd5e15fdSBram Moolenaar 1344bd5e15fdSBram Moolenaar return 0; 1345bd5e15fdSBram Moolenaar } 1346bd5e15fdSBram Moolenaar else 1347bd5e15fdSBram Moolenaar { 1348bd5e15fdSBram Moolenaar PyErr_SetString(PyExc_AttributeError, name); 1349bd5e15fdSBram Moolenaar return -1; 1350bd5e15fdSBram Moolenaar } 1351bd5e15fdSBram Moolenaar } 1352bd5e15fdSBram Moolenaar 1353bd5e15fdSBram Moolenaar /* External interface 1354bd5e15fdSBram Moolenaar */ 1355bd5e15fdSBram Moolenaar 1356bd5e15fdSBram Moolenaar void 1357bd5e15fdSBram Moolenaar python3_buffer_free(buf_T *buf) 1358bd5e15fdSBram Moolenaar { 1359bd5e15fdSBram Moolenaar if (buf->b_python3_ref != NULL) 1360bd5e15fdSBram Moolenaar { 1361bd5e15fdSBram Moolenaar BufferObject *bp = buf->b_python3_ref; 1362bd5e15fdSBram Moolenaar bp->buf = INVALID_BUFFER_VALUE; 1363bd5e15fdSBram Moolenaar buf->b_python3_ref = NULL; 1364bd5e15fdSBram Moolenaar } 1365bd5e15fdSBram Moolenaar } 1366bd5e15fdSBram Moolenaar 1367bd5e15fdSBram Moolenaar #if defined(FEAT_WINDOWS) || defined(PROTO) 1368bd5e15fdSBram Moolenaar void 1369bd5e15fdSBram Moolenaar python3_window_free(win_T *win) 1370bd5e15fdSBram Moolenaar { 1371bd5e15fdSBram Moolenaar if (win->w_python3_ref != NULL) 1372bd5e15fdSBram Moolenaar { 1373bd5e15fdSBram Moolenaar WindowObject *wp = win->w_python3_ref; 1374bd5e15fdSBram Moolenaar wp->win = INVALID_WINDOW_VALUE; 1375bd5e15fdSBram Moolenaar win->w_python3_ref = NULL; 1376bd5e15fdSBram Moolenaar } 1377bd5e15fdSBram Moolenaar } 1378bd5e15fdSBram Moolenaar #endif 1379bd5e15fdSBram Moolenaar 1380bd5e15fdSBram Moolenaar static BufListObject TheBufferList = 1381bd5e15fdSBram Moolenaar { 1382bd5e15fdSBram Moolenaar PyObject_HEAD_INIT(&BufListType) 1383bd5e15fdSBram Moolenaar }; 1384bd5e15fdSBram Moolenaar 1385bd5e15fdSBram Moolenaar static WinListObject TheWindowList = 1386bd5e15fdSBram Moolenaar { 1387bd5e15fdSBram Moolenaar PyObject_HEAD_INIT(&WinListType) 1388bd5e15fdSBram Moolenaar }; 1389bd5e15fdSBram Moolenaar 1390bd5e15fdSBram Moolenaar static CurrentObject TheCurrent = 1391bd5e15fdSBram Moolenaar { 1392bd5e15fdSBram Moolenaar PyObject_HEAD_INIT(&CurrentType) 1393bd5e15fdSBram Moolenaar }; 1394bd5e15fdSBram Moolenaar 1395bd5e15fdSBram Moolenaar PyDoc_STRVAR(vim_module_doc,"vim python interface\n"); 1396bd5e15fdSBram Moolenaar 1397bd5e15fdSBram Moolenaar static struct PyModuleDef vimmodule; 1398bd5e15fdSBram Moolenaar 139969154f22SBram Moolenaar #ifndef PROTO 140069154f22SBram Moolenaar PyMODINIT_FUNC Py3Init_vim(void) 1401bd5e15fdSBram Moolenaar { 1402bd5e15fdSBram Moolenaar PyObject *mod; 1403bd5e15fdSBram Moolenaar /* The special value is removed from sys.path in Python3_Init(). */ 1404bd5e15fdSBram Moolenaar static wchar_t *(argv[2]) = {L"/must>not&exist/foo", NULL}; 1405bd5e15fdSBram Moolenaar 1406bd5e15fdSBram Moolenaar PyType_Ready(&BufferType); 1407bd5e15fdSBram Moolenaar PyType_Ready(&RangeType); 1408bd5e15fdSBram Moolenaar PyType_Ready(&WindowType); 1409bd5e15fdSBram Moolenaar PyType_Ready(&BufListType); 1410bd5e15fdSBram Moolenaar PyType_Ready(&WinListType); 1411bd5e15fdSBram Moolenaar PyType_Ready(&CurrentType); 1412bd5e15fdSBram Moolenaar 1413bd5e15fdSBram Moolenaar /* Set sys.argv[] to avoid a crash in warn(). */ 1414bd5e15fdSBram Moolenaar PySys_SetArgv(1, argv); 1415bd5e15fdSBram Moolenaar 1416bd5e15fdSBram Moolenaar mod = PyModule_Create(&vimmodule); 141719e60943SBram Moolenaar if (mod == NULL) 141819e60943SBram Moolenaar return NULL; 1419bd5e15fdSBram Moolenaar 142019e60943SBram Moolenaar VimError = PyErr_NewException("vim.error", NULL, NULL); 142119e60943SBram Moolenaar Py_INCREF(VimError); 1422bd5e15fdSBram Moolenaar 1423bd5e15fdSBram Moolenaar PyModule_AddObject(mod, "error", VimError); 1424bd5e15fdSBram Moolenaar Py_INCREF((PyObject *)(void *)&TheBufferList); 1425bd5e15fdSBram Moolenaar PyModule_AddObject(mod, "buffers", (PyObject *)(void *)&TheBufferList); 1426bd5e15fdSBram Moolenaar Py_INCREF((PyObject *)(void *)&TheCurrent); 1427bd5e15fdSBram Moolenaar PyModule_AddObject(mod, "current", (PyObject *)(void *)&TheCurrent); 1428bd5e15fdSBram Moolenaar Py_INCREF((PyObject *)(void *)&TheWindowList); 1429bd5e15fdSBram Moolenaar PyModule_AddObject(mod, "windows", (PyObject *)(void *)&TheWindowList); 1430bd5e15fdSBram Moolenaar 1431bd5e15fdSBram Moolenaar if (PyErr_Occurred()) 1432bd5e15fdSBram Moolenaar return NULL; 1433bd5e15fdSBram Moolenaar 1434bd5e15fdSBram Moolenaar return mod; 1435bd5e15fdSBram Moolenaar } 143669154f22SBram Moolenaar #endif 1437bd5e15fdSBram Moolenaar 1438bd5e15fdSBram Moolenaar /************************************************************************* 1439bd5e15fdSBram Moolenaar * 4. Utility functions for handling the interface between Vim and Python. 1440bd5e15fdSBram Moolenaar */ 1441bd5e15fdSBram Moolenaar 1442bd5e15fdSBram Moolenaar /* Convert a Vim line into a Python string. 1443bd5e15fdSBram Moolenaar * All internal newlines are replaced by null characters. 1444bd5e15fdSBram Moolenaar * 1445bd5e15fdSBram Moolenaar * On errors, the Python exception data is set, and NULL is returned. 1446bd5e15fdSBram Moolenaar */ 1447170bf1aeSBram Moolenaar static PyObject * 1448170bf1aeSBram Moolenaar LineToString(const char *str) 1449bd5e15fdSBram Moolenaar { 1450bd5e15fdSBram Moolenaar PyObject *result; 1451bd5e15fdSBram Moolenaar Py_ssize_t len = strlen(str); 1452bd5e15fdSBram Moolenaar char *tmp,*p; 1453bd5e15fdSBram Moolenaar 1454bd5e15fdSBram Moolenaar tmp = (char *)alloc((unsigned)(len+1)); 1455bd5e15fdSBram Moolenaar p = tmp; 1456bd5e15fdSBram Moolenaar if (p == NULL) 1457bd5e15fdSBram Moolenaar { 1458bd5e15fdSBram Moolenaar PyErr_NoMemory(); 1459bd5e15fdSBram Moolenaar return NULL; 1460bd5e15fdSBram Moolenaar } 1461bd5e15fdSBram Moolenaar 1462bd5e15fdSBram Moolenaar while (*str) 1463bd5e15fdSBram Moolenaar { 1464bd5e15fdSBram Moolenaar if (*str == '\n') 1465bd5e15fdSBram Moolenaar *p = '\0'; 1466bd5e15fdSBram Moolenaar else 1467bd5e15fdSBram Moolenaar *p = *str; 1468bd5e15fdSBram Moolenaar 1469bd5e15fdSBram Moolenaar ++p; 1470bd5e15fdSBram Moolenaar ++str; 1471bd5e15fdSBram Moolenaar } 1472bd5e15fdSBram Moolenaar *p = '\0'; 1473bd5e15fdSBram Moolenaar 1474*3d64a317SBram Moolenaar result = PyUnicode_Decode(tmp, len, (char *)ENC_OPT, CODEC_ERROR_HANDLER); 1475bd5e15fdSBram Moolenaar 1476bd5e15fdSBram Moolenaar vim_free(tmp); 1477bd5e15fdSBram Moolenaar return result; 1478bd5e15fdSBram Moolenaar } 1479bd5e15fdSBram Moolenaar 1480170bf1aeSBram Moolenaar static void 1481170bf1aeSBram Moolenaar init_structs(void) 1482bd5e15fdSBram Moolenaar { 1483bd5e15fdSBram Moolenaar vim_memset(&OutputType, 0, sizeof(OutputType)); 1484bd5e15fdSBram Moolenaar OutputType.tp_name = "vim.message"; 1485bd5e15fdSBram Moolenaar OutputType.tp_basicsize = sizeof(OutputObject); 1486bd5e15fdSBram Moolenaar OutputType.tp_getattro = OutputGetattro; 1487bd5e15fdSBram Moolenaar OutputType.tp_setattro = OutputSetattro; 1488bd5e15fdSBram Moolenaar OutputType.tp_flags = Py_TPFLAGS_DEFAULT; 1489bd5e15fdSBram Moolenaar OutputType.tp_doc = "vim message object"; 1490bd5e15fdSBram Moolenaar OutputType.tp_methods = OutputMethods; 1491bd5e15fdSBram Moolenaar OutputType.tp_alloc = call_PyType_GenericAlloc; 1492bd5e15fdSBram Moolenaar OutputType.tp_new = call_PyType_GenericNew; 1493bd5e15fdSBram Moolenaar OutputType.tp_free = call_PyObject_Free; 1494bd5e15fdSBram Moolenaar 1495bd5e15fdSBram Moolenaar vim_memset(&BufferType, 0, sizeof(BufferType)); 1496bd5e15fdSBram Moolenaar BufferType.tp_name = "vim.buffer"; 1497bd5e15fdSBram Moolenaar BufferType.tp_basicsize = sizeof(BufferType); 1498bd5e15fdSBram Moolenaar BufferType.tp_dealloc = BufferDestructor; 1499bd5e15fdSBram Moolenaar BufferType.tp_repr = BufferRepr; 1500bd5e15fdSBram Moolenaar BufferType.tp_as_sequence = &BufferAsSeq; 1501bd5e15fdSBram Moolenaar BufferType.tp_as_mapping = &BufferAsMapping; 1502bd5e15fdSBram Moolenaar BufferType.tp_getattro = BufferGetattro; 1503bd5e15fdSBram Moolenaar BufferType.tp_flags = Py_TPFLAGS_DEFAULT; 1504bd5e15fdSBram Moolenaar BufferType.tp_doc = "vim buffer object"; 1505bd5e15fdSBram Moolenaar BufferType.tp_methods = BufferMethods; 1506bd5e15fdSBram Moolenaar BufferType.tp_alloc = call_PyType_GenericAlloc; 1507bd5e15fdSBram Moolenaar BufferType.tp_new = call_PyType_GenericNew; 1508bd5e15fdSBram Moolenaar BufferType.tp_free = call_PyObject_Free; 1509bd5e15fdSBram Moolenaar 151055d5c034SBram Moolenaar vim_memset(&WindowType, 0, sizeof(WindowType)); 151155d5c034SBram Moolenaar WindowType.tp_name = "vim.window"; 151255d5c034SBram Moolenaar WindowType.tp_basicsize = sizeof(WindowObject); 151355d5c034SBram Moolenaar WindowType.tp_dealloc = WindowDestructor; 151455d5c034SBram Moolenaar WindowType.tp_repr = WindowRepr; 151555d5c034SBram Moolenaar WindowType.tp_getattro = WindowGetattro; 151655d5c034SBram Moolenaar WindowType.tp_setattro = WindowSetattro; 151755d5c034SBram Moolenaar WindowType.tp_flags = Py_TPFLAGS_DEFAULT; 151855d5c034SBram Moolenaar WindowType.tp_doc = "vim Window object"; 151955d5c034SBram Moolenaar WindowType.tp_methods = WindowMethods; 152055d5c034SBram Moolenaar WindowType.tp_alloc = call_PyType_GenericAlloc; 152155d5c034SBram Moolenaar WindowType.tp_new = call_PyType_GenericNew; 152255d5c034SBram Moolenaar WindowType.tp_free = call_PyObject_Free; 152355d5c034SBram Moolenaar 1524bd5e15fdSBram Moolenaar vim_memset(&BufListType, 0, sizeof(BufListType)); 1525bd5e15fdSBram Moolenaar BufListType.tp_name = "vim.bufferlist"; 1526bd5e15fdSBram Moolenaar BufListType.tp_basicsize = sizeof(BufListObject); 1527bd5e15fdSBram Moolenaar BufListType.tp_as_sequence = &BufListAsSeq; 1528bd5e15fdSBram Moolenaar BufListType.tp_flags = Py_TPFLAGS_DEFAULT; 1529bd5e15fdSBram Moolenaar BufferType.tp_doc = "vim buffer list"; 1530bd5e15fdSBram Moolenaar 1531bd5e15fdSBram Moolenaar vim_memset(&WinListType, 0, sizeof(WinListType)); 1532bd5e15fdSBram Moolenaar WinListType.tp_name = "vim.windowlist"; 1533bd5e15fdSBram Moolenaar WinListType.tp_basicsize = sizeof(WinListType); 1534bd5e15fdSBram Moolenaar WinListType.tp_as_sequence = &WinListAsSeq; 1535bd5e15fdSBram Moolenaar WinListType.tp_flags = Py_TPFLAGS_DEFAULT; 1536bd5e15fdSBram Moolenaar WinListType.tp_doc = "vim window list"; 1537bd5e15fdSBram Moolenaar 1538bd5e15fdSBram Moolenaar vim_memset(&RangeType, 0, sizeof(RangeType)); 1539bd5e15fdSBram Moolenaar RangeType.tp_name = "vim.range"; 1540bd5e15fdSBram Moolenaar RangeType.tp_basicsize = sizeof(RangeObject); 1541bd5e15fdSBram Moolenaar RangeType.tp_dealloc = RangeDestructor; 1542bd5e15fdSBram Moolenaar RangeType.tp_repr = RangeRepr; 1543bd5e15fdSBram Moolenaar RangeType.tp_as_sequence = &RangeAsSeq; 1544bd5e15fdSBram Moolenaar RangeType.tp_as_mapping = &RangeAsMapping; 1545bd5e15fdSBram Moolenaar RangeType.tp_getattro = RangeGetattro; 1546bd5e15fdSBram Moolenaar RangeType.tp_flags = Py_TPFLAGS_DEFAULT; 1547bd5e15fdSBram Moolenaar RangeType.tp_doc = "vim Range object"; 1548bd5e15fdSBram Moolenaar RangeType.tp_methods = RangeMethods; 1549bd5e15fdSBram Moolenaar RangeType.tp_alloc = call_PyType_GenericAlloc; 1550bd5e15fdSBram Moolenaar RangeType.tp_new = call_PyType_GenericNew; 1551bd5e15fdSBram Moolenaar RangeType.tp_free = call_PyObject_Free; 1552bd5e15fdSBram Moolenaar 1553bd5e15fdSBram Moolenaar vim_memset(&CurrentType, 0, sizeof(CurrentType)); 1554bd5e15fdSBram Moolenaar CurrentType.tp_name = "vim.currentdata"; 1555bd5e15fdSBram Moolenaar CurrentType.tp_basicsize = sizeof(CurrentObject); 1556bd5e15fdSBram Moolenaar CurrentType.tp_getattro = CurrentGetattro; 1557bd5e15fdSBram Moolenaar CurrentType.tp_setattro = CurrentSetattro; 1558bd5e15fdSBram Moolenaar CurrentType.tp_flags = Py_TPFLAGS_DEFAULT; 1559bd5e15fdSBram Moolenaar CurrentType.tp_doc = "vim current object"; 1560bd5e15fdSBram Moolenaar 1561bd5e15fdSBram Moolenaar vim_memset(&vimmodule, 0, sizeof(vimmodule)); 1562bd5e15fdSBram Moolenaar vimmodule.m_name = "vim"; 1563bd5e15fdSBram Moolenaar vimmodule.m_doc = vim_module_doc; 1564bd5e15fdSBram Moolenaar vimmodule.m_size = -1; 1565bd5e15fdSBram Moolenaar vimmodule.m_methods = VimMethods; 1566bd5e15fdSBram Moolenaar } 1567