[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:
parent
31c37a4a5e
commit
2f5c836e08
@ -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_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):
|
||||
non_deterministic_progress.Increment(1)
|
||||
# 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",
|
||||
|
13
lldb/bindings/interface/SBProgressExtensions.i
Normal file
13
lldb/bindings/interface/SBProgressExtensions.i
Normal 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
|
||||
}
|
@ -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"
|
||||
|
@ -88,21 +88,18 @@ class ProgressTesterCommand:
|
||||
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
|
||||
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)
|
||||
|
||||
# Not required for deterministic progress, but required for indeterminate progress.
|
||||
progress.Finalize()
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user