997
@unittest.skipUnless(pty, "the pty and signal modules must be available")
998
def check_input_tty(self, prompt, terminal_input, stdio_encoding=None):
999
if not sys.stdin.isatty() or not sys.stdout.isatty():
1000
self.skipTest("stdin and stdout must be ttys")
1003
pid, fd = pty.fork()
1004
except (OSError, AttributeError) as e:
1007
self.skipTest("pty.fork() raised {}".format(e))
1011
# Make sure we don't get stuck if there's a problem
1014
# Check the error handlers are accounted for
1016
sys.stdin = io.TextIOWrapper(sys.stdin.detach(),
1017
encoding=stdio_encoding,
1018
errors='surrogateescape')
1019
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),
1020
encoding=stdio_encoding,
1022
with open(w, "w") as wpipe:
1023
print("tty =", sys.stdin.isatty() and sys.stdout.isatty(), file=wpipe)
1024
print(ascii(input(prompt)), file=wpipe)
1026
traceback.print_exc()
1028
# We don't want to return to unittest...
1032
os.write(fd, terminal_input + b"\r\n")
1033
# Get results from the pipe
1034
with open(r, "r") as rpipe:
1037
line = rpipe.readline().strip()
1039
# The other end was closed => the child exited
1042
# Check the result was got and corresponds to the user's terminal input
1044
# Something went wrong, try to get at stderr
1045
with open(fd, "r", encoding="ascii", errors="ignore") as child_output:
1046
self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
1047
% (len(lines), child_output.read()))
1049
# Check we did exercise the GNU readline path
1050
self.assertIn(lines[0], {'tty = True', 'tty = False'})
1051
if lines[0] != 'tty = True':
1052
self.skipTest("standard IO in should have been a tty")
1053
input_result = eval(lines[1]) # ascii() -> eval() roundtrip
1055
expected = terminal_input.decode(stdio_encoding, 'surrogateescape')
1057
expected = terminal_input.decode(sys.stdin.encoding) # what else?
1058
self.assertEqual(input_result, expected)
1060
def test_input_tty(self):
1061
# Test input() functionality when wired to a tty (the code path
1062
# is different and invokes GNU readline if available).
1063
self.check_input_tty("prompt", b"quux")
1065
def test_input_tty_non_ascii(self):
1066
# Check stdin/stdout encoding is used when invoking GNU readline
1067
self.check_input_tty("prompté", b"quux\xe9", "utf-8")
1069
def test_input_tty_non_ascii_unicode_errors(self):
1070
# Check stdin/stdout error handler is used when invoking GNU readline
1071
self.check_input_tty("prompté", b"quux\xe9", "ascii")
991
1073
def test_repr(self):
992
1074
self.assertEqual(repr(''), '\'\'')
993
1075
self.assertEqual(repr(0), '0')