| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | '''Tests for WindowsConsoleIO
 | 
					
						
							|  |  |  |  | '''
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import io | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  | import os | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2017-02-04 15:07:46 -08:00
										 |  |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2024-01-25 23:00:52 +03:00
										 |  |  |  | from test.support import os_helper, requires_resource | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | if sys.platform != 'win32': | 
					
						
							|  |  |  |  |     raise unittest.SkipTest("test only relevant on win32") | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | from _testconsole import write_input | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | ConIO = io._WindowsConsoleIO | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | class WindowsConsoleIOTests(unittest.TestCase): | 
					
						
							|  |  |  |  |     def test_abc(self): | 
					
						
							|  |  |  |  |         self.assertTrue(issubclass(ConIO, io.RawIOBase)) | 
					
						
							|  |  |  |  |         self.assertFalse(issubclass(ConIO, io.BufferedIOBase)) | 
					
						
							|  |  |  |  |         self.assertFalse(issubclass(ConIO, io.TextIOBase)) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_open_fd(self): | 
					
						
							| 
									
										
										
										
											2017-02-04 15:07:46 -08:00
										 |  |  |  |         self.assertRaisesRegex(ValueError, | 
					
						
							|  |  |  |  |             "negative file descriptor", ConIO, -1) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 11:09:56 +02:00
										 |  |  |  |         with tempfile.TemporaryFile() as tmpfile: | 
					
						
							|  |  |  |  |             fd = tmpfile.fileno() | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  |             # Windows 10: "Cannot open non-console file" | 
					
						
							|  |  |  |  |             # Earlier: "Cannot open console output buffer for reading" | 
					
						
							| 
									
										
										
										
											2017-02-04 15:07:46 -08:00
										 |  |  |  |             self.assertRaisesRegex(ValueError, | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  |                 "Cannot open (console|non-console file)", ConIO, fd) | 
					
						
							| 
									
										
										
										
											2017-02-04 15:07:46 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             f = ConIO(0) | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         except ValueError: | 
					
						
							|  |  |  |  |             # cannot open console because it's not a real console | 
					
						
							|  |  |  |  |             pass | 
					
						
							|  |  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             self.assertTrue(f.readable()) | 
					
						
							|  |  |  |  |             self.assertFalse(f.writable()) | 
					
						
							|  |  |  |  |             self.assertEqual(0, f.fileno()) | 
					
						
							|  |  |  |  |             f.close()   # multiple close should not crash | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											2024-03-18 19:48:50 +08:00
										 |  |  |  |             with self.assertWarns(RuntimeWarning): | 
					
						
							|  |  |  |  |                 with ConIO(False): | 
					
						
							|  |  |  |  |                     pass | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             f = ConIO(1, 'w') | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         except ValueError: | 
					
						
							|  |  |  |  |             # cannot open console because it's not a real console | 
					
						
							|  |  |  |  |             pass | 
					
						
							|  |  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             self.assertFalse(f.readable()) | 
					
						
							|  |  |  |  |             self.assertTrue(f.writable()) | 
					
						
							|  |  |  |  |             self.assertEqual(1, f.fileno()) | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											2024-03-18 19:48:50 +08:00
										 |  |  |  |             with self.assertWarns(RuntimeWarning): | 
					
						
							|  |  |  |  |                 with ConIO(False): | 
					
						
							|  |  |  |  |                     pass | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             f = ConIO(2, 'w') | 
					
						
							| 
									
										
										
										
											2016-09-08 14:36:18 -07:00
										 |  |  |  |         except ValueError: | 
					
						
							|  |  |  |  |             # cannot open console because it's not a real console | 
					
						
							|  |  |  |  |             pass | 
					
						
							|  |  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2016-09-08 14:34:24 -07:00
										 |  |  |  |             self.assertFalse(f.readable()) | 
					
						
							|  |  |  |  |             self.assertTrue(f.writable()) | 
					
						
							|  |  |  |  |             self.assertEqual(2, f.fileno()) | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_open_name(self): | 
					
						
							| 
									
										
										
										
											2017-02-04 15:07:46 -08:00
										 |  |  |  |         self.assertRaises(ValueError, ConIO, sys.executable) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  |         f = ConIO("CON") | 
					
						
							|  |  |  |  |         self.assertTrue(f.readable()) | 
					
						
							|  |  |  |  |         self.assertFalse(f.writable()) | 
					
						
							|  |  |  |  |         self.assertIsNotNone(f.fileno()) | 
					
						
							|  |  |  |  |         f.close()   # multiple close should not crash | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         f = ConIO('CONIN$') | 
					
						
							|  |  |  |  |         self.assertTrue(f.readable()) | 
					
						
							|  |  |  |  |         self.assertFalse(f.writable()) | 
					
						
							|  |  |  |  |         self.assertIsNotNone(f.fileno()) | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         f = ConIO('CONOUT$', 'w') | 
					
						
							|  |  |  |  |         self.assertFalse(f.readable()) | 
					
						
							|  |  |  |  |         self.assertTrue(f.writable()) | 
					
						
							|  |  |  |  |         self.assertIsNotNone(f.fileno()) | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 06:17:13 -06:00
										 |  |  |  |         # bpo-45354: Windows 11 changed MS-DOS device name handling | 
					
						
							|  |  |  |  |         if sys.getwindowsversion()[:3] < (10, 0, 22000): | 
					
						
							|  |  |  |  |             f = open('C:/con', 'rb', buffering=0) | 
					
						
							|  |  |  |  |             self.assertIsInstance(f, ConIO) | 
					
						
							|  |  |  |  |             f.close() | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-10 04:39:36 +08:00
										 |  |  |  |     def test_subclass_repr(self): | 
					
						
							|  |  |  |  |         class TestSubclass(ConIO): | 
					
						
							|  |  |  |  |             pass | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         f = TestSubclass("CON") | 
					
						
							|  |  |  |  |         with f: | 
					
						
							|  |  |  |  |             self.assertIn(TestSubclass.__name__, repr(f)) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         self.assertIn(TestSubclass.__name__, repr(f)) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 14:50:17 -08:00
										 |  |  |  |     @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), | 
					
						
							|  |  |  |  |         "test does not work on Windows 7 and earlier") | 
					
						
							|  |  |  |  |     def test_conin_conout_names(self): | 
					
						
							|  |  |  |  |         f = open(r'\\.\conin$', 'rb', buffering=0) | 
					
						
							|  |  |  |  |         self.assertIsInstance(f, ConIO) | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 14:50:17 -08:00
										 |  |  |  |         f = open('//?/conout$', 'wb', buffering=0) | 
					
						
							|  |  |  |  |         self.assertIsInstance(f, ConIO) | 
					
						
							|  |  |  |  |         f.close() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     def test_conout_path(self): | 
					
						
							|  |  |  |  |         temp_path = tempfile.mkdtemp() | 
					
						
							| 
									
										
										
										
											2020-08-07 23:18:38 +08:00
										 |  |  |  |         self.addCleanup(os_helper.rmtree, temp_path) | 
					
						
							| 
									
										
										
										
											2017-02-06 14:50:17 -08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         conout_path = os.path.join(temp_path, 'CONOUT$') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         with open(conout_path, 'wb', buffering=0) as f: | 
					
						
							| 
									
										
										
										
											2021-10-05 06:17:13 -06:00
										 |  |  |  |             # bpo-45354: Windows 11 changed MS-DOS device name handling | 
					
						
							|  |  |  |  |             if (6, 1) < sys.getwindowsversion()[:3] < (10, 0, 22000): | 
					
						
							| 
									
										
										
										
											2017-02-06 14:50:17 -08:00
										 |  |  |  |                 self.assertIsInstance(f, ConIO) | 
					
						
							|  |  |  |  |             else: | 
					
						
							|  |  |  |  |                 self.assertNotIsInstance(f, ConIO) | 
					
						
							| 
									
										
										
										
											2017-02-04 16:46:34 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-24 18:55:51 +02:00
										 |  |  |  |     def test_write_empty_data(self): | 
					
						
							|  |  |  |  |         with ConIO('CONOUT$', 'w') as f: | 
					
						
							|  |  |  |  |             self.assertEqual(f.write(b''), 0) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  |     def assertStdinRoundTrip(self, text): | 
					
						
							|  |  |  |  |         stdin = open('CONIN$', 'r') | 
					
						
							|  |  |  |  |         old_stdin = sys.stdin | 
					
						
							|  |  |  |  |         try: | 
					
						
							|  |  |  |  |             sys.stdin = stdin | 
					
						
							|  |  |  |  |             write_input( | 
					
						
							|  |  |  |  |                 stdin.buffer.raw, | 
					
						
							|  |  |  |  |                 (text + '\r\n').encode('utf-16-le', 'surrogatepass') | 
					
						
							|  |  |  |  |             ) | 
					
						
							|  |  |  |  |             actual = input() | 
					
						
							|  |  |  |  |         finally: | 
					
						
							|  |  |  |  |             sys.stdin = old_stdin | 
					
						
							|  |  |  |  |         self.assertEqual(actual, text) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 23:00:52 +03:00
										 |  |  |  |     @requires_resource('console') | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  |     def test_input(self): | 
					
						
							|  |  |  |  |         # ASCII | 
					
						
							|  |  |  |  |         self.assertStdinRoundTrip('abc123') | 
					
						
							|  |  |  |  |         # Non-ASCII | 
					
						
							|  |  |  |  |         self.assertStdinRoundTrip('ϼўТλФЙ') | 
					
						
							|  |  |  |  |         # Combining characters | 
					
						
							|  |  |  |  |         self.assertStdinRoundTrip('A͏B ﬖ̳AA̝') | 
					
						
							| 
									
										
										
										
											2020-02-11 00:58:23 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     # bpo-38325 | 
					
						
							|  |  |  |  |     @unittest.skipIf(True, "Handling Non-BMP characters is broken") | 
					
						
							|  |  |  |  |     def test_input_nonbmp(self): | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  |         # Non-BMP | 
					
						
							|  |  |  |  |         self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd') | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 23:00:52 +03:00
										 |  |  |  |     @requires_resource('console') | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  |     def test_partial_reads(self): | 
					
						
							|  |  |  |  |         # Test that reading less than 1 full character works when stdin | 
					
						
							|  |  |  |  |         # contains multibyte UTF-8 sequences | 
					
						
							|  |  |  |  |         source = 'ϼўТλФЙ\r\n'.encode('utf-16-le') | 
					
						
							|  |  |  |  |         expected = 'ϼўТλФЙ\r\n'.encode('utf-8') | 
					
						
							|  |  |  |  |         for read_count in range(1, 16): | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |             with open('CONIN$', 'rb', buffering=0) as stdin: | 
					
						
							|  |  |  |  |                 write_input(stdin, source) | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |                 actual = b'' | 
					
						
							|  |  |  |  |                 while not actual.endswith(b'\n'): | 
					
						
							|  |  |  |  |                     b = stdin.read(read_count) | 
					
						
							|  |  |  |  |                     actual += b | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |                 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 00:58:23 +01:00
										 |  |  |  |     # bpo-38325 | 
					
						
							|  |  |  |  |     @unittest.skipIf(True, "Handling Non-BMP characters is broken") | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  |     def test_partial_surrogate_reads(self): | 
					
						
							|  |  |  |  |         # Test that reading less than 1 full character works when stdin | 
					
						
							|  |  |  |  |         # contains surrogate pairs that cannot be decoded to UTF-8 without | 
					
						
							|  |  |  |  |         # reading an extra character. | 
					
						
							|  |  |  |  |         source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le') | 
					
						
							|  |  |  |  |         expected = '\U00101FFF\U00101001\r\n'.encode('utf-8') | 
					
						
							|  |  |  |  |         for read_count in range(1, 16): | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |             with open('CONIN$', 'rb', buffering=0) as stdin: | 
					
						
							|  |  |  |  |                 write_input(stdin, source) | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |                 actual = b'' | 
					
						
							|  |  |  |  |                 while not actual.endswith(b'\n'): | 
					
						
							|  |  |  |  |                     b = stdin.read(read_count) | 
					
						
							|  |  |  |  |                     actual += b | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |                 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 23:00:52 +03:00
										 |  |  |  |     @requires_resource('console') | 
					
						
							| 
									
										
										
										
											2016-10-08 12:37:33 -07:00
										 |  |  |  |     def test_ctrl_z(self): | 
					
						
							|  |  |  |  |         with open('CONIN$', 'rb', buffering=0) as stdin: | 
					
						
							|  |  |  |  |             source = '\xC4\x1A\r\n'.encode('utf-16-le') | 
					
						
							|  |  |  |  |             expected = '\xC4'.encode('utf-8') | 
					
						
							|  |  |  |  |             write_input(stdin, source) | 
					
						
							|  |  |  |  |             a, b = stdin.read(1), stdin.readall() | 
					
						
							|  |  |  |  |             self.assertEqual(expected[0:1], a) | 
					
						
							|  |  |  |  |             self.assertEqual(expected[1:], b) | 
					
						
							| 
									
										
										
										
											2016-10-03 09:04:58 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-30 21:22:36 -07:00
										 |  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |  |     unittest.main() |