Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lldb/bindings/interface/SBProgressDocstrings.i
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions lldb/bindings/interface/SBProgressExtensions.i
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions lldb/bindings/interfaces.swig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
23 changes: 10 additions & 13 deletions lldb/test/API/tools/lldb-dap/progress/Progress_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down