Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you skip over a list comprehension in Python's debugger (pdb)?

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.

like image 334
davidavr Avatar asked Jun 10 '11 15:06

davidavr


People also ask

How do I skip pdb?

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 .

How do you skip a loop in pdb?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.

How do I use pdb debugger in Python?

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

How do I stop pdb debugging?

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.


2 Answers

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

like image 70
Sven Marnach Avatar answered Oct 24 '22 05:10

Sven Marnach


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 like next, 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

like image 3
user1767754 Avatar answered Oct 24 '22 04:10

user1767754