[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
chelcassanova wrote: Closing this PR, we wanted to be able to do this _without_ using the `PythonDataObjects` class in the swig wrapper that contains callback functions but this looks to be unfeasible for callbacks that return SB objects https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
https://github.com/chelcassanova closed https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
https://github.com/bulbazord commented: Is this just moving code around? The summary says what you're doing but not why, what's the motivation? https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
@@ -0,0 +1,119 @@ +/* + Typemaps specific to callback functions in LLDB. If editing this file + use the Python C API to access Python objects instead of using PythonDataObjects. +*/ + +// For Log::LogOutputCallback +%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonLogOutputCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { + $1 = $input == Py_None; + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); +} + +// For lldb::SBDebuggerDestroyCallback +%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + $1 = $input == Py_None; + $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)); +} +// For lldb::SBPlatformLocateModuleCallback +%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, + void *callback_baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + if ($input == Py_None) { +$1 = nullptr; +$2 = nullptr; + } else { +PythonCallable callable = Retain($input); +if (!callable.IsValid()) { + PyErr_SetString(PyExc_TypeError, "Need a valid callable object"); + SWIG_fail; +} + +llvm::Expected arg_info = callable.GetArgInfo(); medismailben wrote: Also you can't use llvm here. https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
@@ -0,0 +1,119 @@ +/* + Typemaps specific to callback functions in LLDB. If editing this file + use the Python C API to access Python objects instead of using PythonDataObjects. +*/ + +// For Log::LogOutputCallback +%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonLogOutputCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { + $1 = $input == Py_None; + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); +} + +// For lldb::SBDebuggerDestroyCallback +%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + $1 = $input == Py_None; + $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)); +} +// For lldb::SBPlatformLocateModuleCallback +%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, + void *callback_baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + if ($input == Py_None) { +$1 = nullptr; +$2 = nullptr; + } else { +PythonCallable callable = Retain($input); medismailben wrote: This is a PythonDataObject type https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
@@ -0,0 +1,119 @@ +/* + Typemaps specific to callback functions in LLDB. If editing this file + use the Python C API to access Python objects instead of using PythonDataObjects. medismailben wrote: nit: it would be nice to put the Python C API documentation link. https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
https://github.com/medismailben edited https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
https://github.com/medismailben requested changes to this pull request. https://github.com/llvm/llvm-project/pull/95318 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
llvmbot wrote: @llvm/pr-subscribers-lldb Author: Chelsea Cassanova (chelcassanova) Changes This commit refactors the typemaps and static functions used in the SWIG typemaps and wrappers to be in their own SWIG files that are included in the main `python.swig` file. --- Full diff: https://github.com/llvm/llvm-project/pull/95318.diff 5 Files Affected: - (added) lldb/bindings/python/python-callbacks-typemaps.swig (+119) - (added) lldb/bindings/python/python-callbacks-wrapper.swig (+101) - (modified) lldb/bindings/python/python-typemaps.swig (-116) - (modified) lldb/bindings/python/python-wrapper.swig (-96) - (modified) lldb/bindings/python/python.swig (+2) ``diff diff --git a/lldb/bindings/python/python-callbacks-typemaps.swig b/lldb/bindings/python/python-callbacks-typemaps.swig new file mode 100644 index 0..71f01c1144557 --- /dev/null +++ b/lldb/bindings/python/python-callbacks-typemaps.swig @@ -0,0 +1,119 @@ +/* + Typemaps specific to callback functions in LLDB. If editing this file + use the Python C API to access Python objects instead of using PythonDataObjects. +*/ + +// For Log::LogOutputCallback +%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonLogOutputCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { + $1 = $input == Py_None; + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); +} + +// For lldb::SBDebuggerDestroyCallback +%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + $1 = $input == Py_None; + $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)); +} +// For lldb::SBPlatformLocateModuleCallback +%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, + void *callback_baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + if ($input == Py_None) { +$1 = nullptr; +$2 = nullptr; + } else { +PythonCallable callable = Retain($input); +if (!callable.IsValid()) { + PyErr_SetString(PyExc_TypeError, "Need a valid callable object"); + SWIG_fail; +} + +llvm::Expected arg_info = callable.GetArgInfo(); +if (!arg_info) { + PyErr_SetString(PyExc_TypeError, + ("Could not get arguments: " + + llvm::toString(arg_info.takeError())).c_str()); + SWIG_fail; +} + +if (arg_info.get().max_positional_args != 3) { + PyErr_SetString(PyExc_TypeError, "Expected 3 argument callable object"); + SWIG_fail; +} + +// NOTE: When this is called multiple times, this will leak the Python +// callable object as other callbacks, because this does not call Py_DECREF +// the object. But it should be almost zero impact since this method is +// expected to be called only once. + +// Don't lose the callback reference +Py_INCREF($input); + +$1 = LLDBSwigPythonCallLocateModuleCallback; +$2 = $input; + } +} + +%typemap(typecheck) (lldb::SBPlatformLocateModuleCallback callback, + void
[Lldb-commits] [lldb] [lldb][swig] Refactor callback typemaps and functions (PR #95318)
https://github.com/chelcassanova created https://github.com/llvm/llvm-project/pull/95318 This commit refactors the typemaps and static functions used in the SWIG typemaps and wrappers to be in their own SWIG files that are included in the main `python.swig` file. >From 51ea2794e3ea5369d5197103775476819154b019 Mon Sep 17 00:00:00 2001 From: Chelsea Cassanova Date: Wed, 12 Jun 2024 14:42:39 -0700 Subject: [PATCH] [lldb][swig] Refactor callback typemaps and functions This commit refactors the typemaps and static functions used in the SWIG typemaps and wrappers to be in their own SWIG files that are included in the main `python.swig` file. --- .../python/python-callbacks-typemaps.swig | 119 ++ .../python/python-callbacks-wrapper.swig | 101 +++ lldb/bindings/python/python-typemaps.swig | 116 - lldb/bindings/python/python-wrapper.swig | 96 -- lldb/bindings/python/python.swig | 2 + 5 files changed, 222 insertions(+), 212 deletions(-) create mode 100644 lldb/bindings/python/python-callbacks-typemaps.swig create mode 100644 lldb/bindings/python/python-callbacks-wrapper.swig diff --git a/lldb/bindings/python/python-callbacks-typemaps.swig b/lldb/bindings/python/python-callbacks-typemaps.swig new file mode 100644 index 0..71f01c1144557 --- /dev/null +++ b/lldb/bindings/python/python-callbacks-typemaps.swig @@ -0,0 +1,119 @@ +/* + Typemaps specific to callback functions in LLDB. If editing this file + use the Python C API to access Python objects instead of using PythonDataObjects. +*/ + +// For Log::LogOutputCallback +%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonLogOutputCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonLogOutputCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) { + $1 = $input == Py_None; + $1 = $1 || PyCallable_Check(reinterpret_cast($input)); +} + +// For lldb::SBDebuggerDestroyCallback +%typemap(in) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + // FIXME (filcab): We can't currently check if our callback is already + // LLDBSwigPythonCallPythonSBDebuggerTerminateCallback (to DECREF the previous + // baton) nor can we just remove all traces of a callback, if we want to + // revert to a file logging mechanism. + + // Don't lose the callback reference + Py_INCREF($input); + $1 = LLDBSwigPythonCallPythonSBDebuggerTerminateCallback; + $2 = $input; +} + +%typemap(typecheck) (lldb::SBDebuggerDestroyCallback destroy_callback, void *baton) { + $1 = $input == Py_None; + $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)); +} +// For lldb::SBPlatformLocateModuleCallback +%typemap(in) (lldb::SBPlatformLocateModuleCallback callback, + void *callback_baton) { + if (!($input == Py_None || +PyCallable_Check(reinterpret_cast($input { +PyErr_SetString(PyExc_TypeError, "Need a callable object or None!"); +SWIG_fail; + } + + if ($input == Py_None) { +$1 = nullptr; +$2 = nullptr; + } else { +PythonCallable callable = Retain($input); +if (!callable.IsValid()) { + PyErr_SetString(PyExc_TypeError, "Need a valid callable object"); + SWIG_fail; +} + +llvm::Expected arg_info = callable.GetArgInfo(); +if (!arg_info) { + PyErr_SetString(PyExc_TypeError, + ("Could not get arguments: " + + llvm::toString(arg_info.takeError())).c_str()); + SWIG_fail; +} + +if (arg_info.get().max_positional_args != 3) { + PyErr_SetString(PyExc_TypeError, "Expected 3 argument callable object"); + SWIG_fail; +} + +// NOTE: When this is