[docs] [issue34115] code.InteractiveConsole.interact() closes stdin

Terry J. Reedy report at bugs.python.org
Fri Jul 20 19:40:36 EDT 2018

Terry J. Reedy <tjreedy at udel.edu> added the comment:

To investigate your claim about closing sys.stdin, I ran the following on Windows 10, mostly with 3.7.0, in both the console and IDLE, using various exit methods.

import code
import sys

for i in range(2):
        print(f'Try {i}: closed is', sys.stdin.closed)
    except SystemExit:
        print(f'Exc {i}: closed is', sys.stdin.closed)

^D in IDLE and ^Z+<enter> in console prints 'Try 0/1...False'.
The IC.interact loop catches EOFError and breaks for a normal exit.

'raise SystemExit' and sys.exit() print 'Exc 0/1...False'.

exit() and quit() print 'Exc 0/1...True' on Console.  On Windows Console, sys.stdin.close() does not prevent a second interact call.  This might be considered a bug.

What OS are you running and what is the result and traceback for the code above?

In IDLE, either function exit causes a 'Kill the running process' popup.  If yes, user code execution ceases and the shell window  closes.  If no, SystemExit is caught and 'SysExit...False' is printed.  The latter is true because sys.stdin.close in the user process is 'sys.shell.close()', and the latter does the popup.  The *purpose* of sys.stdin.close is to get the attention of shells that intercept SystemExit, so that users can say 'Close this now'.  Even so, IDLE asks users first to make sure.

Emulating IDLE in a simplified fashion as follows will negate closure.
import _io
class Stdin(_io.TextIOWrapper):
    def close(self): pass
sys.stdin = Stdin()  # To restore, sys.__stdin__ is the original.

History of sys.stdin.close:

Site.py, line 236, Mar 9, 2006, George Brandel added Quitter with __call__ consisting of 'raise SystemExit(code)'.

site.py, line Aug 16, 2006, Kurt Kaiser added the following lines
        # Shells like IDLE catch the SystemExit, but listen when their
        # stdin wrapper is closed.

site.py, line 250, Christian Heimes replace stdin.close with the following to avoid Lib/io.py: RuntimeWarning: Trying to close unclosable fd. (I believe io has since been patched to not do this.)
        fd = -1
        if hasattr(sys.stdin, "fileno"):
            fd = sys.stdin.fileno()
        if fd != 0:
            # Don't close stdin if it wraps fd 0

https://hg.python.org/cpython/rev/82451c88b3c0, 11 Apr 2013
After discussion on #17585 with Antoine Pitrou and Serhiy Storchaka, Roger Serwy reverted the above because it was no longer needed 

Your code assumes that sys.stdin has a buffer attribute.  Not necessarily true when python is started with pythonw.exe.  With the print removed, it also assumes that sys.stdin has file descriptor 0.  Ditto.

Possible doc improvements: the exit/quit doc says that closes.

interact doc mentions catching exceptions and quit/exit issue.

assignee:  -> docs at python
components: +Documentation
nosy: +docs at python, terry.reedy
stage:  -> needs patch
type: behavior -> enhancement
versions: +Python 3.8 -Python 3.7

Python tracker <report at bugs.python.org>

More information about the docs mailing list