Debian Codegraph @

pypy/module/_io/test/apptest_bytesio.py

raw symbols SCIP
1 import _io
2
3 def test_init():
4 raises(TypeError, _io.BytesIO, "12345")
5 buf = b"1234567890"
6 b = _io.BytesIO(buf)
7 assert b.getvalue() == buf
8 b = _io.BytesIO(None)
9 assert b.getvalue() == b""
10 b.__init__(buf * 2)
11 assert b.getvalue() == buf * 2
12 b.__init__(buf)
13 assert b.getvalue() == buf
14
15 def test_init_kwargs():
16 buf = b"1234567890"
17 b = _io.BytesIO(initial_bytes=buf)
18 assert b.read() == buf
19 raises(TypeError, _io.BytesIO, buf, foo=None)
20
21 def test_new():
22 f = _io.BytesIO.__new__(_io.BytesIO)
23 assert not f.closed
24
25 def test_capabilities():
26 f = _io.BytesIO()
27 assert f.readable()
28 assert f.writable()
29 assert f.seekable()
30 f.close()
31 raises(ValueError, f.readable)
32 raises(ValueError, f.writable)
33 raises(ValueError, f.seekable)
34
35 def test_write():
36 f = _io.BytesIO()
37 assert f.write(b"") == 0
38 assert f.write(b"hello") == 5
39 exc = raises(TypeError, f.write, u"lo")
40 assert str(exc.value) == "a bytes-like object is required, not 'str'"
41 import gc; gc.collect()
42 assert f.getvalue() == b"hello"
43 f.close()
44
45 def test_read():
46 f = _io.BytesIO(b"hello")
47 assert f.read() == b"hello"
48 import gc; gc.collect()
49 assert f.read(8192) == b""
50 f.close()
51
52 def test_seek():
53 f = _io.BytesIO(b"hello")
54 assert f.tell() == 0
55 assert f.seek(-1, 2) == 4
56 assert f.tell() == 4
57 assert f.seek(0) == 0
58
59 def test_truncate():
60 f = _io.BytesIO()
61 f.write(b"hello")
62 assert f.truncate(0) == 0
63 assert f.tell() == 5
64 f.seek(0)
65 f.write(b"hello")
66 f.seek(3)
67 assert f.truncate() == 3
68 assert f.getvalue() == b"hel"
69 assert f.truncate(2) == 2
70 assert f.tell() == 3
71
72 def test_setstate():
73 # state is (content, position, __dict__)
74 f = _io.BytesIO(b"hello")
75 content, pos, __dict__ = f.__getstate__()
76 assert (content, pos) == (b"hello", 0)
77 assert __dict__ is None or __dict__ == {}
78 f.__setstate__((b"world", 3, {"a": 1}))
79 assert f.getvalue() == b"world"
80 assert f.read() == b"ld"
81 assert f.a == 1
82 assert f.__getstate__() == (b"world", 5, {"a": 1})
83 raises(TypeError, f.__setstate__, (b"", 0))
84 f.close()
85 raises(ValueError, f.__getstate__)
86 raises(ValueError, f.__setstate__, ("world", 3, {"a": 1}))
87
88 def test_readinto():
89 for methodname in ["readinto", "readinto1"]:
90 b = _io.BytesIO(b"hello")
91 readinto = getattr(b, methodname)
92 a1 = bytearray(b't')
93 a2 = bytearray(b'testing')
94 assert readinto(a1) == 1
95 assert readinto(a2) == 4
96 b.seek(0)
97 m = memoryview(bytearray(b"world"))
98 assert readinto(m) == 5
99 #
100 exc = raises(TypeError, readinto, u"hello")
101 msg = str(exc.value)
102 # print(msg)
103 assert " read-write b" in msg and msg.endswith(", not str")
104 #
105 exc = raises(TypeError, readinto, memoryview(b"hello"))
106 msg = str(exc.value)
107 # print(msg)
108 assert " read-write b" in msg and msg.endswith(", not memoryview")
109 #
110 b.close()
111 assert a1 == b"h"
112 assert a2 == b"elloing"
113 raises(ValueError, readinto, bytearray(b"hello"))
114
115 def test_getbuffer():
116 memio = _io.BytesIO(b"1234567890")
117 buf = memio.getbuffer()
118 assert bytes(buf) == b"1234567890"
119 memio.seek(5)
120 buf.release()
121 buf = memio.getbuffer()
122 assert bytes(buf) == b"1234567890"
123 assert buf[5] == ord(b"6")
124 # Mutating the buffer updates the BytesIO
125 buf[3:6] = b"abc"
126 assert bytes(buf) == b"123abc7890"
127 assert memio.getvalue() == b"123abc7890"
128 # After the buffer gets released, we can resize the BytesIO again
129 buf.release()
130 memio.truncate()
131 memio.close()
132 raises(ValueError, memio.getbuffer)
133
134 def test_read1():
135 memio = _io.BytesIO(b"1234567890")
136 assert memio.read1() == b"1234567890"
137
138 def test_readline():
139 f = _io.BytesIO(b'abc\ndef\nxyzzy\nfoo\x00bar\nanother line')
140 assert f.readline() == b'abc\n'
141 assert f.readline(10) == b'def\n'
142 assert f.readline(2) == b'xy'
143 assert f.readline(4) == b'zzy\n'
144 assert f.readline() == b'foo\x00bar\n'
145 assert f.readline(None) == b'another line'
146 raises(TypeError, f.readline, 5.3)
147
148 def test_overread():
149 f = _io.BytesIO(b'abc')
150 assert f.readline(10) == b'abc'
151
152 def test_subclass_bytesio_rawiobase():
153 class X(_io.BytesIO, _io._RawIOBase):
154 pass
155 assert X.mro() == [X, _io.BytesIO, _io._BufferedIOBase, _io._RawIOBase, _io._IOBase, object]
156 # can instantiate
157 x = X(b'abc')
158 # check that _RawIOBase methods work
159 res = _io._RawIOBase.read(x)
160 assert res == b'abc'
161
162 def test_getbuffer_export_count():
163 memio = _io.BytesIO()
164 buf = memio.getbuffer()
165 assert bytes(buf) == b""
166 # write raises BufferError while buffer is exported
167 raises(BufferError, memio.write, b'x')
168 buf2 = memio.getbuffer()
169 raises(BufferError, memio.write, b'x')
170 buf.release()
171 # still one export remaining
172 raises(BufferError, memio.write, b'x')
173 buf2.release()
174 # all released, write succeeds
175 memio.write(b'x')
176
177 def test_truncate_on_read_only():
178 rawio = _io.BytesIO(b"abc")
179 bufio = _io.BufferedReader(rawio)
180 assert not bufio.writable()
181 with raises(_io.UnsupportedOperation):
182 bufio.truncate()
183 with raises(_io.UnsupportedOperation):
184 bufio.truncate(0)