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: Command 'open(0,"wb").close()' cause crash of Python interpreter [interactive mode]
Type: crash Stage: test needed
Components: Interpreter Core, IO Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ezio.melotti, i386x, jcea, pitrou, stutzbach
Priority: normal Keywords:

Created on 2011-09-13 06:51 by i386x, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg143951 - (view) Author: Jiří Kučera (i386x) Date: 2011-09-13 06:51
Invoking the `close' method of `_io.BufferedWriter' instance created by `open(0,"wb")' command cause the Python interpreter crash.

Python interpreter info:
  mode: interactive
  version info: Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32

Operanting system info:
  Microsoft Windows XP
  Home Edition
  Version 2002
  Service Pack 3

Commands:
>>> fd = open(0,"wb")
>>> fd.close()
msg143957 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2011-09-13 11:57
Under Python 3, open(integer) tries to open a file descriptor.

So, "f=open(0,...); f.close()" closes stdin, rightly shutting down the interpreter. It is not a crash, it is a shutdown. Tested under Linux.

The point is if opening a file descriptor is actually supported in Python 3...

In python 2.7 I get this: "TypeError: coercing to Unicode: need string or buffer, int found".
msg143958 - (view) Author: Jesús Cea Avión (jcea) * (Python committer) Date: 2011-09-13 12:03
In "help(open)" I see this:

"""
    file is either a text or byte string giving the name (and the path
    if the file isn't in the current working directory) of the file to
    be opened or an integer file descriptor of the file to be
    wrapped. (If a file descriptor is given, it is closed when the
    returned I/O object is closed, unless closefd is set to False.)
"""

So, file descriptors are allowed.

The interpreter shutdowns because your are closing STDIN. This is correct, in my opinion.

Closing this bug as "invalid". If you think this is an error, feel free to argue.
msg143964 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-09-13 12:56
fd support is intentional, see Modules/_io/_iomodule.c:318

OTOH closing sys.stdin doesn't exit Python, so I'm not sure why closing fd 0 should.

I was also thinking about possible security implications of this, but if someone tries to pass '0' as filename, it will most likely be passed to open as a string.
History
Date User Action Args
2022-04-11 14:57:21adminsetgithub: 57178
2011-09-13 12:56:22ezio.melottisetmessages: + msg143964
2011-09-13 12:03:22jceasetstatus: open -> closed
resolution: not a bug
messages: + msg143958
2011-09-13 11:57:45jceasetnosy: + jcea
messages: + msg143957
2011-09-13 10:02:16ezio.melottisetnosy: + pitrou, benjamin.peterson, stutzbach, ezio.melotti
stage: test needed

components: + IO
versions: + Python 3.3
2011-09-13 06:51:34i386xcreate