Skip to content

Commit 2f5c836

Browse files
authored
[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.
1 parent 31c37a4 commit 2f5c836

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

lldb/bindings/interface/SBProgressDocstrings.i

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ rely on the garbage collection when using lldb.SBProgress.
4646
4747
Non-deterministic progresses behave the same, but omit the total in the constructor. ::
4848
49-
non_deterministic_progress = lldb.SBProgress('Non deterministic progress, 'Detail', lldb.SBDebugger)
49+
non_deterministic_progress = lldb.SBProgress('Non deterministic progress', 'Detail', lldb.SBDebugger)
5050
for i in range(10):
5151
non_deterministic_progress.Increment(1)
5252
# Explicitly send a progressEnd, otherwise this will be sent
5353
# when the python runtime cleans up this object.
5454
non_deterministic_progress.Finalize()
55+
56+
Additionally for Python, progress is supported in a with statement. ::
57+
with lldb.SBProgress('Non deterministic progress', 'Detail', lldb.SBDebugger) as progress:
58+
for i in range(10):
59+
progress.Increment(1)
60+
# The progress object is automatically finalized when the with statement
61+
5562
") lldb::SBProgress;
5663

5764
%feature("docstring",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%extend lldb::SBProgress {
2+
#ifdef SWIGPYTHON
3+
%pythoncode %{
4+
def __enter__(self):
5+
'''No-op for with statement'''
6+
pass
7+
8+
def __exit__(self, exc_type, exc_value, traceback):
9+
'''Finalize the progress object'''
10+
self.Finalize()
11+
%}
12+
#endif
13+
}

lldb/bindings/interfaces.swig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
%include "./interface/SBModuleSpecExtensions.i"
201201
%include "./interface/SBModuleSpecListExtensions.i"
202202
%include "./interface/SBProcessExtensions.i"
203+
%include "./interface/SBProgressExtensions.i"
203204
%include "./interface/SBProcessInfoListExtensions.i"
204205
%include "./interface/SBQueueItemExtensions.i"
205206
%include "./interface/SBScriptObjectExtensions.i"

lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,18 @@ def __call__(self, debugger, command, exe_ctx, result):
8888
progress = lldb.SBProgress(
8989
"Progress tester", "Initial Detail", total, debugger
9090
)
91-
9291
# Check to see if total is set to None to indicate an indeterminate progress
9392
# then default to 10 steps.
94-
if total is None:
95-
total = 10
96-
97-
for i in range(1, total):
98-
if cmd_options.no_details:
99-
progress.Increment(1)
100-
else:
101-
progress.Increment(1, f"Step {i}")
102-
time.sleep(cmd_options.seconds)
103-
104-
# Not required for deterministic progress, but required for indeterminate progress.
105-
progress.Finalize()
93+
with progress:
94+
if total is None:
95+
total = 10
96+
97+
for i in range(1, total):
98+
if cmd_options.no_details:
99+
progress.Increment(1)
100+
else:
101+
progress.Increment(1, f"Step {i}")
102+
time.sleep(cmd_options.seconds)
106103

107104

108105
def __lldb_init_module(debugger, dict):

0 commit comments

Comments
 (0)