File: apptest_stringio.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (367 lines) | stat: -rw-r--r-- 8,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from pytest import raises
from _io import StringIO

def test_stringio():
    sio = StringIO()
    sio.write(u'Hello ')
    sio.write(u'world')
    assert sio.getvalue() == u'Hello world'

    assert StringIO(u"hello").read() == u'hello'

def test_capabilities():
    sio = StringIO()
    assert sio.readable()
    assert sio.writable()
    assert sio.seekable()
    assert not sio.isatty()
    assert not sio.closed
    assert not sio.line_buffering
    sio.close()
    raises(ValueError, sio.readable)
    raises(ValueError, sio.writable)
    raises(ValueError, sio.seekable)
    raises(ValueError, sio.isatty)
    assert sio.closed
    assert sio.errors is None

def test_closed():
    sio = StringIO()
    sio.close()
    raises(ValueError, sio.read, 1)
    raises(ValueError, sio.write, u"text")

def test_read():
    buf = u"1234567890"
    sio = StringIO(buf)

    assert sio.read(0) == ''
    assert buf[:1] == sio.read(1)
    assert buf[1:5] == sio.read(4)
    assert buf[5:] == sio.read(900)
    assert u"" == sio.read()

def test_read_binary():
    # data is from a test_imghdr test for a GIF file
    buf_in = (u'\x47\x49\x46\x38\x39\x61\x10\x00\x10\x00\xf6\x64\x00\xeb'
              u'\xbb\x18\xeb\xbe\x21\xf3\xc1\x1a\xfa\xc7\x19\xfd\xcb\x1b'
              u'\xff\xcc\x1c\xeb')
    assert len(buf_in) == 32
    sio = StringIO(buf_in)
    buf_out = sio.read(32)
    assert buf_in == buf_out

def test_readline():
    sio = StringIO(u'123\n456')
    assert sio.readline(0) == ''
    assert sio.readline(2) == '12'
    assert sio.readline(None) == '3\n'
    assert sio.readline() == '456'

def test_seek():

    s = u"1234567890"
    sio = StringIO(s)

    sio.read(5)
    sio.seek(0)
    r = sio.read()
    assert r == s

    sio.seek(3)
    r = sio.read()
    assert r == s[3:]
    raises(TypeError, sio.seek, 0.0)

    exc_info = raises(ValueError, sio.seek, -3)
    assert exc_info.value.args[0] == "Negative seek position -3"

    raises(ValueError, sio.seek, 3, -1)
    raises(ValueError, sio.seek, 3, -3)

    sio.close()
    raises(ValueError, sio.seek, 0)

def test_overseek():

    s = u"1234567890"
    sio = StringIO(s)

    res = sio.seek(11)
    assert res == 11
    res = sio.read()
    assert res == u""
    assert sio.tell() == 11
    assert sio.getvalue() == s
    sio.write(u"")
    assert sio.getvalue() == s
    sio.write(s)
    assert sio.getvalue() == s + u"\0" + s

def test_tell():

    s = u"1234567890"
    sio = StringIO(s)

    assert sio.tell() == 0
    sio.seek(5)
    assert sio.tell() == 5
    sio.seek(10000)
    assert sio.tell() == 10000

    sio.close()
    raises(ValueError, sio.tell)

def test_truncate():

    s = u"1234567890"
    sio = StringIO(s)

    raises(ValueError, sio.truncate, -1)
    sio.seek(6)
    res = sio.truncate()
    assert res == 6
    assert sio.getvalue() == s[:6]
    res = sio.truncate(4)
    assert res == 4
    assert sio.getvalue() == s[:4]
    assert sio.tell() == 6
    sio.seek(0, 2)
    sio.write(s)
    assert sio.getvalue() == s[:4] + s
    pos = sio.tell()
    res = sio.truncate(None)
    assert res == pos
    assert sio.tell() == pos
    raises(TypeError, sio.truncate, '0')
    sio.close()
    raises(ValueError, sio.truncate, 0)

def test_write_error():

    exc_info = raises(TypeError, StringIO, 3)
    assert "int" in exc_info.value.args[0]

    sio = StringIO(u"")
    exc_info = raises(TypeError, sio.write, 3)
    assert "int" in exc_info.value.args[0]

def test_newline_none():

    sio = StringIO(u"a\nb\r\nc\rd", newline=None)
    res = list(sio)
    assert res == [u"a\n", u"b\n", u"c\n", u"d"]
    sio.seek(0)
    res = sio.read(1)
    assert res == u"a"
    res = sio.read(2)
    assert res == u"\nb"
    res = sio.read(2)
    assert res == u"\nc"
    res = sio.read(1)
    assert res == u"\n"

    sio = StringIO(newline=None)
    res = sio.write(u"a\n")
    assert res == 2
    res = sio.write(u"b\r\n")
    assert res == 3
    res = sio.write(u"c\rd")
    assert res == 3
    sio.seek(0)
    res = sio.read()
    assert res == u"a\nb\nc\nd"
    sio = StringIO(u"a\r\nb", newline=None)
    res = sio.read(3)
    assert res == u"a\nb"

def test_newline_empty():

    sio = StringIO(u"a\nb\r\nc\rd", newline="")
    res = list(sio)
    assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]
    sio.seek(0)
    res = sio.read(4)
    assert res == u"a\nb\r"
    res = sio.read(2)
    assert res == u"\nc"
    res = sio.read(1)
    assert res == u"\r"

    sio = StringIO(newline="")
    res = sio.write(u"a\n")
    assert res == 2
    res = sio.write(u"b\r")
    assert res == 2
    res = sio.write(u"\nc")
    assert res == 2
    res = sio.write(u"\rd")
    assert res == 2
    sio.seek(0)
    res = list(sio)
    assert res == [u"a\n", u"b\r\n", u"c\r", u"d"]

def test_newline_lf():

    sio = StringIO(u"a\nb\r\nc\rd")
    res = list(sio)
    assert res == [u"a\n", u"b\r\n", u"c\rd"]

def test_newline_cr():

    sio = StringIO(u"a\nb\r\nc\rd", newline="\r")
    res = sio.read()
    assert res == u"a\rb\r\rc\rd"
    sio.seek(0)
    res = list(sio)
    assert res == [u"a\r", u"b\r", u"\r", u"c\r", u"d"]

def test_newline_crlf():

    sio = StringIO(u"a\nb\r\nc\rd", newline="\r\n")
    res = sio.read()
    assert res == u"a\r\nb\r\r\nc\rd"
    sio.seek(0)
    res = list(sio)
    assert res == [u"a\r\n", u"b\r\r\n", u"c\rd"]

def test_newline_property():

    sio = StringIO(newline=None)
    assert sio.newlines is None
    sio.write(u"a\n")
    assert sio.newlines == "\n"
    sio.write(u"b\r\n")
    assert sio.newlines == ("\n", "\r\n")
    sio.write(u"c\rd")
    assert sio.newlines == ("\r", "\n", "\r\n")
    exc = raises(TypeError, StringIO, newline=b'\n')
    assert 'bytes' in str(exc.value)

def test_iterator():

    s = u"1234567890\n"
    sio = StringIO(s * 10)

    assert iter(sio) is sio
    assert hasattr(sio, "__iter__")
    assert hasattr(sio, "__next__")

    i = 0
    for line in sio:
        assert line == s
        i += 1
    assert i == 10
    sio.seek(0)
    i = 0
    for line in sio:
        assert line == s
        i += 1
    assert i == 10
    sio.seek(len(s) * 10 +1)
    assert list(sio) == []
    sio = StringIO(s * 2)
    sio.close()
    raises(ValueError, next, sio)

def test_getstate():

    sio = StringIO()
    state = sio.__getstate__()
    assert len(state) == 4
    assert isinstance(state[0], str)
    assert isinstance(state[1], str)
    assert isinstance(state[2], int)
    assert state[3] is None or isinstance(state[3], dict)
    sio.close()
    raises(ValueError, sio.__getstate__)

def test_setstate():

    sio = StringIO()
    sio.__setstate__((u"no error", u"\n", 0, None))
    sio.__setstate__((u"no error", u"", 0, {"spam": 3}))
    raises(ValueError, sio.__setstate__, (u"", u"f", 0, None))
    raises(ValueError, sio.__setstate__, (u"", u"", -1, None))
    raises(TypeError, sio.__setstate__, (b"", u"", 0, None))
    raises(TypeError, sio.__setstate__, (u"", u"", 0.0, None))
    raises(TypeError, sio.__setstate__, (u"", u"", 0, 0))
    raises(TypeError, sio.__setstate__, (u"len-test", 0))
    raises(TypeError, sio.__setstate__)
    raises(TypeError, sio.__setstate__, 0)
    sio.close()
    raises(ValueError, sio.__setstate__, (u"closed", u"", 0, None))

def test_roundtrip_translation():
    sio1 = StringIO(u'a\nb', newline='\r\n')
    pos = sio1.seek(1)
    assert sio1.getvalue() == u'a\r\nb'
    state = sio1.__getstate__()
    sio2 = StringIO()
    sio2.__setstate__(state)
    assert sio2.getvalue() == u'a\r\nb'
    assert sio2.tell() == pos

def test_roundtrip_state():
    s = u'12345678'
    sio1 = StringIO(s)
    sio1.foo = 42
    sio1.seek(2)
    assert sio1.getvalue() == s
    state = sio1.__getstate__()
    sio2 = StringIO()
    sio2.__setstate__(state)
    assert sio2.getvalue() == s
    assert sio2.foo == 42
    assert sio2.tell() == 2

def rwbuffer(u, **kwargs):
    sio = StringIO(u, **kwargs)
    sio.seek(0)
    sio.write(u)
    sio.seek(0)
    return sio

def test_rwbuffer_read():
    s = u'12345678'
    sio = StringIO(s)
    sio.seek(0)
    sio.write(u'aaa')
    sio.seek(0)
    assert sio.read() == u'aaa45678'

def test_rwbuffer_readline():
    sio = rwbuffer(u'123\n456')
    assert sio.readline(0) == ''
    assert sio.readline(2) == '12'
    assert sio.readline(None) == '3\n'
    assert sio.readline() == '456'

def test_rwbuffer_readline():
    sio = rwbuffer(u'123\n456')
    assert sio.readline(0) == ''
    assert sio.readline(2) == '12'
    assert sio.readline(None) == '3\n'
    assert sio.readline() == '456'

def test_rwbuffer_newline_none():
    sio = rwbuffer(u"a\nb\r\nc\rd", newline=None)
    res = list(sio)
    assert res == [u"a\n", u"b\n", u"c\n", u"d"]

def test_reinit():
    obj = StringIO("\x00")
    obj.writelines("\x00")
    try:
        obj.__init__(1j)
    except TypeError as e:
        pass
    obj.__getstate__()

def test_issue5140():
    obj = StringIO("1")
    try:
        obj.__init__("", ())
    except:
        pass
    assert obj.__next__() == "1"