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: python3 open() does not check argument type before attempting to read() or write()
Type: crash Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mo Ali, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-01-17 14:43 by Mo Ali, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Screen Shot 2017-01-17 at 10.09.35 AM.png Mo Ali, 2017-01-17 15:10
Messages (4)
msg285644 - (view) Author: Mo Ali (Mo Ali) Date: 2017-01-17 14:43
Python3 open(), read(), or write()doesn't check argument type before action causing a hang.  Would like to catch exceptions but not without an exception to return.  See below.

Python3.6:
Python 3.6.0 (default, Dec 24 2016, 08:01:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test = False
>>> with open(str(test)) as f:
...     fail = f.read()
...


python2.7:
╰─λ python2                                                                                                                                                                                     0 < 09:35:31
Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> test = False
>>> with open(test) as f:
...     fail = f.read()
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, bool found
msg285648 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-17 15:06
The first example raises an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'False'

Unless you have a file named "False" in the current directory of course.

What behavior you expected?
msg285649 - (view) Author: Mo Ali (Mo Ali) Date: 2017-01-17 15:10
Serhiy,

I expected a type error or a filenotfound like you received, however mine doesn't return the same. It just hangs.  I've attached a picture.  Also, I meant this to be for 3.6 not 3.5.

>>> test = False
>>> with open(test) as f:
...     fail = f.read()
...
msg285653 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-17 15:20
Builtin open() in Python 3 (and io.open() in Python 2.7) accept unicode strings, byte strings and integers as the first arguments (general path-like objects also are supported in Python 3.6, but it doesn't matter). bool is a subtype of int, and False is equal to integer 0. 0 is a file descriptor of sys.stdin.

>>> import sys
>>> sys.stdin.fileno()
0

Thus open(False) is equivalent to open(sys.stdin.fileno()). It creates a file object that wraps the file descriptor 0. This is legitimate operation.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73483
2017-01-17 15:20:15serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg285653

stage: resolved
2017-01-17 15:10:11Mo Alisetfiles: + Screen Shot 2017-01-17 at 10.09.35 AM.png

messages: + msg285649
versions: + Python 3.6, - Python 3.5
2017-01-17 15:06:54serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg285648
2017-01-17 14:43:36Mo Alicreate