[Lldb-commits] [lldb] [SBProgress] Add swig support for `with` statement in Python (PR #133527)
via lldb-commits
lldb-commits at lists.llvm.org
Fri Mar 28 15:25:04 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Jacob Lalonde (Jlalond)
<details>
<summary>Changes</summary>
We recently added an explicit finalize to SBProgress, #<!-- -->128966. I realized while adding some additional implementations of SBProgress that we should to add `with` support for ease of use. This patch addresses adding and `__enter()__` method (which a no-op) and an `__exit()__` to swig. I also refactor the emitter for the test to leverage `with` instead of explicitly calling finalize, and I've updated the docstrings.
---
Full diff: https://github.com/llvm/llvm-project/pull/133527.diff
4 Files Affected:
- (modified) lldb/bindings/interface/SBProgressDocstrings.i (+7)
- (added) lldb/bindings/interface/SBProgressExtensions.i (+13)
- (modified) lldb/bindings/interfaces.swig (+1)
- (modified) lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py (+10-13)
``````````diff
diff --git a/lldb/bindings/interface/SBProgressDocstrings.i b/lldb/bindings/interface/SBProgressDocstrings.i
index 8d252ef1f370c..0aff4c502f3a4 100644
--- a/lldb/bindings/interface/SBProgressDocstrings.i
+++ b/lldb/bindings/interface/SBProgressDocstrings.i
@@ -52,6 +52,13 @@ Non-deterministic progresses behave the same, but omit the total in the construc
# Explicitly send a progressEnd, otherwise this will be sent
# when the python runtime cleans up this object.
non_deterministic_progress.Finalize()
+
+Additionally for Python, progress is supported in a with statement. ::
+ with lldb.SBProgress('Non deterministic progress, 'Detail', lldb.SBDebugger) as progress:
+ for i in range(10):
+ progress.Increment(1)
+ # The progress object is automatically finalized when the with statement
+
") lldb::SBProgress;
%feature("docstring",
diff --git a/lldb/bindings/interface/SBProgressExtensions.i b/lldb/bindings/interface/SBProgressExtensions.i
new file mode 100644
index 0000000000000..6ecf3a1af93b7
--- /dev/null
+++ b/lldb/bindings/interface/SBProgressExtensions.i
@@ -0,0 +1,13 @@
+%extend lldb::SBProgress {
+#ifdef SWIGPYTHON
+ %pythoncode %{
+ def __enter__(self):
+ '''No-op for with statement'''
+ pass
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ '''Finalize the progress object'''
+ self.Finalize()
+ %}
+#endif
+}
\ No newline at end of file
diff --git a/lldb/bindings/interfaces.swig b/lldb/bindings/interfaces.swig
index 08df9a1a8d539..6da56e4e0fa52 100644
--- a/lldb/bindings/interfaces.swig
+++ b/lldb/bindings/interfaces.swig
@@ -200,6 +200,7 @@
%include "./interface/SBModuleSpecExtensions.i"
%include "./interface/SBModuleSpecListExtensions.i"
%include "./interface/SBProcessExtensions.i"
+%include "./interface/SBProgressExtensions.i"
%include "./interface/SBProcessInfoListExtensions.i"
%include "./interface/SBQueueItemExtensions.i"
%include "./interface/SBScriptObjectExtensions.i"
diff --git a/lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py b/lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py
index e94a09676e067..445d1bdf4e496 100644
--- a/lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py
+++ b/lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py
@@ -88,21 +88,18 @@ def __call__(self, debugger, command, exe_ctx, result):
progress = lldb.SBProgress(
"Progress tester", "Initial Detail", total, debugger
)
-
# Check to see if total is set to None to indicate an indeterminate progress
# then default to 10 steps.
- if total is None:
- total = 10
-
- for i in range(1, total):
- if cmd_options.no_details:
- progress.Increment(1)
- else:
- progress.Increment(1, f"Step {i}")
- time.sleep(cmd_options.seconds)
-
- # Not required for deterministic progress, but required for indeterminate progress.
- progress.Finalize()
+ with progress:
+ if total is None:
+ total = 10
+
+ for i in range(1, total):
+ if cmd_options.no_details:
+ progress.Increment(1)
+ else:
+ progress.Increment(1, f"Step {i}")
+ time.sleep(cmd_options.seconds)
def __lldb_init_module(debugger, dict):
``````````
</details>
https://github.com/llvm/llvm-project/pull/133527
More information about the lldb-commits
mailing list