Skip to content

- Python 3.13.7 REPL consumes first two elements of any generator before iteration #140486

@rmelim

Description

@rmelim

Bug report

Bug description:

Bug Report – Python 3.13.7 REPL consumes first two elements of any generator

Description

In Python 3.13.7, when using the interactive REPL, any generator object appears to be advanced by two steps before user code begins iterating over it. This behavior does not occur when running the same code from a .py script, where the output is correct.

Steps to Reproduce

  1. Start Python 3.13.7 in interactive mode (python).
  2. Enter the following code:
g = (n for n in range(5))
for x in g:
    print(x, end=" ")

Actual Result
2 3 4

Expected Result
0 1 2 3 4

Additional Examples

Example 1 – Using chr()

g = (chr(n) for n in range(65, 91)) 
for x in g: 
    print(x, end=" ") 

Actual: C D E ... Z
Expected: A B C ... Z

Example 2 – Using a generator function

def letters(): 
     for n in range(65, 91): 
        yield chr(n) 

for x in letters(): 
    print(x, end=" ") 

Actual: C D E ... Z
Expected: A B C ... Z

Environment:

Python version: 3.13.7 (confirmed with sys.version)
Operating System: Windows 11
Shells tested: PowerShell, CMD, Git Bash (MINGW64)
Also reproducible with python -S (so no sitecustomize or usercustomize interference)

Notes:

The issue occurs only in the interactive REPL.
Running the same code from a .py script produces the correct output.
This strongly suggests that the REPL is consuming two items from any generator before user iteration begins.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions