In pdb
the next
instruction does not step over list comprehensions, instead it steps through each iteration. Is there a way to step over them so debugging will continue at the next line after the list comprehension?
I've had to resort to listing the code, setting a breakpoint at the next line, and then continuing execution to the next breakpoint. This is annoying and I figured there must be a better way.
Whenever you want to leave the pdb console, type the command quit or exit . If you would like to explicitly restart a program at any place within the program, you can do so with the command run .
Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.
Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
To start execution, you use the continue or c command. If the program executes successfully, you will be taken back to the (Pdb) prompt where you can restart the execution again. At this point, you can use quit / q or Ctrl+D to exit the debugger.
You can use the until
command. Output of help until
in pdb
:
unt(il)
Continue execution until the line with a number greater than the current one is reached or until the current frame returns
Elaborating on Sven's reply as I had to describe until
and next
to a colleague recently. It is not specific for the list comprehension that is stuck
with next but for loops in general.:
The
until
command is likenext
, except that it explicitly continues until execution reaches a line in the same function with a line number higher than the current value.
Which means that you can step over loops with until
Just to cover step
and next
for completeness:
The
step
command is used to execute the current line and then stop at the next execution point
Which means that it will go instruction by instruction. Note concatenating instrucitons with ;
will be handled as one instruction.
var A=0; var B=0 #Note: that will count as one instruction
The next command is like step, but does not enter functions called from the statement being executed. In effect, it steps all the way through the function call to the next statement in the current function in a single operation.
The next
helps to jump over multiple instructions, in a function of multiple variable definitions it will jump over all of them.
Here is an example that demonstrates the scenario:
Example: pdb_until.py
aVar = 3
x = [i for i in range(0,30)]
bVar = 5
Running this with Pdb:
python -m pdb pdb_until.py
Starts our interactive session:
> pdb_until.py(1)<module>()
-> aVar = 3
(Pdb) step #our Input, we step
> pdb_until.py(2)<module>()
-> x = [i for i in range(0,10)]
(Pdb) next #our Input, next
> pdb_until.py(2)<module>()
-> x = [i for i in range(0,10)]
(Pdb) next #our Input, we are now stuck on line 2
> pdb_until.py(2)<module>()
-> x = [i for i in range(0,10)]
(Pdb) until #our Input, until steps over (runs list comp)
Source: The Python Standard Library by Example, Doug Hellmann
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With