Debian Codegraph @

pypy/module/_io/test/test_interp_textio.py

raw symbols SCIP
1 import pytest
2 try:
3 from hypothesis import given, strategies as st, settings, example
4 except ImportError:
5 pytest.skip("hypothesis required")
6 import os
7 from rpython.rlib.rbigint import rbigint
8 from pypy.module._io.interp_bytesio import W_BytesIO
9 from pypy.module._io.interp_textio import (W_TextIOWrapper, DecodeBuffer,
10 W_IncrementalNewlineDecoder, PositionCookie,
11 SEEN_CR, SEEN_LF)
12
13 # workaround suggestion for slowness by David McIver:
14 # force hypothesis to initialize some lazy stuff
15 # (which takes a lot of time, which trips the timer otherwise)
16 st.text().example()
17
18 def translate_newlines(text):
19 text = text.replace(u'\r\n', u'\n')
20 text = text.replace(u'\r', u'\n')
21 return text.replace(u'\n', os.linesep)
22
23 @st.composite
24 def st_readline(
25 draw,
26 st_nlines=st.integers(min_value=0, max_value=10),
27 characters=st.characters(blacklist_characters=u'\r\n')):
28 n_lines = draw(st_nlines)
29 fragments = []
30 limits = []
31 for _ in range(n_lines):
32 line = draw(st.text(characters))
33 fragments.append(line)
34 ending = draw(st.sampled_from([u'\n', u'\r', u'\r\n']))
35 fragments.append(ending)
36 limit = draw(st.integers(min_value=0, max_value=len(line) + 5))
37 limits.append(limit)
38 limits.append(-1)
39 return (u''.join(fragments), limits)
40
41 def test_newlines_bug(space):
42 import _io
43 w_stream = W_BytesIO(space)
44 w_stream.descr_init(space, space.newbytes(b"a\nb\nc\r"))
45 w_textio = W_TextIOWrapper(space)
46 w_textio.descr_init(
47 space, w_stream,
48 encoding='utf-8', w_errors=space.newtext('surrogatepass'),
49 w_newline=None)
50 w_textio.read_w(space)
51 assert w_textio.w_decoder.seennl == SEEN_LF | SEEN_CR
52
53 @given(data=st_readline(),
54 mode=st.sampled_from(['\r', '\n', '\r\n', '']))
55 @settings(deadline=None, database=None)
56 @example(data=(u'\n\r\n', [0, -1, 2, -1, 0, -1]), mode='\r')
57 def test_readline(space, data, mode):
58 txt, limits = data
59 w_stream = W_BytesIO(space)
60 w_stream.descr_init(space, space.newbytes(txt.encode('utf-8')))
61 w_textio = W_TextIOWrapper(space)
62 w_textio.descr_init(
63 space, w_stream,
64 encoding='utf-8', w_errors=space.newtext('surrogatepass'),
65 w_newline=space.newtext(mode))
66 lines = []
67 for limit in limits:
68 w_line = w_textio.readline_w(space, space.newint(limit))
69 line = space.utf8_w(w_line).decode('utf-8')
70 if limit >= 0:
71 assert len(line) <= limit
72 if line:
73 lines.append(line)
74 elif limit:
75 break
76 assert txt.startswith(u''.join(lines))
77
78 @given(data=st_readline())
79 @settings(deadline=None, database=None)
80 @example(data=(u'\n\r\n', [0, -1, 2, -1, 0, -1]))
81 def test_readline_none(space, data):
82 txt, limits = data
83 w_stream = W_BytesIO(space)
84 w_stream.descr_init(space, space.newbytes(txt.encode('utf-8')))
85 w_textio = W_TextIOWrapper(space)
86 w_textio.descr_init(
87 space, w_stream,
88 encoding='utf-8', w_errors=space.newtext('surrogatepass'),
89 w_newline=space.w_None)
90 lines = []
91 for limit in limits:
92 w_line = w_textio.readline_w(space, space.newint(limit))
93 line = space.utf8_w(w_line).decode('utf-8')
94 if limit >= 0:
95 assert len(line) <= limit
96 if line:
97 lines.append(line)
98 elif limit:
99 break
100 output = txt.replace("\r\n", "\n").replace("\r", "\n")
101
102 assert output.startswith(u''.join(lines))
103 @given(data=st_readline())
104 @settings(deadline=None, database=None)
105 @example(data=(u'\n\r\n', [0, -1, 2, -1, 0, -1]))
106 def test_incremental_decoder(space, data):
107 txt, limits = data
108 w_dec = W_IncrementalNewlineDecoder(space)
109 w_dec.descr_init(space, space.w_None, 1)
110 w_txt = space.newutf8(txt.encode("utf-8"), len(txt))
111 w_res = w_dec.decode_w(space, w_txt, True)
112 line = space.utf8_w(w_res).decode('utf-8')
113 output = txt.replace(u"\r\n", u"\n").replace(u"\r", u"\n")
114 assert line == output
115
116 w_dec.descr_init(space, space.w_None, 0)
117 w_txt = space.newutf8(txt.encode("utf-8"), len(txt))
118 w_res = w_dec.decode_w(space, w_txt, True)
119 line = space.utf8_w(w_res).decode('utf-8')
120 assert line == txt
121
122 @given(st.text())
123 def test_read_buffer(text):
124 buf = DecodeBuffer(text.encode('utf8'), len(text))
125 chars, size = buf.get_chars(-1)
126 assert chars.decode('utf8') == text
127 assert len(text) == size
128 assert buf.exhausted()
129
130 @given(st.text(), st.lists(st.integers(min_value=0)))
131 @example(u'\x80', [1])
132 def test_readn_buffer(text, sizes):
133 buf = DecodeBuffer(text.encode('utf8'), len(text))
134 strings = []
135 for n in sizes:
136 chars, size = buf.get_chars(n)
137 s = chars.decode('utf8')
138 assert size == len(s)
139 if not buf.exhausted():
140 assert len(s) == n
141 else:
142 assert len(s) <= n
143 strings.append(s)
144 assert ''.join(strings) == text[:sum(sizes)]
145
146 @given(content=st_readline(characters=st.characters(blacklist_categories='C')))
147 def test_tell(space, content):
148 txt, limits = content
149
150 restxt = translate_newlines(txt)
151 resstr = restxt.encode('utf-8')
152 for read_chars_before_seeking in range(len(restxt)):
153 w_stream = W_BytesIO(space)
154 w_stream.descr_init(space, space.newbytes(txt.encode('utf-8')))
155
156 w_textio = W_TextIOWrapper(space)
157 w_textio.descr_init(
158 space, w_stream,
159 encoding='utf-8'
160 )
161
162 w_res1 = w_textio.read_w(space, space.newint(read_chars_before_seeking))
163 assert space.len_w(w_res1) <= read_chars_before_seeking
164 w_tell = w_textio.tell_w(space)
165 w_textio.seek_w(space, space.newint(0)) # seek to beginning
166 w_textio.seek_w(space, w_tell) # then back to position
167 w_res2 = w_textio.read_w(space)
168 w_res = space.add(w_res1, w_res2)
169 res = space.text_w(w_res)
170 assert res == resstr
171
172 @given(content=st_readline(characters=st.characters(blacklist_categories='C')))
173 def test_getstate_setstate(space, content):
174 txt, limits = content
175
176 restxt = translate_newlines(txt)
177 for read_chars_before_getstate in range(len(restxt)):
178 w_stream = W_BytesIO(space)
179 w_stream.descr_init(space, space.newbytes(txt.encode('utf-8')))
180
181 w_textio = W_TextIOWrapper(space)
182 w_textio.descr_init(
183 space, w_stream,
184 encoding='utf-8'
185 )
186 w_state_start = w_textio.w_decoder.getstate_w(space)
187 w_res1 = w_textio.read_w(space, space.newint(read_chars_before_getstate))
188 w_current_state = w_textio.w_decoder.getstate_w(space)
189 w_textio.w_decoder.setstate_w(space, w_state_start)
190 w_textio.w_decoder.setstate_w(space, w_current_state)
191 w_res2 = w_textio.read_w(space)
192 w_res = space.add(w_res1, w_res2)
193 res = space.text_w(w_res).decode("utf-8")
194 assert res == restxt
195
196 def test_cookie(space):
197 val1 = eval("0x" + "1" * 128)
198 cookie1 = PositionCookie(rbigint.fromlong(val1))
199 val2 = cookie1.pack()
200 cookie = PositionCookie(val2)
201 assert cookie.start_pos > 0
202 assert cookie.dec_flags > 0
203 assert cookie.bytes_to_feed > 0
204 assert cookie.chars_to_skip > 0
205 assert cookie.need_eof > 0
206 assert cookie.start_pos == cookie.start_pos
207 assert cookie.dec_flags == cookie.dec_flags
208 assert cookie.bytes_to_feed == cookie.bytes_to_feed
209 assert cookie.chars_to_skip == cookie.chars_to_skip
210 assert cookie.need_eof == cookie.need_eof