$ echo "Your code is bad and you should feel bad" > "<stdin>"
$ python
Python 3.6.0 (default, Dec 28 2016, 19:53:26)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + '2'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Your code is bad and you should feel bad
TypeError: unsupported operand type(s) for +: 'int' and 'str'
为什么Python将字符串"<stdin>"
与匹配该文件名的文件混淆?我不希望Python试图从我的磁盘读取任何文件,如果它遇到未处理的异常。为什么打印回溯时Python从当前目录读取?
您也可以与"<string>"
名得到它:
$ echo "pining for the fjords" > "<string>"
$ python -c 'wat'
Traceback (most recent call last):
File "<string>", line 1, in <module>
pining for the fjords
NameError: name 'wat' is not defined
有什么办法来防止这种行为,或者是硬编码到REPL?
哦,这是一个可爱的bug。你应该报告它。 – kindall