| 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 |
try: |
| 15 |
from _testconsole import write_input |
| 16 |
except Exception: |
| 17 |
write_input = None |
| 18 |
|
| 19 |
ConIO = io._WindowsConsoleIO |
| 20 |
|
| 21 |
class WindowsConsoleIOTests(unittest.TestCase): |
| 22 |
def test_abc(self): |
| 23 |
self.assertTrue(issubclass(ConIO, io.RawIOBase)) |
| 24 |
self.assertFalse(issubclass(ConIO, io.BufferedIOBase)) |
| 25 |
self.assertFalse(issubclass(ConIO, io.TextIOBase)) |
| 26 |
|
| 27 |
def test_open_fd(self): |
| 28 |
self.assertRaisesRegex(ValueError, |
| 29 |
"negative file descriptor", ConIO, -1) |
| 30 |
|
| 31 |
with tempfile.TemporaryFile() as tmpfile: |
| 32 |
fd = tmpfile.fileno() |
| 33 |
|
| 34 |
|
| 35 |
self.assertRaisesRegex(ValueError, |
| 36 |
"Cannot open (console|non-console file)", ConIO, fd) |
| 37 |
|
| 38 |
try: |
| 39 |
f = ConIO(0) |
| 40 |
except ValueError: |
| 41 |
|
| 42 |
pass |
| 43 |
else: |
| 44 |
self.assertTrue(f.readable()) |
| 45 |
self.assertFalse(f.writable()) |
| 46 |
self.assertEqual(0, f.fileno()) |
| 47 |
f.close() |
| 48 |
f.close() |
| 49 |
|
| 50 |
try: |
| 51 |
f = ConIO(1, 'w') |
| 52 |
except ValueError: |
| 53 |
|
| 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 |
|
| 62 |
try: |
| 63 |
f = ConIO(2, 'w') |
| 64 |
except ValueError: |
| 65 |
|
| 66 |
pass |
| 67 |
else: |
| 68 |
self.assertFalse(f.readable()) |
| 69 |
self.assertTrue(f.writable()) |
| 70 |
self.assertEqual(2, f.fileno()) |
| 71 |
f.close() |
| 72 |
f.close() |
| 73 |
|
| 74 |
def test_open_name(self): |
| 75 |
self.assertRaises(ValueError, ConIO, sys.executable) |
| 76 |
|
| 77 |
f = ConIO("CON") |
| 78 |
self.assertTrue(f.readable()) |
| 79 |
self.assertFalse(f.writable()) |
| 80 |
self.assertIsNotNone(f.fileno()) |
| 81 |
f.close() |
| 82 |
f.close() |
| 83 |
|
| 84 |
f = ConIO('CONIN$') |
| 85 |
self.assertTrue(f.readable()) |
| 86 |
self.assertFalse(f.writable()) |
| 87 |
self.assertIsNotNone(f.fileno()) |
| 88 |
f.close() |
| 89 |
f.close() |
| 90 |
|
| 91 |
f = ConIO('CONOUT$', 'w') |
| 92 |
self.assertFalse(f.readable()) |
| 93 |
self.assertTrue(f.writable()) |
| 94 |
self.assertIsNotNone(f.fileno()) |
| 95 |
f.close() |
| 96 |
f.close() |
| 97 |
|
| 98 |
|
| 99 |
if sys.getwindowsversion()[:3] < (10, 0, 22000): |
| 100 |
f = open('C:/con', 'rb', buffering=0) |
| 101 |
self.assertIsInstance(f, ConIO) |
| 102 |
f.close() |
| 103 |
|
| 104 |
@unittest.skipIf(sys.getwindowsversion()[:2] <= (6, 1), |
| 105 |
"test does not work on Windows 7 and earlier") |
| 106 |
def test_conin_conout_names(self): |
| 107 |
f = open(r'\\.\conin$', 'rb', buffering=0) |
| 108 |
self.assertIsInstance(f, ConIO) |
| 109 |
f.close() |
| 110 |
|
| 111 |
f = open('//?/conout$', 'wb', buffering=0) |
| 112 |
self.assertIsInstance(f, ConIO) |
| 113 |
f.close() |
| 114 |
|
| 115 |
def test_conout_path(self): |
| 116 |
temp_path = tempfile.mkdtemp() |
| 117 |
self.addCleanup(os_helper.rmtree, temp_path) |
| 118 |
|
| 119 |
conout_path = os.path.join(temp_path, 'CONOUT$') |
| 120 |
|
| 121 |
with open(conout_path, 'wb', buffering=0) as f: |
| 122 |
|
| 123 |
if (6, 1) < sys.getwindowsversion()[:3] < (10, 0, 22000): |
| 124 |
self.assertIsInstance(f, ConIO) |
| 125 |
else: |
| 126 |
self.assertNotIsInstance(f, ConIO) |
| 127 |
|
| 128 |
def test_write_empty_data(self): |
| 129 |
with ConIO('CONOUT$', 'w') as f: |
| 130 |
self.assertEqual(f.write(b''), 0) |
| 131 |
|
| 132 |
@unittest.skipIf(not write_input, "no _testconsole.write_input") |
| 133 |
def assertStdinRoundTrip(self, text): |
| 134 |
stdin = open('CONIN$', 'r') |
| 135 |
old_stdin = sys.stdin |
| 136 |
try: |
| 137 |
sys.stdin = stdin |
| 138 |
write_input( |
| 139 |
stdin.buffer.raw, |
| 140 |
(text + '\r\n').encode('utf-16-le', 'surrogatepass') |
| 141 |
) |
| 142 |
actual = input() |
| 143 |
finally: |
| 144 |
sys.stdin = old_stdin |
| 145 |
self.assertEqual(actual, text) |
| 146 |
|
| 147 |
@requires_resource('console') |
| 148 |
def test_input(self): |
| 149 |
|
| 150 |
self.assertStdinRoundTrip('abc123') |
| 151 |
|
| 152 |
self.assertStdinRoundTrip('ϼўТλФЙ') |
| 153 |
|
| 154 |
self.assertStdinRoundTrip('A͏B ﬖ̳AA̝') |
| 155 |
|
| 156 |
|
| 157 |
@unittest.skipIf(True, "Handling Non-BMP characters is broken") |
| 158 |
def test_input_nonbmp(self): |
| 159 |
|
| 160 |
self.assertStdinRoundTrip('\U00100000\U0010ffff\U0010fffd') |
| 161 |
|
| 162 |
@requires_resource('console') |
| 163 |
@unittest.skipIf(not write_input, "no _testconsole.write_input") |
| 164 |
def test_partial_reads(self): |
| 165 |
|
| 166 |
|
| 167 |
source = 'ϼўТλФЙ\r\n'.encode('utf-16-le') |
| 168 |
expected = 'ϼўТλФЙ\r\n'.encode('utf-8') |
| 169 |
for read_count in range(1, 16): |
| 170 |
with open('CONIN$', 'rb', buffering=0) as stdin: |
| 171 |
write_input(stdin, source) |
| 172 |
|
| 173 |
actual = b'' |
| 174 |
while not actual.endswith(b'\n'): |
| 175 |
b = stdin.read(read_count) |
| 176 |
actual += b |
| 177 |
|
| 178 |
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
| 179 |
|
| 180 |
|
| 181 |
@unittest.skipIf(True, "Handling Non-BMP characters is broken") |
| 182 |
def test_partial_surrogate_reads(self): |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
source = '\U00101FFF\U00101001\r\n'.encode('utf-16-le') |
| 187 |
expected = '\U00101FFF\U00101001\r\n'.encode('utf-8') |
| 188 |
for read_count in range(1, 16): |
| 189 |
with open('CONIN$', 'rb', buffering=0) as stdin: |
| 190 |
write_input(stdin, source) |
| 191 |
|
| 192 |
actual = b'' |
| 193 |
while not actual.endswith(b'\n'): |
| 194 |
b = stdin.read(read_count) |
| 195 |
actual += b |
| 196 |
|
| 197 |
self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count)) |
| 198 |
|
| 199 |
@requires_resource('console') |
| 200 |
@unittest.skipIf(not write_input, "no _testconsole.write_input") |
| 201 |
def test_ctrl_z(self): |
| 202 |
with open('CONIN$', 'rb', buffering=0) as stdin: |
| 203 |
source = '\xC4\x1A\r\n'.encode('utf-16-le') |
| 204 |
expected = '\xC4'.encode('utf-8') |
| 205 |
write_input(stdin, source) |
| 206 |
a, b = stdin.read(1), stdin.readall() |
| 207 |
self.assertEqual(expected[0:1], a) |
| 208 |
self.assertEqual(expected[1:], b) |
| 209 |
|
| 210 |
if __name__ == "__main__": |
| 211 |
unittest.main() |