1 | n/a | import sys |
---|
2 | n/a | import os |
---|
3 | n/a | import unittest |
---|
4 | n/a | from array import array |
---|
5 | n/a | from weakref import proxy |
---|
6 | n/a | |
---|
7 | n/a | import io |
---|
8 | n/a | import _pyio as pyio |
---|
9 | n/a | |
---|
10 | n/a | from test.support import TESTFN |
---|
11 | n/a | from collections import UserList |
---|
12 | n/a | |
---|
13 | n/a | class AutoFileTests: |
---|
14 | n/a | # file tests for which a test file is automatically set up |
---|
15 | n/a | |
---|
16 | n/a | def setUp(self): |
---|
17 | n/a | self.f = self.open(TESTFN, 'wb') |
---|
18 | n/a | |
---|
19 | n/a | def tearDown(self): |
---|
20 | n/a | if self.f: |
---|
21 | n/a | self.f.close() |
---|
22 | n/a | os.remove(TESTFN) |
---|
23 | n/a | |
---|
24 | n/a | def testWeakRefs(self): |
---|
25 | n/a | # verify weak references |
---|
26 | n/a | p = proxy(self.f) |
---|
27 | n/a | p.write(b'teststring') |
---|
28 | n/a | self.assertEqual(self.f.tell(), p.tell()) |
---|
29 | n/a | self.f.close() |
---|
30 | n/a | self.f = None |
---|
31 | n/a | self.assertRaises(ReferenceError, getattr, p, 'tell') |
---|
32 | n/a | |
---|
33 | n/a | def testAttributes(self): |
---|
34 | n/a | # verify expected attributes exist |
---|
35 | n/a | f = self.f |
---|
36 | n/a | f.name # merely shouldn't blow up |
---|
37 | n/a | f.mode # ditto |
---|
38 | n/a | f.closed # ditto |
---|
39 | n/a | |
---|
40 | n/a | def testReadinto(self): |
---|
41 | n/a | # verify readinto |
---|
42 | n/a | self.f.write(b'12') |
---|
43 | n/a | self.f.close() |
---|
44 | n/a | a = array('b', b'x'*10) |
---|
45 | n/a | self.f = self.open(TESTFN, 'rb') |
---|
46 | n/a | n = self.f.readinto(a) |
---|
47 | n/a | self.assertEqual(b'12', a.tobytes()[:n]) |
---|
48 | n/a | |
---|
49 | n/a | def testReadinto_text(self): |
---|
50 | n/a | # verify readinto refuses text files |
---|
51 | n/a | a = array('b', b'x'*10) |
---|
52 | n/a | self.f.close() |
---|
53 | n/a | self.f = self.open(TESTFN, 'r') |
---|
54 | n/a | if hasattr(self.f, "readinto"): |
---|
55 | n/a | self.assertRaises(TypeError, self.f.readinto, a) |
---|
56 | n/a | |
---|
57 | n/a | def testWritelinesUserList(self): |
---|
58 | n/a | # verify writelines with instance sequence |
---|
59 | n/a | l = UserList([b'1', b'2']) |
---|
60 | n/a | self.f.writelines(l) |
---|
61 | n/a | self.f.close() |
---|
62 | n/a | self.f = self.open(TESTFN, 'rb') |
---|
63 | n/a | buf = self.f.read() |
---|
64 | n/a | self.assertEqual(buf, b'12') |
---|
65 | n/a | |
---|
66 | n/a | def testWritelinesIntegers(self): |
---|
67 | n/a | # verify writelines with integers |
---|
68 | n/a | self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) |
---|
69 | n/a | |
---|
70 | n/a | def testWritelinesIntegersUserList(self): |
---|
71 | n/a | # verify writelines with integers in UserList |
---|
72 | n/a | l = UserList([1,2,3]) |
---|
73 | n/a | self.assertRaises(TypeError, self.f.writelines, l) |
---|
74 | n/a | |
---|
75 | n/a | def testWritelinesNonString(self): |
---|
76 | n/a | # verify writelines with non-string object |
---|
77 | n/a | class NonString: |
---|
78 | n/a | pass |
---|
79 | n/a | |
---|
80 | n/a | self.assertRaises(TypeError, self.f.writelines, |
---|
81 | n/a | [NonString(), NonString()]) |
---|
82 | n/a | |
---|
83 | n/a | def testErrors(self): |
---|
84 | n/a | f = self.f |
---|
85 | n/a | self.assertEqual(f.name, TESTFN) |
---|
86 | n/a | self.assertFalse(f.isatty()) |
---|
87 | n/a | self.assertFalse(f.closed) |
---|
88 | n/a | |
---|
89 | n/a | if hasattr(f, "readinto"): |
---|
90 | n/a | self.assertRaises((OSError, TypeError), f.readinto, "") |
---|
91 | n/a | f.close() |
---|
92 | n/a | self.assertTrue(f.closed) |
---|
93 | n/a | |
---|
94 | n/a | def testMethods(self): |
---|
95 | n/a | methods = [('fileno', ()), |
---|
96 | n/a | ('flush', ()), |
---|
97 | n/a | ('isatty', ()), |
---|
98 | n/a | ('__next__', ()), |
---|
99 | n/a | ('read', ()), |
---|
100 | n/a | ('write', (b"",)), |
---|
101 | n/a | ('readline', ()), |
---|
102 | n/a | ('readlines', ()), |
---|
103 | n/a | ('seek', (0,)), |
---|
104 | n/a | ('tell', ()), |
---|
105 | n/a | ('write', (b"",)), |
---|
106 | n/a | ('writelines', ([],)), |
---|
107 | n/a | ('__iter__', ()), |
---|
108 | n/a | ] |
---|
109 | n/a | methods.append(('truncate', ())) |
---|
110 | n/a | |
---|
111 | n/a | # __exit__ should close the file |
---|
112 | n/a | self.f.__exit__(None, None, None) |
---|
113 | n/a | self.assertTrue(self.f.closed) |
---|
114 | n/a | |
---|
115 | n/a | for methodname, args in methods: |
---|
116 | n/a | method = getattr(self.f, methodname) |
---|
117 | n/a | # should raise on closed file |
---|
118 | n/a | self.assertRaises(ValueError, method, *args) |
---|
119 | n/a | |
---|
120 | n/a | # file is closed, __exit__ shouldn't do anything |
---|
121 | n/a | self.assertEqual(self.f.__exit__(None, None, None), None) |
---|
122 | n/a | # it must also return None if an exception was given |
---|
123 | n/a | try: |
---|
124 | n/a | 1/0 |
---|
125 | n/a | except: |
---|
126 | n/a | self.assertEqual(self.f.__exit__(*sys.exc_info()), None) |
---|
127 | n/a | |
---|
128 | n/a | def testReadWhenWriting(self): |
---|
129 | n/a | self.assertRaises(OSError, self.f.read) |
---|
130 | n/a | |
---|
131 | n/a | class CAutoFileTests(AutoFileTests, unittest.TestCase): |
---|
132 | n/a | open = io.open |
---|
133 | n/a | |
---|
134 | n/a | class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
---|
135 | n/a | open = staticmethod(pyio.open) |
---|
136 | n/a | |
---|
137 | n/a | |
---|
138 | n/a | class OtherFileTests: |
---|
139 | n/a | |
---|
140 | n/a | def testModeStrings(self): |
---|
141 | n/a | # check invalid mode strings |
---|
142 | n/a | for mode in ("", "aU", "wU+", "U+", "+U", "rU+"): |
---|
143 | n/a | try: |
---|
144 | n/a | f = self.open(TESTFN, mode) |
---|
145 | n/a | except ValueError: |
---|
146 | n/a | pass |
---|
147 | n/a | else: |
---|
148 | n/a | f.close() |
---|
149 | n/a | self.fail('%r is an invalid file mode' % mode) |
---|
150 | n/a | |
---|
151 | n/a | def testBadModeArgument(self): |
---|
152 | n/a | # verify that we get a sensible error message for bad mode argument |
---|
153 | n/a | bad_mode = "qwerty" |
---|
154 | n/a | try: |
---|
155 | n/a | f = self.open(TESTFN, bad_mode) |
---|
156 | n/a | except ValueError as msg: |
---|
157 | n/a | if msg.args[0] != 0: |
---|
158 | n/a | s = str(msg) |
---|
159 | n/a | if TESTFN in s or bad_mode not in s: |
---|
160 | n/a | self.fail("bad error message for invalid mode: %s" % s) |
---|
161 | n/a | # if msg.args[0] == 0, we're probably on Windows where there may be |
---|
162 | n/a | # no obvious way to discover why open() failed. |
---|
163 | n/a | else: |
---|
164 | n/a | f.close() |
---|
165 | n/a | self.fail("no error for invalid mode: %s" % bad_mode) |
---|
166 | n/a | |
---|
167 | n/a | def testSetBufferSize(self): |
---|
168 | n/a | # make sure that explicitly setting the buffer size doesn't cause |
---|
169 | n/a | # misbehaviour especially with repeated close() calls |
---|
170 | n/a | for s in (-1, 0, 1, 512): |
---|
171 | n/a | try: |
---|
172 | n/a | f = self.open(TESTFN, 'wb', s) |
---|
173 | n/a | f.write(str(s).encode("ascii")) |
---|
174 | n/a | f.close() |
---|
175 | n/a | f.close() |
---|
176 | n/a | f = self.open(TESTFN, 'rb', s) |
---|
177 | n/a | d = int(f.read().decode("ascii")) |
---|
178 | n/a | f.close() |
---|
179 | n/a | f.close() |
---|
180 | n/a | except OSError as msg: |
---|
181 | n/a | self.fail('error setting buffer size %d: %s' % (s, str(msg))) |
---|
182 | n/a | self.assertEqual(d, s) |
---|
183 | n/a | |
---|
184 | n/a | def testTruncateOnWindows(self): |
---|
185 | n/a | # SF bug <http://www.python.org/sf/801631> |
---|
186 | n/a | # "file.truncate fault on windows" |
---|
187 | n/a | |
---|
188 | n/a | os.unlink(TESTFN) |
---|
189 | n/a | f = self.open(TESTFN, 'wb') |
---|
190 | n/a | |
---|
191 | n/a | try: |
---|
192 | n/a | f.write(b'12345678901') # 11 bytes |
---|
193 | n/a | f.close() |
---|
194 | n/a | |
---|
195 | n/a | f = self.open(TESTFN,'rb+') |
---|
196 | n/a | data = f.read(5) |
---|
197 | n/a | if data != b'12345': |
---|
198 | n/a | self.fail("Read on file opened for update failed %r" % data) |
---|
199 | n/a | if f.tell() != 5: |
---|
200 | n/a | self.fail("File pos after read wrong %d" % f.tell()) |
---|
201 | n/a | |
---|
202 | n/a | f.truncate() |
---|
203 | n/a | if f.tell() != 5: |
---|
204 | n/a | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
---|
205 | n/a | |
---|
206 | n/a | f.close() |
---|
207 | n/a | size = os.path.getsize(TESTFN) |
---|
208 | n/a | if size != 5: |
---|
209 | n/a | self.fail("File size after ftruncate wrong %d" % size) |
---|
210 | n/a | finally: |
---|
211 | n/a | f.close() |
---|
212 | n/a | os.unlink(TESTFN) |
---|
213 | n/a | |
---|
214 | n/a | def testIteration(self): |
---|
215 | n/a | # Test the complex interaction when mixing file-iteration and the |
---|
216 | n/a | # various read* methods. |
---|
217 | n/a | dataoffset = 16384 |
---|
218 | n/a | filler = b"ham\n" |
---|
219 | n/a | assert not dataoffset % len(filler), \ |
---|
220 | n/a | "dataoffset must be multiple of len(filler)" |
---|
221 | n/a | nchunks = dataoffset // len(filler) |
---|
222 | n/a | testlines = [ |
---|
223 | n/a | b"spam, spam and eggs\n", |
---|
224 | n/a | b"eggs, spam, ham and spam\n", |
---|
225 | n/a | b"saussages, spam, spam and eggs\n", |
---|
226 | n/a | b"spam, ham, spam and eggs\n", |
---|
227 | n/a | b"spam, spam, spam, spam, spam, ham, spam\n", |
---|
228 | n/a | b"wonderful spaaaaaam.\n" |
---|
229 | n/a | ] |
---|
230 | n/a | methods = [("readline", ()), ("read", ()), ("readlines", ()), |
---|
231 | n/a | ("readinto", (array("b", b" "*100),))] |
---|
232 | n/a | |
---|
233 | n/a | try: |
---|
234 | n/a | # Prepare the testfile |
---|
235 | n/a | bag = self.open(TESTFN, "wb") |
---|
236 | n/a | bag.write(filler * nchunks) |
---|
237 | n/a | bag.writelines(testlines) |
---|
238 | n/a | bag.close() |
---|
239 | n/a | # Test for appropriate errors mixing read* and iteration |
---|
240 | n/a | for methodname, args in methods: |
---|
241 | n/a | f = self.open(TESTFN, 'rb') |
---|
242 | n/a | if next(f) != filler: |
---|
243 | n/a | self.fail, "Broken testfile" |
---|
244 | n/a | meth = getattr(f, methodname) |
---|
245 | n/a | meth(*args) # This simply shouldn't fail |
---|
246 | n/a | f.close() |
---|
247 | n/a | |
---|
248 | n/a | # Test to see if harmless (by accident) mixing of read* and |
---|
249 | n/a | # iteration still works. This depends on the size of the internal |
---|
250 | n/a | # iteration buffer (currently 8192,) but we can test it in a |
---|
251 | n/a | # flexible manner. Each line in the bag o' ham is 4 bytes |
---|
252 | n/a | # ("h", "a", "m", "\n"), so 4096 lines of that should get us |
---|
253 | n/a | # exactly on the buffer boundary for any power-of-2 buffersize |
---|
254 | n/a | # between 4 and 16384 (inclusive). |
---|
255 | n/a | f = self.open(TESTFN, 'rb') |
---|
256 | n/a | for i in range(nchunks): |
---|
257 | n/a | next(f) |
---|
258 | n/a | testline = testlines.pop(0) |
---|
259 | n/a | try: |
---|
260 | n/a | line = f.readline() |
---|
261 | n/a | except ValueError: |
---|
262 | n/a | self.fail("readline() after next() with supposedly empty " |
---|
263 | n/a | "iteration-buffer failed anyway") |
---|
264 | n/a | if line != testline: |
---|
265 | n/a | self.fail("readline() after next() with empty buffer " |
---|
266 | n/a | "failed. Got %r, expected %r" % (line, testline)) |
---|
267 | n/a | testline = testlines.pop(0) |
---|
268 | n/a | buf = array("b", b"\x00" * len(testline)) |
---|
269 | n/a | try: |
---|
270 | n/a | f.readinto(buf) |
---|
271 | n/a | except ValueError: |
---|
272 | n/a | self.fail("readinto() after next() with supposedly empty " |
---|
273 | n/a | "iteration-buffer failed anyway") |
---|
274 | n/a | line = buf.tobytes() |
---|
275 | n/a | if line != testline: |
---|
276 | n/a | self.fail("readinto() after next() with empty buffer " |
---|
277 | n/a | "failed. Got %r, expected %r" % (line, testline)) |
---|
278 | n/a | |
---|
279 | n/a | testline = testlines.pop(0) |
---|
280 | n/a | try: |
---|
281 | n/a | line = f.read(len(testline)) |
---|
282 | n/a | except ValueError: |
---|
283 | n/a | self.fail("read() after next() with supposedly empty " |
---|
284 | n/a | "iteration-buffer failed anyway") |
---|
285 | n/a | if line != testline: |
---|
286 | n/a | self.fail("read() after next() with empty buffer " |
---|
287 | n/a | "failed. Got %r, expected %r" % (line, testline)) |
---|
288 | n/a | try: |
---|
289 | n/a | lines = f.readlines() |
---|
290 | n/a | except ValueError: |
---|
291 | n/a | self.fail("readlines() after next() with supposedly empty " |
---|
292 | n/a | "iteration-buffer failed anyway") |
---|
293 | n/a | if lines != testlines: |
---|
294 | n/a | self.fail("readlines() after next() with empty buffer " |
---|
295 | n/a | "failed. Got %r, expected %r" % (line, testline)) |
---|
296 | n/a | f.close() |
---|
297 | n/a | |
---|
298 | n/a | # Reading after iteration hit EOF shouldn't hurt either |
---|
299 | n/a | f = self.open(TESTFN, 'rb') |
---|
300 | n/a | try: |
---|
301 | n/a | for line in f: |
---|
302 | n/a | pass |
---|
303 | n/a | try: |
---|
304 | n/a | f.readline() |
---|
305 | n/a | f.readinto(buf) |
---|
306 | n/a | f.read() |
---|
307 | n/a | f.readlines() |
---|
308 | n/a | except ValueError: |
---|
309 | n/a | self.fail("read* failed after next() consumed file") |
---|
310 | n/a | finally: |
---|
311 | n/a | f.close() |
---|
312 | n/a | finally: |
---|
313 | n/a | os.unlink(TESTFN) |
---|
314 | n/a | |
---|
315 | n/a | class COtherFileTests(OtherFileTests, unittest.TestCase): |
---|
316 | n/a | open = io.open |
---|
317 | n/a | |
---|
318 | n/a | class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
---|
319 | n/a | open = staticmethod(pyio.open) |
---|
320 | n/a | |
---|
321 | n/a | |
---|
322 | n/a | def tearDownModule(): |
---|
323 | n/a | # Historically, these tests have been sloppy about removing TESTFN. |
---|
324 | n/a | # So get rid of it no matter what. |
---|
325 | n/a | if os.path.exists(TESTFN): |
---|
326 | n/a | os.unlink(TESTFN) |
---|
327 | n/a | |
---|
328 | n/a | if __name__ == '__main__': |
---|
329 | n/a | unittest.main() |
---|