~ubuntu-branches/ubuntu/precise/python3.2/precise-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_builtin.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2012-03-09 18:40:39 UTC
  • mfrom: (30.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120309184039-j3yk2emxr1plyo21
Tags: 3.2.3~rc1-1
* Python 3.2.3 release candidate 1.
* Update to 20120309 from the 3.2 branch.
* Fix libpython.a symlink. Closes: #660146.
* Build-depend on xauth.
* Run the gdb tests for the debug build only.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import warnings
7
7
import collections
8
8
import io
 
9
import os
9
10
import ast
10
11
import types
11
12
import builtins
12
13
import random
 
14
import traceback
13
15
from test.support import fcmp, TESTFN, unlink,  run_unittest, check_warnings
14
16
from operator import neg
 
17
try:
 
18
    import pty, signal
 
19
except ImportError:
 
20
    pty = signal = None
15
21
 
16
22
 
17
23
class Squares:
988
994
            fp.close()
989
995
            unlink(TESTFN)
990
996
 
 
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")
 
1001
        r, w = os.pipe()
 
1002
        try:
 
1003
            pid, fd = pty.fork()
 
1004
        except (OSError, AttributeError) as e:
 
1005
            os.close(r)
 
1006
            os.close(w)
 
1007
            self.skipTest("pty.fork() raised {}".format(e))
 
1008
        if pid == 0:
 
1009
            # Child
 
1010
            try:
 
1011
                # Make sure we don't get stuck if there's a problem
 
1012
                signal.alarm(2)
 
1013
                os.close(r)
 
1014
                # Check the error handlers are accounted for
 
1015
                if stdio_encoding:
 
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,
 
1021
                                                  errors='replace')
 
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)
 
1025
            except:
 
1026
                traceback.print_exc()
 
1027
            finally:
 
1028
                # We don't want to return to unittest...
 
1029
                os._exit(0)
 
1030
        # Parent
 
1031
        os.close(w)
 
1032
        os.write(fd, terminal_input + b"\r\n")
 
1033
        # Get results from the pipe
 
1034
        with open(r, "r") as rpipe:
 
1035
            lines = []
 
1036
            while True:
 
1037
                line = rpipe.readline().strip()
 
1038
                if line == "":
 
1039
                    # The other end was closed => the child exited
 
1040
                    break
 
1041
                lines.append(line)
 
1042
        # Check the result was got and corresponds to the user's terminal input
 
1043
        if len(lines) != 2:
 
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()))
 
1048
        os.close(fd)
 
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
 
1054
        if stdio_encoding:
 
1055
            expected = terminal_input.decode(stdio_encoding, 'surrogateescape')
 
1056
        else:
 
1057
            expected = terminal_input.decode(sys.stdin.encoding)  # what else?
 
1058
        self.assertEqual(input_result, expected)
 
1059
 
 
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")
 
1064
 
 
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")
 
1068
 
 
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")
 
1072
 
991
1073
    def test_repr(self):
992
1074
        self.assertEqual(repr(''), '\'\'')
993
1075
        self.assertEqual(repr(0), '0')