python / python/cpython

repl buffering conflicts with sys.stdin

オープン
#99,751 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic-repl type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

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

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

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

>>>

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

$ cat example.py 
import sys
print('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 linux
Type "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 that readline() returns nothing at all, because world 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.py
import sys
print('hello', sys.stdin.readline())
$ (cat example.py; echo world) | python3 -i
Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
Type "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 -i
Python 3.11.0 (main, Oct 24 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> hello world

(ie: working).

Your environment

Stock python3-3.11.0-1.fc37.x86_64 package installed on Fedora 37.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

python -i インタープリターのエントリーポイントから始め、レポートにあるコマンドを使って動作を再現し、即時にパイプされた入力と sleep 0.1 によって遅延された入力を比較します。sys.stdin.readline() との関係で REPL が入力をどのように消費するかを追跡します。完了の条件は、例が入力を読み取る前に REPL がパイプされた入力を消費せず、報告されている再現結果が一貫して同じ動作をすることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。