[SBProgress] Add swig support for with statement in Python (#133527)

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.
This commit is contained in:
Jacob Lalonde 2025-03-29 15:21:51 -07:00 committed by GitHub
parent 31c37a4a5e
commit 2f5c836e08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 13 deletions

View File

@ -46,12 +46,19 @@ rely on the garbage collection when using lldb.SBProgress.
Non-deterministic progresses behave the same, but omit the total in the constructor. :: Non-deterministic progresses behave the same, but omit the total in the constructor. ::
non_deterministic_progress = lldb.SBProgress('Non deterministic progress, 'Detail', lldb.SBDebugger) non_deterministic_progress = lldb.SBProgress('Non deterministic progress', 'Detail', lldb.SBDebugger)
for i in range(10): for i in range(10):
non_deterministic_progress.Increment(1) non_deterministic_progress.Increment(1)
# Explicitly send a progressEnd, otherwise this will be sent # Explicitly send a progressEnd, otherwise this will be sent
# when the python runtime cleans up this object. # when the python runtime cleans up this object.
non_deterministic_progress.Finalize() 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; ") lldb::SBProgress;
%feature("docstring", %feature("docstring",

View File

@ -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
}

View File

@ -200,6 +200,7 @@
%include "./interface/SBModuleSpecExtensions.i" %include "./interface/SBModuleSpecExtensions.i"
%include "./interface/SBModuleSpecListExtensions.i" %include "./interface/SBModuleSpecListExtensions.i"
%include "./interface/SBProcessExtensions.i" %include "./interface/SBProcessExtensions.i"
%include "./interface/SBProgressExtensions.i"
%include "./interface/SBProcessInfoListExtensions.i" %include "./interface/SBProcessInfoListExtensions.i"
%include "./interface/SBQueueItemExtensions.i" %include "./interface/SBQueueItemExtensions.i"
%include "./interface/SBScriptObjectExtensions.i" %include "./interface/SBScriptObjectExtensions.i"

View File

@ -88,21 +88,18 @@ class ProgressTesterCommand:
progress = lldb.SBProgress( progress = lldb.SBProgress(
"Progress tester", "Initial Detail", total, debugger "Progress tester", "Initial Detail", total, debugger
) )
# Check to see if total is set to None to indicate an indeterminate progress # Check to see if total is set to None to indicate an indeterminate progress
# then default to 10 steps. # then default to 10 steps.
if total is None: with progress:
total = 10 if total is None:
total = 10
for i in range(1, total): for i in range(1, total):
if cmd_options.no_details: if cmd_options.no_details:
progress.Increment(1) progress.Increment(1)
else: else:
progress.Increment(1, f"Step {i}") progress.Increment(1, f"Step {i}")
time.sleep(cmd_options.seconds) time.sleep(cmd_options.seconds)
# Not required for deterministic progress, but required for indeterminate progress.
progress.Finalize()
def __lldb_init_module(debugger, dict): def __lldb_init_module(debugger, dict):