|
| 1 | +'''Tests for WindowsConsoleIO |
| 2 | +''' |
| 3 | + |
| 4 | +import io |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | +from test.support import os_helper, requires_resource |
| 10 | + |
| 11 | +if sys.platform != 'win32': |
| 12 | + raise unittest.SkipTest("test only relevant on win32") |
| 13 | + |
| 14 | +from _testconsole import write_input |
| 15 | + |
| 16 | +ConIO = io._WindowsConsoleIO |
| 17 | + |
| 18 | +class WindowsConsoleIOTests(unittest.TestCase): |
| 19 | + def test_abc(self): |
| 20 | + self.assertIsSubclass(ConIO, io.RawIOBase) |
| 21 | + self.assertNotIsSubclass(ConIO, io.BufferedIOBase) |
| 22 | + self.assertNotIsSubclass(ConIO, io.TextIOBase) |
| 23 | + |
| 24 | + def test_open_fd(self): |
| 25 | + self.assertRaisesRegex(ValueError, |
| 26 | + "negative file descriptor", ConIO, -1) |
| 27 | + |
| 28 | + with tempfile.TemporaryFile() as tmpfile: |
| 29 | + fd = tmpfile.fileno() |
| 30 | + # Windows 10: "Cannot open non-console file" |
| 31 | + # Earlier: "Cannot open console output buffer for reading" |
| 32 | + self.assertRaisesRegex(ValueError, |
| 33 | + "Cannot open (console|non-console file)", ConIO, fd) |
| 34 | + |
| 35 | + try: |
| 36 | + f = ConIO(0) |
| 37 | + except ValueError: |
| 38 | + # cannot open console because it's not a real console |
| 39 | + pass |
| 40 | + else: |
| 41 | + self.assertTrue(f.readable()) |
| 42 | + self.assertFalse(f.writable()) |
| 43 | + self.assertEqual(0, f.fileno()) |
| 44 | + f.close() # multiple close should not crash |
| 45 | + f.close() |
| 46 | + with self.assertWarns(RuntimeWarning): |
| 47 | + with ConIO(False): |
| 48 | + pass |
| 49 | + |
| 50 | + try: |
| 51 | + f = ConIO(1, 'w') |
| 52 | + except ValueError: |
| 53 | + # cannot open console because it's not a real console |
| 54 | + pass |
| 55 | + else: |
| 56 | + self.assertFalse(f.readable()) |
| 57 | + self.assertTrue(f.writable()) |
| 58 | + self.assertEqual(1, f.fileno()) |
| 59 | + f.close() |
| 60 | + f.close() |
| 61 | + with self.assertWarns(RuntimeWarning): |
| 62 | + with ConIO(False): |
| 63 | + pass |
| 64 | + |
| 65 | + try: |
| 66 | + f = ConIO(2, 'w') |
| 67 | + except ValueError: |
| 68 | + # cannot open console because it's not a real console |
| 69 | + pass |
| 70 | + else: |
| 71 | + self.assertFalse(f.readable()) |
| 72 | + self.assertTrue(f.writable()) |
| 73 | + self.assertEqual(2, f.fileno()) |
| 74 | + f.close() |
| 75 | + f.close() |
| 76 | + |
| 77 | + def test_open_name(self): |
| 78 | + self.assertRaises(ValueError, ConIO, sys.executable) |
| 79 | + |
| 80 | + f = ConIO("CON") |
| 81 | + self.assertTrue(f.readable()) |
| 82 | + self.assertFalse(f.writable()) |
| 83 | + self.assertIsNotNone(f.fileno()) |
| 84 | + f.close() # multiple close should not crash |
| 85 | + f.close() |
| 86 | + |
| 87 | + f = ConIO('CONIN$') |
| 88 | + self.assertTrue(f.readable()) |
| 89 | + self.assertFalse(f.writable()) |
| 90 | + self.assertIsNotNone(f.fileno()) |
| 91 | + f.close() |
| 92 | + f.close() |
| 93 | + |
| 94 | + f = ConIO('CONOUT$', 'w') |
| 95 | + self.assertFalse(f.readable()) |
| 96 | + self.assertTrue(f.writable()) |
| 97 | + self.assertIsNotNone(f.fileno()) |
| 98 | + f.close() |
| 99 | + f.close() |
| 100 | + |
| 101 | + # bpo-45354: Windows 11 changed MS-DOS device name handling |
| 102 | + if sys.getwindowsversion()[:3] < (10, 0, 22000): |
| 103 | + f = open('C:/con', 'rb', buffering=0) |
| 104 | + self.assertIsInstance(f, ConIO) |
| 105 | + f.close() |
| 106 | + |
| 107 | + def test_subclass_repr(self): |
| 108 | + class TestSubclass(ConIO): |
| 109 | + pass |
| 110 | + |
| 111 | + f = TestSubclass("CON") |
| 112 | + with f: |
| 113 | + self.assertIn(TestSubclass.__name__, repr(f)) |
| 114 | + |
| 115 | + self.assertIn(TestSubclass.__name__, repr(f)) |
| 116 | + |
| 117 | + @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), |
| 118 | + "test does not work on Windows 7 and earlier") |
| 119 | + def test_conin_conout_names(self): |
| 120 | + f = open(r'\\.\conin$', 'rb', buffering=0) |
| 121 | + self.assertIsInstance(f, ConIO) |
| 122 | + f.close() |
| 123 | + |
| 124 | + f = open('//?/conout$', 'wb', buffering=0) |
| 125 | + self.assertIsInstance(f, ConIO) |
| 126 | + f.close() |
| 127 | + |
| 128 | + def test_conout_path(self): |
| 129 | + temp_path = tempfile.mkdtemp() |
| 130 | + self.addCleanup(os_helper.rmtree, temp_path) |
| 131 | + |
| 132 | + conout_path = os.path.join(temp_path, 'CONOUT$') |
| 133 | + |
| 134 | + with open(conout_path, 'wb', buffering=0) as f: |
| 135 | + # bpo-45354: Windows 11 changed MS-DOS device name handling |
| 136 | + if (6, 1) < sys.getwindowsversion()[:3] < (10, 0, 22000): |
| 137 | + self.assertIsInstance(f, ConIO) |
| 138 | + else: |
| 139 | + self.assertNotIsInstance(f, ConIO) |
| 140 | + |
| 141 | + def test_write_empty_data(self): |
| 142 | + with ConIO('CONOUT$', 'w') as f: |
| 143 | + self.assertEqual(f.write(b''), 0) |
| 144 | + |
| 145 | + @requires_resource('console') |
| 146 | + def test_write(self): |
| 147 | + testcases = [] |
| 148 | + with ConIO('CONOUT$', 'w') as f: |
| 149 | + for a in [ |
| 150 | + b'', |
| 151 | + b'abc', |
| 152 | + b'\xc2\xa7\xe2\x98\x83\xf0\x9f\x90\x8d', |
| 153 | + b'\xff'*10, |
| 154 | + ]: |
| 155 | + for b in b'\xc2\xa7', b'\xe2\x98\x83', b'\xf0\x9f\x90\x8d': |
| 156 | + testcases.append(a + b) |
| 157 | + for i in range(1, len(b)): |
| 158 | + data = a + b[:i] |
| 159 | + testcases.append(data + b'z') |
| 160 | + testcases.append(data + b'\xff') |
| 161 | + # incomplete multibyte sequence |
| 162 | + with self.subTest(data=data): |
| 163 | + self.assertEqual(f.write(data), len(a)) |
| 164 | + for data in testcases: |
| 165 | + with self.subTest(data=data): |
| 166 | + self.assertEqual(f.write(data), len(data)) |
| 167 | + |
| 168 | + def assertStdinRoundTrip(self, text): |
| 169 | + stdin = open('CONIN$', 'r') |
| 170 | + old_stdin = sys.stdin |
| 171 | + try: |
| 172 | + sys.stdin = stdin |
| 173 | + write_input( |
| 174 | + stdin.buffer.raw, |
| 175 | + (text + '\r\n').encode('utf-16-le', 'surrogatepass') |
| 176 | + ) |
| 177 | + actual = input() |
| 178 | + finally: |
| 179 | + sys.stdin = old_stdin |
| 180 | + self.assertEqual(actual, text) |
| 181 | + |
| 182 | + @requires_resource('console') |
| 183 | + def test_input(self): |
| 184 | + # ASCII |
| 185 | + self.assertStdinRoundTrip('abc123') |
| 186 | + # Non-ASCII |
| 187 | + self.assertStdinRoundTrip('ϼўТλФЙ') |
| 188 | + # Combining characters |
| 189 | + self.assertStdinRoundTrip('A͏B ﬖ̳AA̝') |
| 190 | + |
| 191 | + # bpo-38325 |
| 192 | + @unittest.skipIf(True, "Handling Non-BMP characters is broken") |
| 193 | + def test_input_nonbmp(self): |
| 194 | + # Non-BMP |
| 195 | + self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd') |
| 196 | + |
| 197 | + @requires_resource('console') |
| 198 | + def test_partial_reads(self): |
| 199 | + # Test that reading less than 1 full character works when stdin |
| 200 | + # contains multibyte UTF-8 sequences |
| 201 | + source = 'ϼўТλФЙ\r\n'.encode('utf-16-le') |
| 202 | + expected = 'ϼўТλФЙ\r\n'.encode('utf-8') |
| 203 | + for read_count in range(1, 16): |
| 204 | + with open('CONIN$', 'rb', buffering=0) as stdin: |
| 205 | + write_input(stdin, source) |
| 206 | + |
| 207 | + actual = b'' |
| 208 | + while not actual.endswith(b'\n'): |
| 209 | + b = stdin.read(read_count) |
| 210 | + actual += b |
| 211 | + |
| 212 | + self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
| 213 | + |
| 214 | + # bpo-38325 |
| 215 | + @unittest.skipIf(True, "Handling Non-BMP characters is broken") |
| 216 | + def test_partial_surrogate_reads(self): |
| 217 | + # Test that reading less than 1 full character works when stdin |
| 218 | + # contains surrogate pairs that cannot be decoded to UTF-8 without |
| 219 | + # reading an extra character. |
| 220 | + source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le') |
| 221 | + expected = '\U00101FFF\U00101001\r\n'.encode('utf-8') |
| 222 | + for read_count in range(1, 16): |
| 223 | + with open('CONIN$', 'rb', buffering=0) as stdin: |
| 224 | + write_input(stdin, source) |
| 225 | + |
| 226 | + actual = b'' |
| 227 | + while not actual.endswith(b'\n'): |
| 228 | + b = stdin.read(read_count) |
| 229 | + actual += b |
| 230 | + |
| 231 | + self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
| 232 | + |
| 233 | + @requires_resource('console') |
| 234 | + def test_ctrl_z(self): |
| 235 | + with open('CONIN$', 'rb', buffering=0) as stdin: |
| 236 | + source = '\xC4\x1A\r\n'.encode('utf-16-le') |
| 237 | + expected = '\xC4'.encode('utf-8') |
| 238 | + write_input(stdin, source) |
| 239 | + a, b = stdin.read(1), stdin.readall() |
| 240 | + self.assertEqual(expected[0:1], a) |
| 241 | + self.assertEqual(expected[1:], b) |
| 242 | + |
| 243 | +if __name__ == "__main__": |
| 244 | + unittest.main() |
0 commit comments