annotate Lib/test/test_file.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children 17542d36effd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54719
cdf252c87433 Try to restore the old test_file and test_univnewlines as new, different files
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
1 # NOTE: this file tests the new `io` library backported from Python 3.x.
cdf252c87433 Try to restore the old test_file and test_univnewlines as new, different files
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
2 # Similar tests for the builtin file object can be found in test_file2k.py.
cdf252c87433 Try to restore the old test_file and test_univnewlines as new, different files
Antoine Pitrou <solipsis@pitrou.net>
parents: 54717
diff changeset
3
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
4 from __future__ import print_function
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
5
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22736
diff changeset
6 import sys
15303
0820994e2d5e Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents: 14211
diff changeset
7 import os
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
8 import unittest
22718
c3d637308954 Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21608
diff changeset
9 from array import array
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31864
diff changeset
10 from weakref import proxy
15303
0820994e2d5e Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents: 14211
diff changeset
11
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
12 import io
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
13 import _pyio as pyio
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
14
58817
70e115c34f97 Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents: 58669
diff changeset
15 from test.test_support import TESTFN, run_unittest
14211
a856c92b1d7b New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
16 from UserList import UserList
a856c92b1d7b New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
17
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
18 class AutoFileTests(unittest.TestCase):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
19 # file tests for which a test file is automatically set up
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
20
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
21 def setUp(self):
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
22 self.f = self.open(TESTFN, 'wb')
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31864
diff changeset
23
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
24 def tearDown(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
25 if self.f:
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
26 self.f.close()
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
27 os.remove(TESTFN)
28967
9959d8fa4ddf Somewhere along the way, the softspace attr of file objects became read-
Tim Peters <tim.peters@gmail.com>
parents: 24595
diff changeset
28
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
29 def testWeakRefs(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
30 # verify weak references
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
31 p = proxy(self.f)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
32 p.write(b'teststring')
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
33 self.assertEqual(self.f.tell(), p.tell())
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
34 self.f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
35 self.f = None
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
36 self.assertRaises(ReferenceError, getattr, p, 'tell')
28967
9959d8fa4ddf Somewhere along the way, the softspace attr of file objects became read-
Tim Peters <tim.peters@gmail.com>
parents: 24595
diff changeset
37
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
38 def testAttributes(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
39 # verify expected attributes exist
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
40 f = self.f
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
41 f.name # merely shouldn't blow up
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
42 f.mode # ditto
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
43 f.closed # ditto
22718
c3d637308954 Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21608
diff changeset
44
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
45 def testReadinto(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
46 # verify readinto
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
47 self.f.write(b'12')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
48 self.f.close()
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
49 a = array('b', b'x'*10)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
50 self.f = self.open(TESTFN, 'rb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
51 n = self.f.readinto(a)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
52 self.assertEqual(b'12', a.tostring()[:n])
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
53
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
54 def testReadinto_text(self):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
55 # verify readinto refuses text files
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
56 a = array('b', b'x'*10)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
57 self.f.close()
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
58 self.f = self.open(TESTFN, 'r')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
59 if hasattr(self.f, "readinto"):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
60 self.assertRaises(TypeError, self.f.readinto, a)
14211
a856c92b1d7b New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
61
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
62 def testWritelinesUserList(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
63 # verify writelines with instance sequence
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
64 l = UserList([b'1', b'2'])
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
65 self.f.writelines(l)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
66 self.f.close()
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
67 self.f = self.open(TESTFN, 'rb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
68 buf = self.f.read()
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
69 self.assertEqual(buf, b'12')
15303
0820994e2d5e Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents: 14211
diff changeset
70
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
71 def testWritelinesIntegers(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
72 # verify writelines with integers
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
73 self.assertRaises(TypeError, self.f.writelines, [1, 2, 3])
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34962
diff changeset
74
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
75 def testWritelinesIntegersUserList(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
76 # verify writelines with integers in UserList
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
77 l = UserList([1,2,3])
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
78 self.assertRaises(TypeError, self.f.writelines, l)
35959
fca7337710fd Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents: 34962
diff changeset
79
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
80 def testWritelinesNonString(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
81 # verify writelines with non-string object
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
82 class NonString:
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
83 pass
20924
21fa75790822 Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 15957
diff changeset
84
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
85 self.assertRaises(TypeError, self.f.writelines,
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
86 [NonString(), NonString()])
21608
fb34f55a03fa SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents: 20938
diff changeset
87
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
88 def testErrors(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
89 f = self.f
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
90 self.assertEqual(f.name, TESTFN)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54719
diff changeset
91 self.assertTrue(not f.isatty())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54719
diff changeset
92 self.assertTrue(not f.closed)
38679
4e798caa082e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38671
diff changeset
93
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
94 if hasattr(f, "readinto"):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
95 self.assertRaises((IOError, TypeError), f.readinto, "")
31864
a40d79fe2a69 If a file is opened with an explicit buffer size >= 1, repeated
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 30364
diff changeset
96 f.close()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54719
diff changeset
97 self.assertTrue(f.closed)
31864
a40d79fe2a69 If a file is opened with an explicit buffer size >= 1, repeated
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 30364
diff changeset
98
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
99 def testMethods(self):
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
100 methods = [('fileno', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
101 ('flush', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
102 ('isatty', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
103 ('next', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
104 ('read', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
105 ('write', (b"",)),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
106 ('readline', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
107 ('readlines', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
108 ('seek', (0,)),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
109 ('tell', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
110 ('write', (b"",)),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
111 ('writelines', ([],)),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
112 ('__iter__', ()),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
113 ]
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
114 if not sys.platform.startswith('atheos'):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
115 methods.append(('truncate', ()))
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22736
diff changeset
116
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
117 # __exit__ should close the file
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
118 self.f.__exit__(None, None, None)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54719
diff changeset
119 self.assertTrue(self.f.closed)
21608
fb34f55a03fa SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents: 20938
diff changeset
120
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
121 for methodname, args in methods:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
122 method = getattr(self.f, methodname)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
123 # should raise on closed file
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
124 self.assertRaises(ValueError, method, *args)
30364
80a4b8f6ae0e SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents: 28967
diff changeset
125
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
126 # file is closed, __exit__ shouldn't do anything
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
127 self.assertEqual(self.f.__exit__(None, None, None), None)
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
128 # it must also return None if an exception was given
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
129 try:
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 61679
diff changeset
130 1 // 0
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
131 except:
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
132 self.assertEqual(self.f.__exit__(*sys.exc_info()), None)
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
133
50270
05e010515c4b Backport of r67908:
Martin v. Löwis <martin@v.loewis.de>
parents: 50150
diff changeset
134 def testReadWhenWriting(self):
05e010515c4b Backport of r67908:
Martin v. Löwis <martin@v.loewis.de>
parents: 50150
diff changeset
135 self.assertRaises(IOError, self.f.read)
30364
80a4b8f6ae0e SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents: 28967
diff changeset
136
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
137 class CAutoFileTests(AutoFileTests):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
138 open = io.open
58714
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
139
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
140 class PyAutoFileTests(AutoFileTests):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
141 open = staticmethod(pyio.open)
58714
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
142
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
143
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
144 class OtherFileTests(unittest.TestCase):
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
145
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
146 def testModeStrings(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
147 # check invalid mode strings
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
148 for mode in ("", "aU", "wU+"):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
149 try:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
150 f = self.open(TESTFN, mode)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
151 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
152 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
153 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
154 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
155 self.fail('%r is an invalid file mode' % mode)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
156
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
157 def testStdin(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
158 # This causes the interpreter to exit on OSF1 v5.1.
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
159 if sys.platform != 'osf1V5':
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
160 self.assertRaises((IOError, ValueError), sys.stdin.seek, -1)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
161 else:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
162 print((
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
163 ' Skipping sys.stdin.seek(-1), it may crash the interpreter.'
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
164 ' Test manually.'), file=sys.__stdout__)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
165 self.assertRaises((IOError, ValueError), sys.stdin.truncate)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
166
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
167 def testBadModeArgument(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
168 # verify that we get a sensible error message for bad mode argument
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
169 bad_mode = "qwerty"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
170 try:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
171 f = self.open(TESTFN, bad_mode)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
172 except ValueError as msg:
63656
63f38379e4ed Fix deprecation warnings in test_file.py
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63556
diff changeset
173 if msg.args[0] != 0:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
174 s = str(msg)
59951
e9b958e8391b Merged revisions 73824,78887,78895,78900,79024 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58714
diff changeset
175 if TESTFN in s or bad_mode not in s:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
176 self.fail("bad error message for invalid mode: %s" % s)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
177 # if msg.args[0] == 0, we're probably on Windows where there may be
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
178 # no obvious way to discover why open() failed.
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
179 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
180 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
181 self.fail("no error for invalid mode: %s" % bad_mode)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
182
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
183 def testSetBufferSize(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
184 # make sure that explicitly setting the buffer size doesn't cause
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
185 # misbehaviour especially with repeated close() calls
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
186 for s in (-1, 0, 1, 512):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
187 try:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
188 f = self.open(TESTFN, 'wb', s)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
189 f.write(str(s).encode("ascii"))
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
190 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
191 f.close()
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
192 f = self.open(TESTFN, 'rb', s)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
193 d = int(f.read().decode("ascii"))
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
194 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
195 f.close()
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
196 except IOError as msg:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
197 self.fail('error setting buffer size %d: %s' % (s, str(msg)))
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59664
diff changeset
198 self.assertEqual(d, s)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
199
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
200 def testTruncateOnWindows(self):
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
201 # SF bug <http://www.python.org/sf/801631>
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
202 # "file.truncate fault on windows"
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
203
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
204 os.unlink(TESTFN)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
205 f = self.open(TESTFN, 'wb')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
206
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
207 try:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
208 f.write(b'12345678901') # 11 bytes
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
209 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
210
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
211 f = self.open(TESTFN,'rb+')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
212 data = f.read(5)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
213 if data != b'12345':
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
214 self.fail("Read on file opened for update failed %r" % data)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
215 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
216 self.fail("File pos after read wrong %d" % f.tell())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
217
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
218 f.truncate()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
219 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
220 self.fail("File pos after ftruncate wrong %d" % f.tell())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
221
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
222 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
223 size = os.path.getsize(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
224 if size != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
225 self.fail("File size after ftruncate wrong %d" % size)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
226 finally:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
227 f.close()
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
228 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
229
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
230 def testIteration(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
231 # Test the complex interaction when mixing file-iteration and the
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
232 # various read* methods.
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
233 dataoffset = 16384
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
234 filler = b"ham\n"
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
235 assert not dataoffset % len(filler), \
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
236 "dataoffset must be multiple of len(filler)"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
237 nchunks = dataoffset // len(filler)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
238 testlines = [
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
239 b"spam, spam and eggs\n",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
240 b"eggs, spam, ham and spam\n",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
241 b"saussages, spam, spam and eggs\n",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
242 b"spam, ham, spam and eggs\n",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
243 b"spam, spam, spam, spam, spam, ham, spam\n",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
244 b"wonderful spaaaaaam.\n"
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
245 ]
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
246 methods = [("readline", ()), ("read", ()), ("readlines", ()),
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
247 ("readinto", (array("b", b" "*100),))]
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
248
36493
574c25fe6028 New test code failed to close the file. This caused
Tim Peters <tim.peters@gmail.com>
parents: 36485
diff changeset
249 try:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
250 # Prepare the testfile
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
251 bag = self.open(TESTFN, "wb")
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
252 bag.write(filler * nchunks)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
253 bag.writelines(testlines)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
254 bag.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
255 # Test for appropriate errors mixing read* and iteration
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
256 for methodname, args in methods:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
257 f = self.open(TESTFN, 'rb')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
258 if next(f) != filler:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
259 self.fail, "Broken testfile"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
260 meth = getattr(f, methodname)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
261 meth(*args) # This simply shouldn't fail
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
262 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
263
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
264 # Test to see if harmless (by accident) mixing of read* and
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
265 # iteration still works. This depends on the size of the internal
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
266 # iteration buffer (currently 8192,) but we can test it in a
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
267 # flexible manner. Each line in the bag o' ham is 4 bytes
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
268 # ("h", "a", "m", "\n"), so 4096 lines of that should get us
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
269 # exactly on the buffer boundary for any power-of-2 buffersize
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
270 # between 4 and 16384 (inclusive).
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
271 f = self.open(TESTFN, 'rb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
272 for i in range(nchunks):
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
273 next(f)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
274 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
275 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
276 line = f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
277 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
278 self.fail("readline() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
279 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
280 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
281 self.fail("readline() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
282 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
283 testline = testlines.pop(0)
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
284 buf = array("b", b"\x00" * len(testline))
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
285 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
286 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
287 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
288 self.fail("readinto() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
289 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
290 line = buf.tostring()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
291 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
292 self.fail("readinto() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
293 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
294
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
295 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
296 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
297 line = f.read(len(testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
298 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
299 self.fail("read() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
300 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
301 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
302 self.fail("read() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
303 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
304 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
305 lines = f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
306 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
307 self.fail("readlines() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
308 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
309 if lines != testlines:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
310 self.fail("readlines() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
311 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
312 # Reading after iteration hit EOF shouldn't hurt either
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
313 f = self.open(TESTFN, 'rb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
314 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
315 for line in f:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
316 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
317 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
318 f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
319 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
320 f.read()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
321 f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
322 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
323 self.fail("read* failed after next() consumed file")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
324 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
325 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
326 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
327 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
328
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
329 class COtherFileTests(OtherFileTests):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
330 open = io.open
46329
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
331
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
332 class PyOtherFileTests(OtherFileTests):
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
333 open = staticmethod(pyio.open)
50102
a6ddd94983ea Backport issue 4597 to python 2.5.3: Fixed several opcodes that weren't always
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 47867
diff changeset
334
47867
60fd0886811c #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 38695
diff changeset
335
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
336 def test_main():
38684
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
337 # Historically, these tests have been sloppy about removing TESTFN.
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
338 # So get rid of it no matter what.
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
339 try:
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
340 run_unittest(CAutoFileTests, PyAutoFileTests,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents: 51311
diff changeset
341 COtherFileTests, PyOtherFileTests)
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
342 finally:
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
343 if os.path.exists(TESTFN):
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
344 os.unlink(TESTFN)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
345
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
346 if __name__ == '__main__':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
347 test_main()