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