How do I skip a loop with pdb?
Question:
How can I skip over a loop using pdb.set_trace()
?
For example,
pdb.set_trace()
for i in range(5):
print(i)
print('Done!')
pdb
prompts before the loop. I input a command. All 1-5 values are returned and then I’d like to be prompted with pdb
again before the print('Done!')
executes.
Answers:
If I understood this correctly.
One possible way of doing this would be:
Once you get you pdb
prompt . Just hit n
(next) 10 times to exit the loop.
However, I am not aware of a way to exit a loop in pdb
.
You could use r
to exit a function though.
You should set a breakpoint after the loop (“break main.py:4” presuming the above lines are in a file called main.py) and then continue (“c”).
Try the until
statement.
Go to the last line of the loop (with next
or n
) and then use until
or unt
. This will take you to the next line, right after the loop.
http://www.doughellmann.com/PyMOTW/pdb/ has a good explanation
You can set another breakpoint after the loop and jump to it (when debugging) with c
:
pdb.set_trace()
for i in range(5):
print(i)
pdb.set_trace()
print('Done!')
In the link mentioned by the accepted answer (https://pymotw.com/3/pdb/), I found this section somewhat more helpful:
To let execution run until a specific line, pass the line number to
the until command.
Here’s an example of how that can work re: loops:
It spares you from two things: having to create extra breakpoints, and having to navigate to the end of a loop (especially when you might have already iterated such that you wouldn’t be able to without re-running the debugger).
Here’s the Python docs on until
. Btw I’m using pdb++
as a drop-in for the standard debugger (hence the formatting) but until
works the same in both.
How can I skip over a loop using pdb.set_trace()
?
For example,
pdb.set_trace()
for i in range(5):
print(i)
print('Done!')
pdb
prompts before the loop. I input a command. All 1-5 values are returned and then I’d like to be prompted with pdb
again before the print('Done!')
executes.
If I understood this correctly.
One possible way of doing this would be:
Once you get you pdb
prompt . Just hit n
(next) 10 times to exit the loop.
However, I am not aware of a way to exit a loop in pdb
.
You could use r
to exit a function though.
You should set a breakpoint after the loop (“break main.py:4” presuming the above lines are in a file called main.py) and then continue (“c”).
Try the until
statement.
Go to the last line of the loop (with next
or n
) and then use until
or unt
. This will take you to the next line, right after the loop.
http://www.doughellmann.com/PyMOTW/pdb/ has a good explanation
You can set another breakpoint after the loop and jump to it (when debugging) with c
:
pdb.set_trace()
for i in range(5):
print(i)
pdb.set_trace()
print('Done!')
In the link mentioned by the accepted answer (https://pymotw.com/3/pdb/), I found this section somewhat more helpful:
To let execution run until a specific line, pass the line number to
the until command.
Here’s an example of how that can work re: loops:
It spares you from two things: having to create extra breakpoints, and having to navigate to the end of a loop (especially when you might have already iterated such that you wouldn’t be able to without re-running the debugger).
Here’s the Python docs on until
. Btw I’m using pdb++
as a drop-in for the standard debugger (hence the formatting) but until
works the same in both.