blob: 9a61e48881d903ca0cf98bad049cc86b9c86674b [file] [log] [blame]
Steve Dower39294992016-08-30 21:22:36 -07001'''Tests for WindowsConsoleIO
Steve Dower39294992016-08-30 21:22:36 -07002'''
3
4import io
Steve Dower0c8ee602017-02-04 16:46:34 -08005import os
Steve Dower39294992016-08-30 21:22:36 -07006import sys
Steve Dower722e3e22017-02-04 15:07:46 -08007import tempfile
Steve Dower0c8ee602017-02-04 16:46:34 -08008import unittest
Steve Dower2dfa6cb2017-02-06 14:50:17 -08009from test import support
Steve Dower39294992016-08-30 21:22:36 -070010
11if sys.platform != 'win32':
12 raise unittest.SkipTest("test only relevant on win32")
13
Steve Dower312cef72016-10-03 09:04:58 -070014from _testconsole import write_input
15
Steve Dower39294992016-08-30 21:22:36 -070016ConIO = io._WindowsConsoleIO
17
18class 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 Dower722e3e22017-02-04 15:07:46 -080025 self.assertRaisesRegex(ValueError,
26 "negative file descriptor", ConIO, -1)
27
Miss Islington (bot)a2a807f2019-07-03 02:31:39 -070028 with tempfile.TemporaryFile() as tmpfile:
29 fd = tmpfile.fileno()
Steve Dower0c8ee602017-02-04 16:46:34 -080030 # Windows 10: "Cannot open non-console file"
31 # Earlier: "Cannot open console output buffer for reading"
Steve Dower722e3e22017-02-04 15:07:46 -080032 self.assertRaisesRegex(ValueError,
Steve Dower0c8ee602017-02-04 16:46:34 -080033 "Cannot open (console|non-console file)", ConIO, fd)
Steve Dower722e3e22017-02-04 15:07:46 -080034
Steve Dower33df0c32016-09-08 14:36:18 -070035 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070036 f = ConIO(0)
Steve Dower33df0c32016-09-08 14:36:18 -070037 except ValueError:
38 # cannot open console because it's not a real console
39 pass
40 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070041 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()
Steve Dower39294992016-08-30 21:22:36 -070046
Steve Dower33df0c32016-09-08 14:36:18 -070047 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070048 f = ConIO(1, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070049 except ValueError:
50 # cannot open console because it's not a real console
51 pass
52 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070053 self.assertFalse(f.readable())
54 self.assertTrue(f.writable())
55 self.assertEqual(1, f.fileno())
56 f.close()
57 f.close()
Steve Dower39294992016-08-30 21:22:36 -070058
Steve Dower33df0c32016-09-08 14:36:18 -070059 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070060 f = ConIO(2, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070061 except ValueError:
62 # cannot open console because it's not a real console
63 pass
64 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070065 self.assertFalse(f.readable())
66 self.assertTrue(f.writable())
67 self.assertEqual(2, f.fileno())
68 f.close()
69 f.close()
Steve Dower39294992016-08-30 21:22:36 -070070
71 def test_open_name(self):
Steve Dower722e3e22017-02-04 15:07:46 -080072 self.assertRaises(ValueError, ConIO, sys.executable)
73
Steve Dower39294992016-08-30 21:22:36 -070074 f = ConIO("CON")
75 self.assertTrue(f.readable())
76 self.assertFalse(f.writable())
77 self.assertIsNotNone(f.fileno())
78 f.close() # multiple close should not crash
79 f.close()
80
81 f = ConIO('CONIN$')
82 self.assertTrue(f.readable())
83 self.assertFalse(f.writable())
84 self.assertIsNotNone(f.fileno())
85 f.close()
86 f.close()
87
88 f = ConIO('CONOUT$', 'w')
89 self.assertFalse(f.readable())
90 self.assertTrue(f.writable())
91 self.assertIsNotNone(f.fileno())
92 f.close()
93 f.close()
94
Steve Dower0c8ee602017-02-04 16:46:34 -080095 f = open('C:/con', 'rb', buffering=0)
96 self.assertIsInstance(f, ConIO)
97 f.close()
98
Steve Dower2dfa6cb2017-02-06 14:50:17 -080099 @unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1),
100 "test does not work on Windows 7 and earlier")
101 def test_conin_conout_names(self):
102 f = open(r'\\.\conin$', 'rb', buffering=0)
103 self.assertIsInstance(f, ConIO)
104 f.close()
Steve Dower0c8ee602017-02-04 16:46:34 -0800105
Steve Dower2dfa6cb2017-02-06 14:50:17 -0800106 f = open('//?/conout$', 'wb', buffering=0)
107 self.assertIsInstance(f, ConIO)
108 f.close()
109
110 def test_conout_path(self):
111 temp_path = tempfile.mkdtemp()
112 self.addCleanup(support.rmtree, temp_path)
113
114 conout_path = os.path.join(temp_path, 'CONOUT$')
115
116 with open(conout_path, 'wb', buffering=0) as f:
117 if sys.getwindowsversion()[:2] > (6, 1):
118 self.assertIsInstance(f, ConIO)
119 else:
120 self.assertNotIsInstance(f, ConIO)
Steve Dower0c8ee602017-02-04 16:46:34 -0800121
Serhiy Storchaka42c35d92018-02-24 18:55:51 +0200122 def test_write_empty_data(self):
123 with ConIO('CONOUT$', 'w') as f:
124 self.assertEqual(f.write(b''), 0)
125
Steve Dower312cef72016-10-03 09:04:58 -0700126 def assertStdinRoundTrip(self, text):
127 stdin = open('CONIN$', 'r')
128 old_stdin = sys.stdin
129 try:
130 sys.stdin = stdin
131 write_input(
132 stdin.buffer.raw,
133 (text + '\r\n').encode('utf-16-le', 'surrogatepass')
134 )
135 actual = input()
136 finally:
137 sys.stdin = old_stdin
138 self.assertEqual(actual, text)
139
140 def test_input(self):
141 # ASCII
142 self.assertStdinRoundTrip('abc123')
143 # Non-ASCII
144 self.assertStdinRoundTrip('ϼўТλФЙ')
145 # Combining characters
146 self.assertStdinRoundTrip('A͏B ﬖ̳AA̝')
147 # Non-BMP
148 self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
149
150 def test_partial_reads(self):
151 # Test that reading less than 1 full character works when stdin
152 # contains multibyte UTF-8 sequences
153 source = 'ϼўТλФЙ\r\n'.encode('utf-16-le')
154 expected = 'ϼўТλФЙ\r\n'.encode('utf-8')
155 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700156 with open('CONIN$', 'rb', buffering=0) as stdin:
157 write_input(stdin, source)
Steve Dower312cef72016-10-03 09:04:58 -0700158
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700159 actual = b''
160 while not actual.endswith(b'\n'):
161 b = stdin.read(read_count)
162 actual += b
Steve Dower312cef72016-10-03 09:04:58 -0700163
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700164 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
Steve Dower312cef72016-10-03 09:04:58 -0700165
166 def test_partial_surrogate_reads(self):
167 # Test that reading less than 1 full character works when stdin
168 # contains surrogate pairs that cannot be decoded to UTF-8 without
169 # reading an extra character.
170 source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
171 expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
172 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700173 with open('CONIN$', 'rb', buffering=0) as stdin:
174 write_input(stdin, source)
175
176 actual = b''
177 while not actual.endswith(b'\n'):
178 b = stdin.read(read_count)
179 actual += b
180
181 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
182
183 def test_ctrl_z(self):
184 with open('CONIN$', 'rb', buffering=0) as stdin:
185 source = '\xC4\x1A\r\n'.encode('utf-16-le')
186 expected = '\xC4'.encode('utf-8')
Steve Dower312cef72016-10-03 09:04:58 -0700187 write_input(stdin, source)
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700188 a, b = stdin.read(1), stdin.readall()
189 self.assertEqual(expected[0:1], a)
190 self.assertEqual(expected[1:], b)
Steve Dower312cef72016-10-03 09:04:58 -0700191
Steve Dower39294992016-08-30 21:22:36 -0700192if __name__ == "__main__":
193 unittest.main()