cpython icon indicating copy to clipboard operation
cpython copied to clipboard

pdb: 'n' and 'until' do not skip list comprehension after setting a breakpoint

Open lixiaoyaol opened this issue 7 months ago • 2 comments

Bug report

Bug description:

Bug Description:

When debugging in Python using pdb, I set a breakpoint at a line with a list comprehension. However, when I use n or until, the debugger steps into the list comprehension multiple times (equal to its iteration count), rather than skipping to the next line. This behavior is unexpected and inconsistent with how n is supposed to work.

Steps to Reproduce:

  1. Run the following code in Python 3.12 with pdb:
import random

def fitness(x):
    return x ** 2

breakpoint()  # Enter pdb here
population = [random.randint(0, 31) for _ in range(10)]

for generation in range(100):
    scores = [fitness(ind) ** 2 for ind in population]
    selected = random.choices(population, weights=scores, k=10)

    next_gen = []
    for i in range(0, 10, 2):
        p1, p2 = selected[i], selected[i + 1]
        cut = random.randint(1, 4)
        mask = (1 << cut) - 1
        child1 = (p1 & mask) | (p2 & ~mask)
        child2 = (p2 & mask) | (p1 & ~mask)

        if random.random() < 0.1:
            child1 ^= 1 << random.randint(0, 4)
        if random.random() < 0.1:
            child2 ^= 1 << random.randint(0, 4)
        next_gen.append([child1, child2])

    population = next_gen

  1. At the (Pdb) prompt, enter: b 6 n

  2. When it breaks at line 6 (population = [...]), type: n until until 8

  3. It does not proceed to line 8 (the next executable line), but instead steps into the list comprehension 10 times.

Expected Behavior: After hitting the breakpoint, using n or until should step over the entire list comprehension expression and proceed to the next line of code (for generation in range(10): in this case), not into each iteration of the list comprehension.

Actual Behavior: The debugger enters the list comprehension and steps through each iteration, behaving as if the list comprehension is an expanded for-loop.

Environment:

Python Version: 3.12.2

Platform: Windows 11

IDE: Visual Studio Code

Execution: Running via terminal/debug console with built-in pdb

CPython versions tested on:

3.12

Operating systems tested on:

Windows

lixiaoyaol avatar Jun 28 '25 05:06 lixiaoyaol

LGTM, cc @gaogaotiantian

LamentXU123 avatar Jun 28 '25 06:06 LamentXU123

Thanks for the detailed explanation. Next time you can make the example shorter :) Just to get to the actual problem. I'll take a look into it.

gaogaotiantian avatar Jun 28 '25 17:06 gaogaotiantian

Thanks for the report @lixiaoyaol and for the fix Tian!

picnixz avatar Nov 16 '25 23:11 picnixz