pdb: 'n' and 'until' do not skip list comprehension after setting a breakpoint · Issue #136057 · python/cpython · GitHub
Skip to content

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

Description

@lixiaoyaol

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

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions