[Lldb-commits] [lldb] [lldb] Set return status to Failed when Python command raises uncaught exception (PR #113996)

via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 28 20:32:49 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/113996.diff


3 Files Affected:

- (modified) lldb/bindings/python/python-wrapper.swig (+11-2) 
- (added) lldb/test/API/commands/command/script/exception/TestCommandException.py (+27) 
- (added) lldb/test/API/commands/command/script/exception/throw_command.py (+6) 


``````````diff
diff --git a/lldb/bindings/python/python-wrapper.swig b/lldb/bindings/python/python-wrapper.swig
index b72a462d04643b..dfe762e026788a 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -592,6 +592,8 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyOb
   return sb_ptr;
 }
 
+#include "lldb/Interpreter/CommandReturnObject.h"
+
 bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
     const char *python_function_name, const char *session_dictionary_name,
     lldb::DebuggerSP debugger, const char *args,
@@ -621,6 +623,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
     pfunc(debugger_arg, PythonString(args),
           SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
 
+  if (PyErr_Occurred())
+    cmd_retobj.SetStatus(eReturnStatusFailed);
+
   return true;
 }
 
@@ -642,6 +647,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject(
   pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
         SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
 
+  if (PyErr_Occurred())
+    cmd_retobj.SetStatus(eReturnStatusFailed);
+
   return true;
 }
 
@@ -740,8 +748,6 @@ lldb_private::python::SWIGBridge::LLDBSwigPythonHandleOptionArgumentCompletionFo
   return dict_sp;
 }
 
-#include "lldb/Interpreter/CommandReturnObject.h"
-
 bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject(
     PyObject *implementor, lldb::DebuggerSP debugger, lldb_private::StructuredDataImpl &args_impl,
     lldb_private::CommandReturnObject &cmd_retobj,
@@ -760,6 +766,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject(
   pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), SWIGBridge::ToSWIGWrapper(args_impl),
         SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), SWIGBridge::ToSWIGWrapper(cmd_retobj).obj());
 
+  if (PyErr_Occurred())
+    cmd_retobj.SetStatus(eReturnStatusFailed);
+
   return true;
 }
 
diff --git a/lldb/test/API/commands/command/script/exception/TestCommandException.py b/lldb/test/API/commands/command/script/exception/TestCommandException.py
new file mode 100644
index 00000000000000..73484137d82b3e
--- /dev/null
+++ b/lldb/test/API/commands/command/script/exception/TestCommandException.py
@@ -0,0 +1,27 @@
+import os
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class TestCase(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def test(self):
+        """
+        Check that a Python command, which raises an unhandled exception, has
+        its status set to failed.
+        """
+        command_path = os.path.join(self.getSourceDir(), "throw_command.py")
+        self.runCmd(f"command script import {command_path}")
+
+        with open(os.devnull, "w") as devnull:
+            self.dbg.SetErrorFileHandle(devnull, False)
+            result = lldb.SBCommandReturnObject()
+            self.ci.HandleCommand("throw", result)
+
+        self.assertEqual(
+            result.GetStatus(),
+            lldb.eReturnStatusFailed,
+            "command unexpectedly succeeded",
+        )
diff --git a/lldb/test/API/commands/command/script/exception/throw_command.py b/lldb/test/API/commands/command/script/exception/throw_command.py
new file mode 100644
index 00000000000000..7c4989850cb19a
--- /dev/null
+++ b/lldb/test/API/commands/command/script/exception/throw_command.py
@@ -0,0 +1,6 @@
+import lldb
+
+
+ at lldb.command()
+def throw(debugger, cmd, ctx, result, _):
+    raise Exception("command failed")

``````````

</details>


https://github.com/llvm/llvm-project/pull/113996


More information about the lldb-commits mailing list