Skip to content

Commit 75f96b8

Browse files
committed
[lldb] Set return status to Failed when Python command raises uncaught exception
1 parent 00ca207 commit 75f96b8

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ void *lldb_private::python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyOb
592592
return sb_ptr;
593593
}
594594

595+
#include "lldb/Interpreter/CommandReturnObject.h"
596+
595597
bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
596598
const char *python_function_name, const char *session_dictionary_name,
597599
lldb::DebuggerSP debugger, const char *args,
@@ -621,6 +623,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommand(
621623
pfunc(debugger_arg, PythonString(args),
622624
SWIGBridge::ToSWIGWrapper(std::move(exe_ctx_ref_sp)), cmd_retobj_arg.obj(), dict);
623625

626+
if (PyErr_Occurred())
627+
cmd_retobj.SetStatus(eReturnStatusFailed);
628+
624629
return true;
625630
}
626631

@@ -642,6 +647,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallCommandObject(
642647
pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), PythonString(args),
643648
SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), cmd_retobj_arg.obj());
644649

650+
if (PyErr_Occurred())
651+
cmd_retobj.SetStatus(eReturnStatusFailed);
652+
645653
return true;
646654
}
647655

@@ -740,8 +748,6 @@ lldb_private::python::SWIGBridge::LLDBSwigPythonHandleOptionArgumentCompletionFo
740748
return dict_sp;
741749
}
742750

743-
#include "lldb/Interpreter/CommandReturnObject.h"
744-
745751
bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject(
746752
PyObject *implementor, lldb::DebuggerSP debugger, lldb_private::StructuredDataImpl &args_impl,
747753
lldb_private::CommandReturnObject &cmd_retobj,
@@ -760,6 +766,9 @@ bool lldb_private::python::SWIGBridge::LLDBSwigPythonCallParsedCommandObject(
760766
pfunc(SWIGBridge::ToSWIGWrapper(std::move(debugger)), SWIGBridge::ToSWIGWrapper(args_impl),
761767
SWIGBridge::ToSWIGWrapper(exe_ctx_ref_sp), SWIGBridge::ToSWIGWrapper(cmd_retobj).obj());
762768

769+
if (PyErr_Occurred())
770+
cmd_retobj.SetStatus(eReturnStatusFailed);
771+
763772
return true;
764773
}
765774

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import lldb
3+
from lldbsuite.test.decorators import *
4+
from lldbsuite.test.lldbtest import *
5+
6+
7+
class TestCase(TestBase):
8+
NO_DEBUG_INFO_TESTCASE = True
9+
10+
def test(self):
11+
"""
12+
Check that a Python command, which raises an unhandled exception, has
13+
its status set to failed.
14+
"""
15+
command_path = os.path.join(self.getSourceDir(), "throw_command.py")
16+
self.runCmd(f"command script import {command_path}")
17+
18+
with open(os.devnull, "w") as devnull:
19+
self.dbg.SetErrorFileHandle(devnull, False)
20+
result = lldb.SBCommandReturnObject()
21+
self.ci.HandleCommand("throw", result)
22+
23+
self.assertEqual(
24+
result.GetStatus(),
25+
lldb.eReturnStatusFailed,
26+
"command unexpectedly succeeded",
27+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import lldb
2+
3+
4+
@lldb.command()
5+
def throw(debugger, cmd, ctx, result, _):
6+
raise Exception("command failed")

0 commit comments

Comments
 (0)