This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Reading from a file is stuck infinitely when the file name is Boolean
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, vinayakuh
Priority: normal Keywords:

Created on 2021-09-30 06:26 by vinayakuh, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg402933 - (view) Author: Vinayak Hosamani (vinayakuh) Date: 2021-09-30 06:26
Below snippet works fine on Python2

>>> import json
>>> tc_report_file = False
>>> tc_data = json.load(open(tc_report_file))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, bool found
>>>

as we can see it is throwing an exception

same piece of code is stuck at Python3.8.10

vinayakh@ats-engine:~/stf_files$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import json
>>> tc_report_file = False
>>> tc_data = json.load(open(tc_report_file))
msg402934 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-30 06:38
You can demonstrate this without the json module:

# python2
>>> open(False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, bool found

# python3
>>> open(False).readlines()

The python3 version doesn't return.
msg402935 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-09-30 06:56
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
msg402936 - (view) Author: Vinayak Hosamani (vinayakuh) Date: 2021-09-30 07:04
Thanks for the clarification Eric.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89490
2021-09-30 07:04:27vinayakuhsetmessages: + msg402936
2021-09-30 06:56:59eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg402935

stage: resolved
2021-09-30 06:43:22eric.smithsettype: behavior
components: + Interpreter Core, - Library (Lib)
title: json loads is stuck infinitely when the file name is Boolean -> Reading from a file is stuck infinitely when the file name is Boolean
2021-09-30 06:38:05eric.smithsetnosy: + eric.smith
messages: + msg402934
2021-09-30 06:26:57vinayakuhcreate