为什么 Python3.2 需要 3 个 ctrl+D 才能退出,而 Python3.3 只需要 2 个?

Why does Python3.2 require 3 ctrl+D to exit, but Python3.3 only 2?

提问人:user48206 提问时间:11/23/2016 更新时间:11/23/2016 访问量:235

问:

运行以下脚本:

import sys
sys.stdout.write(sys.stdin.read())

然后输入:

a<ctrl-d><ctrl-d><ctrl-d>

在 Python 2.6、2.7、3.3、3.4、3.5 中,程序将在 2 后终止,但在 Python 3.1 和 3.2 中,它需要 3。<ctrl-d>

关于为什么会这样,有什么线索吗?

Python stdin eof tty

评论

4赞 Martijn Pieters 11/23/2016
它甚至不应该需要 2 个。请参阅此错误报告。简短的回答是:因为错误,而这些错误已修复。
0赞 Martijn Pieters 11/23/2016
Python 3 有了一个新的 I/O 子系统,大多数错误在 3.3 中得到了解决。因此,在 3.0-3.2 中可能存在问题。我不会确切地挖掘针对此特定行为修复了哪个错误。
0赞 Terry Jan Reedy 11/23/2016
@MartijnPieters一个 ^D(文件结束)通常就足够了,但是对于挂起的输入,第一个 ^D 结束输入流(在 IDLE Shell 中,打印 0 表示写入的 0 个字符),第二个 ^D 会终止程序。在 Windows 控制台中,需要 ^Z\n。
0赞 user48206 11/23/2016
@MartijnPieters 为什么不需要 2 个?根据 POSIX,第一个 EOF 刷新待处理的输入,第二个 EOF(没有待处理的输入)导致 read() 返回 0 stackoverflow.com/a/21261742/4956890
0赞 Martijn Pieters 11/23/2016
@TerryJanReedy:是的,我错过了那里的待处理输入。

答: 暂无答案