Patch from Filipe Cabecinhas!
Attached is a small python fix to save the current stout and std err when starting a python session, then diverting them (as it was before), and restoring the previous values afterwards. Otherwise, a python script could suddenly find itself without output.
git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@151693 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Interpreter/ScriptInterpreterPython.cpp b/source/Interpreter/ScriptInterpreterPython.cpp
index c584a81..ccdf748 100644
--- a/source/Interpreter/ScriptInterpreterPython.cpp
+++ b/source/Interpreter/ScriptInterpreterPython.cpp
@@ -303,6 +303,15 @@
void
ScriptInterpreterPython::LeaveSession ()
{
+ PyObject *sysmod = PyImport_AddModule ("sys");
+ PyObject *sysdict = PyModule_GetDict (sysmod);
+
+ if (m_new_sysout && sysmod && sysdict)
+ {
+ PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout);
+ PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_sysout);
+ }
+
m_session_is_active = false;
}
@@ -336,16 +345,18 @@
PyRun_SimpleString (run_string.GetData());
run_string.Clear();
-
+
PyObject *sysmod = PyImport_AddModule ("sys");
PyObject *sysdict = PyModule_GetDict (sysmod);
-
+
if (m_new_sysout && sysmod && sysdict)
{
+ m_old_sysout = PyDict_GetItemString(sysdict, "stdout");
+ m_old_syserr = PyDict_GetItemString(sysdict, "stderr");
PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_new_sysout);
PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_new_sysout);
}
-
+
if (PyErr_Occurred())
PyErr_Clear ();
}