[Lldb-commits] [lldb] r151693 - in /lldb/trunk: include/lldb/Interpreter/ScriptInterpreterPython.h source/Interpreter/ScriptInterpreterPython.cpp

Johnny Chen johnny.chen at apple.com
Tue Feb 28 17:52:13 PST 2012


Author: johnny
Date: Tue Feb 28 19:52:13 2012
New Revision: 151693

URL: http://llvm.org/viewvc/llvm-project?rev=151693&view=rev
Log:
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.

Modified:
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=151693&r1=151692&r2=151693&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Tue Feb 28 19:52:13 2012
@@ -252,6 +252,8 @@
     lldb::InputReaderSP m_embedded_thread_input_reader_sp;
     FILE *m_dbg_stdout;
     void *m_new_sysout; // This is a PyObject.
+    void *m_old_sysout; // This is a PyObject.
+    void *m_old_syserr; // This is a PyObject.
     std::string m_dictionary_name;
     TerminalState m_terminal_state;
     bool m_session_is_active;

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=151693&r1=151692&r2=151693&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Tue Feb 28 19:52:13 2012
@@ -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 ();
 }





More information about the lldb-commits mailing list