blob: a78fa4d7d919fffb8846651523120b9956c74f56 [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
28 fd, _ = tempfile.mkstemp()
29 try:
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 finally:
35 os.close(fd)
36
Steve Dower33df0c32016-09-08 14:36:18 -070037 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070038 f = ConIO(0)
Steve Dower33df0c32016-09-08 14:36:18 -070039 except ValueError:
40 # cannot open console because it's not a real console
41 pass
42 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070043 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 Dower39294992016-08-30 21:22:36 -070048
Steve Dower33df0c32016-09-08 14:36:18 -070049 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070050 f = ConIO(1, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070051 except ValueError:
52 # cannot open console because it's not a real console
53 pass
54 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070055 self.assertFalse(f.readable())
56 self.assertTrue(f.writable())
57 self.assertEqual(1, f.fileno())
58 f.close()
59 f.close()
Steve Dower39294992016-08-30 21:22:36 -070060
Steve Dower33df0c32016-09-08 14:36:18 -070061 try:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070062 f = ConIO(2, 'w')
Steve Dower33df0c32016-09-08 14:36:18 -070063 except ValueError:
64 # cannot open console because it's not a real console
65 pass
66 else:
Steve Dowerf09e2fa2016-09-08 14:34:24 -070067 self.assertFalse(f.readable())
68 self.assertTrue(f.writable())
69 self.assertEqual(2, f.fileno())
70 f.close()
71 f.close()
Steve Dower39294992016-08-30 21:22:36 -070072
73 def test_open_name(self):
Steve Dower722e3e22017-02-04 15:07:46 -080074 self.assertRaises(ValueError, ConIO, sys.executable)
75
Steve Dower39294992016-08-30 21:22:36 -070076 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 Dower0c8ee602017-02-04 16:46:34 -080097 f = open('C:/con', 'rb', buffering=0)
98 self.assertIsInstance(f, ConIO)
99 f.close()
100
Steve Dower2dfa6cb2017-02-06 14:50:17 -0800101 @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 Dower0c8ee602017-02-04 16:46:34 -0800107
Steve Dower2dfa6cb2017-02-06 14:50:17 -0800108 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 Dower0c8ee602017-02-04 16:46:34 -0800123
Serhiy Storchaka42c35d92018-02-24 18:55:51 +0200124 def test_write_empty_data(self):
125 with ConIO('CONOUT$', 'w') as f:
126 self.assertEqual(f.write(b''), 0)
127
Steve Dower312cef72016-10-03 09:04:58 -0700128 def assertStdinRoundTrip(self, text):
129 stdin = open('CONIN$', 'r')
130 old_stdin = sys.stdin
131 try:
132 sys.stdin = stdin
133 write_input(
134 stdin.buffer.raw,
135 (text + '\r\n').encode('utf-16-le', 'surrogatepass')
136 )
137 actual = input()
138 finally:
139 sys.stdin = old_stdin
140 self.assertEqual(actual, text)
141
142 def test_input(self):
143 # ASCII
144 self.assertStdinRoundTrip('abc123')
145 # Non-ASCII
146 self.assertStdinRoundTrip('ϼўТλФЙ')
147 # Combining characters
148 self.assertStdinRoundTrip('A͏B ﬖ̳AA̝')
149 # Non-BMP
150 self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd')
151
152 def test_partial_reads(self):
153 # Test that reading less than 1 full character works when stdin
154 # contains multibyte UTF-8 sequences
155 source = 'ϼўТλФЙ\r\n'.encode('utf-16-le')
156 expected = 'ϼўТλФЙ\r\n'.encode('utf-8')
157 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700158 with open('CONIN$', 'rb', buffering=0) as stdin:
159 write_input(stdin, source)
Steve Dower312cef72016-10-03 09:04:58 -0700160
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700161 actual = b''
162 while not actual.endswith(b'\n'):
163 b = stdin.read(read_count)
164 actual += b
Steve Dower312cef72016-10-03 09:04:58 -0700165
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700166 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
Steve Dower312cef72016-10-03 09:04:58 -0700167
168 def test_partial_surrogate_reads(self):
169 # Test that reading less than 1 full character works when stdin
170 # contains surrogate pairs that cannot be decoded to UTF-8 without
171 # reading an extra character.
172 source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le')
173 expected = '\U00101FFF\U00101001\r\n'.encode('utf-8')
174 for read_count in range(1, 16):
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700175 with open('CONIN$', 'rb', buffering=0) as stdin:
176 write_input(stdin, source)
177
178 actual = b''
179 while not actual.endswith(b'\n'):
180 b = stdin.read(read_count)
181 actual += b
182
183 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
184
185 def test_ctrl_z(self):
186 with open('CONIN$', 'rb', buffering=0) as stdin:
187 source = '\xC4\x1A\r\n'.encode('utf-16-le')
188 expected = '\xC4'.encode('utf-8')
Steve Dower312cef72016-10-03 09:04:58 -0700189 write_input(stdin, source)
Steve Dowerc6f9b2b2016-10-08 12:37:33 -0700190 a, b = stdin.read(1), stdin.readall()
191 self.assertEqual(expected[0:1], a)
192 self.assertEqual(expected[1:], b)
Steve Dower312cef72016-10-03 09:04:58 -0700193
Steve Dower39294992016-08-30 21:22:36 -0700194if __name__ == "__main__":
195 unittest.main()