[Lldb-commits] [lldb] r263161 - Fixed the python interpreter so that it correctly inherits the top IOHandler's files instead of always using stdin/out/err.

Greg Clayton via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 10 12:49:32 PST 2016


Author: gclayton
Date: Thu Mar 10 14:49:32 2016
New Revision: 263161

URL: http://llvm.org/viewvc/llvm-project?rev=263161&view=rev
Log:
Fixed the python interpreter so that it correctly inherits the top IOHandler's files instead of always using stdin/out/err.

Removed lldb_private::File::Duplicate() and the copy constructor and the assignment operator that used to duplicate the file handles and made them private so no one uses them. Previously the lldb_private::File::Duplicate() function duplicated files that used file descriptors, (int) but not file streams (FILE *), so the lldb_private::File::Duplicate() function only worked some of the time. No one else excep thee ScriptInterpreterPython was using these functions, so that aren't needed nor desired. Previously every time you would drop into the python interpreter we would duplicate files, and now we avoid this file churn.

<rdar://problem/24877720>


Modified:
    lldb/trunk/include/lldb/Host/File.h
    lldb/trunk/source/Host/common/File.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h

Modified: lldb/trunk/include/lldb/Host/File.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/File.h?rev=263161&r1=263160&r2=263161&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/File.h (original)
+++ lldb/trunk/include/lldb/Host/File.h Thu Mar 10 14:49:32 2016
@@ -74,8 +74,6 @@ public:
     {
     }
 
-    File (const File &rhs);
-    
     //------------------------------------------------------------------
     /// Constructor with path.
     ///
@@ -138,9 +136,6 @@ public:
     //------------------------------------------------------------------
     ~File() override;
 
-    File &
-    operator= (const File &rhs);
-
     bool
     IsValid() const override
     {
@@ -226,9 +221,6 @@ public:
     void
     Clear ();
     
-    Error
-    Duplicate (const File &rhs);
-
     int
     GetDescriptor() const;
 
@@ -557,6 +549,9 @@ protected:
     LazyBool m_is_interactive;
     LazyBool m_is_real_terminal;
     LazyBool m_supports_colors;
+
+private:
+    DISALLOW_COPY_AND_ASSIGN(File);
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/source/Host/common/File.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/File.cpp?rev=263161&r1=263160&r2=263161&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/File.cpp (original)
+++ lldb/trunk/source/Host/common/File.cpp Thu Mar 10 14:49:32 2016
@@ -109,27 +109,6 @@ File::File (const FileSpec& filespec,
     }
 }
 
-File::File (const File &rhs) :
-    IOObject(eFDTypeFile, false),
-    m_descriptor (kInvalidDescriptor),
-    m_stream (kInvalidStream),
-    m_options (0),
-    m_own_stream (false),
-    m_is_interactive (eLazyBoolCalculate),
-    m_is_real_terminal (eLazyBoolCalculate)
-{
-    Duplicate (rhs);
-}
-    
-
-File &
-File::operator = (const File &rhs)
-{
-    if (this != &rhs)
-        Duplicate (rhs);        
-    return *this;
-}
-
 File::~File()
 {
     Close ();
@@ -215,7 +194,6 @@ File::GetStream ()
     return m_stream;
 }
 
-
 void
 File::SetStream (FILE *fh, bool transfer_ownership)
 {
@@ -226,35 +204,6 @@ File::SetStream (FILE *fh, bool transfer
 }
 
 Error
-File::Duplicate (const File &rhs)
-{
-    Error error;
-    if (IsValid ())
-        Close();
-
-    if (rhs.DescriptorIsValid())
-    {
-#ifdef _WIN32
-        m_descriptor = ::_dup(rhs.GetDescriptor());
-#else
-        m_descriptor = dup(rhs.GetDescriptor());
-#endif
-        if (!DescriptorIsValid())
-            error.SetErrorToErrno();
-        else
-        {
-            m_options = rhs.m_options;
-            m_should_close_fd = true;
-        }
-    }
-    else
-    {
-        error.SetErrorString ("invalid file to duplicate");
-    }
-    return error;
-}
-
-Error
 File::Open (const char *path, uint32_t options, uint32_t permissions)
 {
     Error error;

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=263161&r1=263160&r2=263161&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Thu Mar 10 14:49:32 2016
@@ -547,6 +547,27 @@ ScriptInterpreterPython::LeaveSession ()
 }
 
 bool
+ScriptInterpreterPython::SetStdHandle(File &file, const char *py_name, PythonFile &save_file, const char *mode)
+{
+    if (file.IsValid())
+    {
+        // Flush the file before giving it to python to avoid interleaved output.
+        file.Flush();
+
+        PythonDictionary &sys_module_dict = GetSysModuleDictionary();
+
+        save_file = sys_module_dict.GetItemForKey(PythonString(py_name)).AsType<PythonFile>();
+
+        PythonFile new_file(file, mode);
+        sys_module_dict.SetItemForKey(PythonString(py_name), new_file);
+        return true;
+    }
+    else
+        save_file.Reset();
+    return false;
+}
+
+bool
 ScriptInterpreterPython::EnterSession (uint16_t on_entry_flags,
                                        FILE *in,
                                        FILE *out,
@@ -604,54 +625,31 @@ ScriptInterpreterPython::EnterSession (u
         if (!in_file.IsValid() || !out_file.IsValid() || !err_file.IsValid())
             m_interpreter.GetDebugger().AdoptTopIOHandlerFilesIfInvalid (in_sp, out_sp, err_sp);
 
-        m_saved_stdin.Reset();
 
-        if ((on_entry_flags & Locker::NoSTDIN) == 0)
+        if (on_entry_flags & Locker::NoSTDIN)
         {
-            // STDIN is enabled
-            if (!in_file.IsValid() && in_sp)
-                in_file = in_sp->GetFile();
-            if (in_file.IsValid())
+            m_saved_stdin.Reset();
+        }
+        else
+        {
+            if (!SetStdHandle(in_file, "stdin", m_saved_stdin, "r"))
             {
-                // Flush the file before giving it to python to avoid interleaved output.
-                in_file.Flush();
-
-                m_saved_stdin = sys_module_dict.GetItemForKey(PythonString("stdin")).AsType<PythonFile>();
-                // This call can deadlock your process if the file is locked
-                PythonFile new_file(in_file, "r");
-                sys_module_dict.SetItemForKey (PythonString("stdin"), new_file);
+                if (in_sp)
+                    SetStdHandle(in_sp->GetFile(), "stdin", m_saved_stdin, "r");
             }
         }
 
-        if (!out_file.IsValid() && out_sp)
-            out_file = out_sp->GetFile();
-        if (out_file.IsValid())
+        if (!SetStdHandle(out_file, "stdout", m_saved_stdout, "w"))
         {
-            // Flush the file before giving it to python to avoid interleaved output.
-            out_file.Flush();
-
-            m_saved_stdout = sys_module_dict.GetItemForKey(PythonString("stdout")).AsType<PythonFile>();
-
-            PythonFile new_file(out_file, "w");
-            sys_module_dict.SetItemForKey (PythonString("stdout"), new_file);
+            if (out_sp)
+                SetStdHandle(out_sp->GetFile(), "stdout", m_saved_stdout, "w");
         }
-        else
-            m_saved_stdout.Reset();
 
-        if (!err_file.IsValid() && err_sp)
-            err_file = err_sp->GetFile();
-        if (err_file.IsValid())
+        if (!SetStdHandle(err_file, "stderr", m_saved_stderr, "w"))
         {
-            // Flush the file before giving it to python to avoid interleaved output.
-            err_file.Flush();
-
-            m_saved_stderr = sys_module_dict.GetItemForKey(PythonString("stderr")).AsType<PythonFile>();
-
-            PythonFile new_file(err_file, "w");
-            sys_module_dict.SetItemForKey (PythonString("stderr"), new_file);
+            if (err_sp)
+                SetStdHandle(err_sp->GetFile(), "stderr", m_saved_stderr, "w");
         }
-        else
-            m_saved_stderr.Reset();
     }
 
     if (PyErr_Occurred())

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h?rev=263161&r1=263160&r2=263161&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h Thu Mar 10 14:49:32 2016
@@ -581,6 +581,9 @@ protected:
     bool
     GetEmbeddedInterpreterModuleObjects ();
 
+    bool
+    SetStdHandle(File &file, const char *py_name, PythonFile &save_file, const char *mode);
+
     PythonFile m_saved_stdin;
     PythonFile m_saved_stdout;
     PythonFile m_saved_stderr;




More information about the lldb-commits mailing list