98

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.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Rhys
  • 4,926
  • 14
  • 41
  • 64

5 Answers5

173

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

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
shreddd
  • 10,975
  • 9
  • 33
  • 34
  • 2
    From the article: go to the last line of the for loop by using `n`, and then type `until`. This will go until at least that current line is exceeded. – sachinruk Nov 02 '18 at 05:48
  • Works with list comprehensions too. You don't have to care about the last line. – Shiva Nov 23 '18 at 10:24
  • What would you do, if going to the last line of the loop is cumbersome? Is there really no equivalent to `f` used by R's `debug` function which "finishes execution of the current loop or function" (https://adv-r.hadley.nz/debugging.html)? – Qaswed Jun 17 '19 at 12:51
  • Using `until` (or its short form `unt`) without arguments will always go down a line (unless you hit a return statement), so you can also just use it a bunch of times instead of `n`. – Boris Verkhovskiy Nov 22 '19 at 21:39
  • It does not work with `breakpoint()` set in the loop in `python 3`. The `VB Editor` can accept breakpoints 'on the fly', but this is most likely not the case with `pdb`. – Timo Sep 23 '20 at 05:53
  • For me `until` does do the job as described in `python3` when stopped by a `breakpoint()`. – Bastian Oct 07 '21 at 15:24
5

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").

mike
  • 403
  • 3
  • 6
3

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:

enter image description here

enter image description here

enter image description here

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.

Zach Valenta
  • 1,783
  • 1
  • 20
  • 35
1

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!')
Qaswed
  • 3,649
  • 7
  • 27
  • 47
-18

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.