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

PythonList ComprehensionPdb

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondavidavrView Question on Stackoverflow
Solution 1 - PythonSven MarnachView Answer on Stackoverflow
Solution 2 - Pythonuser1767754View Answer on Stackoverflow