[Lldb-commits] [lldb] Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… (PR #95181)

2024-06-11 Thread Chelsea Cassanova via lldb-commits

https://github.com/chelcassanova closed 
https://github.com/llvm/llvm-project/pull/95181
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… (PR #95181)

2024-06-11 Thread Med Ismail Bennani via lldb-commits

https://github.com/medismailben approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/95181
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [lldb] Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… (PR #95181)

2024-06-11 Thread via lldb-commits

llvmbot wrote:




@llvm/pr-subscribers-lldb

Author: Chelsea Cassanova (chelcassanova)


Changes

…andOverrideCallback (#94518)"

This reverts commit 7cff05ada05e87408966d56b4c1675033187ff5c. 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.

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


4 Files Affected:

- (modified) lldb/bindings/python/python-typemaps.swig (+17-1) 
- (modified) lldb/bindings/python/python-wrapper.swig (+22) 
- (modified) lldb/include/lldb/API/SBCommandInterpreter.h (-2) 
- (added) lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py 
(+30) 


``diff
diff --git a/lldb/bindings/python/python-typemaps.swig 
b/lldb/bindings/python/python-typemaps.swig
index 8d4b740e5f35c..c39594c7df041 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -427,7 +427,6 @@ template <> bool SetNumberFromPyObject(double 
, PyObject *obj) {
   free($1);
 }
 
-
 // For Log::LogOutputCallback
 %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
   if (!($input == Py_None ||
@@ -476,6 +475,23 @@ template <> bool SetNumberFromPyObject(double 
, PyObject *obj) {
   $1 = $1 || PyCallable_Check(reinterpret_cast($input));
 }
 
+%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) {
+  if (!($input == Py_None ||
+PyCallable_Check(reinterpret_cast($input {
+PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
+SWIG_fail;
+  }
+
+  // Don't lose the callback reference.
+  Py_INCREF($input);
+  $1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback;
+  $2 = $input;
+}
+%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) {
+  $1 = $input == Py_None;
+  $1 = $1 || PyCallable_Check(reinterpret_cast($input));
+}
+
 %typemap(in) lldb::FileSP {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
diff --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index 1370afc885d43..bd3de8ce5d46c 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -1099,6 +1099,28 @@ static void 
LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t
   }
 }
 
+static bool 
LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void 
*baton, const char **argv) {
+  bool ret_val = false;
+  if (baton != Py_None) {
+SWIG_PYTHON_THREAD_BEGIN_BLOCK;
+// Create a PyList of items since we're going to pass it to the callback 
as a tuple
+// of arguments.
+PyObject *py_argv = PyList_New(0);
+for (const char **arg = argv; arg && *arg; arg++) {
+  std::string arg_string = *arg;
+  PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), 
arg_string.size());
+  PyList_Append(py_argv, py_string);
+}
+
+PyObject *result = PyObject_CallObject(
+reinterpret_cast(baton), PyList_AsTuple(py_argv));
+ret_val = result ? PyObject_IsTrue(result) : false;
+Py_XDECREF(result);
+SWIG_PYTHON_THREAD_END_BLOCK;
+  }
+  return ret_val;
+}
+
 static SBError LLDBSwigPythonCallLocateModuleCallback(
 void *callback_baton, const SBModuleSpec _spec_sb,
 SBFileSpec _file_spec_sb, SBFileSpec _file_spec_sb) {
diff --git a/lldb/include/lldb/API/SBCommandInterpreter.h 
b/lldb/include/lldb/API/SBCommandInterpreter.h
index 639309aa32bfc..b7e39b7858616 100644
--- a/lldb/include/lldb/API/SBCommandInterpreter.h
+++ b/lldb/include/lldb/API/SBCommandInterpreter.h
@@ -265,11 +265,9 @@ class SBCommandInterpreter {
   // Catch commands before they execute by registering a callback that will get
   // called when the command gets executed. This allows GUI or command line
   // interfaces to intercept a command and stop it from happening
-#ifndef SWIG
   bool SetCommandOverrideCallback(const char *command_name,
   lldb::CommandOverrideCallback callback,
   void *baton);
-#endif
 
   /// Return true if the command interpreter is the active IO handler.
   ///
diff --git 
a/lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py 
b/lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py
new file mode 100644
index 0..a09c4a71c9948
--- /dev/null
+++ b/lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py
@@ -0,0 +1,30 @@
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class CommandOverrideCallback(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.line = line_number("main.c", "Hello world.")
+
+def test_command_override_callback(self):
+self.build()
+exe = self.getBuildArtifact("a.out")
+
+target = self.dbg.CreateTarget(exe)

[Lldb-commits] [lldb] Reland "[lldb][api-test] Add API test for SBCommandInterpreter::Comm… (PR #95181)

2024-06-11 Thread Chelsea Cassanova via lldb-commits

https://github.com/chelcassanova created 
https://github.com/llvm/llvm-project/pull/95181

…andOverrideCallback (#94518)"

This reverts commit 7cff05ada05e87408966d56b4c1675033187ff5c. 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.

>From 6cee5d2638b2b50bc8c4538014c2d3c77c98ecba Mon Sep 17 00:00:00 2001
From: Chelsea Cassanova 
Date: Tue, 11 Jun 2024 15:33:55 -0700
Subject: [PATCH] Reland  "[lldb][api-test] Add API test for
 SBCommandInterpreter::CommandOverrideCallback (#94518)"

This reverts commit 7cff05ada05e87408966d56b4c1675033187ff5c.
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.
---
 lldb/bindings/python/python-typemaps.swig | 18 ++-
 lldb/bindings/python/python-wrapper.swig  | 22 ++
 lldb/include/lldb/API/SBCommandInterpreter.h  |  2 --
 .../TestCommandOverrideCallback.py| 30 +++
 4 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 
lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py

diff --git a/lldb/bindings/python/python-typemaps.swig 
b/lldb/bindings/python/python-typemaps.swig
index 8d4b740e5f35c..c39594c7df041 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -427,7 +427,6 @@ template <> bool SetNumberFromPyObject(double 
, PyObject *obj) {
   free($1);
 }
 
-
 // For Log::LogOutputCallback
 %typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
   if (!($input == Py_None ||
@@ -476,6 +475,23 @@ template <> bool SetNumberFromPyObject(double 
, PyObject *obj) {
   $1 = $1 || PyCallable_Check(reinterpret_cast($input));
 }
 
+%typemap(in) (lldb::CommandOverrideCallback callback, void *baton) {
+  if (!($input == Py_None ||
+PyCallable_Check(reinterpret_cast($input {
+PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
+SWIG_fail;
+  }
+
+  // Don't lose the callback reference.
+  Py_INCREF($input);
+  $1 = LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback;
+  $2 = $input;
+}
+%typemap(typecheck) (lldb::CommandOverrideCallback callback, void *baton) {
+  $1 = $input == Py_None;
+  $1 = $1 || PyCallable_Check(reinterpret_cast($input));
+}
+
 %typemap(in) lldb::FileSP {
   PythonFile py_file(PyRefType::Borrowed, $input);
   if (!py_file) {
diff --git a/lldb/bindings/python/python-wrapper.swig 
b/lldb/bindings/python/python-wrapper.swig
index 1370afc885d43..bd3de8ce5d46c 100644
--- a/lldb/bindings/python/python-wrapper.swig
+++ b/lldb/bindings/python/python-wrapper.swig
@@ -1099,6 +1099,28 @@ static void 
LLDBSwigPythonCallPythonSBDebuggerTerminateCallback(lldb::user_id_t
   }
 }
 
+static bool 
LLDBSwigPythonCallPythonSBCommandInterpreterSetCommandOverrideCallback(void 
*baton, const char **argv) {
+  bool ret_val = false;
+  if (baton != Py_None) {
+SWIG_PYTHON_THREAD_BEGIN_BLOCK;
+// Create a PyList of items since we're going to pass it to the callback 
as a tuple
+// of arguments.
+PyObject *py_argv = PyList_New(0);
+for (const char **arg = argv; arg && *arg; arg++) {
+  std::string arg_string = *arg;
+  PyObject *py_string = PyUnicode_FromStringAndSize(arg_string.c_str(), 
arg_string.size());
+  PyList_Append(py_argv, py_string);
+}
+
+PyObject *result = PyObject_CallObject(
+reinterpret_cast(baton), PyList_AsTuple(py_argv));
+ret_val = result ? PyObject_IsTrue(result) : false;
+Py_XDECREF(result);
+SWIG_PYTHON_THREAD_END_BLOCK;
+  }
+  return ret_val;
+}
+
 static SBError LLDBSwigPythonCallLocateModuleCallback(
 void *callback_baton, const SBModuleSpec _spec_sb,
 SBFileSpec _file_spec_sb, SBFileSpec _file_spec_sb) {
diff --git a/lldb/include/lldb/API/SBCommandInterpreter.h 
b/lldb/include/lldb/API/SBCommandInterpreter.h
index 639309aa32bfc..b7e39b7858616 100644
--- a/lldb/include/lldb/API/SBCommandInterpreter.h
+++ b/lldb/include/lldb/API/SBCommandInterpreter.h
@@ -265,11 +265,9 @@ class SBCommandInterpreter {
   // Catch commands before they execute by registering a callback that will get
   // called when the command gets executed. This allows GUI or command line
   // interfaces to intercept a command and stop it from happening
-#ifndef SWIG
   bool SetCommandOverrideCallback(const char *command_name,
   lldb::CommandOverrideCallback callback,
   void *baton);
-#endif
 
   /// Return true if the command interpreter is the active IO handler.
   ///
diff --git 
a/lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py 
b/lldb/test/API/python_api/interpreter/TestCommandOverrideCallback.py
new file mode 100644
index 0..a09c4a71c9948
--- /dev/null
+++