Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_file.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 | 9b3c5085b4a4 7d69d04522e3 |
| rev | line source |
|---|---|
|
23777
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
22736
diff
changeset
|
1 import sys |
|
15303
0820994e2d5e
Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents:
14211
diff
changeset
|
2 import os |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
3 import unittest |
|
22718
c3d637308954
Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21608
diff
changeset
|
4 from array import array |
|
32159
2c169ca5fa69
Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents:
31864
diff
changeset
|
5 from weakref import proxy |
|
15303
0820994e2d5e
Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents:
14211
diff
changeset
|
6 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
7 import io |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
8 import _pyio as pyio |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
9 |
|
59615
b93cfafcdcad
Merged revisions 78093 via svnmerge from
Georg Brandl <georg@python.org>
parents:
56726
diff
changeset
|
10 from test.support import TESTFN, run_unittest |
|
45302
b1cb6b44b798
Move UserList to collections.
Raymond Hettinger <python@rcn.com>
parents:
44543
diff
changeset
|
11 from collections import UserList |
|
14211
a856c92b1d7b
New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
12 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
13 class AutoFileTests(unittest.TestCase): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
14 # file tests for which a test file is automatically set up |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
15 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
16 def setUp(self): |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
17 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
|
18 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
19 def tearDown(self): |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
20 if self.f: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
21 self.f.close() |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
22 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
|
23 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
24 def testWeakRefs(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
25 # verify weak references |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
26 p = proxy(self.f) |
|
42918
9220931e4741
More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents:
42182
diff
changeset
|
27 p.write(b'teststring') |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
28 self.assertEqual(self.f.tell(), p.tell()) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
29 self.f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
30 self.f = None |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
31 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
|
32 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
33 def testAttributes(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
34 # verify expected attributes exist |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
35 f = self.f |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
36 f.name # merely shouldn't blow up |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
37 f.mode # ditto |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
38 f.closed # ditto |
|
34962
047434acb5a9
Disallow opening files with modes 'aU' or 'wU' as specified by PEP
Skip Montanaro <skip@pobox.com>
parents:
34921
diff
changeset
|
39 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
40 def testReadinto(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
41 # verify readinto |
|
42918
9220931e4741
More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents:
42182
diff
changeset
|
42 self.f.write(b'12') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
43 self.f.close() |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
44 a = array('b', b'x'*10) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
45 self.f = self.open(TESTFN, 'rb') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
46 n = self.f.readinto(a) |
|
66310
9fe76c26ea9f
#9424: Replace deprecated assert* methods in the Python test suite.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
65759
diff
changeset
|
47 self.assertEqual(b'12', a.tobytes()[:n]) |
|
14211
a856c92b1d7b
New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff
changeset
|
48 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
49 def testReadinto_text(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
50 # verify readinto refuses text files |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
51 a = array('b', b'x'*10) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
52 self.f.close() |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
53 self.f = self.open(TESTFN, 'r') |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
54 if hasattr(self.f, "readinto"): |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
55 self.assertRaises(TypeError, self.f.readinto, a) |
|
15303
0820994e2d5e
Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents:
14211
diff
changeset
|
56 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
57 def testWritelinesUserList(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
58 # verify writelines with instance sequence |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
59 l = UserList([b'1', b'2']) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
60 self.f.writelines(l) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
61 self.f.close() |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
62 self.f = self.open(TESTFN, 'rb') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
63 buf = self.f.read() |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
64 self.assertEqual(buf, b'12') |
|
35959
fca7337710fd
Improve test coverage. Hope the test_file changes work the same on windows.
Neal Norwitz <nnorwitz@gmail.com>
parents:
34962
diff
changeset
|
65 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
66 def testWritelinesIntegers(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
67 # verify writelines with integers |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
68 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
|
69 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
70 def testWritelinesIntegersUserList(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
71 # verify writelines with integers in UserList |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
72 l = UserList([1,2,3]) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
73 self.assertRaises(TypeError, self.f.writelines, l) |
|
20924
21fa75790822
Fix SF buf #476953: Bad more for opening file gives bad msg.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
15957
diff
changeset
|
74 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
75 def testWritelinesNonString(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
76 # verify writelines with non-string object |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
77 class NonString: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
78 pass |
|
21608
fb34f55a03fa
SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents:
20938
diff
changeset
|
79 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
80 self.assertRaises(TypeError, self.f.writelines, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
81 [NonString(), NonString()]) |
|
22718
c3d637308954
Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21608
diff
changeset
|
82 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
83 def testErrors(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
84 f = self.f |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
85 self.assertEqual(f.name, TESTFN) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51991
diff
changeset
|
86 self.assertTrue(not f.isatty()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51991
diff
changeset
|
87 self.assertTrue(not f.closed) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
88 |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
89 if hasattr(f, "readinto"): |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
90 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
|
91 f.close() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51991
diff
changeset
|
92 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
|
93 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
94 def testMethods(self): |
|
42172
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
95 methods = [('fileno', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
96 ('flush', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
97 ('isatty', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
98 ('__next__', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
99 ('read', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
100 ('write', (b"",)), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
101 ('readline', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
102 ('readlines', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
103 ('seek', (0,)), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
104 ('tell', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
105 ('write', (b"",)), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
106 ('writelines', ([],)), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
107 ('__iter__', ()), |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
108 ] |
|
56726
14b55a4e8725
Remove AtheOS support, as per PEP 11 (which claims that all code was removed in Python 3.0).
Antoine Pitrou <solipsis@pitrou.net>
parents:
55593
diff
changeset
|
109 methods.append(('truncate', ())) |
|
23777
bec1b942e0bc
Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents:
22736
diff
changeset
|
110 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
111 # __exit__ should close the file |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
112 self.f.__exit__(None, None, None) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51991
diff
changeset
|
113 self.assertTrue(self.f.closed) |
|
21608
fb34f55a03fa
SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents:
20938
diff
changeset
|
114 |
|
42172
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
115 for methodname, args in methods: |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
116 method = getattr(self.f, methodname) |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
117 # should raise on closed file |
|
3db0083f6683
Add proper tests for closed files to various I/O operations,
Guido van Rossum <guido@python.org>
parents:
42168
diff
changeset
|
118 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
|
119 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
120 # file is closed, __exit__ shouldn't do anything |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
121 self.assertEqual(self.f.__exit__(None, None, None), None) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
122 # it must also return None if an exception was given |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
123 try: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
124 1/0 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
125 except: |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
126 self.assertEqual(self.f.__exit__(*sys.exc_info()), None) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
127 |
|
50268
3965314bb922
Merged revisions 67910 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
128 def testReadWhenWriting(self): |
|
3965314bb922
Merged revisions 67910 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents:
47201
diff
changeset
|
129 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
|
130 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
131 class CAutoFileTests(AutoFileTests): |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
132 open = io.open |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
133 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
134 class PyAutoFileTests(AutoFileTests): |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
135 open = staticmethod(pyio.open) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
136 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
137 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
138 class OtherFileTests(unittest.TestCase): |
|
36485
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36033
diff
changeset
|
139 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
140 def testModeStrings(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
141 # check invalid mode strings |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
142 for mode in ("", "aU", "wU+"): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
143 try: |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
144 f = self.open(TESTFN, mode) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
145 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
146 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
147 else: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
148 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
149 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
|
150 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
151 def testStdin(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
152 # This causes the interpreter to exit on OSF1 v5.1. |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
153 if sys.platform != 'osf1V5': |
|
42182
0e4460803b4a
Fix test_file.py. It was passing on OSX for the wrong reason
Guido van Rossum <guido@python.org>
parents:
42172
diff
changeset
|
154 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
|
155 else: |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
156 print(( |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
157 ' Skipping sys.stdin.seek(-1), it may crash the interpreter.' |
|
40775
4cdf21a0dbbc
Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents:
40587
diff
changeset
|
158 ' Test manually.'), file=sys.__stdout__) |
|
42182
0e4460803b4a
Fix test_file.py. It was passing on OSX for the wrong reason
Guido van Rossum <guido@python.org>
parents:
42172
diff
changeset
|
159 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
|
160 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
161 def testBadModeArgument(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
162 # verify that we get a sensible error message for bad mode argument |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
163 bad_mode = "qwerty" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
164 try: |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
165 f = self.open(TESTFN, bad_mode) |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
166 except ValueError as msg: |
|
41274
bfc4e8046102
When removing indexing/slicing on exceptions some places were changed
Brett Cannon <bcannon@gmail.com>
parents:
40959
diff
changeset
|
167 if msg.args[0] != 0: |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
168 s = str(msg) |
|
59682
d9ec4c8ce695
Merged revisions 79024 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59615
diff
changeset
|
169 if TESTFN in s or bad_mode not in s: |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
170 self.fail("bad error message for invalid mode: %s" % s) |
|
44543
674c9b89d124
The final occurrences of exception slicing.
Georg Brandl <georg@python.org>
parents:
43681
diff
changeset
|
171 # if msg.args[0] == 0, we're probably on Windows where there may be |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
172 # no obvious way to discover why open() failed. |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
173 else: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
174 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
175 self.fail("no error for invalid mode: %s" % bad_mode) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
176 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
177 def testSetBufferSize(self): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
178 # make sure that explicitly setting the buffer size doesn't cause |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
179 # misbehaviour especially with repeated close() calls |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
180 for s in (-1, 0, 1, 512): |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
181 try: |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
182 f = self.open(TESTFN, 'wb', s) |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
183 f.write(str(s).encode("ascii")) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
184 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
185 f.close() |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
186 f = self.open(TESTFN, 'rb', s) |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
187 d = int(f.read().decode("ascii")) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
188 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
189 f.close() |
|
40587
6b18a095c2e7
SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
190 except IOError as msg: |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
191 self.fail('error setting buffer size %d: %s' % (s, str(msg))) |
|
66340
2beb25369289
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55593
diff
changeset
|
192 self.assertEqual(d, s) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
193 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
194 def testTruncateOnWindows(self): |
|
43681
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
195 # SF bug <http://www.python.org/sf/801631> |
|
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
196 # "file.truncate fault on windows" |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
197 |
|
43681
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
198 os.unlink(TESTFN) |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
199 f = self.open(TESTFN, 'wb') |
|
43681
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
200 |
|
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
201 try: |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
202 f.write(b'12345678901') # 11 bytes |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
203 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
204 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
205 f = self.open(TESTFN,'rb+') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
206 data = f.read(5) |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
207 if data != b'12345': |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
208 self.fail("Read on file opened for update failed %r" % data) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
209 if f.tell() != 5: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
210 self.fail("File pos after read wrong %d" % f.tell()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
211 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
212 f.truncate() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
213 if f.tell() != 5: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
214 self.fail("File pos after ftruncate wrong %d" % f.tell()) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
215 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
216 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
217 size = os.path.getsize(TESTFN) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
218 if size != 5: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
219 self.fail("File size after ftruncate wrong %d" % size) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
220 finally: |
|
43681
7075d464d938
Patch # 1323 by Amaury Forgeot d'Arc.
Guido van Rossum <guido@python.org>
parents:
42918
diff
changeset
|
221 f.close() |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
222 os.unlink(TESTFN) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
223 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
224 def testIteration(self): |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
225 # Test the complex interaction when mixing file-iteration and the |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
226 # various read* methods. |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
227 dataoffset = 16384 |
|
42918
9220931e4741
More changes needed to make things work once bytes and str are truly divorced.
Guido van Rossum <guido@python.org>
parents:
42182
diff
changeset
|
228 filler = b"ham\n" |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
229 assert not dataoffset % len(filler), \ |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
230 "dataoffset must be multiple of len(filler)" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
231 nchunks = dataoffset // len(filler) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
232 testlines = [ |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
233 b"spam, spam and eggs\n", |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
234 b"eggs, spam, ham and spam\n", |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
235 b"saussages, spam, spam and eggs\n", |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
236 b"spam, ham, spam and eggs\n", |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
237 b"spam, spam, spam, spam, spam, ham, spam\n", |
|
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
238 b"wonderful spaaaaaam.\n" |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
239 ] |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
240 methods = [("readline", ()), ("read", ()), ("readlines", ()), |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
241 ("readinto", (array("b", b" "*100),))] |
|
36485
99dfdcc6d613
SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents:
36033
diff
changeset
|
242 |
|
36493
574c25fe6028
New test code failed to close the file. This caused
Tim Peters <tim.peters@gmail.com>
parents:
36485
diff
changeset
|
243 try: |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
244 # Prepare the testfile |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
245 bag = self.open(TESTFN, "wb") |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
246 bag.write(filler * nchunks) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
247 bag.writelines(testlines) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
248 bag.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
249 # Test for appropriate errors mixing read* and iteration |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
250 for methodname, args in methods: |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
251 f = self.open(TESTFN, 'rb') |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41274
diff
changeset
|
252 if next(f) != filler: |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
253 self.fail, "Broken testfile" |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
254 meth = getattr(f, methodname) |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
255 meth(*args) # This simply shouldn't fail |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
256 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
257 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
258 # Test to see if harmless (by accident) mixing of read* and |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
259 # iteration still works. This depends on the size of the internal |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
260 # iteration buffer (currently 8192,) but we can test it in a |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
261 # flexible manner. Each line in the bag o' ham is 4 bytes |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
262 # ("h", "a", "m", "\n"), so 4096 lines of that should get us |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
263 # exactly on the buffer boundary for any power-of-2 buffersize |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
264 # between 4 and 16384 (inclusive). |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
265 f = self.open(TESTFN, 'rb') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
266 for i in range(nchunks): |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
41274
diff
changeset
|
267 next(f) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
268 testline = testlines.pop(0) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
269 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
270 line = f.readline() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
271 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
272 self.fail("readline() after next() with supposedly empty " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
273 "iteration-buffer failed anyway") |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
274 if line != testline: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
275 self.fail("readline() after next() with empty buffer " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
276 "failed. Got %r, expected %r" % (line, testline)) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
277 testline = testlines.pop(0) |
|
42168
f0979a86670f
Made test_file pass. This meant adding support for read(-1) and read()
Guido van Rossum <guido@python.org>
parents:
41575
diff
changeset
|
278 buf = array("b", b"\x00" * len(testline)) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
279 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
280 f.readinto(buf) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
281 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
282 self.fail("readinto() after next() with supposedly empty " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
283 "iteration-buffer failed anyway") |
|
64355
fa8b57f987c5
Issue #8990: array.fromstring() and array.tostring() get renamed to
Antoine Pitrou <solipsis@pitrou.net>
parents:
59682
diff
changeset
|
284 line = buf.tobytes() |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
285 if line != testline: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
286 self.fail("readinto() after next() with empty buffer " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
287 "failed. Got %r, expected %r" % (line, testline)) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
288 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
289 testline = testlines.pop(0) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
290 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
291 line = f.read(len(testline)) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
292 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
293 self.fail("read() after next() with supposedly empty " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
294 "iteration-buffer failed anyway") |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
295 if line != testline: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
296 self.fail("read() after next() with empty buffer " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
297 "failed. Got %r, expected %r" % (line, testline)) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
298 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
299 lines = f.readlines() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
300 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
301 self.fail("readlines() after next() with supposedly empty " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
302 "iteration-buffer failed anyway") |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
303 if lines != testlines: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
304 self.fail("readlines() after next() with empty buffer " |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
305 "failed. Got %r, expected %r" % (line, testline)) |
|
65759
f9721a8f87bd
Fix resource warning in test_file. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64355
diff
changeset
|
306 f.close() |
|
f9721a8f87bd
Fix resource warning in test_file. Patch by Brian Brazil.
Antoine Pitrou <solipsis@pitrou.net>
parents:
64355
diff
changeset
|
307 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
308 # Reading after iteration hit EOF shouldn't hurt either |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
309 f = self.open(TESTFN, 'rb') |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
310 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
311 for line in f: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
312 pass |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
313 try: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
314 f.readline() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
315 f.readinto(buf) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
316 f.read() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
317 f.readlines() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
318 except ValueError: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
319 self.fail("read* failed after next() consumed file") |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
320 finally: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
321 f.close() |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
322 finally: |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
323 os.unlink(TESTFN) |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
324 |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
325 class COtherFileTests(OtherFileTests): |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
326 open = io.open |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
327 |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
328 class PyOtherFileTests(OtherFileTests): |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
329 open = staticmethod(pyio.open) |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
330 |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
331 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
332 def test_main(): |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
333 # Historically, these tests have been sloppy about removing TESTFN. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
334 # So get rid of it no matter what. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
335 try: |
|
51991
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
336 run_unittest(CAutoFileTests, PyAutoFileTests, |
|
3049ac17e256
merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents:
50268
diff
changeset
|
337 COtherFileTests, PyOtherFileTests) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
338 finally: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
339 if os.path.exists(TESTFN): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38673
diff
changeset
|
340 os.unlink(TESTFN) |
|
38673
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
341 |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
342 if __name__ == '__main__': |
|
a19c20c5e5c7
Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
343 test_main() |
