annotate Lib/test/test_memoryio.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 3c9232a684ae
children 1d3155750808
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1 """Unit tests for memory-based file-like objects.
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2 StringIO -- for unicode strings
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3 BytesIO -- for bytes
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4 """
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46724
diff changeset
7 from test import support
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
8
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
9 import io
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
10 import _pyio as pyio
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
11 import pickle
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
12
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
13 class MemorySeekTestMixin:
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
14
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
15 def testInit(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
16 buf = self.buftype("1234567890")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
17 bytesIo = self.ioclass(buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
18
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
19 def testRead(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
20 buf = self.buftype("1234567890")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
21 bytesIo = self.ioclass(buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
22
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
23 self.assertEqual(buf[:1], bytesIo.read(1))
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
24 self.assertEqual(buf[1:5], bytesIo.read(4))
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
25 self.assertEqual(buf[5:], bytesIo.read(900))
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
26 self.assertEqual(self.EOF, bytesIo.read())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
27
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
28 def testReadNoArgs(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
29 buf = self.buftype("1234567890")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
30 bytesIo = self.ioclass(buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
31
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
32 self.assertEqual(buf, bytesIo.read())
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
33 self.assertEqual(self.EOF, bytesIo.read())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
34
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
35 def testSeek(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
36 buf = self.buftype("1234567890")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
37 bytesIo = self.ioclass(buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
38
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
39 bytesIo.read(5)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
40 bytesIo.seek(0)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
41 self.assertEqual(buf, bytesIo.read())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
42
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
43 bytesIo.seek(3)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
44 self.assertEqual(buf[3:], bytesIo.read())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
45 self.assertRaises(TypeError, bytesIo.seek, 0.0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
46
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
47 def testTell(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
48 buf = self.buftype("1234567890")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
49 bytesIo = self.ioclass(buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
50
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
51 self.assertEqual(0, bytesIo.tell())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
52 bytesIo.seek(5)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
53 self.assertEqual(5, bytesIo.tell())
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
54 bytesIo.seek(10000)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66302
diff changeset
55 self.assertEqual(10000, bytesIo.tell())
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
56
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
57
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
58 class MemoryTestMixin:
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
59
53649
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
60 def test_detach(self):
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
61 buf = self.ioclass()
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
62 self.assertRaises(self.UnsupportedOperation, buf.detach)
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
63
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
64 def write_ops(self, f, t):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
65 self.assertEqual(f.write(t("blah.")), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
66 self.assertEqual(f.seek(0), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67 self.assertEqual(f.write(t("Hello.")), 6)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68 self.assertEqual(f.tell(), 6)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
69 self.assertEqual(f.seek(5), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
70 self.assertEqual(f.tell(), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
71 self.assertEqual(f.write(t(" world\n\n\n")), 9)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
72 self.assertEqual(f.seek(0), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
73 self.assertEqual(f.write(t("h")), 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
74 self.assertEqual(f.truncate(12), 12)
58631
d887db501a22 Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55609
diff changeset
75 self.assertEqual(f.tell(), 1)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
77 def test_write(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78 buf = self.buftype("hello world\n")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
81 self.write_ops(memio, self.buftype)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
82 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
84 self.write_ops(memio, self.buftype)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86 self.assertRaises(TypeError, memio.write, None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 self.assertRaises(ValueError, memio.write, self.buftype(""))
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 def test_writelines(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 self.assertEqual(memio.writelines([buf] * 100), None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95 self.assertEqual(memio.getvalue(), buf * 100)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 memio.writelines([])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97 self.assertEqual(memio.getvalue(), buf * 100)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
98 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
99 self.assertRaises(TypeError, memio.writelines, [buf] + [1])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
100 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
101 self.assertRaises(TypeError, memio.writelines, None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
102 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
103 self.assertRaises(ValueError, memio.writelines, [])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
104
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
105 def test_writelines_error(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
106 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
107 def error_gen():
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
108 yield self.buftype('spam')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
109 raise KeyboardInterrupt
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
110
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
111 self.assertRaises(KeyboardInterrupt, memio.writelines, error_gen())
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
112
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
113 def test_truncate(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
116
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
117 self.assertRaises(ValueError, memio.truncate, -1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
118 memio.seek(6)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
119 self.assertEqual(memio.truncate(), 6)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
120 self.assertEqual(memio.getvalue(), buf[:6])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
121 self.assertEqual(memio.truncate(4), 4)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
122 self.assertEqual(memio.getvalue(), buf[:4])
58631
d887db501a22 Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55609
diff changeset
123 self.assertEqual(memio.tell(), 6)
d887db501a22 Merged revisions 77895-77896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 55609
diff changeset
124 memio.seek(0, 2)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
125 memio.write(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
126 self.assertEqual(memio.getvalue(), buf[:4] + buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
127 pos = memio.tell()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
128 self.assertEqual(memio.truncate(None), pos)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
129 self.assertEqual(memio.tell(), pos)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
130 self.assertRaises(TypeError, memio.truncate, '0')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
131 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
132 self.assertRaises(ValueError, memio.truncate, 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
133
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
134 def test_init(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
135 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
136 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
137 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
138 memio = self.ioclass(None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
139 self.assertEqual(memio.getvalue(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
140 memio.__init__(buf * 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
141 self.assertEqual(memio.getvalue(), buf * 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
142 memio.__init__(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
143 self.assertEqual(memio.getvalue(), buf)
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
144 self.assertRaises(TypeError, memio.__init__, [])
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
145
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
146 def test_read(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
147 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
148 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
149
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
150 self.assertEqual(memio.read(0), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
151 self.assertEqual(memio.read(1), buf[:1])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
152 self.assertEqual(memio.read(4), buf[1:5])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
153 self.assertEqual(memio.read(900), buf[5:])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
154 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
155 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
156 self.assertEqual(memio.read(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
157 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
158 self.assertEqual(memio.tell(), 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
159 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
160 self.assertEqual(memio.read(-1), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
161 memio.seek(0)
46695
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
162 self.assertEqual(type(memio.read()), type(buf))
46709
7486c29f93a3 Fixed a small bug introduced by r62778.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46695
diff changeset
163 memio.seek(100)
7486c29f93a3 Fixed a small bug introduced by r62778.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46695
diff changeset
164 self.assertEqual(type(memio.read()), type(buf))
46695
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
165 memio.seek(0)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
166 self.assertEqual(memio.read(None), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
167 self.assertRaises(TypeError, memio.read, '')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
168 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
169 self.assertRaises(ValueError, memio.read)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
170
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
171 def test_readline(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
172 buf = self.buftype("1234567890\n")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
173 memio = self.ioclass(buf * 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
174
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
175 self.assertEqual(memio.readline(0), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
176 self.assertEqual(memio.readline(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
177 self.assertEqual(memio.readline(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
178 self.assertEqual(memio.readline(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
179 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
180 self.assertEqual(memio.readline(5), buf[:5])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
181 self.assertEqual(memio.readline(5), buf[5:10])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
182 self.assertEqual(memio.readline(5), buf[10:15])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
183 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
184 self.assertEqual(memio.readline(-1), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
185 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
186 self.assertEqual(memio.readline(0), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
187
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
188 buf = self.buftype("1234567890\n")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
189 memio = self.ioclass((buf * 3)[:-1])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
190 self.assertEqual(memio.readline(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
191 self.assertEqual(memio.readline(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
192 self.assertEqual(memio.readline(), buf[:-1])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
193 self.assertEqual(memio.readline(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
194 memio.seek(0)
46695
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
195 self.assertEqual(type(memio.readline()), type(buf))
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
196 self.assertEqual(memio.readline(), buf)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
197 self.assertRaises(TypeError, memio.readline, '')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
198 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
199 self.assertRaises(ValueError, memio.readline)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
200
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
201 def test_readlines(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
202 buf = self.buftype("1234567890\n")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
203 memio = self.ioclass(buf * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
204
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
205 self.assertEqual(memio.readlines(), [buf] * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
206 memio.seek(5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
207 self.assertEqual(memio.readlines(), [buf[5:]] + [buf] * 9)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
208 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
209 self.assertEqual(memio.readlines(15), [buf] * 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
210 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
211 self.assertEqual(memio.readlines(-1), [buf] * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
212 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
213 self.assertEqual(memio.readlines(0), [buf] * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
214 memio.seek(0)
46695
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
215 self.assertEqual(type(memio.readlines()[0]), type(buf))
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
216 memio.seek(0)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
217 self.assertEqual(memio.readlines(None), [buf] * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
218 self.assertRaises(TypeError, memio.readlines, '')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
219 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
220 self.assertRaises(ValueError, memio.readlines)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
221
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
222 def test_iterator(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
223 buf = self.buftype("1234567890\n")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
224 memio = self.ioclass(buf * 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
225
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
226 self.assertEqual(iter(memio), memio)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55581
diff changeset
227 self.assertTrue(hasattr(memio, '__iter__'))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55581
diff changeset
228 self.assertTrue(hasattr(memio, '__next__'))
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
229 i = 0
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
230 for line in memio:
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
231 self.assertEqual(line, buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
232 i += 1
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
233 self.assertEqual(i, 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
234 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
235 i = 0
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
236 for line in memio:
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
237 self.assertEqual(line, buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
238 i += 1
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
239 self.assertEqual(i, 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
240 memio = self.ioclass(buf * 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
241 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
242 self.assertRaises(ValueError, memio.__next__)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
243
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
244 def test_getvalue(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
245 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
246 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
247
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
248 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
249 memio.read()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
250 self.assertEqual(memio.getvalue(), buf)
46695
02a8142d83db Added tests to check the buffer type returned during reading.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46694
diff changeset
251 self.assertEqual(type(memio.getvalue()), type(buf))
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
252 memio = self.ioclass(buf * 1000)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
253 self.assertEqual(memio.getvalue()[-3:], self.buftype("890"))
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
254 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
255 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
256 self.assertRaises(ValueError, memio.getvalue)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
257
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
258 def test_seek(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
259 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
260 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
261
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
262 memio.read(5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
263 self.assertRaises(ValueError, memio.seek, -1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
264 self.assertRaises(ValueError, memio.seek, 1, -1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
265 self.assertRaises(ValueError, memio.seek, 1, 3)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
266 self.assertEqual(memio.seek(0), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
267 self.assertEqual(memio.seek(0, 0), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
268 self.assertEqual(memio.read(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
269 self.assertEqual(memio.seek(3), 3)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
270 self.assertEqual(memio.seek(0, 1), 3)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
271 self.assertEqual(memio.read(), buf[3:])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
272 self.assertEqual(memio.seek(len(buf)), len(buf))
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
273 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
274 memio.seek(len(buf) + 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
275 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
276 self.assertEqual(memio.seek(0, 2), len(buf))
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
277 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
278 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
279 self.assertRaises(ValueError, memio.seek, 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
280
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
281 def test_overseek(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
282 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
283 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
284
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
285 self.assertEqual(memio.seek(len(buf) + 1), 11)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
286 self.assertEqual(memio.read(), self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
287 self.assertEqual(memio.tell(), 11)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
288 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
289 memio.write(self.EOF)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
290 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
291 memio.write(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
292 self.assertEqual(memio.getvalue(), buf + self.buftype('\0') + buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
293
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
294 def test_tell(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
295 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
296 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
297
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
298 self.assertEqual(memio.tell(), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
299 memio.seek(5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
300 self.assertEqual(memio.tell(), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
301 memio.seek(10000)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
302 self.assertEqual(memio.tell(), 10000)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
303 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
304 self.assertRaises(ValueError, memio.tell)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
305
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
306 def test_flush(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
307 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
308 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
309
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
310 self.assertEqual(memio.flush(), None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
311
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
312 def test_flags(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
313 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
314
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
315 self.assertEqual(memio.writable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
316 self.assertEqual(memio.readable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
317 self.assertEqual(memio.seekable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
318 self.assertEqual(memio.isatty(), False)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
319 self.assertEqual(memio.closed, False)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
320 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
321 self.assertEqual(memio.writable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
322 self.assertEqual(memio.readable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
323 self.assertEqual(memio.seekable(), True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
324 self.assertRaises(ValueError, memio.isatty)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
325 self.assertEqual(memio.closed, True)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
326
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
327 def test_subclassing(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
328 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
329 def test1():
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
330 class MemIO(self.ioclass):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
331 pass
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
332 m = MemIO(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
333 return m.getvalue()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
334 def test2():
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
335 class MemIO(self.ioclass):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
336 def __init__(me, a, b):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
337 self.ioclass.__init__(me, a)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
338 m = MemIO(buf, None)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
339 return m.getvalue()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
340 self.assertEqual(test1(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
341 self.assertEqual(test2(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
342
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
343 def test_instance_dict_leak(self):
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
344 # Test case for issue #6242.
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
345 # This will be caught by regrtest.py -R if this leak.
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
346 for _ in range(100):
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
347 memio = self.ioclass()
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
348 memio.foo = 1
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
349
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
350 def test_pickling(self):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
351 buf = self.buftype("1234567890")
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
352 memio = self.ioclass(buf)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
353 memio.foo = 42
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
354 memio.seek(2)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
355
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
356 class PickleTestMemIO(self.ioclass):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
357 def __init__(me, initvalue, foo):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
358 self.ioclass.__init__(me, initvalue)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
359 me.foo = foo
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
360 # __getnewargs__ is undefined on purpose. This checks that PEP 307
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
361 # is used to provide pickling support.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
362
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
363 # Pickle expects the class to be on the module level. Here we use a
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
364 # little hack to allow the PickleTestMemIO class to derive from
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
365 # self.ioclass without having to define all combinations explictly on
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
366 # the module-level.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
367 import __main__
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
368 PickleTestMemIO.__module__ = '__main__'
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
369 __main__.PickleTestMemIO = PickleTestMemIO
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
370 submemio = PickleTestMemIO(buf, 80)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
371 submemio.seek(2)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
372
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
373 # We only support pickle protocol 2 and onward since we use extended
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
374 # __reduce__ API of PEP 307 to provide pickling support.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
375 for proto in range(2, pickle.HIGHEST_PROTOCOL):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
376 for obj in (memio, submemio):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
377 obj2 = pickle.loads(pickle.dumps(obj, protocol=proto))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
378 self.assertEqual(obj.getvalue(), obj2.getvalue())
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
379 self.assertEqual(obj.__class__, obj2.__class__)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
380 self.assertEqual(obj.foo, obj2.foo)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
381 self.assertEqual(obj.tell(), obj2.tell())
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
382 obj.close()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
383 self.assertRaises(ValueError, pickle.dumps, obj, proto)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
384 del __main__.PickleTestMemIO
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
385
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
386
64496
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
387 class BytesIOMixin:
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
388
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
389 def test_getbuffer(self):
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
390 memio = self.ioclass(b"1234567890")
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
391 buf = memio.getbuffer()
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
392 self.assertEqual(bytes(buf), b"1234567890")
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
393 memio.seek(5)
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
394 buf = memio.getbuffer()
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
395 self.assertEqual(bytes(buf), b"1234567890")
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
396 # Trying to change the size of the BytesIO while a buffer is exported
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
397 # raises a BufferError.
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
398 self.assertRaises(BufferError, memio.write, b'x' * 100)
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
399 self.assertRaises(BufferError, memio.truncate)
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
400 # Mutating the buffer updates the BytesIO
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
401 buf[3:6] = b"abc"
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
402 self.assertEqual(bytes(buf), b"123abc7890")
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
403 self.assertEqual(memio.getvalue(), b"123abc7890")
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
404 # After the buffer gets released, we can resize the BytesIO again
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
405 del buf
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
406 support.gc_collect()
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
407 memio.truncate()
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
408
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
409
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
410 class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin,
21405812f312 Issue #5506: BytesIO objects now have a getbuffer() method exporting a
Antoine Pitrou <solipsis@pitrou.net>
parents: 64355
diff changeset
411 BytesIOMixin, unittest.TestCase):
53649
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
412
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
413 UnsupportedOperation = pyio.UnsupportedOperation
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
414
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
415 @staticmethod
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
416 def buftype(s):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
417 return s.encode("ascii")
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
418 ioclass = pyio.BytesIO
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
419 EOF = b""
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
420
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
421 def test_read1(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
422 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
423 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
424
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
425 self.assertRaises(TypeError, memio.read1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
426 self.assertEqual(memio.read(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
427
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
428 def test_readinto(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
429 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
430 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
431
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
432 b = bytearray(b"hello")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
433 self.assertEqual(memio.readinto(b), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
434 self.assertEqual(b, b"12345")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
435 self.assertEqual(memio.readinto(b), 5)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
436 self.assertEqual(b, b"67890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
437 self.assertEqual(memio.readinto(b), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
438 self.assertEqual(b, b"67890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
439 b = bytearray(b"hello world")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
440 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
441 self.assertEqual(memio.readinto(b), 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
442 self.assertEqual(b, b"1234567890d")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
443 b = bytearray(b"")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
444 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
445 self.assertEqual(memio.readinto(b), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
446 self.assertEqual(b, b"")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
447 self.assertRaises(TypeError, memio.readinto, '')
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
448 import array
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
449 a = array.array('b', b"hello world")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
450 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
451 memio.readinto(a)
64355
fa8b57f987c5 Issue #8990: array.fromstring() and array.tostring() get renamed to
Antoine Pitrou <solipsis@pitrou.net>
parents: 59615
diff changeset
452 self.assertEqual(a.tobytes(), b"1234567890d")
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
453 memio.close()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
454 self.assertRaises(ValueError, memio.readinto, b)
66302
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58631
diff changeset
455 memio = self.ioclass(b"123")
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58631
diff changeset
456 b = bytearray()
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58631
diff changeset
457 memio.seek(42)
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58631
diff changeset
458 memio.readinto(b)
7cdbdbf3480b Merged revisions 86587 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 58631
diff changeset
459 self.assertEqual(b, b"")
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
460
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
461 def test_relative_seek(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
462 buf = self.buftype("1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
463 memio = self.ioclass(buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
464
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
465 self.assertEqual(memio.seek(-1, 1), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
466 self.assertEqual(memio.seek(3, 1), 3)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
467 self.assertEqual(memio.seek(-4, 1), 0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
468 self.assertEqual(memio.seek(-1, 2), 9)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
469 self.assertEqual(memio.seek(1, 1), 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
470 self.assertEqual(memio.seek(1, 2), 11)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
471 memio.seek(-3, 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
472 self.assertEqual(memio.read(), buf[-3:])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
473 memio.seek(0)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
474 memio.seek(1, 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
475 self.assertEqual(memio.read(), buf[1:])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
476
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
477 def test_unicode(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
478 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
479
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480 self.assertRaises(TypeError, self.ioclass, "1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
481 self.assertRaises(TypeError, memio.write, "1234567890")
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
482 self.assertRaises(TypeError, memio.writelines, ["1234567890"])
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
483
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
484 def test_bytes_array(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
485 buf = b"1234567890"
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
486 import array
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
487 a = array.array('b', list(buf))
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
488 memio = self.ioclass(a)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
489 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
490 self.assertEqual(memio.write(a), 10)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
491 self.assertEqual(memio.getvalue(), buf)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
492
55581
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54599
diff changeset
493 def test_issue5449(self):
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54599
diff changeset
494 buf = self.buftype("1234567890")
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54599
diff changeset
495 self.ioclass(initial_bytes=buf)
1acd0277d225 Merged revisions 74316,74335 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54599
diff changeset
496 self.assertRaises(TypeError, self.ioclass, buf, foo=None)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
497
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
498
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
499 class TextIOTestMixin:
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
500
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
501 def test_newlines_property(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
502 memio = self.ioclass(newline=None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
503 # The C StringIO decodes newlines in write() calls, but the Python
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
504 # implementation only does when reading. This function forces them to
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
505 # be decoded for testing.
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
506 def force_decode():
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
507 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
508 memio.read()
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
509 self.assertEqual(memio.newlines, None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
510 memio.write("a\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
511 force_decode()
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
512 self.assertEqual(memio.newlines, "\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
513 memio.write("b\r\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
514 force_decode()
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
515 self.assertEqual(memio.newlines, ("\n", "\r\n"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
516 memio.write("c\rd")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
517 force_decode()
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
518 self.assertEqual(memio.newlines, ("\r", "\n", "\r\n"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
519
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
520 def test_relative_seek(self):
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521 memio = self.ioclass()
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 self.assertRaises(IOError, memio.seek, -1, 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524 self.assertRaises(IOError, memio.seek, 3, 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
525 self.assertRaises(IOError, memio.seek, -3, 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526 self.assertRaises(IOError, memio.seek, -1, 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 self.assertRaises(IOError, memio.seek, 1, 1)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528 self.assertRaises(IOError, memio.seek, 1, 2)
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
529
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
530 def test_textio_properties(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
531 memio = self.ioclass()
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
532
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
533 # These are just dummy values but we nevertheless check them for fear
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
534 # of unexpected breakage.
54599
1f8d98720acc give the C implementation of TextIOWrapper the errors property #6217
Benjamin Peterson <benjamin@python.org>
parents: 53649
diff changeset
535 self.assertIsNone(memio.encoding)
1f8d98720acc give the C implementation of TextIOWrapper the errors property #6217
Benjamin Peterson <benjamin@python.org>
parents: 53649
diff changeset
536 self.assertIsNone(memio.errors)
1f8d98720acc give the C implementation of TextIOWrapper the errors property #6217
Benjamin Peterson <benjamin@python.org>
parents: 53649
diff changeset
537 self.assertFalse(memio.line_buffering)
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
538
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
539 def test_newline_none(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
540 # newline=None
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
541 memio = self.ioclass("a\nb\r\nc\rd", newline=None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
542 self.assertEqual(list(memio), ["a\n", "b\n", "c\n", "d"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
543 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
544 self.assertEqual(memio.read(1), "a")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
545 self.assertEqual(memio.read(2), "\nb")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
546 self.assertEqual(memio.read(2), "\nc")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
547 self.assertEqual(memio.read(1), "\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
548 memio = self.ioclass(newline=None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
549 self.assertEqual(2, memio.write("a\n"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
550 self.assertEqual(3, memio.write("b\r\n"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
551 self.assertEqual(3, memio.write("c\rd"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
552 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
553 self.assertEqual(memio.read(), "a\nb\nc\nd")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
554 memio = self.ioclass("a\r\nb", newline=None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
555 self.assertEqual(memio.read(3), "a\nb")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
556
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
557 def test_newline_empty(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
558 # newline=""
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
559 memio = self.ioclass("a\nb\r\nc\rd", newline="")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
560 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
561 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
562 self.assertEqual(memio.read(4), "a\nb\r")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
563 self.assertEqual(memio.read(2), "\nc")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
564 self.assertEqual(memio.read(1), "\r")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
565 memio = self.ioclass(newline="")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
566 self.assertEqual(2, memio.write("a\n"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
567 self.assertEqual(2, memio.write("b\r"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
568 self.assertEqual(2, memio.write("\nc"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
569 self.assertEqual(2, memio.write("\rd"))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
570 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
571 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\r", "d"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
572
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
573 def test_newline_lf(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
574 # newline="\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
575 memio = self.ioclass("a\nb\r\nc\rd")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
576 self.assertEqual(list(memio), ["a\n", "b\r\n", "c\rd"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
577
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
578 def test_newline_cr(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
579 # newline="\r"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
580 memio = self.ioclass("a\nb\r\nc\rd", newline="\r")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
581 self.assertEqual(memio.read(), "a\rb\r\rc\rd")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
582 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
583 self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
584
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
585 def test_newline_crlf(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
586 # newline="\r\n"
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
587 memio = self.ioclass("a\nb\r\nc\rd", newline="\r\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
588 self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
589 memio.seek(0)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
590 self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
591
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
592 def test_issue5265(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
593 # StringIO can duplicate newlines in universal newlines mode
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
594 memio = self.ioclass("a\r\nb\r\n", newline=None)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
595 self.assertEqual(memio.read(5), "a\nb\n")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
596
55609
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
597 def test_newline_argument(self):
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
598 self.assertRaises(TypeError, self.ioclass, newline=b"\n")
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
599 self.assertRaises(ValueError, self.ioclass, newline="error")
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
600 # These should not raise an error
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
601 for newline in (None, "", "\n", "\r", "\r\n"):
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
602 self.ioclass(newline=newline)
9d40586a6afc Merged revisions 74126,74130-74131,74149,74155,74157,74180-74183,74398 via svnmerge from
Georg Brandl <georg@python.org>
parents: 55593
diff changeset
603
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
604
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
605 class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
606 TextIOTestMixin, unittest.TestCase):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
607 buftype = str
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
608 ioclass = pyio.StringIO
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
609 UnsupportedOperation = pyio.UnsupportedOperation
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
610 EOF = ""
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
611
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
612
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
613 class PyStringIOPickleTest(TextIOTestMixin, unittest.TestCase):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
614 """Test if pickle restores properly the internal state of StringIO.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
615 """
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
616 buftype = str
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
617 UnsupportedOperation = pyio.UnsupportedOperation
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
618 EOF = ""
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
619
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
620 class ioclass(pyio.StringIO):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
621 def __new__(cls, *args, **kwargs):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
622 return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs)))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
623 def __init__(self, *args, **kwargs):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
624 pass
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
625
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
626
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
627 class CBytesIOTest(PyBytesIOTest):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
628 ioclass = io.BytesIO
53649
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
629 UnsupportedOperation = io.UnsupportedOperation
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
630
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
631 def test_getstate(self):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
632 memio = self.ioclass()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
633 state = memio.__getstate__()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
634 self.assertEqual(len(state), 3)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
635 bytearray(state[0]) # Check if state[0] supports the buffer interface.
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55551
diff changeset
636 self.assertIsInstance(state[1], int)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66301
diff changeset
637 self.assertTrue(isinstance(state[2], dict) or state[2] is None)
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
638 memio.close()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
639 self.assertRaises(ValueError, memio.__getstate__)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
640
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
641 def test_setstate(self):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
642 # This checks whether __setstate__ does proper input validation.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
643 memio = self.ioclass()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
644 memio.__setstate__((b"no error", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
645 memio.__setstate__((bytearray(b"no error"), 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
646 memio.__setstate__((b"no error", 0, {'spam': 3}))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
647 self.assertRaises(ValueError, memio.__setstate__, (b"", -1, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
648 self.assertRaises(TypeError, memio.__setstate__, ("unicode", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
649 self.assertRaises(TypeError, memio.__setstate__, (b"", 0.0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
650 self.assertRaises(TypeError, memio.__setstate__, (b"", 0, 0))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
651 self.assertRaises(TypeError, memio.__setstate__, (b"len-test", 0))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
652 self.assertRaises(TypeError, memio.__setstate__)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
653 self.assertRaises(TypeError, memio.__setstate__, 0)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
654 memio.close()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
655 self.assertRaises(ValueError, memio.__setstate__, (b"closed", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
656
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
657
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
658 class CStringIOTest(PyStringIOTest):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
659 ioclass = io.StringIO
53649
bc04cebca7e6 implement a detach() method for BufferedIOBase and TextIOBase #5883
Benjamin Peterson <benjamin@python.org>
parents: 52247
diff changeset
660 UnsupportedOperation = io.UnsupportedOperation
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
661
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
662 # XXX: For the Python version of io.StringIO, this is highly
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
663 # dependent on the encoding used for the underlying buffer.
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
664 def test_widechar(self):
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
665 buf = self.buftype("\U0002030a\U00020347")
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
666 memio = self.ioclass(buf)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
667
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
668 self.assertEqual(memio.getvalue(), buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
669 self.assertEqual(memio.write(buf), len(buf))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
670 self.assertEqual(memio.tell(), len(buf))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
671 self.assertEqual(memio.getvalue(), buf)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
672 self.assertEqual(memio.write(buf), len(buf))
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
673 self.assertEqual(memio.tell(), len(buf) * 2)
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 47625
diff changeset
674 self.assertEqual(memio.getvalue(), buf + buf)
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47201
diff changeset
675
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
676 def test_getstate(self):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
677 memio = self.ioclass()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
678 state = memio.__getstate__()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
679 self.assertEqual(len(state), 4)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55551
diff changeset
680 self.assertIsInstance(state[0], str)
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55551
diff changeset
681 self.assertIsInstance(state[1], str)
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55551
diff changeset
682 self.assertIsInstance(state[2], int)
66310
9fe76c26ea9f #9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 66301
diff changeset
683 self.assertTrue(isinstance(state[3], dict) or state[3] is None)
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
684 memio.close()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
685 self.assertRaises(ValueError, memio.__getstate__)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
686
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
687 def test_setstate(self):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
688 # This checks whether __setstate__ does proper input validation.
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
689 memio = self.ioclass()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
690 memio.__setstate__(("no error", "\n", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
691 memio.__setstate__(("no error", "", 0, {'spam': 3}))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
692 self.assertRaises(ValueError, memio.__setstate__, ("", "f", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
693 self.assertRaises(ValueError, memio.__setstate__, ("", "", -1, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
694 self.assertRaises(TypeError, memio.__setstate__, (b"", "", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
695 self.assertRaises(TypeError, memio.__setstate__, ("", b"", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
696 self.assertRaises(TypeError, memio.__setstate__, ("", "", 0.0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
697 self.assertRaises(TypeError, memio.__setstate__, ("", "", 0, 0))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
698 self.assertRaises(TypeError, memio.__setstate__, ("len-test", 0))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
699 self.assertRaises(TypeError, memio.__setstate__)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
700 self.assertRaises(TypeError, memio.__setstate__, 0)
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
701 memio.close()
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
702 self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
703
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
704
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
705 class CStringIOPickleTest(PyStringIOPickleTest):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
706 UnsupportedOperation = io.UnsupportedOperation
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
707
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
708 class ioclass(io.StringIO):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
709 def __new__(cls, *args, **kwargs):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
710 return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs)))
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
711 def __init__(self, *args, **kwargs):
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
712 pass
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
713
47625
1bfea956d0d7 Issue 2918: Merge StringIO and cStringIO.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47201
diff changeset
714
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
715 def test_main():
55398
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
716 tests = [PyBytesIOTest, PyStringIOTest, CBytesIOTest, CStringIOTest,
538850b38931 Issue #6218: Make io.BytesIO and io.StringIO picklable.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 55397
diff changeset
717 PyStringIOPickleTest, CStringIOPickleTest]
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46724
diff changeset
718 support.run_unittest(*tests)
46694
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
719
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
720 if __name__ == '__main__':
cda816c3a1e4 Added fast alternate io.BytesIO implementation and its test suite.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
721 test_main()