[llvm-branch-commits] [lldb] r270098 - Fix an issue where scripted commands would not actually print any of their output if an immediate output file was set in the result object via a Python file object

Francis Ricci via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu May 19 13:02:47 PDT 2016


Author: fjricci
Date: Thu May 19 15:02:44 2016
New Revision: 270098

URL: http://llvm.org/viewvc/llvm-project?rev=270098&view=rev
Log:
Fix an issue where scripted commands would not actually print any of their output if an immediate output file was set in the result object via a Python file object

Fixes rdar://24130303

Added:
    lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/
    lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/TestCommandScriptImmediateOutput.py
    lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/custom_command.py
Modified:
    lldb/branches/release_38/include/lldb/Host/File.h
    lldb/branches/release_38/scripts/Python/python-typemaps.swig
    lldb/branches/release_38/source/Host/common/File.cpp
    lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h

Modified: lldb/branches/release_38/include/lldb/Host/File.h
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/include/lldb/Host/File.h?rev=270098&r1=270097&r2=270098&view=diff
==============================================================================
--- lldb/branches/release_38/include/lldb/Host/File.h (original)
+++ lldb/branches/release_38/include/lldb/Host/File.h Thu May 19 15:02:44 2016
@@ -223,6 +223,9 @@ public:
     Error
     Close() override;
     
+    void
+    Clear ();
+    
     Error
     Duplicate (const File &rhs);
 

Added: lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/TestCommandScriptImmediateOutput.py
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/TestCommandScriptImmediateOutput.py?rev=270098&view=auto
==============================================================================
--- lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/TestCommandScriptImmediateOutput.py (added)
+++ lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/TestCommandScriptImmediateOutput.py Thu May 19 15:02:44 2016
@@ -0,0 +1,37 @@
+"""
+Test that LLDB correctly allows scripted commands to set an immediate output file
+"""
+
+from __future__ import print_function
+
+
+
+import os, time
+import lldb
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test.lldbpexpect import *
+
+class CommandScriptImmediateOutputTestCase (PExpectTest):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def setUp(self):
+        # Call super's setUp().
+        PExpectTest.setUp(self)
+
+    @skipIfRemote # test not remote-ready llvm.org/pr24813
+    @expectedFlakeyFreeBSD("llvm.org/pr25172 fails rarely on the buildbot")
+    @expectedFlakeyLinux("llvm.org/pr25172")
+    @expectedFailureWindows("llvm.org/pr22274: need a pexpect replacement for windows")
+    def test_command_script_immediate_output (self):
+        """Test that LLDB correctly allows scripted commands to set an immediate output file."""
+        self.launch(timeout=5)
+
+        script = os.path.join(os.getcwd(), 'custom_command.py')
+        prompt = "(lldb)"
+        
+        self.sendline('command script import %s' % script, patterns=[prompt])
+        self.sendline('command script add -f custom_command.command_function mycommand', patterns=[prompt])
+        self.sendline('mycommand', patterns='this is a test string, just a test string')
+        self.sendline('command script delete mycommand', patterns=[prompt])
+        self.quit(gracefully=False)

Added: lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/custom_command.py
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/custom_command.py?rev=270098&view=auto
==============================================================================
--- lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/custom_command.py (added)
+++ lldb/branches/release_38/packages/Python/lldbsuite/test/functionalities/command_script_immediate_output/custom_command.py Thu May 19 15:02:44 2016
@@ -0,0 +1,8 @@
+from __future__ import print_function
+
+import sys
+
+def command_function(debugger, command, exe_ctx, result, internal_dict):
+        result.SetImmediateOutputFile(sys.__stdout__)
+        print('this is a test string, just a test string', file=result)
+

Modified: lldb/branches/release_38/scripts/Python/python-typemaps.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/scripts/Python/python-typemaps.swig?rev=270098&r1=270097&r2=270098&view=diff
==============================================================================
--- lldb/branches/release_38/scripts/Python/python-typemaps.swig (original)
+++ lldb/branches/release_38/scripts/Python/python-typemaps.swig Thu May 19 15:02:44 2016
@@ -545,7 +545,9 @@
          return nullptr;
 
       $1 = file.GetStream();
-   }
+      if ($1)
+         file.Clear();
+    }
 }
 
 %typemap(out) FILE * {

Modified: lldb/branches/release_38/source/Host/common/File.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/source/Host/common/File.cpp?rev=270098&r1=270097&r2=270098&view=diff
==============================================================================
--- lldb/branches/release_38/source/Host/common/File.cpp (original)
+++ lldb/branches/release_38/source/Host/common/File.cpp Thu May 19 15:02:44 2016
@@ -399,6 +399,15 @@ File::Close ()
     return error;
 }
 
+void
+File::Clear ()
+{
+    m_stream = nullptr;
+    m_descriptor = -1;
+    m_options = 0;
+    m_own_stream = false;
+    m_is_interactive = m_supports_colors = m_is_real_terminal = eLazyBoolCalculate;
+}
 
 Error
 File::GetFileSpec (FileSpec &file_spec) const

Modified: lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp?rev=270098&r1=270097&r2=270098&view=diff
==============================================================================
--- lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp (original)
+++ lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp Thu May 19 15:02:44 2016
@@ -23,6 +23,8 @@
 
 #include <stdio.h>
 
+#include "llvm/ADT/StringSwitch.h"
+
 using namespace lldb_private;
 using namespace lldb;
 
@@ -1156,6 +1158,22 @@ PythonFile::Reset(File &file, const char
 #endif
 }
 
+uint32_t
+PythonFile::GetOptionsFromMode(llvm::StringRef mode)
+{
+    if (mode.empty())
+        return 0;
+
+    return llvm::StringSwitch<uint32_t>(mode.str().c_str())
+    .Case("r",   File::eOpenOptionRead)
+    .Case("w",   File::eOpenOptionWrite)
+    .Case("a",   File::eOpenOptionAppend|File::eOpenOptionCanCreate)
+    .Case("r+",  File::eOpenOptionRead|File::eOpenOptionWrite)
+    .Case("w+",  File::eOpenOptionRead|File::eOpenOptionWrite|File::eOpenOptionCanCreate|File::eOpenOptionTruncate)
+    .Case("a+",  File::eOpenOptionRead|File::eOpenOptionWrite|File::eOpenOptionCanCreate)
+    .Default(0);
+}
+
 bool
 PythonFile::GetUnderlyingFile(File &file) const
 {
@@ -1166,6 +1184,8 @@ PythonFile::GetUnderlyingFile(File &file
     // We don't own the file descriptor returned by this function, make sure the
     // File object knows about that.
     file.SetDescriptor(PyObject_AsFileDescriptor(m_py_obj), false);
+    PythonString py_mode = GetAttributeValue("mode").AsType<PythonString>();
+    file.SetOptions(PythonFile::GetOptionsFromMode(py_mode.GetString()));
     return file.IsValid();
 }
 

Modified: lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h?rev=270098&r1=270097&r2=270098&view=diff
==============================================================================
--- lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h (original)
+++ lldb/branches/release_38/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h Thu May 19 15:02:44 2016
@@ -525,6 +525,8 @@ class PythonFile : public PythonObject
     void Reset(PyRefType type, PyObject *py_obj) override;
     void Reset(File &file, const char *mode);
 
+    static uint32_t GetOptionsFromMode(llvm::StringRef mode);
+    
     bool GetUnderlyingFile(File &file) const;
 };
 




More information about the llvm-branch-commits mailing list