1 | n/a | """Unit tests for memory-based file-like objects. |
---|
2 | n/a | StringIO -- for unicode strings |
---|
3 | n/a | BytesIO -- for bytes |
---|
4 | n/a | """ |
---|
5 | n/a | |
---|
6 | n/a | import unittest |
---|
7 | n/a | from test import support |
---|
8 | n/a | |
---|
9 | n/a | import io |
---|
10 | n/a | import _pyio as pyio |
---|
11 | n/a | import pickle |
---|
12 | n/a | import sys |
---|
13 | n/a | |
---|
14 | n/a | class MemorySeekTestMixin: |
---|
15 | n/a | |
---|
16 | n/a | def testInit(self): |
---|
17 | n/a | buf = self.buftype("1234567890") |
---|
18 | n/a | bytesIo = self.ioclass(buf) |
---|
19 | n/a | |
---|
20 | n/a | def testRead(self): |
---|
21 | n/a | buf = self.buftype("1234567890") |
---|
22 | n/a | bytesIo = self.ioclass(buf) |
---|
23 | n/a | |
---|
24 | n/a | self.assertEqual(buf[:1], bytesIo.read(1)) |
---|
25 | n/a | self.assertEqual(buf[1:5], bytesIo.read(4)) |
---|
26 | n/a | self.assertEqual(buf[5:], bytesIo.read(900)) |
---|
27 | n/a | self.assertEqual(self.EOF, bytesIo.read()) |
---|
28 | n/a | |
---|
29 | n/a | def testReadNoArgs(self): |
---|
30 | n/a | buf = self.buftype("1234567890") |
---|
31 | n/a | bytesIo = self.ioclass(buf) |
---|
32 | n/a | |
---|
33 | n/a | self.assertEqual(buf, bytesIo.read()) |
---|
34 | n/a | self.assertEqual(self.EOF, bytesIo.read()) |
---|
35 | n/a | |
---|
36 | n/a | def testSeek(self): |
---|
37 | n/a | buf = self.buftype("1234567890") |
---|
38 | n/a | bytesIo = self.ioclass(buf) |
---|
39 | n/a | |
---|
40 | n/a | bytesIo.read(5) |
---|
41 | n/a | bytesIo.seek(0) |
---|
42 | n/a | self.assertEqual(buf, bytesIo.read()) |
---|
43 | n/a | |
---|
44 | n/a | bytesIo.seek(3) |
---|
45 | n/a | self.assertEqual(buf[3:], bytesIo.read()) |
---|
46 | n/a | self.assertRaises(TypeError, bytesIo.seek, 0.0) |
---|
47 | n/a | |
---|
48 | n/a | def testTell(self): |
---|
49 | n/a | buf = self.buftype("1234567890") |
---|
50 | n/a | bytesIo = self.ioclass(buf) |
---|
51 | n/a | |
---|
52 | n/a | self.assertEqual(0, bytesIo.tell()) |
---|
53 | n/a | bytesIo.seek(5) |
---|
54 | n/a | self.assertEqual(5, bytesIo.tell()) |
---|
55 | n/a | bytesIo.seek(10000) |
---|
56 | n/a | self.assertEqual(10000, bytesIo.tell()) |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | class MemoryTestMixin: |
---|
60 | n/a | |
---|
61 | n/a | def test_detach(self): |
---|
62 | n/a | buf = self.ioclass() |
---|
63 | n/a | self.assertRaises(self.UnsupportedOperation, buf.detach) |
---|
64 | n/a | |
---|
65 | n/a | def write_ops(self, f, t): |
---|
66 | n/a | self.assertEqual(f.write(t("blah.")), 5) |
---|
67 | n/a | self.assertEqual(f.seek(0), 0) |
---|
68 | n/a | self.assertEqual(f.write(t("Hello.")), 6) |
---|
69 | n/a | self.assertEqual(f.tell(), 6) |
---|
70 | n/a | self.assertEqual(f.seek(5), 5) |
---|
71 | n/a | self.assertEqual(f.tell(), 5) |
---|
72 | n/a | self.assertEqual(f.write(t(" world\n\n\n")), 9) |
---|
73 | n/a | self.assertEqual(f.seek(0), 0) |
---|
74 | n/a | self.assertEqual(f.write(t("h")), 1) |
---|
75 | n/a | self.assertEqual(f.truncate(12), 12) |
---|
76 | n/a | self.assertEqual(f.tell(), 1) |
---|
77 | n/a | |
---|
78 | n/a | def test_write(self): |
---|
79 | n/a | buf = self.buftype("hello world\n") |
---|
80 | n/a | memio = self.ioclass(buf) |
---|
81 | n/a | |
---|
82 | n/a | self.write_ops(memio, self.buftype) |
---|
83 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
84 | n/a | memio = self.ioclass() |
---|
85 | n/a | self.write_ops(memio, self.buftype) |
---|
86 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
87 | n/a | self.assertRaises(TypeError, memio.write, None) |
---|
88 | n/a | memio.close() |
---|
89 | n/a | self.assertRaises(ValueError, memio.write, self.buftype("")) |
---|
90 | n/a | |
---|
91 | n/a | def test_writelines(self): |
---|
92 | n/a | buf = self.buftype("1234567890") |
---|
93 | n/a | memio = self.ioclass() |
---|
94 | n/a | |
---|
95 | n/a | self.assertEqual(memio.writelines([buf] * 100), None) |
---|
96 | n/a | self.assertEqual(memio.getvalue(), buf * 100) |
---|
97 | n/a | memio.writelines([]) |
---|
98 | n/a | self.assertEqual(memio.getvalue(), buf * 100) |
---|
99 | n/a | memio = self.ioclass() |
---|
100 | n/a | self.assertRaises(TypeError, memio.writelines, [buf] + [1]) |
---|
101 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
102 | n/a | self.assertRaises(TypeError, memio.writelines, None) |
---|
103 | n/a | memio.close() |
---|
104 | n/a | self.assertRaises(ValueError, memio.writelines, []) |
---|
105 | n/a | |
---|
106 | n/a | def test_writelines_error(self): |
---|
107 | n/a | memio = self.ioclass() |
---|
108 | n/a | def error_gen(): |
---|
109 | n/a | yield self.buftype('spam') |
---|
110 | n/a | raise KeyboardInterrupt |
---|
111 | n/a | |
---|
112 | n/a | self.assertRaises(KeyboardInterrupt, memio.writelines, error_gen()) |
---|
113 | n/a | |
---|
114 | n/a | def test_truncate(self): |
---|
115 | n/a | buf = self.buftype("1234567890") |
---|
116 | n/a | memio = self.ioclass(buf) |
---|
117 | n/a | |
---|
118 | n/a | self.assertRaises(ValueError, memio.truncate, -1) |
---|
119 | n/a | memio.seek(6) |
---|
120 | n/a | self.assertEqual(memio.truncate(), 6) |
---|
121 | n/a | self.assertEqual(memio.getvalue(), buf[:6]) |
---|
122 | n/a | self.assertEqual(memio.truncate(4), 4) |
---|
123 | n/a | self.assertEqual(memio.getvalue(), buf[:4]) |
---|
124 | n/a | self.assertEqual(memio.tell(), 6) |
---|
125 | n/a | memio.seek(0, 2) |
---|
126 | n/a | memio.write(buf) |
---|
127 | n/a | self.assertEqual(memio.getvalue(), buf[:4] + buf) |
---|
128 | n/a | pos = memio.tell() |
---|
129 | n/a | self.assertEqual(memio.truncate(None), pos) |
---|
130 | n/a | self.assertEqual(memio.tell(), pos) |
---|
131 | n/a | self.assertRaises(TypeError, memio.truncate, '0') |
---|
132 | n/a | memio.close() |
---|
133 | n/a | self.assertRaises(ValueError, memio.truncate, 0) |
---|
134 | n/a | |
---|
135 | n/a | def test_init(self): |
---|
136 | n/a | buf = self.buftype("1234567890") |
---|
137 | n/a | memio = self.ioclass(buf) |
---|
138 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
139 | n/a | memio = self.ioclass(None) |
---|
140 | n/a | self.assertEqual(memio.getvalue(), self.EOF) |
---|
141 | n/a | memio.__init__(buf * 2) |
---|
142 | n/a | self.assertEqual(memio.getvalue(), buf * 2) |
---|
143 | n/a | memio.__init__(buf) |
---|
144 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
145 | n/a | self.assertRaises(TypeError, memio.__init__, []) |
---|
146 | n/a | |
---|
147 | n/a | def test_read(self): |
---|
148 | n/a | buf = self.buftype("1234567890") |
---|
149 | n/a | memio = self.ioclass(buf) |
---|
150 | n/a | |
---|
151 | n/a | self.assertEqual(memio.read(0), self.EOF) |
---|
152 | n/a | self.assertEqual(memio.read(1), buf[:1]) |
---|
153 | n/a | self.assertEqual(memio.read(4), buf[1:5]) |
---|
154 | n/a | self.assertEqual(memio.read(900), buf[5:]) |
---|
155 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
156 | n/a | memio.seek(0) |
---|
157 | n/a | self.assertEqual(memio.read(), buf) |
---|
158 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
159 | n/a | self.assertEqual(memio.tell(), 10) |
---|
160 | n/a | memio.seek(0) |
---|
161 | n/a | self.assertEqual(memio.read(-1), buf) |
---|
162 | n/a | memio.seek(0) |
---|
163 | n/a | self.assertEqual(type(memio.read()), type(buf)) |
---|
164 | n/a | memio.seek(100) |
---|
165 | n/a | self.assertEqual(type(memio.read()), type(buf)) |
---|
166 | n/a | memio.seek(0) |
---|
167 | n/a | self.assertEqual(memio.read(None), buf) |
---|
168 | n/a | self.assertRaises(TypeError, memio.read, '') |
---|
169 | n/a | memio.seek(len(buf) + 1) |
---|
170 | n/a | self.assertEqual(memio.read(1), self.EOF) |
---|
171 | n/a | memio.seek(len(buf) + 1) |
---|
172 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
173 | n/a | memio.close() |
---|
174 | n/a | self.assertRaises(ValueError, memio.read) |
---|
175 | n/a | |
---|
176 | n/a | def test_readline(self): |
---|
177 | n/a | buf = self.buftype("1234567890\n") |
---|
178 | n/a | memio = self.ioclass(buf * 2) |
---|
179 | n/a | |
---|
180 | n/a | self.assertEqual(memio.readline(0), self.EOF) |
---|
181 | n/a | self.assertEqual(memio.readline(), buf) |
---|
182 | n/a | self.assertEqual(memio.readline(), buf) |
---|
183 | n/a | self.assertEqual(memio.readline(), self.EOF) |
---|
184 | n/a | memio.seek(0) |
---|
185 | n/a | self.assertEqual(memio.readline(5), buf[:5]) |
---|
186 | n/a | self.assertEqual(memio.readline(5), buf[5:10]) |
---|
187 | n/a | self.assertEqual(memio.readline(5), buf[10:15]) |
---|
188 | n/a | memio.seek(0) |
---|
189 | n/a | self.assertEqual(memio.readline(-1), buf) |
---|
190 | n/a | memio.seek(0) |
---|
191 | n/a | self.assertEqual(memio.readline(0), self.EOF) |
---|
192 | n/a | # Issue #24989: Buffer overread |
---|
193 | n/a | memio.seek(len(buf) * 2 + 1) |
---|
194 | n/a | self.assertEqual(memio.readline(), self.EOF) |
---|
195 | n/a | |
---|
196 | n/a | buf = self.buftype("1234567890\n") |
---|
197 | n/a | memio = self.ioclass((buf * 3)[:-1]) |
---|
198 | n/a | self.assertEqual(memio.readline(), buf) |
---|
199 | n/a | self.assertEqual(memio.readline(), buf) |
---|
200 | n/a | self.assertEqual(memio.readline(), buf[:-1]) |
---|
201 | n/a | self.assertEqual(memio.readline(), self.EOF) |
---|
202 | n/a | memio.seek(0) |
---|
203 | n/a | self.assertEqual(type(memio.readline()), type(buf)) |
---|
204 | n/a | self.assertEqual(memio.readline(), buf) |
---|
205 | n/a | self.assertRaises(TypeError, memio.readline, '') |
---|
206 | n/a | memio.close() |
---|
207 | n/a | self.assertRaises(ValueError, memio.readline) |
---|
208 | n/a | |
---|
209 | n/a | def test_readlines(self): |
---|
210 | n/a | buf = self.buftype("1234567890\n") |
---|
211 | n/a | memio = self.ioclass(buf * 10) |
---|
212 | n/a | |
---|
213 | n/a | self.assertEqual(memio.readlines(), [buf] * 10) |
---|
214 | n/a | memio.seek(5) |
---|
215 | n/a | self.assertEqual(memio.readlines(), [buf[5:]] + [buf] * 9) |
---|
216 | n/a | memio.seek(0) |
---|
217 | n/a | self.assertEqual(memio.readlines(15), [buf] * 2) |
---|
218 | n/a | memio.seek(0) |
---|
219 | n/a | self.assertEqual(memio.readlines(-1), [buf] * 10) |
---|
220 | n/a | memio.seek(0) |
---|
221 | n/a | self.assertEqual(memio.readlines(0), [buf] * 10) |
---|
222 | n/a | memio.seek(0) |
---|
223 | n/a | self.assertEqual(type(memio.readlines()[0]), type(buf)) |
---|
224 | n/a | memio.seek(0) |
---|
225 | n/a | self.assertEqual(memio.readlines(None), [buf] * 10) |
---|
226 | n/a | self.assertRaises(TypeError, memio.readlines, '') |
---|
227 | n/a | # Issue #24989: Buffer overread |
---|
228 | n/a | memio.seek(len(buf) * 10 + 1) |
---|
229 | n/a | self.assertEqual(memio.readlines(), []) |
---|
230 | n/a | memio.close() |
---|
231 | n/a | self.assertRaises(ValueError, memio.readlines) |
---|
232 | n/a | |
---|
233 | n/a | def test_iterator(self): |
---|
234 | n/a | buf = self.buftype("1234567890\n") |
---|
235 | n/a | memio = self.ioclass(buf * 10) |
---|
236 | n/a | |
---|
237 | n/a | self.assertEqual(iter(memio), memio) |
---|
238 | n/a | self.assertTrue(hasattr(memio, '__iter__')) |
---|
239 | n/a | self.assertTrue(hasattr(memio, '__next__')) |
---|
240 | n/a | i = 0 |
---|
241 | n/a | for line in memio: |
---|
242 | n/a | self.assertEqual(line, buf) |
---|
243 | n/a | i += 1 |
---|
244 | n/a | self.assertEqual(i, 10) |
---|
245 | n/a | memio.seek(0) |
---|
246 | n/a | i = 0 |
---|
247 | n/a | for line in memio: |
---|
248 | n/a | self.assertEqual(line, buf) |
---|
249 | n/a | i += 1 |
---|
250 | n/a | self.assertEqual(i, 10) |
---|
251 | n/a | # Issue #24989: Buffer overread |
---|
252 | n/a | memio.seek(len(buf) * 10 + 1) |
---|
253 | n/a | self.assertEqual(list(memio), []) |
---|
254 | n/a | memio = self.ioclass(buf * 2) |
---|
255 | n/a | memio.close() |
---|
256 | n/a | self.assertRaises(ValueError, memio.__next__) |
---|
257 | n/a | |
---|
258 | n/a | def test_getvalue(self): |
---|
259 | n/a | buf = self.buftype("1234567890") |
---|
260 | n/a | memio = self.ioclass(buf) |
---|
261 | n/a | |
---|
262 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
263 | n/a | memio.read() |
---|
264 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
265 | n/a | self.assertEqual(type(memio.getvalue()), type(buf)) |
---|
266 | n/a | memio = self.ioclass(buf * 1000) |
---|
267 | n/a | self.assertEqual(memio.getvalue()[-3:], self.buftype("890")) |
---|
268 | n/a | memio = self.ioclass(buf) |
---|
269 | n/a | memio.close() |
---|
270 | n/a | self.assertRaises(ValueError, memio.getvalue) |
---|
271 | n/a | |
---|
272 | n/a | def test_seek(self): |
---|
273 | n/a | buf = self.buftype("1234567890") |
---|
274 | n/a | memio = self.ioclass(buf) |
---|
275 | n/a | |
---|
276 | n/a | memio.read(5) |
---|
277 | n/a | self.assertRaises(ValueError, memio.seek, -1) |
---|
278 | n/a | self.assertRaises(ValueError, memio.seek, 1, -1) |
---|
279 | n/a | self.assertRaises(ValueError, memio.seek, 1, 3) |
---|
280 | n/a | self.assertEqual(memio.seek(0), 0) |
---|
281 | n/a | self.assertEqual(memio.seek(0, 0), 0) |
---|
282 | n/a | self.assertEqual(memio.read(), buf) |
---|
283 | n/a | self.assertEqual(memio.seek(3), 3) |
---|
284 | n/a | self.assertEqual(memio.seek(0, 1), 3) |
---|
285 | n/a | self.assertEqual(memio.read(), buf[3:]) |
---|
286 | n/a | self.assertEqual(memio.seek(len(buf)), len(buf)) |
---|
287 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
288 | n/a | memio.seek(len(buf) + 1) |
---|
289 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
290 | n/a | self.assertEqual(memio.seek(0, 2), len(buf)) |
---|
291 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
292 | n/a | memio.close() |
---|
293 | n/a | self.assertRaises(ValueError, memio.seek, 0) |
---|
294 | n/a | |
---|
295 | n/a | def test_overseek(self): |
---|
296 | n/a | buf = self.buftype("1234567890") |
---|
297 | n/a | memio = self.ioclass(buf) |
---|
298 | n/a | |
---|
299 | n/a | self.assertEqual(memio.seek(len(buf) + 1), 11) |
---|
300 | n/a | self.assertEqual(memio.read(), self.EOF) |
---|
301 | n/a | self.assertEqual(memio.tell(), 11) |
---|
302 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
303 | n/a | memio.write(self.EOF) |
---|
304 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
305 | n/a | memio.write(buf) |
---|
306 | n/a | self.assertEqual(memio.getvalue(), buf + self.buftype('\0') + buf) |
---|
307 | n/a | |
---|
308 | n/a | def test_tell(self): |
---|
309 | n/a | buf = self.buftype("1234567890") |
---|
310 | n/a | memio = self.ioclass(buf) |
---|
311 | n/a | |
---|
312 | n/a | self.assertEqual(memio.tell(), 0) |
---|
313 | n/a | memio.seek(5) |
---|
314 | n/a | self.assertEqual(memio.tell(), 5) |
---|
315 | n/a | memio.seek(10000) |
---|
316 | n/a | self.assertEqual(memio.tell(), 10000) |
---|
317 | n/a | memio.close() |
---|
318 | n/a | self.assertRaises(ValueError, memio.tell) |
---|
319 | n/a | |
---|
320 | n/a | def test_flush(self): |
---|
321 | n/a | buf = self.buftype("1234567890") |
---|
322 | n/a | memio = self.ioclass(buf) |
---|
323 | n/a | |
---|
324 | n/a | self.assertEqual(memio.flush(), None) |
---|
325 | n/a | |
---|
326 | n/a | def test_flags(self): |
---|
327 | n/a | memio = self.ioclass() |
---|
328 | n/a | |
---|
329 | n/a | self.assertEqual(memio.writable(), True) |
---|
330 | n/a | self.assertEqual(memio.readable(), True) |
---|
331 | n/a | self.assertEqual(memio.seekable(), True) |
---|
332 | n/a | self.assertEqual(memio.isatty(), False) |
---|
333 | n/a | self.assertEqual(memio.closed, False) |
---|
334 | n/a | memio.close() |
---|
335 | n/a | self.assertRaises(ValueError, memio.writable) |
---|
336 | n/a | self.assertRaises(ValueError, memio.readable) |
---|
337 | n/a | self.assertRaises(ValueError, memio.seekable) |
---|
338 | n/a | self.assertRaises(ValueError, memio.isatty) |
---|
339 | n/a | self.assertEqual(memio.closed, True) |
---|
340 | n/a | |
---|
341 | n/a | def test_subclassing(self): |
---|
342 | n/a | buf = self.buftype("1234567890") |
---|
343 | n/a | def test1(): |
---|
344 | n/a | class MemIO(self.ioclass): |
---|
345 | n/a | pass |
---|
346 | n/a | m = MemIO(buf) |
---|
347 | n/a | return m.getvalue() |
---|
348 | n/a | def test2(): |
---|
349 | n/a | class MemIO(self.ioclass): |
---|
350 | n/a | def __init__(me, a, b): |
---|
351 | n/a | self.ioclass.__init__(me, a) |
---|
352 | n/a | m = MemIO(buf, None) |
---|
353 | n/a | return m.getvalue() |
---|
354 | n/a | self.assertEqual(test1(), buf) |
---|
355 | n/a | self.assertEqual(test2(), buf) |
---|
356 | n/a | |
---|
357 | n/a | def test_instance_dict_leak(self): |
---|
358 | n/a | # Test case for issue #6242. |
---|
359 | n/a | # This will be caught by regrtest.py -R if this leak. |
---|
360 | n/a | for _ in range(100): |
---|
361 | n/a | memio = self.ioclass() |
---|
362 | n/a | memio.foo = 1 |
---|
363 | n/a | |
---|
364 | n/a | def test_pickling(self): |
---|
365 | n/a | buf = self.buftype("1234567890") |
---|
366 | n/a | memio = self.ioclass(buf) |
---|
367 | n/a | memio.foo = 42 |
---|
368 | n/a | memio.seek(2) |
---|
369 | n/a | |
---|
370 | n/a | class PickleTestMemIO(self.ioclass): |
---|
371 | n/a | def __init__(me, initvalue, foo): |
---|
372 | n/a | self.ioclass.__init__(me, initvalue) |
---|
373 | n/a | me.foo = foo |
---|
374 | n/a | # __getnewargs__ is undefined on purpose. This checks that PEP 307 |
---|
375 | n/a | # is used to provide pickling support. |
---|
376 | n/a | |
---|
377 | n/a | # Pickle expects the class to be on the module level. Here we use a |
---|
378 | n/a | # little hack to allow the PickleTestMemIO class to derive from |
---|
379 | n/a | # self.ioclass without having to define all combinations explicitly on |
---|
380 | n/a | # the module-level. |
---|
381 | n/a | import __main__ |
---|
382 | n/a | PickleTestMemIO.__module__ = '__main__' |
---|
383 | n/a | PickleTestMemIO.__qualname__ = PickleTestMemIO.__name__ |
---|
384 | n/a | __main__.PickleTestMemIO = PickleTestMemIO |
---|
385 | n/a | submemio = PickleTestMemIO(buf, 80) |
---|
386 | n/a | submemio.seek(2) |
---|
387 | n/a | |
---|
388 | n/a | # We only support pickle protocol 2 and onward since we use extended |
---|
389 | n/a | # __reduce__ API of PEP 307 to provide pickling support. |
---|
390 | n/a | for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): |
---|
391 | n/a | for obj in (memio, submemio): |
---|
392 | n/a | obj2 = pickle.loads(pickle.dumps(obj, protocol=proto)) |
---|
393 | n/a | self.assertEqual(obj.getvalue(), obj2.getvalue()) |
---|
394 | n/a | self.assertEqual(obj.__class__, obj2.__class__) |
---|
395 | n/a | self.assertEqual(obj.foo, obj2.foo) |
---|
396 | n/a | self.assertEqual(obj.tell(), obj2.tell()) |
---|
397 | n/a | obj2.close() |
---|
398 | n/a | self.assertRaises(ValueError, pickle.dumps, obj2, proto) |
---|
399 | n/a | del __main__.PickleTestMemIO |
---|
400 | n/a | |
---|
401 | n/a | |
---|
402 | n/a | class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase): |
---|
403 | n/a | # Test _pyio.BytesIO; class also inherited for testing C implementation |
---|
404 | n/a | |
---|
405 | n/a | UnsupportedOperation = pyio.UnsupportedOperation |
---|
406 | n/a | |
---|
407 | n/a | @staticmethod |
---|
408 | n/a | def buftype(s): |
---|
409 | n/a | return s.encode("ascii") |
---|
410 | n/a | ioclass = pyio.BytesIO |
---|
411 | n/a | EOF = b"" |
---|
412 | n/a | |
---|
413 | n/a | def test_getbuffer(self): |
---|
414 | n/a | memio = self.ioclass(b"1234567890") |
---|
415 | n/a | buf = memio.getbuffer() |
---|
416 | n/a | self.assertEqual(bytes(buf), b"1234567890") |
---|
417 | n/a | memio.seek(5) |
---|
418 | n/a | buf = memio.getbuffer() |
---|
419 | n/a | self.assertEqual(bytes(buf), b"1234567890") |
---|
420 | n/a | # Trying to change the size of the BytesIO while a buffer is exported |
---|
421 | n/a | # raises a BufferError. |
---|
422 | n/a | self.assertRaises(BufferError, memio.write, b'x' * 100) |
---|
423 | n/a | self.assertRaises(BufferError, memio.truncate) |
---|
424 | n/a | self.assertRaises(BufferError, memio.close) |
---|
425 | n/a | self.assertFalse(memio.closed) |
---|
426 | n/a | # Mutating the buffer updates the BytesIO |
---|
427 | n/a | buf[3:6] = b"abc" |
---|
428 | n/a | self.assertEqual(bytes(buf), b"123abc7890") |
---|
429 | n/a | self.assertEqual(memio.getvalue(), b"123abc7890") |
---|
430 | n/a | # After the buffer gets released, we can resize and close the BytesIO |
---|
431 | n/a | # again |
---|
432 | n/a | del buf |
---|
433 | n/a | support.gc_collect() |
---|
434 | n/a | memio.truncate() |
---|
435 | n/a | memio.close() |
---|
436 | n/a | self.assertRaises(ValueError, memio.getbuffer) |
---|
437 | n/a | |
---|
438 | n/a | def test_read1(self): |
---|
439 | n/a | buf = self.buftype("1234567890") |
---|
440 | n/a | self.assertEqual(self.ioclass(buf).read1(), buf) |
---|
441 | n/a | self.assertEqual(self.ioclass(buf).read1(-1), buf) |
---|
442 | n/a | |
---|
443 | n/a | def test_readinto(self): |
---|
444 | n/a | buf = self.buftype("1234567890") |
---|
445 | n/a | memio = self.ioclass(buf) |
---|
446 | n/a | |
---|
447 | n/a | b = bytearray(b"hello") |
---|
448 | n/a | self.assertEqual(memio.readinto(b), 5) |
---|
449 | n/a | self.assertEqual(b, b"12345") |
---|
450 | n/a | self.assertEqual(memio.readinto(b), 5) |
---|
451 | n/a | self.assertEqual(b, b"67890") |
---|
452 | n/a | self.assertEqual(memio.readinto(b), 0) |
---|
453 | n/a | self.assertEqual(b, b"67890") |
---|
454 | n/a | b = bytearray(b"hello world") |
---|
455 | n/a | memio.seek(0) |
---|
456 | n/a | self.assertEqual(memio.readinto(b), 10) |
---|
457 | n/a | self.assertEqual(b, b"1234567890d") |
---|
458 | n/a | b = bytearray(b"") |
---|
459 | n/a | memio.seek(0) |
---|
460 | n/a | self.assertEqual(memio.readinto(b), 0) |
---|
461 | n/a | self.assertEqual(b, b"") |
---|
462 | n/a | self.assertRaises(TypeError, memio.readinto, '') |
---|
463 | n/a | import array |
---|
464 | n/a | a = array.array('b', b"hello world") |
---|
465 | n/a | memio = self.ioclass(buf) |
---|
466 | n/a | memio.readinto(a) |
---|
467 | n/a | self.assertEqual(a.tobytes(), b"1234567890d") |
---|
468 | n/a | memio.close() |
---|
469 | n/a | self.assertRaises(ValueError, memio.readinto, b) |
---|
470 | n/a | memio = self.ioclass(b"123") |
---|
471 | n/a | b = bytearray() |
---|
472 | n/a | memio.seek(42) |
---|
473 | n/a | memio.readinto(b) |
---|
474 | n/a | self.assertEqual(b, b"") |
---|
475 | n/a | |
---|
476 | n/a | def test_relative_seek(self): |
---|
477 | n/a | buf = self.buftype("1234567890") |
---|
478 | n/a | memio = self.ioclass(buf) |
---|
479 | n/a | |
---|
480 | n/a | self.assertEqual(memio.seek(-1, 1), 0) |
---|
481 | n/a | self.assertEqual(memio.seek(3, 1), 3) |
---|
482 | n/a | self.assertEqual(memio.seek(-4, 1), 0) |
---|
483 | n/a | self.assertEqual(memio.seek(-1, 2), 9) |
---|
484 | n/a | self.assertEqual(memio.seek(1, 1), 10) |
---|
485 | n/a | self.assertEqual(memio.seek(1, 2), 11) |
---|
486 | n/a | memio.seek(-3, 2) |
---|
487 | n/a | self.assertEqual(memio.read(), buf[-3:]) |
---|
488 | n/a | memio.seek(0) |
---|
489 | n/a | memio.seek(1, 1) |
---|
490 | n/a | self.assertEqual(memio.read(), buf[1:]) |
---|
491 | n/a | |
---|
492 | n/a | def test_unicode(self): |
---|
493 | n/a | memio = self.ioclass() |
---|
494 | n/a | |
---|
495 | n/a | self.assertRaises(TypeError, self.ioclass, "1234567890") |
---|
496 | n/a | self.assertRaises(TypeError, memio.write, "1234567890") |
---|
497 | n/a | self.assertRaises(TypeError, memio.writelines, ["1234567890"]) |
---|
498 | n/a | |
---|
499 | n/a | def test_bytes_array(self): |
---|
500 | n/a | buf = b"1234567890" |
---|
501 | n/a | import array |
---|
502 | n/a | a = array.array('b', list(buf)) |
---|
503 | n/a | memio = self.ioclass(a) |
---|
504 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
505 | n/a | self.assertEqual(memio.write(a), 10) |
---|
506 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
507 | n/a | |
---|
508 | n/a | def test_issue5449(self): |
---|
509 | n/a | buf = self.buftype("1234567890") |
---|
510 | n/a | self.ioclass(initial_bytes=buf) |
---|
511 | n/a | self.assertRaises(TypeError, self.ioclass, buf, foo=None) |
---|
512 | n/a | |
---|
513 | n/a | |
---|
514 | n/a | class TextIOTestMixin: |
---|
515 | n/a | |
---|
516 | n/a | def test_newlines_property(self): |
---|
517 | n/a | memio = self.ioclass(newline=None) |
---|
518 | n/a | # The C StringIO decodes newlines in write() calls, but the Python |
---|
519 | n/a | # implementation only does when reading. This function forces them to |
---|
520 | n/a | # be decoded for testing. |
---|
521 | n/a | def force_decode(): |
---|
522 | n/a | memio.seek(0) |
---|
523 | n/a | memio.read() |
---|
524 | n/a | self.assertEqual(memio.newlines, None) |
---|
525 | n/a | memio.write("a\n") |
---|
526 | n/a | force_decode() |
---|
527 | n/a | self.assertEqual(memio.newlines, "\n") |
---|
528 | n/a | memio.write("b\r\n") |
---|
529 | n/a | force_decode() |
---|
530 | n/a | self.assertEqual(memio.newlines, ("\n", "\r\n")) |
---|
531 | n/a | memio.write("c\rd") |
---|
532 | n/a | force_decode() |
---|
533 | n/a | self.assertEqual(memio.newlines, ("\r", "\n", "\r\n")) |
---|
534 | n/a | |
---|
535 | n/a | def test_relative_seek(self): |
---|
536 | n/a | memio = self.ioclass() |
---|
537 | n/a | |
---|
538 | n/a | self.assertRaises(OSError, memio.seek, -1, 1) |
---|
539 | n/a | self.assertRaises(OSError, memio.seek, 3, 1) |
---|
540 | n/a | self.assertRaises(OSError, memio.seek, -3, 1) |
---|
541 | n/a | self.assertRaises(OSError, memio.seek, -1, 2) |
---|
542 | n/a | self.assertRaises(OSError, memio.seek, 1, 1) |
---|
543 | n/a | self.assertRaises(OSError, memio.seek, 1, 2) |
---|
544 | n/a | |
---|
545 | n/a | def test_textio_properties(self): |
---|
546 | n/a | memio = self.ioclass() |
---|
547 | n/a | |
---|
548 | n/a | # These are just dummy values but we nevertheless check them for fear |
---|
549 | n/a | # of unexpected breakage. |
---|
550 | n/a | self.assertIsNone(memio.encoding) |
---|
551 | n/a | self.assertIsNone(memio.errors) |
---|
552 | n/a | self.assertFalse(memio.line_buffering) |
---|
553 | n/a | |
---|
554 | n/a | def test_newline_default(self): |
---|
555 | n/a | memio = self.ioclass("a\nb\r\nc\rd") |
---|
556 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) |
---|
557 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
558 | n/a | |
---|
559 | n/a | memio = self.ioclass() |
---|
560 | n/a | self.assertEqual(memio.write("a\nb\r\nc\rd"), 8) |
---|
561 | n/a | memio.seek(0) |
---|
562 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) |
---|
563 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
564 | n/a | |
---|
565 | n/a | def test_newline_none(self): |
---|
566 | n/a | # newline=None |
---|
567 | n/a | memio = self.ioclass("a\nb\r\nc\rd", newline=None) |
---|
568 | n/a | self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"]) |
---|
569 | n/a | memio.seek(0) |
---|
570 | n/a | self.assertEqual(memio.read(1), "a") |
---|
571 | n/a | self.assertEqual(memio.read(2), "\nb") |
---|
572 | n/a | self.assertEqual(memio.read(2), "\nc") |
---|
573 | n/a | self.assertEqual(memio.read(1), "\n") |
---|
574 | n/a | self.assertEqual(memio.getvalue(), "a\nb\nc\nd") |
---|
575 | n/a | |
---|
576 | n/a | memio = self.ioclass(newline=None) |
---|
577 | n/a | self.assertEqual(2, memio.write("a\n")) |
---|
578 | n/a | self.assertEqual(3, memio.write("b\r\n")) |
---|
579 | n/a | self.assertEqual(3, memio.write("c\rd")) |
---|
580 | n/a | memio.seek(0) |
---|
581 | n/a | self.assertEqual(memio.read(), "a\nb\nc\nd") |
---|
582 | n/a | self.assertEqual(memio.getvalue(), "a\nb\nc\nd") |
---|
583 | n/a | |
---|
584 | n/a | memio = self.ioclass("a\r\nb", newline=None) |
---|
585 | n/a | self.assertEqual(memio.read(3), "a\nb") |
---|
586 | n/a | |
---|
587 | n/a | def test_newline_empty(self): |
---|
588 | n/a | # newline="" |
---|
589 | n/a | memio = self.ioclass("a\nb\r\nc\rd", newline="") |
---|
590 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) |
---|
591 | n/a | memio.seek(0) |
---|
592 | n/a | self.assertEqual(memio.read(4), "a\nb\r") |
---|
593 | n/a | self.assertEqual(memio.read(2), "\nc") |
---|
594 | n/a | self.assertEqual(memio.read(1), "\r") |
---|
595 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
596 | n/a | |
---|
597 | n/a | memio = self.ioclass(newline="") |
---|
598 | n/a | self.assertEqual(2, memio.write("a\n")) |
---|
599 | n/a | self.assertEqual(2, memio.write("b\r")) |
---|
600 | n/a | self.assertEqual(2, memio.write("\nc")) |
---|
601 | n/a | self.assertEqual(2, memio.write("\rd")) |
---|
602 | n/a | memio.seek(0) |
---|
603 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"]) |
---|
604 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
605 | n/a | |
---|
606 | n/a | def test_newline_lf(self): |
---|
607 | n/a | # newline="\n" |
---|
608 | n/a | memio = self.ioclass("a\nb\r\nc\rd", newline="\n") |
---|
609 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) |
---|
610 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
611 | n/a | |
---|
612 | n/a | memio = self.ioclass(newline="\n") |
---|
613 | n/a | self.assertEqual(memio.write("a\nb\r\nc\rd"), 8) |
---|
614 | n/a | memio.seek(0) |
---|
615 | n/a | self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"]) |
---|
616 | n/a | self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd") |
---|
617 | n/a | |
---|
618 | n/a | def test_newline_cr(self): |
---|
619 | n/a | # newline="\r" |
---|
620 | n/a | memio = self.ioclass("a\nb\r\nc\rd", newline="\r") |
---|
621 | n/a | self.assertEqual(memio.read(), "a\rb\r\rc\rd") |
---|
622 | n/a | memio.seek(0) |
---|
623 | n/a | self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"]) |
---|
624 | n/a | self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd") |
---|
625 | n/a | |
---|
626 | n/a | memio = self.ioclass(newline="\r") |
---|
627 | n/a | self.assertEqual(memio.write("a\nb\r\nc\rd"), 8) |
---|
628 | n/a | memio.seek(0) |
---|
629 | n/a | self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"]) |
---|
630 | n/a | memio.seek(0) |
---|
631 | n/a | self.assertEqual(memio.readlines(), ["a\r", "b\r", "\r", "c\r", "d"]) |
---|
632 | n/a | self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd") |
---|
633 | n/a | |
---|
634 | n/a | def test_newline_crlf(self): |
---|
635 | n/a | # newline="\r\n" |
---|
636 | n/a | memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n") |
---|
637 | n/a | self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd") |
---|
638 | n/a | memio.seek(0) |
---|
639 | n/a | self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"]) |
---|
640 | n/a | memio.seek(0) |
---|
641 | n/a | self.assertEqual(memio.readlines(), ["a\r\n", "b\r\r\n", "c\rd"]) |
---|
642 | n/a | self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd") |
---|
643 | n/a | |
---|
644 | n/a | memio = self.ioclass(newline="\r\n") |
---|
645 | n/a | self.assertEqual(memio.write("a\nb\r\nc\rd"), 8) |
---|
646 | n/a | memio.seek(0) |
---|
647 | n/a | self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"]) |
---|
648 | n/a | self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd") |
---|
649 | n/a | |
---|
650 | n/a | def test_issue5265(self): |
---|
651 | n/a | # StringIO can duplicate newlines in universal newlines mode |
---|
652 | n/a | memio = self.ioclass("a\r\nb\r\n", newline=None) |
---|
653 | n/a | self.assertEqual(memio.read(5), "a\nb\n") |
---|
654 | n/a | self.assertEqual(memio.getvalue(), "a\nb\n") |
---|
655 | n/a | |
---|
656 | n/a | def test_newline_argument(self): |
---|
657 | n/a | self.assertRaises(TypeError, self.ioclass, newline=b"\n") |
---|
658 | n/a | self.assertRaises(ValueError, self.ioclass, newline="error") |
---|
659 | n/a | # These should not raise an error |
---|
660 | n/a | for newline in (None, "", "\n", "\r", "\r\n"): |
---|
661 | n/a | self.ioclass(newline=newline) |
---|
662 | n/a | |
---|
663 | n/a | |
---|
664 | n/a | class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin, |
---|
665 | n/a | TextIOTestMixin, unittest.TestCase): |
---|
666 | n/a | buftype = str |
---|
667 | n/a | ioclass = pyio.StringIO |
---|
668 | n/a | UnsupportedOperation = pyio.UnsupportedOperation |
---|
669 | n/a | EOF = "" |
---|
670 | n/a | |
---|
671 | n/a | def test_lone_surrogates(self): |
---|
672 | n/a | # Issue #20424 |
---|
673 | n/a | memio = self.ioclass('\ud800') |
---|
674 | n/a | self.assertEqual(memio.read(), '\ud800') |
---|
675 | n/a | |
---|
676 | n/a | memio = self.ioclass() |
---|
677 | n/a | memio.write('\ud800') |
---|
678 | n/a | self.assertEqual(memio.getvalue(), '\ud800') |
---|
679 | n/a | |
---|
680 | n/a | |
---|
681 | n/a | class PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase): |
---|
682 | n/a | """Test if pickle restores properly the internal state of StringIO. |
---|
683 | n/a | """ |
---|
684 | n/a | buftype = str |
---|
685 | n/a | UnsupportedOperation = pyio.UnsupportedOperation |
---|
686 | n/a | EOF = "" |
---|
687 | n/a | |
---|
688 | n/a | class ioclass(pyio.StringIO): |
---|
689 | n/a | def __new__(cls, *args, **kwargs): |
---|
690 | n/a | return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs))) |
---|
691 | n/a | def __init__(self, *args, **kwargs): |
---|
692 | n/a | pass |
---|
693 | n/a | |
---|
694 | n/a | |
---|
695 | n/a | class CBytesIOTest(PyBytesIOTest): |
---|
696 | n/a | ioclass = io.BytesIO |
---|
697 | n/a | UnsupportedOperation = io.UnsupportedOperation |
---|
698 | n/a | |
---|
699 | n/a | def test_getstate(self): |
---|
700 | n/a | memio = self.ioclass() |
---|
701 | n/a | state = memio.__getstate__() |
---|
702 | n/a | self.assertEqual(len(state), 3) |
---|
703 | n/a | bytearray(state[0]) # Check if state[0] supports the buffer interface. |
---|
704 | n/a | self.assertIsInstance(state[1], int) |
---|
705 | n/a | if state[2] is not None: |
---|
706 | n/a | self.assertIsInstance(state[2], dict) |
---|
707 | n/a | memio.close() |
---|
708 | n/a | self.assertRaises(ValueError, memio.__getstate__) |
---|
709 | n/a | |
---|
710 | n/a | def test_setstate(self): |
---|
711 | n/a | # This checks whether __setstate__ does proper input validation. |
---|
712 | n/a | memio = self.ioclass() |
---|
713 | n/a | memio.__setstate__((b"no error", 0, None)) |
---|
714 | n/a | memio.__setstate__((bytearray(b"no error"), 0, None)) |
---|
715 | n/a | memio.__setstate__((b"no error", 0, {'spam': 3})) |
---|
716 | n/a | self.assertRaises(ValueError, memio.__setstate__, (b"", -1, None)) |
---|
717 | n/a | self.assertRaises(TypeError, memio.__setstate__, ("unicode", 0, None)) |
---|
718 | n/a | self.assertRaises(TypeError, memio.__setstate__, (b"", 0.0, None)) |
---|
719 | n/a | self.assertRaises(TypeError, memio.__setstate__, (b"", 0, 0)) |
---|
720 | n/a | self.assertRaises(TypeError, memio.__setstate__, (b"len-test", 0)) |
---|
721 | n/a | self.assertRaises(TypeError, memio.__setstate__) |
---|
722 | n/a | self.assertRaises(TypeError, memio.__setstate__, 0) |
---|
723 | n/a | memio.close() |
---|
724 | n/a | self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None)) |
---|
725 | n/a | |
---|
726 | n/a | check_sizeof = support.check_sizeof |
---|
727 | n/a | |
---|
728 | n/a | @support.cpython_only |
---|
729 | n/a | def test_sizeof(self): |
---|
730 | n/a | basesize = support.calcobjsize('P2n2Pn') |
---|
731 | n/a | check = self.check_sizeof |
---|
732 | n/a | self.assertEqual(object.__sizeof__(io.BytesIO()), basesize) |
---|
733 | n/a | check(io.BytesIO(), basesize ) |
---|
734 | n/a | check(io.BytesIO(b'a' * 1000), basesize + sys.getsizeof(b'a' * 1000)) |
---|
735 | n/a | |
---|
736 | n/a | # Various tests of copy-on-write behaviour for BytesIO. |
---|
737 | n/a | |
---|
738 | n/a | def _test_cow_mutation(self, mutation): |
---|
739 | n/a | # Common code for all BytesIO copy-on-write mutation tests. |
---|
740 | n/a | imm = b' ' * 1024 |
---|
741 | n/a | old_rc = sys.getrefcount(imm) |
---|
742 | n/a | memio = self.ioclass(imm) |
---|
743 | n/a | self.assertEqual(sys.getrefcount(imm), old_rc + 1) |
---|
744 | n/a | mutation(memio) |
---|
745 | n/a | self.assertEqual(sys.getrefcount(imm), old_rc) |
---|
746 | n/a | |
---|
747 | n/a | @support.cpython_only |
---|
748 | n/a | def test_cow_truncate(self): |
---|
749 | n/a | # Ensure truncate causes a copy. |
---|
750 | n/a | def mutation(memio): |
---|
751 | n/a | memio.truncate(1) |
---|
752 | n/a | self._test_cow_mutation(mutation) |
---|
753 | n/a | |
---|
754 | n/a | @support.cpython_only |
---|
755 | n/a | def test_cow_write(self): |
---|
756 | n/a | # Ensure write that would not cause a resize still results in a copy. |
---|
757 | n/a | def mutation(memio): |
---|
758 | n/a | memio.seek(0) |
---|
759 | n/a | memio.write(b'foo') |
---|
760 | n/a | self._test_cow_mutation(mutation) |
---|
761 | n/a | |
---|
762 | n/a | @support.cpython_only |
---|
763 | n/a | def test_cow_setstate(self): |
---|
764 | n/a | # __setstate__ should cause buffer to be released. |
---|
765 | n/a | memio = self.ioclass(b'foooooo') |
---|
766 | n/a | state = memio.__getstate__() |
---|
767 | n/a | def mutation(memio): |
---|
768 | n/a | memio.__setstate__(state) |
---|
769 | n/a | self._test_cow_mutation(mutation) |
---|
770 | n/a | |
---|
771 | n/a | @support.cpython_only |
---|
772 | n/a | def test_cow_mutable(self): |
---|
773 | n/a | # BytesIO should accept only Bytes for copy-on-write sharing, since |
---|
774 | n/a | # arbitrary buffer-exporting objects like bytearray() aren't guaranteed |
---|
775 | n/a | # to be immutable. |
---|
776 | n/a | ba = bytearray(1024) |
---|
777 | n/a | old_rc = sys.getrefcount(ba) |
---|
778 | n/a | memio = self.ioclass(ba) |
---|
779 | n/a | self.assertEqual(sys.getrefcount(ba), old_rc) |
---|
780 | n/a | |
---|
781 | n/a | class CStringIOTest(PyStringIOTest): |
---|
782 | n/a | ioclass = io.StringIO |
---|
783 | n/a | UnsupportedOperation = io.UnsupportedOperation |
---|
784 | n/a | |
---|
785 | n/a | # XXX: For the Python version of io.StringIO, this is highly |
---|
786 | n/a | # dependent on the encoding used for the underlying buffer. |
---|
787 | n/a | def test_widechar(self): |
---|
788 | n/a | buf = self.buftype("\U0002030a\U00020347") |
---|
789 | n/a | memio = self.ioclass(buf) |
---|
790 | n/a | |
---|
791 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
792 | n/a | self.assertEqual(memio.write(buf), len(buf)) |
---|
793 | n/a | self.assertEqual(memio.tell(), len(buf)) |
---|
794 | n/a | self.assertEqual(memio.getvalue(), buf) |
---|
795 | n/a | self.assertEqual(memio.write(buf), len(buf)) |
---|
796 | n/a | self.assertEqual(memio.tell(), len(buf) * 2) |
---|
797 | n/a | self.assertEqual(memio.getvalue(), buf + buf) |
---|
798 | n/a | |
---|
799 | n/a | def test_getstate(self): |
---|
800 | n/a | memio = self.ioclass() |
---|
801 | n/a | state = memio.__getstate__() |
---|
802 | n/a | self.assertEqual(len(state), 4) |
---|
803 | n/a | self.assertIsInstance(state[0], str) |
---|
804 | n/a | self.assertIsInstance(state[1], str) |
---|
805 | n/a | self.assertIsInstance(state[2], int) |
---|
806 | n/a | if state[3] is not None: |
---|
807 | n/a | self.assertIsInstance(state[3], dict) |
---|
808 | n/a | memio.close() |
---|
809 | n/a | self.assertRaises(ValueError, memio.__getstate__) |
---|
810 | n/a | |
---|
811 | n/a | def test_setstate(self): |
---|
812 | n/a | # This checks whether __setstate__ does proper input validation. |
---|
813 | n/a | memio = self.ioclass() |
---|
814 | n/a | memio.__setstate__(("no error", "\n", 0, None)) |
---|
815 | n/a | memio.__setstate__(("no error", "", 0, {'spam': 3})) |
---|
816 | n/a | self.assertRaises(ValueError, memio.__setstate__, ("", "f", 0, None)) |
---|
817 | n/a | self.assertRaises(ValueError, memio.__setstate__, ("", "", -1, None)) |
---|
818 | n/a | self.assertRaises(TypeError, memio.__setstate__, (b"", "", 0, None)) |
---|
819 | n/a | self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None)) |
---|
820 | n/a | self.assertRaises(TypeError, memio.__setstate__, ("", "", 0.0, None)) |
---|
821 | n/a | self.assertRaises(TypeError, memio.__setstate__, ("", "", 0, 0)) |
---|
822 | n/a | self.assertRaises(TypeError, memio.__setstate__, ("len-test", 0)) |
---|
823 | n/a | self.assertRaises(TypeError, memio.__setstate__) |
---|
824 | n/a | self.assertRaises(TypeError, memio.__setstate__, 0) |
---|
825 | n/a | memio.close() |
---|
826 | n/a | self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None)) |
---|
827 | n/a | |
---|
828 | n/a | |
---|
829 | n/a | class CStringIOPickleTest(PyStringIOPickleTest): |
---|
830 | n/a | UnsupportedOperation = io.UnsupportedOperation |
---|
831 | n/a | |
---|
832 | n/a | class ioclass(io.StringIO): |
---|
833 | n/a | def __new__(cls, *args, **kwargs): |
---|
834 | n/a | return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs))) |
---|
835 | n/a | def __init__(self, *args, **kwargs): |
---|
836 | n/a | pass |
---|
837 | n/a | |
---|
838 | n/a | |
---|
839 | n/a | if __name__ == '__main__': |
---|
840 | n/a | unittest.main() |
---|