Debian Codegraph @

pypy/module/_io/test/test_win32consoleio.py

raw symbols SCIP
1 import os
2 import pytest
3
4 if os.name != 'nt':
5 pytest.skip('Windows only tests')
6 if not os.environ.get("PYPY_ENABLE_WINCONSOLEIO", False):
7 pytest.skip('_WindowsConsoleIO disabled')
8
9
10 from rpython.tool.udir import udir
11 from pypy.interpreter.gateway import interp2app
12 from pypy.module._io import interp_win32consoleio
13 from pypy.conftest import option
14 from rpython.rtyper.lltypesystem import rffi
15
16 class AppTestWinConsoleIO:
17 spaceconfig = dict(usemodules=['_io', '_cffi_backend'])
18
19 def setup_class(cls):
20 tmpfile = udir.join('tmpfile')
21 tmpfile.write("a\nb\nc", mode='wb')
22 cls.w_tmpfile = cls.space.wrap(str(tmpfile))
23 cls.w_posix = cls.space.appexec([], """():
24 import %s as m;
25 return m""" % os.name)
26 cls.w_conout_path = cls.space.wrap(str(udir.join('CONOUT$')))
27 if option.runappdirect:
28 def write_input(module, console, s):
29 from pypy.module._io.test._testconsole import write_input as tst_write_input
30 return tst_write_input(console, s)
31 cls.w_write_input = write_input
32 else:
33 def cls_write_input(w_console, w_s):
34 from lib_pypy._testconsole import write_input as tst_write_input
35 space = cls.space
36 handle = rffi.cast(rffi.INT_real, w_console.handle)
37 s = space.utf8_w(w_s).decode('utf-8')
38 return space.wrap(tst_write_input(handle, s))
39 cls.w_write_input = cls.space.wrap(interp2app(cls_write_input))
40
41 def test_open_fd(self):
42 import _io
43 os = self.posix
44 fd = os.open(self.tmpfile, os.O_RDONLY, 0o666)
45 #w_fd = self.tmpfile.fileno()
46 # Windows 10: "Cannot open non-console file"
47 # Earlier: "Cannot open console output buffer for reading"
48 raises(ValueError, _io._WindowsConsoleIO, fd)
49
50 raises(ValueError, _io._WindowsConsoleIO, -1)
51
52 try:
53 f = _io._WindowsConsoleIO(0)
54 except ValueError:
55 # cannot open console because it's not a real console
56 pass
57 else:
58 assert f.readable()
59 assert not f.writable()
60 assert 0 == f.fileno()
61 f.close() # multiple close should not crash
62 f.close()
63
64 try:
65 f = _io._WindowsConsoleIO(1, 'w')
66 except ValueError:
67 # cannot open console because it's not a real console
68 pass
69 else:
70 assert not f.readable()
71 assert f.writable()
72 assert 1 == f.fileno()
73 f.close()
74 f.close()
75
76 try:
77 f = _io._WindowsConsoleIO(2, 'w')
78 except ValueError:
79 # cannot open console because it's not a real console
80 pass
81 else:
82 assert not f.readable()
83 assert f.writable()
84 assert 2 == f.fileno()
85 f.close()
86 f.close()
87
88 def test_constructor(self):
89 import _io
90
91 f = _io._WindowsConsoleIO("CON")
92 assert f.readable()
93 assert not f.writable()
94 assert f.fileno() != None
95 f.close() # multiple close should not crash
96 f.close()
97
98 f = _io._WindowsConsoleIO('CONIN$')
99 assert f.readable()
100 assert not f.writable()
101 assert f.fileno() != None
102 f.close()
103 f.close()
104
105 f = _io._WindowsConsoleIO('CONOUT$', 'w')
106 assert not f.readable()
107 assert f.writable()
108 assert f.fileno() != None
109 f.close()
110 f.close()
111
112 f = open('C:\\con', 'rb', buffering=0)
113 assert isinstance(f,_io._WindowsConsoleIO)
114 f.close()
115
116 def test_conin_conout_names(self):
117 import _io
118 f = open(r'\\.\conin$', 'rb', buffering=0)
119 assert type(f) is _io._WindowsConsoleIO
120 f.close()
121
122 f = open('//?/conout$', 'wb', buffering=0)
123 assert isinstance(f , _io._WindowsConsoleIO)
124 f.close()
125
126 def test_conout_path(self):
127 import _io
128
129 with open(self.conout_path, 'wb', buffering=0) as f:
130 assert type(f) is _io._WindowsConsoleIO
131
132 def test_write_empty_data(self):
133 import _io
134 with _io._WindowsConsoleIO('CONOUT$', 'w') as f:
135 assert f.write(b'') == 0
136
137 def test_write_data(self):
138 import _io
139 with _io._WindowsConsoleIO('CONOUT$', 'w') as f:
140 assert f.write(b'abdefg') == 6
141 assert f.write(b'\r\r') == 2
142
143 @pytest.mark.skip('test hangs')
144 def test_partial_reads(self):
145 import _io
146 # Test that reading less than 1 full character works when stdin
147 # contains multibyte UTF-8 sequences. Converted to utf-16-le.
148 source = '\u03fc\u045e\u0422\u03bb\u0424\u0419\u005c\u0072\u005c\u006e'
149 # converted to utf-8
150 expected = '\xcf\xbc\xd1\x9e\xd0\xa2\xce\xbb\xd0\xa4\xd0\x99\x5c\x72\x5c\x6e'
151 for read_count in range(1, 16):
152 with open('CONIN$', 'rb', buffering=0) as stdin:
153 self.write_input(stdin, source)
154
155 actual = b''
156 while not actual.endswith(b'\n'):
157 b = stdin.read(read_count)
158 print('got', b)
159 actual += b
160
161 self.assertEqual(actual, expected, 'stdin.read({})'.format(read_count))
162
163 assert actual == source
164
165
166
167 class TestGetConsoleType:
168 def test_conout(self, space):
169 w_file = space.newtext('CONOUT$')
170 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_file)
171 assert consoletype == 'w'
172
173 def test_conin(self, space):
174 w_file = space.newtext('CONIN$')
175 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_file)
176 assert consoletype == 'r'
177
178 def test_con(self, space):
179 w_file = space.newtext('CON')
180 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_file)
181 assert consoletype == 'x'
182
183 def test_conin2(self, space):
184 w_file = space.newtext('\\\\.\\conin$')
185 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_file)
186 assert consoletype == 'r'
187
188 def test_con2(self, space):
189 w_file = space.newtext('\\\\?\\con')
190 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_file)
191 assert consoletype == 'x'
192
193 def test_invalid_fd(self, space):
194 # An invalid fd (e.g. 3 when not open) must return '\0', not raise.
195 w_fd = space.newint(3)
196 consoletype = interp_win32consoleio._pyio_get_console_type(space, w_fd)
197 assert consoletype == '\0'