[issue45327] Reading from a file is stuck infinitely when the file name is Boolean

Eric V. Smith report at bugs.python.org
Thu Sep 30 02:56:59 EDT 2021


Eric V. Smith <eric at trueblade.com> added the comment:

The issue is that False is causing a read from stdin, since False == 0.

>>> open(0).readlines()
test
['test\n']

Here I typed "test", followed by Ctrl-D (end of file). readlines() then completed and printed its result.

I think the basic answer here is "don't do this". It's not a bug, and is documented.

https://docs.python.org/3/library/functions.html#open
says:
file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.

So an integer file descriptor False is causing stdin to be wrapped.

>>> False == 0
True
>>> isinstance(False, int)
True

----------
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45327>
_______________________________________


More information about the Python-bugs-list mailing list