[New-bugs-announce] [issue34115] code.InteractiveConsole.interact() closes stdin

Yonatan Zunger report at bugs.python.org
Sat Jul 14 18:49:43 EDT 2018


New submission from Yonatan Zunger <zunger at humu.com>:

code.InteractiveConsole.interact() closes stdin on exit, which can be very surprising to successive code, not least future calls to interact(). A simple repro with a workaround is:

import code
import io
import os
import sys

def run():
    print(sys.stdin.buffer.raw)
    dupstdin = os.dup(0)
    try:
        code.InteractiveConsole().interact()
    except SystemExit:
        pass
    finally:
        # Workaround: Without this line, the second call to run() will fail with a ValueError when
        # it tries to call input().
        sys.stdin = io.TextIOWrapper(
            io.BufferedReader(io.FileIO(dupstdin, mode='rb', closefd=False)),
            encoding='utf8')

run()
run()


- The exciting behavior appears to happen inside the exec() of a 'quit()' command, and I haven't searched it out further.
- That behavior inside exec() is likely there for a good reason, in which case the best fix is probably to just save and restore stdin in the code library.

----------
components: Library (Lib)
messages: 321666
nosy: Yonatan Zunger
priority: normal
severity: normal
status: open
title: code.InteractiveConsole.interact() closes stdin
type: behavior
versions: Python 3.7

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


More information about the New-bugs-announce mailing list