annotate Lib/test/test_file.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents eb550e839460
children e9b958e8391b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
3 import unittest
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
4 import itertools
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
5 import time
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
6 import threading
22718
c3d637308954 Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21608
diff changeset
7 from array import array
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31864
diff changeset
8 from weakref import proxy
15303
0820994e2d5e Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents: 14211
diff changeset
9
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
10 from test import test_support
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
11 from test.test_support import TESTFN, findfile, 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
12 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
13
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
14 class AutoFileTests(unittest.TestCase):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
15 # 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
16
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
17 def setUp(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
18 self.f = open(TESTFN, 'wb')
32159
2c169ca5fa69 Add weakref support to array.array and file objects.
Raymond Hettinger <python@rcn.com>
parents: 31864
diff changeset
19
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
20 def tearDown(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
21 if self.f:
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
22 self.f.close()
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
23 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
24
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
25 def testWeakRefs(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
26 # verify weak references
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
27 p = proxy(self.f)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
28 p.write('teststring')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
29 self.assertEquals(self.f.tell(), p.tell())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
30 self.f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
31 self.f = None
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
32 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
33
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
34 def testAttributes(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
35 # verify expected attributes exist
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
36 f = self.f
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
37 softspace = f.softspace
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
38 f.name # merely shouldn't blow up
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
39 f.mode # ditto
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
40 f.closed # ditto
22718
c3d637308954 Convert file.readinto() to stop using METH_OLDARGS & PyArg_Parse.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21608
diff changeset
41
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
42 # verify softspace is writable
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
43 f.softspace = softspace # merely shouldn't blow up
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
44
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
45 # verify the others aren't
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
46 for attr in 'name', 'mode', 'closed':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
47 self.assertRaises((AttributeError, TypeError), setattr, f, attr, 'oops')
14211
a856c92b1d7b New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
48
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
49 def testReadinto(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
50 # verify readinto
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
51 self.f.write('12')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
52 self.f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
53 a = array('c', 'x'*10)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
54 self.f = open(TESTFN, 'rb')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
55 n = self.f.readinto(a)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
56 self.assertEquals('12', a.tostring()[:n])
14211
a856c92b1d7b New test suite for file objects by Jeremy Hilton. This will need
Marc-André Lemburg <mal@egenix.com>
parents:
diff changeset
57
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
58 def testWritelinesUserList(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
59 # verify writelines with instance sequence
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
60 l = UserList(['1', '2'])
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
61 self.f.writelines(l)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
62 self.f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
63 self.f = open(TESTFN, 'rb')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
64 buf = self.f.read()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
65 self.assertEquals(buf, '12')
15303
0820994e2d5e Clean up the temporary file when done with it.
Fred Drake <fdrake@acm.org>
parents: 14211
diff changeset
66
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
67 def testWritelinesIntegers(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
68 # verify writelines with integers
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
69 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
70
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
71 def testWritelinesIntegersUserList(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
72 # verify writelines with integers in UserList
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
73 l = UserList([1,2,3])
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
74 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
75
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
76 def testWritelinesNonString(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
77 # verify writelines with non-string object
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
78 class NonString:
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
79 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
80
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
81 self.assertRaises(TypeError, self.f.writelines,
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
82 [NonString(), NonString()])
21608
fb34f55a03fa SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents: 20938
diff changeset
83
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
84 def testRepr(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
85 # verify repr works
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
86 self.assert_(repr(self.f).startswith("<open file '" + TESTFN))
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):
58714
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
89 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
90 self.f = open(TESTFN, 'rb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
91 f = self.f
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
92 self.assertEquals(f.name, TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
93 self.assert_(not f.isatty())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
94 self.assert_(not f.closed)
38679
4e798caa082e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38671
diff changeset
95
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
96 self.assertRaises(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
97 f.close()
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
98 self.assert_(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
99
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
100 def testMethods(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
101 methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
102 'readline', 'readlines', 'seek', 'tell', 'truncate',
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
103 'write', 'xreadlines', '__iter__']
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
104 if sys.platform.startswith('atheos'):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
105 methods.remove('truncate')
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22736
diff changeset
106
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
107 # __exit__ should close the file
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
108 self.f.__exit__(None, None, None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
109 self.assert_(self.f.closed)
21608
fb34f55a03fa SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents: 20938
diff changeset
110
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
111 for methodname in methods:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
112 method = getattr(self.f, methodname)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
113 # should raise on closed file
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
114 self.assertRaises(ValueError, method)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
115 self.assertRaises(ValueError, self.f.writelines, [])
30364
80a4b8f6ae0e SF bug 801631: file.truncate fault on windows.
Tim Peters <tim.peters@gmail.com>
parents: 28967
diff changeset
116
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
117 # file is closed, __exit__ shouldn't do anything
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
118 self.assertEquals(self.f.__exit__(None, None, None), None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
119 # it must also return None if an exception was given
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
120 try:
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
121 1/0
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
122 except:
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
123 self.assertEquals(self.f.__exit__(*sys.exc_info()), None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
124
50266
3a259a1e1d9c Merged revisions 67908 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents: 50111
diff changeset
125 def testReadWhenWriting(self):
3a259a1e1d9c Merged revisions 67908 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents: 50111
diff changeset
126 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
127
58714
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
128 def testIssue5677(self):
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
129 # Remark: Do not perform more than one test per open file,
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
130 # since that does NOT catch the readline error on Windows.
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
131 data = 'xxx'
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
132 for mode in ['w', 'wb', 'a', 'ab']:
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
133 for attr in ['read', 'readline', 'readlines']:
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
134 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
135 self.f.write(data)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
136 self.assertRaises(IOError, getattr(self.f, attr))
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
137 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
138
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
139 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
140 self.f.write(data)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
141 self.assertRaises(IOError, lambda: [line for line in self.f])
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
142 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
143
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
144 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
145 self.f.write(data)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
146 self.assertRaises(IOError, self.f.readinto, bytearray(len(data)))
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
147 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
148
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
149 for mode in ['r', 'rb', 'U', 'Ub', 'Ur', 'rU', 'rbU', 'rUb']:
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
150 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
151 self.assertRaises(IOError, self.f.write, data)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
152 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
153
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
154 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
155 self.assertRaises(IOError, self.f.writelines, [data, data])
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
156 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
157
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
158 self.f = open(TESTFN, mode)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
159 self.assertRaises(IOError, self.f.truncate)
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
160 self.f.close()
eb550e839460 Merged revisions 77989 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 50424
diff changeset
161
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
162 class OtherFileTests(unittest.TestCase):
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
163
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
164 def testOpenDir(self):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
165 this_dir = os.path.dirname(__file__)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
166 for mode in (None, "w"):
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
167 try:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
168 if mode:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
169 f = open(this_dir, mode)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
170 else:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
171 f = open(this_dir)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
172 except IOError as e:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
173 self.assertEqual(e.filename, this_dir)
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
174 else:
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
175 self.fail("opening a directory didn't raise an IOError")
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50266
diff changeset
176
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
177 def testModeStrings(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
178 # check invalid mode strings
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
179 for mode in ("", "aU", "wU+"):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
180 try:
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
181 f = open(TESTFN, mode)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
182 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
183 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
184 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
185 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
186 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
187
49297
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
188 # Some invalid modes fail on Windows, but pass on Unix
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
189 # Issue3965: avoid a crash on Windows when filename is unicode
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
190 for name in (TESTFN, unicode(TESTFN), unicode(TESTFN + '\t')):
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
191 try:
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
192 f = open(name, "rr")
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
193 except IOError:
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
194 pass
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
195 else:
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
196 f.close()
5e5b15b692e1 #3965: on Windows, open() crashes if the filename or the mode is invalid,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47864
diff changeset
197
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
198 def testStdin(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
199 # 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
200 if sys.platform != 'osf1V5':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
201 self.assertRaises(IOError, sys.stdin.seek, -1)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
202 else:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
203 print >>sys.__stdout__, (
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
204 ' Skipping sys.stdin.seek(-1), it may crash the interpreter.'
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
205 ' Test manually.')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
206 self.assertRaises(IOError, sys.stdin.truncate)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
207
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
208 def testUnicodeOpen(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
209 # verify repr works for unicode too
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
210 f = open(unicode(TESTFN), "w")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
211 self.assert_(repr(f).startswith("<open file u'" + TESTFN))
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
212 f.close()
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
213 os.unlink(TESTFN)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
214
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
215 def testBadModeArgument(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
216 # 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
217 bad_mode = "qwerty"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
218 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
219 f = open(TESTFN, bad_mode)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
220 except ValueError, msg:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
221 if msg[0] != 0:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
222 s = str(msg)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
223 if s.find(TESTFN) != -1 or s.find(bad_mode) == -1:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
224 self.fail("bad error message for invalid mode: %s" % s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
225 # if msg[0] == 0, we're probably on Windows where there may be
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
226 # no obvious way to discover why open() failed.
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
227 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
228 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
229 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
230
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
231 def testSetBufferSize(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
232 # 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
233 # misbehaviour especially with repeated close() calls
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
234 for s in (-1, 0, 1, 512):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
235 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
236 f = open(TESTFN, 'w', s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
237 f.write(str(s))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
238 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
239 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
240 f = open(TESTFN, 'r', s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
241 d = int(f.read())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
242 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
243 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
244 except IOError, msg:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
245 self.fail('error setting buffer size %d: %s' % (s, str(msg)))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
246 self.assertEquals(d, s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
247
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
248 def testTruncateOnWindows(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
249 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
250
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
251 def bug801631():
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
252 # SF bug <http://www.python.org/sf/801631>
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
253 # "file.truncate fault on windows"
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
254 f = open(TESTFN, 'wb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
255 f.write('12345678901') # 11 bytes
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
256 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
257
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
258 f = open(TESTFN,'rb+')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
259 data = f.read(5)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
260 if data != '12345':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
261 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
262 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
263 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
264
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
265 f.truncate()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
266 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
267 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
268
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
269 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
270 size = os.path.getsize(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
271 if size != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
272 self.fail("File size after ftruncate wrong %d" % size)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
273
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
274 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
275 bug801631()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
276 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
277 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
278
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
279 def testIteration(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
280 # Test the complex interaction when mixing file-iteration and the
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
281 # various read* methods. Ostensibly, the mixture could just be tested
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
282 # to work when it should work according to the Python language,
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
283 # instead of fail when it should fail according to the current CPython
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
284 # implementation. People don't always program Python the way they
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
285 # should, though, and the implemenation might change in subtle ways,
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
286 # so we explicitly test for errors, too; the test will just have to
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
287 # be updated when the implementation changes.
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
288 dataoffset = 16384
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
289 filler = "ham\n"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
290 assert not dataoffset % len(filler), \
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
291 "dataoffset must be multiple of len(filler)"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
292 nchunks = dataoffset // len(filler)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
293 testlines = [
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
294 "spam, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
295 "eggs, spam, ham and spam\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
296 "saussages, spam, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
297 "spam, ham, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
298 "spam, spam, spam, spam, spam, ham, spam\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
299 "wonderful spaaaaaam.\n"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
300 ]
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
301 methods = [("readline", ()), ("read", ()), ("readlines", ()),
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
302 ("readinto", (array("c", " "*100),))]
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
303
36493
574c25fe6028 New test code failed to close the file. This caused
Tim Peters <tim.peters@gmail.com>
parents: 36485
diff changeset
304 try:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
305 # Prepare the testfile
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
306 bag = open(TESTFN, "w")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
307 bag.write(filler * nchunks)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
308 bag.writelines(testlines)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
309 bag.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
310 # Test for appropriate errors mixing read* and iteration
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
311 for methodname, args in methods:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
312 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
313 if f.next() != filler:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
314 self.fail, "Broken testfile"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
315 meth = getattr(f, methodname)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
316 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
317 meth(*args)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
318 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
319 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
320 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
321 self.fail("%s%r after next() didn't raise ValueError" %
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
322 (methodname, args))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
323 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
324
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
325 # 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
326 # 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
327 # 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
328 # 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
329 # ("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
330 # 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
331 # between 4 and 16384 (inclusive).
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
332 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
333 for i in range(nchunks):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
334 f.next()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
335 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
336 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
337 line = f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
338 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
339 self.fail("readline() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
340 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
341 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
342 self.fail("readline() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
343 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
344 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
345 buf = array("c", "\x00" * len(testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
346 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
347 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
348 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
349 self.fail("readinto() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
350 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
351 line = buf.tostring()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
352 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
353 self.fail("readinto() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
354 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
355
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
356 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
357 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
358 line = f.read(len(testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
359 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
360 self.fail("read() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
361 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
362 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
363 self.fail("read() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
364 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
365 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
366 lines = f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
367 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
368 self.fail("readlines() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
369 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
370 if lines != testlines:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
371 self.fail("readlines() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
372 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
373 # Reading after iteration hit EOF shouldn't hurt either
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
374 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
375 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
376 for line in f:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
377 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
378 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
379 f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
380 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
381 f.read()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
382 f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
383 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
384 self.fail("read* failed after next() consumed file")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
385 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
386 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
387 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
388 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
389
45453
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
390 class FileSubclassTests(unittest.TestCase):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
391
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
392 def testExit(self):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
393 # test that exiting with context calls subclass' close
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
394 class C(file):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
395 def __init__(self, *args):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
396 self.subclass_closed = False
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
397 file.__init__(self, *args)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
398 def close(self):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
399 self.subclass_closed = True
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
400 file.close(self)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
401
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
402 with C(TESTFN, 'w') as f:
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
403 pass
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
404 self.failUnless(f.subclass_closed)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
405
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
406
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
407 class FileThreadingTests(unittest.TestCase):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
408 # These tests check the ability to call various methods of file objects
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
409 # (including close()) concurrently without crashing the Python interpreter.
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
410 # See #815646, #595601
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
411
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
412 def setUp(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
413 self.f = None
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
414 self.filename = TESTFN
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
415 with open(self.filename, "w") as f:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
416 f.write("\n".join("0123456789"))
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
417 self._count_lock = threading.Lock()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
418 self.close_count = 0
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
419 self.close_success_count = 0
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
420
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
421 def tearDown(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
422 if self.f:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
423 try:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
424 self.f.close()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
425 except (EnvironmentError, ValueError):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
426 pass
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
427 try:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
428 os.remove(self.filename)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
429 except EnvironmentError:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
430 pass
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
431
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
432 def _create_file(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
433 self.f = open(self.filename, "w+")
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
434
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
435 def _close_file(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
436 with self._count_lock:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
437 self.close_count += 1
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
438 self.f.close()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
439 with self._count_lock:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
440 self.close_success_count += 1
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
441
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
442 def _close_and_reopen_file(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
443 self._close_file()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
444 # if close raises an exception thats fine, self.f remains valid so
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
445 # we don't need to reopen.
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
446 self._create_file()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
447
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
448 def _run_workers(self, func, nb_workers, duration=0.2):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
449 with self._count_lock:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
450 self.close_count = 0
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
451 self.close_success_count = 0
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
452 self.do_continue = True
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
453 threads = []
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
454 try:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
455 for i in range(nb_workers):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
456 t = threading.Thread(target=func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
457 t.start()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
458 threads.append(t)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
459 for _ in xrange(100):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
460 time.sleep(duration/100)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
461 with self._count_lock:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
462 if self.close_count-self.close_success_count > nb_workers+1:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
463 if test_support.verbose:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
464 print 'Q',
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
465 break
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
466 time.sleep(duration)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
467 finally:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
468 self.do_continue = False
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
469 for t in threads:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
470 t.join()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
471
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
472 def _test_close_open_io(self, io_func, nb_workers=5):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
473 def worker():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
474 self._create_file()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
475 funcs = itertools.cycle((
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
476 lambda: io_func(),
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
477 lambda: self._close_and_reopen_file(),
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
478 ))
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
479 for f in funcs:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
480 if not self.do_continue:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
481 break
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
482 try:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
483 f()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
484 except (IOError, ValueError):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
485 pass
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
486 self._run_workers(worker, nb_workers)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
487 if test_support.verbose:
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
488 # Useful verbose statistics when tuning this test to take
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
489 # less time to run but still ensuring that its still useful.
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
490 #
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
491 # the percent of close calls that raised an error
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
492 percent = 100. - 100.*self.close_success_count/self.close_count
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
493 print self.close_count, ('%.4f ' % percent),
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
494
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
495 def test_close_open(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
496 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
497 pass
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
498 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
499
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
500 def test_close_open_flush(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
501 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
502 self.f.flush()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
503 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
504
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
505 def test_close_open_iter(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
506 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
507 list(iter(self.f))
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
508 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
509
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
510 def test_close_open_isatty(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
511 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
512 self.f.isatty()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
513 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
514
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
515 def test_close_open_print(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
516 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
517 print >> self.f, ''
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
518 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
519
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
520 def test_close_open_read(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
521 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
522 self.f.read(0)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
523 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
524
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
525 def test_close_open_readinto(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
526 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
527 a = array('c', 'xxxxx')
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
528 self.f.readinto(a)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
529 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
530
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
531 def test_close_open_readline(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
532 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
533 self.f.readline()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
534 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
535
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
536 def test_close_open_readlines(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
537 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
538 self.f.readlines()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
539 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
540
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
541 def test_close_open_seek(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
542 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
543 self.f.seek(0, 0)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
544 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
545
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
546 def test_close_open_tell(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
547 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
548 self.f.tell()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
549 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
550
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
551 def test_close_open_truncate(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
552 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
553 self.f.truncate()
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
554 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
555
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
556 def test_close_open_write(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
557 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
558 self.f.write('')
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
559 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
560
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
561 def test_close_open_writelines(self):
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
562 def io_func():
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
563 self.f.writelines('')
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
564 self._test_close_open_io(io_func)
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
565
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
566
47864
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
567 class StdoutTests(unittest.TestCase):
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
568
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
569 def test_move_stdout_on_write(self):
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
570 # Issue 3242: sys.stdout can be replaced (and freed) during a
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
571 # print statement; prevent a segfault in this case
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
572 save_stdout = sys.stdout
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
573
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
574 class File:
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
575 def write(self, data):
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
576 if '\n' in data:
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
577 sys.stdout = save_stdout
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
578
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
579 try:
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
580 sys.stdout = File()
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
581 print "some text"
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
582 finally:
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
583 sys.stdout = save_stdout
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
584
50111
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
585 def test_del_stdout_before_print(self):
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
586 # Issue 4597: 'print' with no argument wasn't reporting when
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
587 # sys.stdout was deleted.
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
588 save_stdout = sys.stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
589 del sys.stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
590 try:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
591 print
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
592 except RuntimeError as e:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
593 self.assertEquals(str(e), "lost sys.stdout")
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
594 else:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
595 self.fail("Expected RuntimeError")
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
596 finally:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
597 sys.stdout = save_stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
598
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
599
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
600 def test_main():
38684
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
601 # Historically, these tests have been sloppy about removing TESTFN.
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
602 # 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
603 try:
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
604 run_unittest(AutoFileTests, OtherFileTests, FileSubclassTests,
47864
7dbd6b607706 #3242: fix a crash in "print", if sys.stdout is set to a custom object,
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46329
diff changeset
605 FileThreadingTests, StdoutTests)
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
606 finally:
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
607 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
608 os.unlink(TESTFN)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
609
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
610 if __name__ == '__main__':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
611 test_main()