1 | n/a | # Adapted from test_file.py by Daniel Stutzbach |
---|
2 | n/a | |
---|
3 | n/a | import sys |
---|
4 | n/a | import os |
---|
5 | n/a | import io |
---|
6 | n/a | import errno |
---|
7 | n/a | import unittest |
---|
8 | n/a | from array import array |
---|
9 | n/a | from weakref import proxy |
---|
10 | n/a | from functools import wraps |
---|
11 | n/a | |
---|
12 | n/a | from test.support import (TESTFN, TESTFN_UNICODE, check_warnings, run_unittest, |
---|
13 | n/a | make_bad_fd, cpython_only) |
---|
14 | n/a | from collections import UserList |
---|
15 | n/a | |
---|
16 | n/a | import _io # C implementation of io |
---|
17 | n/a | import _pyio # Python implementation of io |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | class AutoFileTests: |
---|
21 | n/a | # file tests for which a test file is automatically set up |
---|
22 | n/a | |
---|
23 | n/a | def setUp(self): |
---|
24 | n/a | self.f = self.FileIO(TESTFN, 'w') |
---|
25 | n/a | |
---|
26 | n/a | def tearDown(self): |
---|
27 | n/a | if self.f: |
---|
28 | n/a | self.f.close() |
---|
29 | n/a | os.remove(TESTFN) |
---|
30 | n/a | |
---|
31 | n/a | def testWeakRefs(self): |
---|
32 | n/a | # verify weak references |
---|
33 | n/a | p = proxy(self.f) |
---|
34 | n/a | p.write(bytes(range(10))) |
---|
35 | n/a | self.assertEqual(self.f.tell(), p.tell()) |
---|
36 | n/a | self.f.close() |
---|
37 | n/a | self.f = None |
---|
38 | n/a | self.assertRaises(ReferenceError, getattr, p, 'tell') |
---|
39 | n/a | |
---|
40 | n/a | def testSeekTell(self): |
---|
41 | n/a | self.f.write(bytes(range(20))) |
---|
42 | n/a | self.assertEqual(self.f.tell(), 20) |
---|
43 | n/a | self.f.seek(0) |
---|
44 | n/a | self.assertEqual(self.f.tell(), 0) |
---|
45 | n/a | self.f.seek(10) |
---|
46 | n/a | self.assertEqual(self.f.tell(), 10) |
---|
47 | n/a | self.f.seek(5, 1) |
---|
48 | n/a | self.assertEqual(self.f.tell(), 15) |
---|
49 | n/a | self.f.seek(-5, 1) |
---|
50 | n/a | self.assertEqual(self.f.tell(), 10) |
---|
51 | n/a | self.f.seek(-5, 2) |
---|
52 | n/a | self.assertEqual(self.f.tell(), 15) |
---|
53 | n/a | |
---|
54 | n/a | def testAttributes(self): |
---|
55 | n/a | # verify expected attributes exist |
---|
56 | n/a | f = self.f |
---|
57 | n/a | |
---|
58 | n/a | self.assertEqual(f.mode, "wb") |
---|
59 | n/a | self.assertEqual(f.closed, False) |
---|
60 | n/a | |
---|
61 | n/a | # verify the attributes are readonly |
---|
62 | n/a | for attr in 'mode', 'closed': |
---|
63 | n/a | self.assertRaises((AttributeError, TypeError), |
---|
64 | n/a | setattr, f, attr, 'oops') |
---|
65 | n/a | |
---|
66 | n/a | def testBlksize(self): |
---|
67 | n/a | # test private _blksize attribute |
---|
68 | n/a | blksize = io.DEFAULT_BUFFER_SIZE |
---|
69 | n/a | # try to get preferred blksize from stat.st_blksize, if available |
---|
70 | n/a | if hasattr(os, 'fstat'): |
---|
71 | n/a | fst = os.fstat(self.f.fileno()) |
---|
72 | n/a | blksize = getattr(fst, 'st_blksize', blksize) |
---|
73 | n/a | self.assertEqual(self.f._blksize, blksize) |
---|
74 | n/a | |
---|
75 | n/a | # verify readinto |
---|
76 | n/a | def testReadintoByteArray(self): |
---|
77 | n/a | self.f.write(bytes([1, 2, 0, 255])) |
---|
78 | n/a | self.f.close() |
---|
79 | n/a | |
---|
80 | n/a | ba = bytearray(b'abcdefgh') |
---|
81 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
82 | n/a | n = f.readinto(ba) |
---|
83 | n/a | self.assertEqual(ba, b'\x01\x02\x00\xffefgh') |
---|
84 | n/a | self.assertEqual(n, 4) |
---|
85 | n/a | |
---|
86 | n/a | def _testReadintoMemoryview(self): |
---|
87 | n/a | self.f.write(bytes([1, 2, 0, 255])) |
---|
88 | n/a | self.f.close() |
---|
89 | n/a | |
---|
90 | n/a | m = memoryview(bytearray(b'abcdefgh')) |
---|
91 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
92 | n/a | n = f.readinto(m) |
---|
93 | n/a | self.assertEqual(m, b'\x01\x02\x00\xffefgh') |
---|
94 | n/a | self.assertEqual(n, 4) |
---|
95 | n/a | |
---|
96 | n/a | m = memoryview(bytearray(b'abcdefgh')).cast('H', shape=[2, 2]) |
---|
97 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
98 | n/a | n = f.readinto(m) |
---|
99 | n/a | self.assertEqual(bytes(m), b'\x01\x02\x00\xffefgh') |
---|
100 | n/a | self.assertEqual(n, 4) |
---|
101 | n/a | |
---|
102 | n/a | def _testReadintoArray(self): |
---|
103 | n/a | self.f.write(bytes([1, 2, 0, 255])) |
---|
104 | n/a | self.f.close() |
---|
105 | n/a | |
---|
106 | n/a | a = array('B', b'abcdefgh') |
---|
107 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
108 | n/a | n = f.readinto(a) |
---|
109 | n/a | self.assertEqual(a, array('B', [1, 2, 0, 255, 101, 102, 103, 104])) |
---|
110 | n/a | self.assertEqual(n, 4) |
---|
111 | n/a | |
---|
112 | n/a | a = array('b', b'abcdefgh') |
---|
113 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
114 | n/a | n = f.readinto(a) |
---|
115 | n/a | self.assertEqual(a, array('b', [1, 2, 0, -1, 101, 102, 103, 104])) |
---|
116 | n/a | self.assertEqual(n, 4) |
---|
117 | n/a | |
---|
118 | n/a | a = array('I', b'abcdefgh') |
---|
119 | n/a | with self.FileIO(TESTFN, 'r') as f: |
---|
120 | n/a | n = f.readinto(a) |
---|
121 | n/a | self.assertEqual(a, array('I', b'\x01\x02\x00\xffefgh')) |
---|
122 | n/a | self.assertEqual(n, 4) |
---|
123 | n/a | |
---|
124 | n/a | def testWritelinesList(self): |
---|
125 | n/a | l = [b'123', b'456'] |
---|
126 | n/a | self.f.writelines(l) |
---|
127 | n/a | self.f.close() |
---|
128 | n/a | self.f = self.FileIO(TESTFN, 'rb') |
---|
129 | n/a | buf = self.f.read() |
---|
130 | n/a | self.assertEqual(buf, b'123456') |
---|
131 | n/a | |
---|
132 | n/a | def testWritelinesUserList(self): |
---|
133 | n/a | l = UserList([b'123', b'456']) |
---|
134 | n/a | self.f.writelines(l) |
---|
135 | n/a | self.f.close() |
---|
136 | n/a | self.f = self.FileIO(TESTFN, 'rb') |
---|
137 | n/a | buf = self.f.read() |
---|
138 | n/a | self.assertEqual(buf, b'123456') |
---|
139 | n/a | |
---|
140 | n/a | def testWritelinesError(self): |
---|
141 | n/a | self.assertRaises(TypeError, self.f.writelines, [1, 2, 3]) |
---|
142 | n/a | self.assertRaises(TypeError, self.f.writelines, None) |
---|
143 | n/a | self.assertRaises(TypeError, self.f.writelines, "abc") |
---|
144 | n/a | |
---|
145 | n/a | def test_none_args(self): |
---|
146 | n/a | self.f.write(b"hi\nbye\nabc") |
---|
147 | n/a | self.f.close() |
---|
148 | n/a | self.f = self.FileIO(TESTFN, 'r') |
---|
149 | n/a | self.assertEqual(self.f.read(None), b"hi\nbye\nabc") |
---|
150 | n/a | self.f.seek(0) |
---|
151 | n/a | self.assertEqual(self.f.readline(None), b"hi\n") |
---|
152 | n/a | self.assertEqual(self.f.readlines(None), [b"bye\n", b"abc"]) |
---|
153 | n/a | |
---|
154 | n/a | def test_reject(self): |
---|
155 | n/a | self.assertRaises(TypeError, self.f.write, "Hello!") |
---|
156 | n/a | |
---|
157 | n/a | def testRepr(self): |
---|
158 | n/a | self.assertEqual(repr(self.f), |
---|
159 | n/a | "<%s.FileIO name=%r mode=%r closefd=True>" % |
---|
160 | n/a | (self.modulename, self.f.name, self.f.mode)) |
---|
161 | n/a | del self.f.name |
---|
162 | n/a | self.assertEqual(repr(self.f), |
---|
163 | n/a | "<%s.FileIO fd=%r mode=%r closefd=True>" % |
---|
164 | n/a | (self.modulename, self.f.fileno(), self.f.mode)) |
---|
165 | n/a | self.f.close() |
---|
166 | n/a | self.assertEqual(repr(self.f), |
---|
167 | n/a | "<%s.FileIO [closed]>" % (self.modulename,)) |
---|
168 | n/a | |
---|
169 | n/a | def testReprNoCloseFD(self): |
---|
170 | n/a | fd = os.open(TESTFN, os.O_RDONLY) |
---|
171 | n/a | try: |
---|
172 | n/a | with self.FileIO(fd, 'r', closefd=False) as f: |
---|
173 | n/a | self.assertEqual(repr(f), |
---|
174 | n/a | "<%s.FileIO name=%r mode=%r closefd=False>" % |
---|
175 | n/a | (self.modulename, f.name, f.mode)) |
---|
176 | n/a | finally: |
---|
177 | n/a | os.close(fd) |
---|
178 | n/a | |
---|
179 | n/a | def testErrors(self): |
---|
180 | n/a | f = self.f |
---|
181 | n/a | self.assertFalse(f.isatty()) |
---|
182 | n/a | self.assertFalse(f.closed) |
---|
183 | n/a | #self.assertEqual(f.name, TESTFN) |
---|
184 | n/a | self.assertRaises(ValueError, f.read, 10) # Open for reading |
---|
185 | n/a | f.close() |
---|
186 | n/a | self.assertTrue(f.closed) |
---|
187 | n/a | f = self.FileIO(TESTFN, 'r') |
---|
188 | n/a | self.assertRaises(TypeError, f.readinto, "") |
---|
189 | n/a | self.assertFalse(f.closed) |
---|
190 | n/a | f.close() |
---|
191 | n/a | self.assertTrue(f.closed) |
---|
192 | n/a | |
---|
193 | n/a | def testMethods(self): |
---|
194 | n/a | methods = ['fileno', 'isatty', 'seekable', 'readable', 'writable', |
---|
195 | n/a | 'read', 'readall', 'readline', 'readlines', |
---|
196 | n/a | 'tell', 'truncate', 'flush'] |
---|
197 | n/a | |
---|
198 | n/a | self.f.close() |
---|
199 | n/a | self.assertTrue(self.f.closed) |
---|
200 | n/a | |
---|
201 | n/a | for methodname in methods: |
---|
202 | n/a | method = getattr(self.f, methodname) |
---|
203 | n/a | # should raise on closed file |
---|
204 | n/a | self.assertRaises(ValueError, method) |
---|
205 | n/a | |
---|
206 | n/a | self.assertRaises(TypeError, self.f.readinto) |
---|
207 | n/a | self.assertRaises(ValueError, self.f.readinto, bytearray(1)) |
---|
208 | n/a | self.assertRaises(TypeError, self.f.seek) |
---|
209 | n/a | self.assertRaises(ValueError, self.f.seek, 0) |
---|
210 | n/a | self.assertRaises(TypeError, self.f.write) |
---|
211 | n/a | self.assertRaises(ValueError, self.f.write, b'') |
---|
212 | n/a | self.assertRaises(TypeError, self.f.writelines) |
---|
213 | n/a | self.assertRaises(ValueError, self.f.writelines, b'') |
---|
214 | n/a | |
---|
215 | n/a | def testOpendir(self): |
---|
216 | n/a | # Issue 3703: opening a directory should fill the errno |
---|
217 | n/a | # Windows always returns "[Errno 13]: Permission denied |
---|
218 | n/a | # Unix uses fstat and returns "[Errno 21]: Is a directory" |
---|
219 | n/a | try: |
---|
220 | n/a | self.FileIO('.', 'r') |
---|
221 | n/a | except OSError as e: |
---|
222 | n/a | self.assertNotEqual(e.errno, 0) |
---|
223 | n/a | self.assertEqual(e.filename, ".") |
---|
224 | n/a | else: |
---|
225 | n/a | self.fail("Should have raised OSError") |
---|
226 | n/a | |
---|
227 | n/a | @unittest.skipIf(os.name == 'nt', "test only works on a POSIX-like system") |
---|
228 | n/a | def testOpenDirFD(self): |
---|
229 | n/a | fd = os.open('.', os.O_RDONLY) |
---|
230 | n/a | with self.assertRaises(OSError) as cm: |
---|
231 | n/a | self.FileIO(fd, 'r') |
---|
232 | n/a | os.close(fd) |
---|
233 | n/a | self.assertEqual(cm.exception.errno, errno.EISDIR) |
---|
234 | n/a | |
---|
235 | n/a | #A set of functions testing that we get expected behaviour if someone has |
---|
236 | n/a | #manually closed the internal file descriptor. First, a decorator: |
---|
237 | n/a | def ClosedFD(func): |
---|
238 | n/a | @wraps(func) |
---|
239 | n/a | def wrapper(self): |
---|
240 | n/a | #forcibly close the fd before invoking the problem function |
---|
241 | n/a | f = self.f |
---|
242 | n/a | os.close(f.fileno()) |
---|
243 | n/a | try: |
---|
244 | n/a | func(self, f) |
---|
245 | n/a | finally: |
---|
246 | n/a | try: |
---|
247 | n/a | self.f.close() |
---|
248 | n/a | except OSError: |
---|
249 | n/a | pass |
---|
250 | n/a | return wrapper |
---|
251 | n/a | |
---|
252 | n/a | def ClosedFDRaises(func): |
---|
253 | n/a | @wraps(func) |
---|
254 | n/a | def wrapper(self): |
---|
255 | n/a | #forcibly close the fd before invoking the problem function |
---|
256 | n/a | f = self.f |
---|
257 | n/a | os.close(f.fileno()) |
---|
258 | n/a | try: |
---|
259 | n/a | func(self, f) |
---|
260 | n/a | except OSError as e: |
---|
261 | n/a | self.assertEqual(e.errno, errno.EBADF) |
---|
262 | n/a | else: |
---|
263 | n/a | self.fail("Should have raised OSError") |
---|
264 | n/a | finally: |
---|
265 | n/a | try: |
---|
266 | n/a | self.f.close() |
---|
267 | n/a | except OSError: |
---|
268 | n/a | pass |
---|
269 | n/a | return wrapper |
---|
270 | n/a | |
---|
271 | n/a | @ClosedFDRaises |
---|
272 | n/a | def testErrnoOnClose(self, f): |
---|
273 | n/a | f.close() |
---|
274 | n/a | |
---|
275 | n/a | @ClosedFDRaises |
---|
276 | n/a | def testErrnoOnClosedWrite(self, f): |
---|
277 | n/a | f.write(b'a') |
---|
278 | n/a | |
---|
279 | n/a | @ClosedFDRaises |
---|
280 | n/a | def testErrnoOnClosedSeek(self, f): |
---|
281 | n/a | f.seek(0) |
---|
282 | n/a | |
---|
283 | n/a | @ClosedFDRaises |
---|
284 | n/a | def testErrnoOnClosedTell(self, f): |
---|
285 | n/a | f.tell() |
---|
286 | n/a | |
---|
287 | n/a | @ClosedFDRaises |
---|
288 | n/a | def testErrnoOnClosedTruncate(self, f): |
---|
289 | n/a | f.truncate(0) |
---|
290 | n/a | |
---|
291 | n/a | @ClosedFD |
---|
292 | n/a | def testErrnoOnClosedSeekable(self, f): |
---|
293 | n/a | f.seekable() |
---|
294 | n/a | |
---|
295 | n/a | @ClosedFD |
---|
296 | n/a | def testErrnoOnClosedReadable(self, f): |
---|
297 | n/a | f.readable() |
---|
298 | n/a | |
---|
299 | n/a | @ClosedFD |
---|
300 | n/a | def testErrnoOnClosedWritable(self, f): |
---|
301 | n/a | f.writable() |
---|
302 | n/a | |
---|
303 | n/a | @ClosedFD |
---|
304 | n/a | def testErrnoOnClosedFileno(self, f): |
---|
305 | n/a | f.fileno() |
---|
306 | n/a | |
---|
307 | n/a | @ClosedFD |
---|
308 | n/a | def testErrnoOnClosedIsatty(self, f): |
---|
309 | n/a | self.assertEqual(f.isatty(), False) |
---|
310 | n/a | |
---|
311 | n/a | def ReopenForRead(self): |
---|
312 | n/a | try: |
---|
313 | n/a | self.f.close() |
---|
314 | n/a | except OSError: |
---|
315 | n/a | pass |
---|
316 | n/a | self.f = self.FileIO(TESTFN, 'r') |
---|
317 | n/a | os.close(self.f.fileno()) |
---|
318 | n/a | return self.f |
---|
319 | n/a | |
---|
320 | n/a | @ClosedFDRaises |
---|
321 | n/a | def testErrnoOnClosedRead(self, f): |
---|
322 | n/a | f = self.ReopenForRead() |
---|
323 | n/a | f.read(1) |
---|
324 | n/a | |
---|
325 | n/a | @ClosedFDRaises |
---|
326 | n/a | def testErrnoOnClosedReadall(self, f): |
---|
327 | n/a | f = self.ReopenForRead() |
---|
328 | n/a | f.readall() |
---|
329 | n/a | |
---|
330 | n/a | @ClosedFDRaises |
---|
331 | n/a | def testErrnoOnClosedReadinto(self, f): |
---|
332 | n/a | f = self.ReopenForRead() |
---|
333 | n/a | a = array('b', b'x'*10) |
---|
334 | n/a | f.readinto(a) |
---|
335 | n/a | |
---|
336 | n/a | class CAutoFileTests(AutoFileTests, unittest.TestCase): |
---|
337 | n/a | FileIO = _io.FileIO |
---|
338 | n/a | modulename = '_io' |
---|
339 | n/a | |
---|
340 | n/a | class PyAutoFileTests(AutoFileTests, unittest.TestCase): |
---|
341 | n/a | FileIO = _pyio.FileIO |
---|
342 | n/a | modulename = '_pyio' |
---|
343 | n/a | |
---|
344 | n/a | |
---|
345 | n/a | class OtherFileTests: |
---|
346 | n/a | |
---|
347 | n/a | def testAbles(self): |
---|
348 | n/a | try: |
---|
349 | n/a | f = self.FileIO(TESTFN, "w") |
---|
350 | n/a | self.assertEqual(f.readable(), False) |
---|
351 | n/a | self.assertEqual(f.writable(), True) |
---|
352 | n/a | self.assertEqual(f.seekable(), True) |
---|
353 | n/a | f.close() |
---|
354 | n/a | |
---|
355 | n/a | f = self.FileIO(TESTFN, "r") |
---|
356 | n/a | self.assertEqual(f.readable(), True) |
---|
357 | n/a | self.assertEqual(f.writable(), False) |
---|
358 | n/a | self.assertEqual(f.seekable(), True) |
---|
359 | n/a | f.close() |
---|
360 | n/a | |
---|
361 | n/a | f = self.FileIO(TESTFN, "a+") |
---|
362 | n/a | self.assertEqual(f.readable(), True) |
---|
363 | n/a | self.assertEqual(f.writable(), True) |
---|
364 | n/a | self.assertEqual(f.seekable(), True) |
---|
365 | n/a | self.assertEqual(f.isatty(), False) |
---|
366 | n/a | f.close() |
---|
367 | n/a | |
---|
368 | n/a | if sys.platform != "win32": |
---|
369 | n/a | try: |
---|
370 | n/a | f = self.FileIO("/dev/tty", "a") |
---|
371 | n/a | except OSError: |
---|
372 | n/a | # When run in a cron job there just aren't any |
---|
373 | n/a | # ttys, so skip the test. This also handles other |
---|
374 | n/a | # OS'es that don't support /dev/tty. |
---|
375 | n/a | pass |
---|
376 | n/a | else: |
---|
377 | n/a | self.assertEqual(f.readable(), False) |
---|
378 | n/a | self.assertEqual(f.writable(), True) |
---|
379 | n/a | if sys.platform != "darwin" and \ |
---|
380 | n/a | 'bsd' not in sys.platform and \ |
---|
381 | n/a | not sys.platform.startswith(('sunos', 'aix')): |
---|
382 | n/a | # Somehow /dev/tty appears seekable on some BSDs |
---|
383 | n/a | self.assertEqual(f.seekable(), False) |
---|
384 | n/a | self.assertEqual(f.isatty(), True) |
---|
385 | n/a | f.close() |
---|
386 | n/a | finally: |
---|
387 | n/a | os.unlink(TESTFN) |
---|
388 | n/a | |
---|
389 | n/a | def testInvalidModeStrings(self): |
---|
390 | n/a | # check invalid mode strings |
---|
391 | n/a | for mode in ("", "aU", "wU+", "rw", "rt"): |
---|
392 | n/a | try: |
---|
393 | n/a | f = self.FileIO(TESTFN, mode) |
---|
394 | n/a | except ValueError: |
---|
395 | n/a | pass |
---|
396 | n/a | else: |
---|
397 | n/a | f.close() |
---|
398 | n/a | self.fail('%r is an invalid file mode' % mode) |
---|
399 | n/a | |
---|
400 | n/a | def testModeStrings(self): |
---|
401 | n/a | # test that the mode attribute is correct for various mode strings |
---|
402 | n/a | # given as init args |
---|
403 | n/a | try: |
---|
404 | n/a | for modes in [('w', 'wb'), ('wb', 'wb'), ('wb+', 'rb+'), |
---|
405 | n/a | ('w+b', 'rb+'), ('a', 'ab'), ('ab', 'ab'), |
---|
406 | n/a | ('ab+', 'ab+'), ('a+b', 'ab+'), ('r', 'rb'), |
---|
407 | n/a | ('rb', 'rb'), ('rb+', 'rb+'), ('r+b', 'rb+')]: |
---|
408 | n/a | # read modes are last so that TESTFN will exist first |
---|
409 | n/a | with self.FileIO(TESTFN, modes[0]) as f: |
---|
410 | n/a | self.assertEqual(f.mode, modes[1]) |
---|
411 | n/a | finally: |
---|
412 | n/a | if os.path.exists(TESTFN): |
---|
413 | n/a | os.unlink(TESTFN) |
---|
414 | n/a | |
---|
415 | n/a | def testUnicodeOpen(self): |
---|
416 | n/a | # verify repr works for unicode too |
---|
417 | n/a | f = self.FileIO(str(TESTFN), "w") |
---|
418 | n/a | f.close() |
---|
419 | n/a | os.unlink(TESTFN) |
---|
420 | n/a | |
---|
421 | n/a | def testBytesOpen(self): |
---|
422 | n/a | # Opening a bytes filename |
---|
423 | n/a | try: |
---|
424 | n/a | fn = TESTFN.encode("ascii") |
---|
425 | n/a | except UnicodeEncodeError: |
---|
426 | n/a | self.skipTest('could not encode %r to ascii' % TESTFN) |
---|
427 | n/a | f = self.FileIO(fn, "w") |
---|
428 | n/a | try: |
---|
429 | n/a | f.write(b"abc") |
---|
430 | n/a | f.close() |
---|
431 | n/a | with open(TESTFN, "rb") as f: |
---|
432 | n/a | self.assertEqual(f.read(), b"abc") |
---|
433 | n/a | finally: |
---|
434 | n/a | os.unlink(TESTFN) |
---|
435 | n/a | |
---|
436 | n/a | @unittest.skipIf(sys.getfilesystemencoding() != 'utf-8', |
---|
437 | n/a | "test only works for utf-8 filesystems") |
---|
438 | n/a | def testUtf8BytesOpen(self): |
---|
439 | n/a | # Opening a UTF-8 bytes filename |
---|
440 | n/a | try: |
---|
441 | n/a | fn = TESTFN_UNICODE.encode("utf-8") |
---|
442 | n/a | except UnicodeEncodeError: |
---|
443 | n/a | self.skipTest('could not encode %r to utf-8' % TESTFN_UNICODE) |
---|
444 | n/a | f = self.FileIO(fn, "w") |
---|
445 | n/a | try: |
---|
446 | n/a | f.write(b"abc") |
---|
447 | n/a | f.close() |
---|
448 | n/a | with open(TESTFN_UNICODE, "rb") as f: |
---|
449 | n/a | self.assertEqual(f.read(), b"abc") |
---|
450 | n/a | finally: |
---|
451 | n/a | os.unlink(TESTFN_UNICODE) |
---|
452 | n/a | |
---|
453 | n/a | def testConstructorHandlesNULChars(self): |
---|
454 | n/a | fn_with_NUL = 'foo\0bar' |
---|
455 | n/a | self.assertRaises(ValueError, self.FileIO, fn_with_NUL, 'w') |
---|
456 | n/a | self.assertRaises(ValueError, self.FileIO, bytes(fn_with_NUL, 'ascii'), 'w') |
---|
457 | n/a | |
---|
458 | n/a | def testInvalidFd(self): |
---|
459 | n/a | self.assertRaises(ValueError, self.FileIO, -10) |
---|
460 | n/a | self.assertRaises(OSError, self.FileIO, make_bad_fd()) |
---|
461 | n/a | if sys.platform == 'win32': |
---|
462 | n/a | import msvcrt |
---|
463 | n/a | self.assertRaises(OSError, msvcrt.get_osfhandle, make_bad_fd()) |
---|
464 | n/a | |
---|
465 | n/a | def testBadModeArgument(self): |
---|
466 | n/a | # verify that we get a sensible error message for bad mode argument |
---|
467 | n/a | bad_mode = "qwerty" |
---|
468 | n/a | try: |
---|
469 | n/a | f = self.FileIO(TESTFN, bad_mode) |
---|
470 | n/a | except ValueError as msg: |
---|
471 | n/a | if msg.args[0] != 0: |
---|
472 | n/a | s = str(msg) |
---|
473 | n/a | if TESTFN in s or bad_mode not in s: |
---|
474 | n/a | self.fail("bad error message for invalid mode: %s" % s) |
---|
475 | n/a | # if msg.args[0] == 0, we're probably on Windows where there may be |
---|
476 | n/a | # no obvious way to discover why open() failed. |
---|
477 | n/a | else: |
---|
478 | n/a | f.close() |
---|
479 | n/a | self.fail("no error for invalid mode: %s" % bad_mode) |
---|
480 | n/a | |
---|
481 | n/a | def testTruncate(self): |
---|
482 | n/a | f = self.FileIO(TESTFN, 'w') |
---|
483 | n/a | f.write(bytes(bytearray(range(10)))) |
---|
484 | n/a | self.assertEqual(f.tell(), 10) |
---|
485 | n/a | f.truncate(5) |
---|
486 | n/a | self.assertEqual(f.tell(), 10) |
---|
487 | n/a | self.assertEqual(f.seek(0, io.SEEK_END), 5) |
---|
488 | n/a | f.truncate(15) |
---|
489 | n/a | self.assertEqual(f.tell(), 5) |
---|
490 | n/a | self.assertEqual(f.seek(0, io.SEEK_END), 15) |
---|
491 | n/a | f.close() |
---|
492 | n/a | |
---|
493 | n/a | def testTruncateOnWindows(self): |
---|
494 | n/a | def bug801631(): |
---|
495 | n/a | # SF bug <http://www.python.org/sf/801631> |
---|
496 | n/a | # "file.truncate fault on windows" |
---|
497 | n/a | f = self.FileIO(TESTFN, 'w') |
---|
498 | n/a | f.write(bytes(range(11))) |
---|
499 | n/a | f.close() |
---|
500 | n/a | |
---|
501 | n/a | f = self.FileIO(TESTFN,'r+') |
---|
502 | n/a | data = f.read(5) |
---|
503 | n/a | if data != bytes(range(5)): |
---|
504 | n/a | self.fail("Read on file opened for update failed %r" % data) |
---|
505 | n/a | if f.tell() != 5: |
---|
506 | n/a | self.fail("File pos after read wrong %d" % f.tell()) |
---|
507 | n/a | |
---|
508 | n/a | f.truncate() |
---|
509 | n/a | if f.tell() != 5: |
---|
510 | n/a | self.fail("File pos after ftruncate wrong %d" % f.tell()) |
---|
511 | n/a | |
---|
512 | n/a | f.close() |
---|
513 | n/a | size = os.path.getsize(TESTFN) |
---|
514 | n/a | if size != 5: |
---|
515 | n/a | self.fail("File size after ftruncate wrong %d" % size) |
---|
516 | n/a | |
---|
517 | n/a | try: |
---|
518 | n/a | bug801631() |
---|
519 | n/a | finally: |
---|
520 | n/a | os.unlink(TESTFN) |
---|
521 | n/a | |
---|
522 | n/a | def testAppend(self): |
---|
523 | n/a | try: |
---|
524 | n/a | f = open(TESTFN, 'wb') |
---|
525 | n/a | f.write(b'spam') |
---|
526 | n/a | f.close() |
---|
527 | n/a | f = open(TESTFN, 'ab') |
---|
528 | n/a | f.write(b'eggs') |
---|
529 | n/a | f.close() |
---|
530 | n/a | f = open(TESTFN, 'rb') |
---|
531 | n/a | d = f.read() |
---|
532 | n/a | f.close() |
---|
533 | n/a | self.assertEqual(d, b'spameggs') |
---|
534 | n/a | finally: |
---|
535 | n/a | try: |
---|
536 | n/a | os.unlink(TESTFN) |
---|
537 | n/a | except: |
---|
538 | n/a | pass |
---|
539 | n/a | |
---|
540 | n/a | def testInvalidInit(self): |
---|
541 | n/a | self.assertRaises(TypeError, self.FileIO, "1", 0, 0) |
---|
542 | n/a | |
---|
543 | n/a | def testWarnings(self): |
---|
544 | n/a | with check_warnings(quiet=True) as w: |
---|
545 | n/a | self.assertEqual(w.warnings, []) |
---|
546 | n/a | self.assertRaises(TypeError, self.FileIO, []) |
---|
547 | n/a | self.assertEqual(w.warnings, []) |
---|
548 | n/a | self.assertRaises(ValueError, self.FileIO, "/some/invalid/name", "rt") |
---|
549 | n/a | self.assertEqual(w.warnings, []) |
---|
550 | n/a | |
---|
551 | n/a | def testUnclosedFDOnException(self): |
---|
552 | n/a | class MyException(Exception): pass |
---|
553 | n/a | class MyFileIO(self.FileIO): |
---|
554 | n/a | def __setattr__(self, name, value): |
---|
555 | n/a | if name == "name": |
---|
556 | n/a | raise MyException("blocked setting name") |
---|
557 | n/a | return super(MyFileIO, self).__setattr__(name, value) |
---|
558 | n/a | fd = os.open(__file__, os.O_RDONLY) |
---|
559 | n/a | self.assertRaises(MyException, MyFileIO, fd) |
---|
560 | n/a | os.close(fd) # should not raise OSError(EBADF) |
---|
561 | n/a | |
---|
562 | n/a | class COtherFileTests(OtherFileTests, unittest.TestCase): |
---|
563 | n/a | FileIO = _io.FileIO |
---|
564 | n/a | modulename = '_io' |
---|
565 | n/a | |
---|
566 | n/a | @cpython_only |
---|
567 | n/a | def testInvalidFd_overflow(self): |
---|
568 | n/a | # Issue 15989 |
---|
569 | n/a | import _testcapi |
---|
570 | n/a | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MAX + 1) |
---|
571 | n/a | self.assertRaises(TypeError, self.FileIO, _testcapi.INT_MIN - 1) |
---|
572 | n/a | |
---|
573 | n/a | class PyOtherFileTests(OtherFileTests, unittest.TestCase): |
---|
574 | n/a | FileIO = _pyio.FileIO |
---|
575 | n/a | modulename = '_pyio' |
---|
576 | n/a | |
---|
577 | n/a | |
---|
578 | n/a | def test_main(): |
---|
579 | n/a | # Historically, these tests have been sloppy about removing TESTFN. |
---|
580 | n/a | # So get rid of it no matter what. |
---|
581 | n/a | try: |
---|
582 | n/a | run_unittest(CAutoFileTests, PyAutoFileTests, |
---|
583 | n/a | COtherFileTests, PyOtherFileTests) |
---|
584 | n/a | finally: |
---|
585 | n/a | if os.path.exists(TESTFN): |
---|
586 | n/a | os.unlink(TESTFN) |
---|
587 | n/a | |
---|
588 | n/a | if __name__ == '__main__': |
---|
589 | n/a | test_main() |
---|