1 | n/a | '''Tests for WindowsConsoleIO |
---|
2 | n/a | ''' |
---|
3 | n/a | |
---|
4 | n/a | import io |
---|
5 | n/a | import os |
---|
6 | n/a | import sys |
---|
7 | n/a | import tempfile |
---|
8 | n/a | import unittest |
---|
9 | n/a | from test import support |
---|
10 | n/a | |
---|
11 | n/a | if sys.platform != 'win32': |
---|
12 | n/a | raise unittest.SkipTest("test only relevant on win32") |
---|
13 | n/a | |
---|
14 | n/a | from _testconsole import write_input |
---|
15 | n/a | |
---|
16 | n/a | ConIO = io._WindowsConsoleIO |
---|
17 | n/a | |
---|
18 | n/a | class WindowsConsoleIOTests(unittest.TestCase): |
---|
19 | n/a | def test_abc(self): |
---|
20 | n/a | self.assertTrue(issubclass(ConIO, io.RawIOBase)) |
---|
21 | n/a | self.assertFalse(issubclass(ConIO, io.BufferedIOBase)) |
---|
22 | n/a | self.assertFalse(issubclass(ConIO, io.TextIOBase)) |
---|
23 | n/a | |
---|
24 | n/a | def test_open_fd(self): |
---|
25 | n/a | self.assertRaisesRegex(ValueError, |
---|
26 | n/a | "negative file descriptor", ConIO, -1) |
---|
27 | n/a | |
---|
28 | n/a | fd, _ = tempfile.mkstemp() |
---|
29 | n/a | try: |
---|
30 | n/a | # Windows 10: "Cannot open non-console file" |
---|
31 | n/a | # Earlier: "Cannot open console output buffer for reading" |
---|
32 | n/a | self.assertRaisesRegex(ValueError, |
---|
33 | n/a | "Cannot open (console|non-console file)", ConIO, fd) |
---|
34 | n/a | finally: |
---|
35 | n/a | os.close(fd) |
---|
36 | n/a | |
---|
37 | n/a | try: |
---|
38 | n/a | f = ConIO(0) |
---|
39 | n/a | except ValueError: |
---|
40 | n/a | # cannot open console because it's not a real console |
---|
41 | n/a | pass |
---|
42 | n/a | else: |
---|
43 | n/a | self.assertTrue(f.readable()) |
---|
44 | n/a | self.assertFalse(f.writable()) |
---|
45 | n/a | self.assertEqual(0, f.fileno()) |
---|
46 | n/a | f.close() # multiple close should not crash |
---|
47 | n/a | f.close() |
---|
48 | n/a | |
---|
49 | n/a | try: |
---|
50 | n/a | f = ConIO(1, 'w') |
---|
51 | n/a | except ValueError: |
---|
52 | n/a | # cannot open console because it's not a real console |
---|
53 | n/a | pass |
---|
54 | n/a | else: |
---|
55 | n/a | self.assertFalse(f.readable()) |
---|
56 | n/a | self.assertTrue(f.writable()) |
---|
57 | n/a | self.assertEqual(1, f.fileno()) |
---|
58 | n/a | f.close() |
---|
59 | n/a | f.close() |
---|
60 | n/a | |
---|
61 | n/a | try: |
---|
62 | n/a | f = ConIO(2, 'w') |
---|
63 | n/a | except ValueError: |
---|
64 | n/a | # cannot open console because it's not a real console |
---|
65 | n/a | pass |
---|
66 | n/a | else: |
---|
67 | n/a | self.assertFalse(f.readable()) |
---|
68 | n/a | self.assertTrue(f.writable()) |
---|
69 | n/a | self.assertEqual(2, f.fileno()) |
---|
70 | n/a | f.close() |
---|
71 | n/a | f.close() |
---|
72 | n/a | |
---|
73 | n/a | def test_open_name(self): |
---|
74 | n/a | self.assertRaises(ValueError, ConIO, sys.executable) |
---|
75 | n/a | |
---|
76 | n/a | f = ConIO("CON") |
---|
77 | n/a | self.assertTrue(f.readable()) |
---|
78 | n/a | self.assertFalse(f.writable()) |
---|
79 | n/a | self.assertIsNotNone(f.fileno()) |
---|
80 | n/a | f.close() # multiple close should not crash |
---|
81 | n/a | f.close() |
---|
82 | n/a | |
---|
83 | n/a | f = ConIO('CONIN$') |
---|
84 | n/a | self.assertTrue(f.readable()) |
---|
85 | n/a | self.assertFalse(f.writable()) |
---|
86 | n/a | self.assertIsNotNone(f.fileno()) |
---|
87 | n/a | f.close() |
---|
88 | n/a | f.close() |
---|
89 | n/a | |
---|
90 | n/a | f = ConIO('CONOUT$', 'w') |
---|
91 | n/a | self.assertFalse(f.readable()) |
---|
92 | n/a | self.assertTrue(f.writable()) |
---|
93 | n/a | self.assertIsNotNone(f.fileno()) |
---|
94 | n/a | f.close() |
---|
95 | n/a | f.close() |
---|
96 | n/a | |
---|
97 | n/a | f = open('C:/con', 'rb', buffering=0) |
---|
98 | n/a | self.assertIsInstance(f, ConIO) |
---|
99 | n/a | f.close() |
---|
100 | n/a | |
---|
101 | n/a | @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), |
---|
102 | n/a | "test does not work on Windows 7 and earlier") |
---|
103 | n/a | def test_conin_conout_names(self): |
---|
104 | n/a | f = open(r'\\.\conin$', 'rb', buffering=0) |
---|
105 | n/a | self.assertIsInstance(f, ConIO) |
---|
106 | n/a | f.close() |
---|
107 | n/a | |
---|
108 | n/a | f = open('//?/conout$', 'wb', buffering=0) |
---|
109 | n/a | self.assertIsInstance(f, ConIO) |
---|
110 | n/a | f.close() |
---|
111 | n/a | |
---|
112 | n/a | def test_conout_path(self): |
---|
113 | n/a | temp_path = tempfile.mkdtemp() |
---|
114 | n/a | self.addCleanup(support.rmtree, temp_path) |
---|
115 | n/a | |
---|
116 | n/a | conout_path = os.path.join(temp_path, 'CONOUT$') |
---|
117 | n/a | |
---|
118 | n/a | with open(conout_path, 'wb', buffering=0) as f: |
---|
119 | n/a | if sys.getwindowsversion()[:2] > (6, 1): |
---|
120 | n/a | self.assertIsInstance(f, ConIO) |
---|
121 | n/a | else: |
---|
122 | n/a | self.assertNotIsInstance(f, ConIO) |
---|
123 | n/a | |
---|
124 | n/a | def assertStdinRoundTrip(self, text): |
---|
125 | n/a | stdin = open('CONIN$', 'r') |
---|
126 | n/a | old_stdin = sys.stdin |
---|
127 | n/a | try: |
---|
128 | n/a | sys.stdin = stdin |
---|
129 | n/a | write_input( |
---|
130 | n/a | stdin.buffer.raw, |
---|
131 | n/a | (text + '\r\n').encode('utf-16-le', 'surrogatepass') |
---|
132 | n/a | ) |
---|
133 | n/a | actual = input() |
---|
134 | n/a | finally: |
---|
135 | n/a | sys.stdin = old_stdin |
---|
136 | n/a | self.assertEqual(actual, text) |
---|
137 | n/a | |
---|
138 | n/a | def test_input(self): |
---|
139 | n/a | # ASCII |
---|
140 | n/a | self.assertStdinRoundTrip('abc123') |
---|
141 | n/a | # Non-ASCII |
---|
142 | n/a | self.assertStdinRoundTrip('ϼÑТλФÐ') |
---|
143 | n/a | # Combining characters |
---|
144 | n/a | self.assertStdinRoundTrip('AÍB ï¬Ì³AAÌ') |
---|
145 | n/a | # Non-BMP |
---|
146 | n/a | self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd') |
---|
147 | n/a | |
---|
148 | n/a | def test_partial_reads(self): |
---|
149 | n/a | # Test that reading less than 1 full character works when stdin |
---|
150 | n/a | # contains multibyte UTF-8 sequences |
---|
151 | n/a | source = 'ϼÑТλФÐ\r\n'.encode('utf-16-le') |
---|
152 | n/a | expected = 'ϼÑТλФÐ\r\n'.encode('utf-8') |
---|
153 | n/a | for read_count in range(1, 16): |
---|
154 | n/a | with open('CONIN$', 'rb', buffering=0) as stdin: |
---|
155 | n/a | write_input(stdin, source) |
---|
156 | n/a | |
---|
157 | n/a | actual = b'' |
---|
158 | n/a | while not actual.endswith(b'\n'): |
---|
159 | n/a | b = stdin.read(read_count) |
---|
160 | n/a | actual += b |
---|
161 | n/a | |
---|
162 | n/a | self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
---|
163 | n/a | |
---|
164 | n/a | def test_partial_surrogate_reads(self): |
---|
165 | n/a | # Test that reading less than 1 full character works when stdin |
---|
166 | n/a | # contains surrogate pairs that cannot be decoded to UTF-8 without |
---|
167 | n/a | # reading an extra character. |
---|
168 | n/a | source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le') |
---|
169 | n/a | expected = '\U00101FFF\U00101001\r\n'.encode('utf-8') |
---|
170 | n/a | for read_count in range(1, 16): |
---|
171 | n/a | with open('CONIN$', 'rb', buffering=0) as stdin: |
---|
172 | n/a | write_input(stdin, source) |
---|
173 | n/a | |
---|
174 | n/a | actual = b'' |
---|
175 | n/a | while not actual.endswith(b'\n'): |
---|
176 | n/a | b = stdin.read(read_count) |
---|
177 | n/a | actual += b |
---|
178 | n/a | |
---|
179 | n/a | self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
---|
180 | n/a | |
---|
181 | n/a | def test_ctrl_z(self): |
---|
182 | n/a | with open('CONIN$', 'rb', buffering=0) as stdin: |
---|
183 | n/a | source = '\xC4\x1A\r\n'.encode('utf-16-le') |
---|
184 | n/a | expected = '\xC4'.encode('utf-8') |
---|
185 | n/a | write_input(stdin, source) |
---|
186 | n/a | a, b = stdin.read(1), stdin.readall() |
---|
187 | n/a | self.assertEqual(expected[0:1], a) |
---|
188 | n/a | self.assertEqual(expected[1:], b) |
---|
189 | n/a | |
---|
190 | n/a | if __name__ == "__main__": |
---|
191 | n/a | unittest.main() |
---|