static int Python_Init(void) { if (!initialised) { #ifdef DYNAMIC_PYTHON if (!python_enabled(TRUE)) { EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); goto fail; } #endif #ifdef PYTHON_HOME Py_SetPythonHome(PYTHON_HOME); #endif init_structs(); #if !defined(MACOS) || defined(MACOS_X_UNIX) Py_Initialize(); #else PyMac_Initialize(); #endif /* initialise threads */ PyEval_InitThreads(); #ifdef DYNAMIC_PYTHON get_exceptions(); #endif if (PythonIO_Init()) goto fail; if (PythonMod_Init()) goto fail; /* Remove the element from sys.path that was added because of our * argv[0] value in PythonMod_Init(). Previously we used an empty * string, but dependinding on the OS we then get an empty entry or * the current directory in sys.path. */ PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); /* the first python thread is vim's, release the lock */ Python_SaveThread(); initialised = 1; } return 0; fail: /* We call PythonIO_Flush() here to print any Python errors. * This is OK, as it is possible to call this function even * if PythonIO_Init() has not completed successfully (it will * not do anything in this case). */ PythonIO_Flush(); return -1; }
/* * External interface */ static void DoPy3Command(exarg_T *eap, const char *cmd) { #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GetPort(&oldPort); /* Check if the Python library is available */ if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python3_Init()) goto theend; RangeStart = eap->line1; RangeEnd = eap->line2; Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *)vim_strsave((char_u *)saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif pygilstate = PyGILState_Ensure(); PyRun_SimpleString((char *)(cmd)); PyGILState_Release(pygilstate); #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); vim_free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) SetPort(oldPort); #endif theend: return; /* keeps lint happy */ }
/* * External interface */ static void DoPythonCommand(exarg_T *eap, const char *cmd) { #ifndef PY_CAN_RECURSE static int recursive = 0; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif #ifndef PY_CAN_RECURSE if (recursive) { EMSG(_("E659: Cannot invoke Python recursively")); return; } ++recursive; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GetPort(&oldPort); /* Check if the Python library is available */ if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python_Init()) goto theend; RangeStart = eap->line1; RangeEnd = eap->line2; Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *)vim_strsave((char_u *)saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif Python_RestoreThread(); /* enter python */ PyRun_SimpleString((char *)(cmd)); Python_SaveThread(); /* leave python */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); vim_free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) SetPort(oldPort); #endif theend: #ifndef PY_CAN_RECURSE --recursive; #endif return; /* keeps lint happy */ }
static int Python3_Init(void) { if (!py3initialised) { #ifdef DYNAMIC_PYTHON3 if (!python3_enabled(TRUE)) { EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); goto fail; } #endif init_structs(); /* initialise threads */ PyEval_InitThreads(); #if !defined(MACOS) || defined(MACOS_X_UNIX) Py_Initialize(); #else PyMac_Initialize(); #endif #ifdef DYNAMIC_PYTHON3 get_py3_exceptions(); #endif if (PythonIO_Init()) goto fail; PyImport_AppendInittab("vim", Py3Init_vim); /* Remove the element from sys.path that was added because of our * argv[0] value in Py3Init_vim(). Previously we used an empty * string, but dependinding on the OS we then get an empty entry or * the current directory in sys.path. */ PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))"); // lock is created and acquired in PyEval_InitThreads() and thread // state is created in Py_Initialize() // there _PyGILState_NoteThreadState() also sets gilcounter to 1 // (python must have threads enabled!) // so the following does both: unlock GIL and save thread state in TLS // without deleting thread state PyGILState_Release(pygilstate); py3initialised = 1; } return 0; fail: /* We call PythonIO_Flush() here to print any Python errors. * This is OK, as it is possible to call this function even * if PythonIO_Init() has not completed successfully (it will * not do anything in this case). */ PythonIO_Flush(); return -1; }
static int Python_Init(void) { if (!initialised) { #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 PyObject *site; #endif #ifdef DYNAMIC_PYTHON if (!python_enabled(TRUE)) { EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); goto fail; } #endif #ifdef PYTHON_HOME # ifdef DYNAMIC_PYTHON if (mch_getenv((char_u *)"PYTHONHOME") == NULL) # endif Py_SetPythonHome(PYTHON_HOME); #endif init_structs(); #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 /* Disable implicit 'import site', because it may cause Vim to exit * when it can't be found. */ Py_NoSiteFlag++; #endif #if !defined(MACOS) || defined(MACOS_X_UNIX) Py_Initialize(); #else PyMac_Initialize(); #endif #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x02070000 /* 'import site' explicitly. */ site = PyImport_ImportModule("site"); if (site == NULL) { EMSG(_("E887: Sorry, this command is disabled, the Python's site module could not be loaded.")); goto fail; } Py_DECREF(site); #endif /* Initialise threads, and below save the state using * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread * specific state (such as the system trace hook), will be lost * between invocations of Python code. */ PyEval_InitThreads(); #ifdef DYNAMIC_PYTHON get_exceptions(); #endif if (PythonIO_Init_io()) goto fail; if (PythonMod_Init()) goto fail; globals = PyModule_GetDict(PyImport_AddModule("__main__")); /* Remove the element from sys.path that was added because of our * argv[0] value in PythonMod_Init(). Previously we used an empty * string, but depending on the OS we then get an empty entry or * the current directory in sys.path. */ PyRun_SimpleString("import sys; sys.path = filter(lambda x: x != '/must>not&exist', sys.path)"); /* lock is created and acquired in PyEval_InitThreads() and thread * state is created in Py_Initialize() * there _PyGILState_NoteThreadState() also sets gilcounter to 1 * (python must have threads enabled!) * so the following does both: unlock GIL and save thread state in TLS * without deleting thread state */ #ifndef PY_CAN_RECURSE saved_python_thread = #endif PyEval_SaveThread(); initialised = 1; } return 0; fail: /* We call PythonIO_Flush() here to print any Python errors. * This is OK, as it is possible to call this function even * if PythonIO_Init_io() has not completed successfully (it will * not do anything in this case). */ PythonIO_Flush(); return -1; }
/* * External interface */ static void DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) { #ifndef PY_CAN_RECURSE static int recursive = 0; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif #ifdef PY_CAN_RECURSE PyGILState_STATE pygilstate; #endif #ifndef PY_CAN_RECURSE if (recursive) { EMSG(_("E659: Cannot invoke Python recursively")); return; } ++recursive; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GetPort(&oldPort); /* Check if the Python library is available */ if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python_Init()) goto theend; init_range(arg); Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *) PY_STRSAVE(saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif #ifdef PY_CAN_RECURSE pygilstate = PyGILState_Ensure(); #else Python_RestoreThread(); /* enter python */ #endif run((char *) cmd, arg #ifdef PY_CAN_RECURSE , &pygilstate #endif ); #ifdef PY_CAN_RECURSE PyGILState_Release(pygilstate); #else Python_SaveThread(); /* leave python */ #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); PyMem_Free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) SetPort(oldPort); #endif theend: #ifndef PY_CAN_RECURSE --recursive; #endif return; }
/* * External interface */ static void DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv) { #ifndef PY_CAN_RECURSE static int recursive = 0; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif #ifndef PY_CAN_RECURSE if (recursive) { EMSG(_("E659: Cannot invoke Python recursively")); return; } ++recursive; #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) GetPort(&oldPort); /* Check if the Python library is available */ if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python_Init()) goto theend; if (rettv == NULL) { RangeStart = eap->line1; RangeEnd = eap->line2; } else { RangeStart = (PyInt) curwin->w_cursor.lnum; RangeEnd = RangeStart; } Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *)vim_strsave((char_u *)saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif Python_RestoreThread(); /* enter python */ if (rettv == NULL) PyRun_SimpleString((char *)(cmd)); else { PyObject *r; r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals); if (r == NULL) { if (PyErr_Occurred() && !msg_silent) PyErr_PrintEx(0); EMSG(_("E858: Eval did not return a valid python object")); } else { if (ConvertFromPyObject(r, rettv) == -1) EMSG(_("E859: Failed to convert returned python object to vim value")); Py_DECREF(r); } PyErr_Clear(); } Python_SaveThread(); /* leave python */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); vim_free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) SetPort(oldPort); #endif theend: #ifndef PY_CAN_RECURSE --recursive; #endif return; }
/* * External interface */ static void DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) { #if defined(MACOS) && !defined(MACOS_X_UNIX) GrafPtr oldPort; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif PyObject *cmdstr; PyObject *cmdbytes; PyGILState_STATE pygilstate; #if defined(MACOS) && !defined(MACOS_X_UNIX) GetPort(&oldPort); /* Check if the Python library is available */ if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python3_Init()) goto theend; init_range(arg); Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *)vim_strsave((char_u *)saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif pygilstate = PyGILState_Ensure(); /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause * SyntaxError (unicode error). */ cmdstr = PyUnicode_Decode(cmd, strlen(cmd), (char *)ENC_OPT, CODEC_ERROR_HANDLER); cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER); Py_XDECREF(cmdstr); run(PyBytes_AsString(cmdbytes), arg, &pygilstate); Py_XDECREF(cmdbytes); PyGILState_Release(pygilstate); #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); vim_free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) SetPort(oldPort); #endif theend: return; /* keeps lint happy */ }
static int Python3_Init(void) { if (!py3initialised) { #ifdef DYNAMIC_PYTHON3 if (!python3_enabled(TRUE)) { EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded.")); goto fail; } #endif init_structs(); #ifdef PYTHON3_HOME Py_SetPythonHome(PYTHON3_HOME); #endif PyImport_AppendInittab("vim", Py3Init_vim); #if !defined(MACOS) || defined(MACOS_X_UNIX) Py_Initialize(); #else PyMac_Initialize(); #endif /* Initialise threads, and below save the state using * PyEval_SaveThread. Without the call to PyEval_SaveThread, thread * specific state (such as the system trace hook), will be lost * between invocations of Python code. */ PyEval_InitThreads(); #ifdef DYNAMIC_PYTHON3 get_py3_exceptions(); #endif if (PythonIO_Init_io()) goto fail; globals = PyModule_GetDict(PyImport_AddModule("__main__")); /* Remove the element from sys.path that was added because of our * argv[0] value in Py3Init_vim(). Previously we used an empty * string, but depending on the OS we then get an empty entry or * the current directory in sys.path. * Only after vim has been imported, the element does exist in * sys.path. */ PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))"); /* lock is created and acquired in PyEval_InitThreads() and thread * state is created in Py_Initialize() * there _PyGILState_NoteThreadState() also sets gilcounter to 1 * (python must have threads enabled!) * so the following does both: unlock GIL and save thread state in TLS * without deleting thread state */ PyEval_SaveThread(); py3initialised = 1; } return 0; fail: /* We call PythonIO_Flush() here to print any Python errors. * This is OK, as it is possible to call this function even * if PythonIO_Init_io() has not completed successfully (it will * not do anything in this case). */ PythonIO_Flush(); return -1; }
/* * External interface */ static void DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg) { #ifndef PY_CAN_RECURSE static int recursive = 0; #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) char *saved_locale; #endif #ifdef PY_CAN_RECURSE PyGILState_STATE pygilstate; #endif #ifndef PY_CAN_RECURSE if (recursive) { emsg(_("E659: Cannot invoke Python recursively")); return; } ++recursive; #endif if (python_end_called) return; if (Python_Init()) goto theend; init_range(arg); Python_Release_Vim(); /* leave vim */ #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) /* Python only works properly when the LC_NUMERIC locale is "C". */ saved_locale = setlocale(LC_NUMERIC, NULL); if (saved_locale == NULL || STRCMP(saved_locale, "C") == 0) saved_locale = NULL; else { /* Need to make a copy, value may change when setting new locale. */ saved_locale = (char *) PY_STRSAVE(saved_locale); (void)setlocale(LC_NUMERIC, "C"); } #endif #ifdef PY_CAN_RECURSE pygilstate = PyGILState_Ensure(); #else Python_RestoreThread(); /* enter python */ #endif run((char *) cmd, arg #ifdef PY_CAN_RECURSE , &pygilstate #endif ); #ifdef PY_CAN_RECURSE PyGILState_Release(pygilstate); #else Python_SaveThread(); /* leave python */ #endif #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) if (saved_locale != NULL) { (void)setlocale(LC_NUMERIC, saved_locale); PyMem_Free(saved_locale); } #endif Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); theend: #ifndef PY_CAN_RECURSE --recursive; #endif return; }