Skip to content

Commit 3308c5b

Browse files
committed
Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… (llvm#95181)
…andOverrideCallback (llvm#94518)" This reverts commit 7cff05a. The API test that was added erroneously imports a module that isn't needed and wouldn't be found which causes a test failures. This reversion removes that import. (cherry picked from commit 3d86eeb)
1 parent 6e13b8b commit 3308c5b

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

lldb/bindings/python/python-typemaps.swig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
427427
free($1);
428428
}
429429

430-
431430
// For Log::LogOutputCallback
432431
%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
433432
if (!($input == Py_None ||
@@ -476,6 +475,23 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
476475
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input));
477476
}
478477

478+
%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) {
479+
if (!($input == Py_None ||
480+
PyCallable_Check(reinterpret_cast<PyObject *>($input)))) {
481+
PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
482+
SWIG_fail;
483+
}
484+
485+
// Don't lose the callback reference.
486+
Py_INCREF($input);
487+
$1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback;
488+
$2 = $input;
489+
}
490+
%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) {
491+
$1 = $input == Py_None;
492+
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input));
493+
}
494+
479495
%typemap(in) lldb::FileSP {
480496
PythonFile py_file(PyRefType::Borrowed, $input);
481497
if (!py_file) {

lldb/bindings/python/python-wrapper.swig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,28 @@ static void LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t
11421142
}
11431143
}
11441144

1145+
static bool LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void *baton, const char **argv) {
1146+
bool ret_val = false;
1147+
if (baton != Py_None) {
1148+
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
1149+
// Create a PyList of items since we're going to pass it to the callback as a tuple
1150+
// of arguments.
1151+
PyObject *py_argv = PyList_New(0);
1152+
for (const char **arg = argv; arg && *arg; arg++) {
1153+
std::string arg_string = *arg;
1154+
PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), arg_string.size());
1155+
PyList_Append(py_argv, py_string);
1156+
}
1157+
1158+
PyObject *result = PyObject_CallObject(
1159+
reinterpret_cast<PyObject *>(baton), PyList_AsTuple(py_argv));
1160+
ret_val = result ? PyObject_IsTrue(result) : false;
1161+
Py_XDECREF(result);
1162+
SWIG_PYTHON_THREAD_END_BLOCK;
1163+
}
1164+
return ret_val;
1165+
}
1166+
11451167
static SBError LLDBSwigPythonCallLocateModuleCallback(
11461168
void *callback_baton, const SBModuleSpec &module_spec_sb,
11471169
SBFileSpec &module_file_spec_sb, SBFileSpec &symbol_file_spec_sb) {

lldb/include/lldb/API/SBCommandInterpreter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,9 @@ class SBCommandInterpreter {
264264
// Catch commands before they execute by registering a callback that will get
265265
// called when the command gets executed. This allows GUI or command line
266266
// interfaces to intercept a command and stop it from happening
267-
#ifndef SWIG
268267
bool SetCommandOverrideCallback(const char *command_name,
269268
lldb::CommandOverrideCallback callback,
270269
void *baton);
271-
#endif
272270

273271
/// Return true if the command interpreter is the active IO handler.
274272
///
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class CommandOverrideCallback(TestBase):
8+
def setUp(self):
9+
TestBase.setUp(self)
10+
self.line = line_number("main.c", "Hello world.")
11+
12+
def test_command_override_callback(self):
13+
self.build()
14+
exe = self.getBuildArtifact("a.out")
15+
16+
target = self.dbg.CreateTarget(exe)
17+
self.assertTrue(target, VALID_TARGET)
18+
19+
ci = self.dbg.GetCommandInterpreter()
20+
self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
21+
22+
command_arg = ""
23+
24+
def foo(*command_args):
25+
nonlocal command_arg
26+
command_arg = command_args[0]
27+
28+
self.assertTrue(ci.SetCommandOverrideCallback("breakpoint set", foo))
29+
self.expect("breakpoint set -n main")
30+
self.assertTrue(command_arg == "breakpoint")

0 commit comments

Comments
 (0)