Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

repl buffering conflicts with sys.stdin #99751

Open
Labels
topic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error
@allisonkarlitskaya

Description

@allisonkarlitskaya

Bug report

When running the interpreter (python -i) the REPL appears to implement its own buffering, independent ofsys.stdin and its internal buffer.

Consider the following (working) example, manually entered into the REPL via the keyboard:

$ python3 -iPython 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import sys>>> print('hello', sys.stdin.readline())worldhello world>>>

This appears to work only because at the time I hit<enter> after theprint() line, theworld isn't available to read yet, and therefore the repl can't possibly consume it. If I put the whole thing into a file, andcat it in one go, however:

$ cat example.py import sysprint('hello', sys.stdin.readline())world$ cat example.py | python3 -i                                          ST 12   peer Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> >>> hello >>> Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'world' is not defined>>> >>>

We can see thatreadline() returns nothing at all, becauseworld has already been consumed by the repl, which attempts (and fails) to interpret it as the next command to be executed.

You can work around this issue by inserting even the tiniest of pauses. Consider:

$ cat example.pyimport sysprint('hello', sys.stdin.readline())
$ (cat example.py; echo world) | python3 -iPython 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> >>> hello >>> Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'world' is not defined>>>

(ie: broken)

but:

$ (cat example.py; sleep 0.1; echo world) | python3 -iPython 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> >>> hello world

(ie: working).

Your environment

Stockpython3-3.11.0-1.fc37.x86_64 package installed on Fedora 37.

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


      [8]ページ先頭

      ©2009-2025 Movatter.jp