annotate Lib/test/test_file.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents b586ca3a3062
children 760a710eb6c1
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):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
89 f = self.f
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
90 self.assertEquals(f.name, TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
91 self.assert_(not f.isatty())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
92 self.assert_(not f.closed)
38679
4e798caa082e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38671
diff changeset
93
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
94 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
95 f.close()
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
96 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
97
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
98 def testMethods(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
99 methods = ['fileno', 'flush', 'isatty', 'next', 'read', 'readinto',
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
100 'readline', 'readlines', 'seek', 'tell', 'truncate',
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
101 'write', 'xreadlines', '__iter__']
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
102 if sys.platform.startswith('atheos'):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
103 methods.remove('truncate')
23777
bec1b942e0bc Patch #488073: AtheOS port.
Martin v. Löwis <martin@v.loewis.de>
parents: 22736
diff changeset
104
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
105 # __exit__ should close the file
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
106 self.f.__exit__(None, None, None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
107 self.assert_(self.f.closed)
21608
fb34f55a03fa SF Patch #494867 test file methods
Neal Norwitz <nnorwitz@gmail.com>
parents: 20938
diff changeset
108
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
109 for methodname in methods:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
110 method = getattr(self.f, methodname)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
111 # should raise on closed file
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
112 self.assertRaises(ValueError, method)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
113 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
114
38695
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
115 # file is closed, __exit__ shouldn't do anything
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
116 self.assertEquals(self.f.__exit__(None, None, None), None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
117 # it must also return None if an exception was given
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
118 try:
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
119 1/0
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
120 except:
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
121 self.assertEquals(self.f.__exit__(*sys.exc_info()), None)
6e8d99718170 Test file.__exit__.
Georg Brandl <georg@python.org>
parents: 38684
diff changeset
122
50266
3a259a1e1d9c Merged revisions 67908 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents: 50111
diff changeset
123 def testReadWhenWriting(self):
3a259a1e1d9c Merged revisions 67908 via svnmerge from
Skip Montanaro <skip@pobox.com>
parents: 50111
diff changeset
124 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
125
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
126 class OtherFileTests(unittest.TestCase):
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
127
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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
141 def testModeStrings(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
142 # check invalid mode strings
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
143 for mode in ("", "aU", "wU+"):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
144 try:
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
145 f = open(TESTFN, mode)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
146 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
147 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
148 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
149 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
150 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
151
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
152 # 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
153 # 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
154 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
155 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
156 f = open(name, "rr")
51311
b586ca3a3062 issue 4804: Provide checks for the format string of strftime, and for the "mode" string of fopen on Windows. These strings are user provided from python and so we can avoid invoking the C runtime invalid parameter handler by first checking that they are valid.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50424
diff changeset
157 except (IOError, ValueError):
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
158 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
159 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
160 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
161
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
162 def testStdin(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
163 # 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
164 if sys.platform != 'osf1V5':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
165 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
166 else:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
167 print >>sys.__stdout__, (
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
168 ' 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
169 ' Test manually.')
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
170 self.assertRaises(IOError, sys.stdin.truncate)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
171
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
172 def testUnicodeOpen(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
173 # verify repr works for unicode too
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
174 f = open(unicode(TESTFN), "w")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
175 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
176 f.close()
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
177 os.unlink(TESTFN)
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
178
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
179 def testBadModeArgument(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
180 # 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
181 bad_mode = "qwerty"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
182 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
183 f = open(TESTFN, bad_mode)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
184 except ValueError, msg:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
185 if msg[0] != 0:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
186 s = str(msg)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
187 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
188 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
189 # 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
190 # no obvious way to discover why open() failed.
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
191 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
192 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
193 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
194
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
195 def testSetBufferSize(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
196 # 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
197 # misbehaviour especially with repeated close() calls
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
198 for s in (-1, 0, 1, 512):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
199 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
200 f = open(TESTFN, 'w', s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
201 f.write(str(s))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
202 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
203 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
204 f = open(TESTFN, 'r', s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
205 d = int(f.read())
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
206 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
207 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
208 except IOError, msg:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
209 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
210 self.assertEquals(d, s)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
211
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
212 def testTruncateOnWindows(self):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
213 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
214
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
215 def bug801631():
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
216 # SF bug <http://www.python.org/sf/801631>
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
217 # "file.truncate fault on windows"
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
218 f = open(TESTFN, 'wb')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
219 f.write('12345678901') # 11 bytes
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
220 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
221
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
222 f = open(TESTFN,'rb+')
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
223 data = f.read(5)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
224 if data != '12345':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
225 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
226 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
227 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
228
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
229 f.truncate()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
230 if f.tell() != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
231 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
232
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
233 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
234 size = os.path.getsize(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
235 if size != 5:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
236 self.fail("File size after ftruncate wrong %d" % size)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
237
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
238 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
239 bug801631()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
240 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
241 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
242
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
243 def testIteration(self):
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
244 # 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
245 # 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
246 # 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
247 # 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
248 # 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
249 # 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
250 # 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
251 # be updated when the implementation changes.
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
252 dataoffset = 16384
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
253 filler = "ham\n"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
254 assert not dataoffset % len(filler), \
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
255 "dataoffset must be multiple of len(filler)"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
256 nchunks = dataoffset // len(filler)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
257 testlines = [
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
258 "spam, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
259 "eggs, spam, ham and spam\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
260 "saussages, spam, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
261 "spam, ham, spam and eggs\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
262 "spam, spam, spam, spam, spam, ham, spam\n",
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
263 "wonderful spaaaaaam.\n"
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 methods = [("readline", ()), ("read", ()), ("readlines", ()),
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
266 ("readinto", (array("c", " "*100),))]
36485
99dfdcc6d613 SF patch #1397960: When mixing file-iteration and
Thomas Wouters <thomas@python.org>
parents: 36033
diff changeset
267
36493
574c25fe6028 New test code failed to close the file. This caused
Tim Peters <tim.peters@gmail.com>
parents: 36485
diff changeset
268 try:
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
269 # Prepare the testfile
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
270 bag = open(TESTFN, "w")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
271 bag.write(filler * nchunks)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
272 bag.writelines(testlines)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
273 bag.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
274 # Test for appropriate errors mixing read* and iteration
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
275 for methodname, args in methods:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
276 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
277 if f.next() != filler:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
278 self.fail, "Broken testfile"
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
279 meth = getattr(f, methodname)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
280 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
281 meth(*args)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
282 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
283 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
284 else:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
285 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
286 (methodname, args))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
287 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
288
38681
c3c52f159999 AutoFileTests.tearDown(): Removed mysterious undocumented
Tim Peters <tim.peters@gmail.com>
parents: 38679
diff changeset
289 # 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
290 # 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
291 # 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
292 # 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
293 # ("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
294 # 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
295 # between 4 and 16384 (inclusive).
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
296 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
297 for i in range(nchunks):
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
298 f.next()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
299 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
300 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
301 line = f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
302 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
303 self.fail("readline() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
304 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
305 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
306 self.fail("readline() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
307 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
308 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
309 buf = array("c", "\x00" * len(testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
310 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
311 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
312 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
313 self.fail("readinto() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
314 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
315 line = buf.tostring()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
316 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
317 self.fail("readinto() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
318 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
319
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
320 testline = testlines.pop(0)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
321 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
322 line = f.read(len(testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
323 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
324 self.fail("read() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
325 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
326 if line != testline:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
327 self.fail("read() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
328 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
329 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
330 lines = f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
331 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
332 self.fail("readlines() after next() with supposedly empty "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
333 "iteration-buffer failed anyway")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
334 if lines != testlines:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
335 self.fail("readlines() after next() with empty buffer "
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
336 "failed. Got %r, expected %r" % (line, testline))
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
337 # Reading after iteration hit EOF shouldn't hurt either
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
338 f = open(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
339 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
340 for line in f:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
341 pass
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
342 try:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
343 f.readline()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
344 f.readinto(buf)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
345 f.read()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
346 f.readlines()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
347 except ValueError:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
348 self.fail("read* failed after next() consumed file")
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
349 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
350 f.close()
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
351 finally:
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
352 os.unlink(TESTFN)
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
353
45453
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
354 class FileSubclassTests(unittest.TestCase):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
355
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
356 def testExit(self):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
357 # 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
358 class C(file):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
359 def __init__(self, *args):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
360 self.subclass_closed = False
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
361 file.__init__(self, *args)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
362 def close(self):
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
363 self.subclass_closed = True
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
364 file.close(self)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
365
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
366 with C(TESTFN, 'w') as f:
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
367 pass
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
368 self.failUnless(f.subclass_closed)
930b2802c037 #2067: file.__exit__() now calls subclasses' close() method.
Georg Brandl <georg@python.org>
parents: 38695
diff changeset
369
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
370
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
371 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
372 # 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
373 # (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
374 # 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
375
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
376 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
377 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
378 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
379 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
380 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
381 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
382 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
383 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
384
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
385 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
386 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
387 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
388 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
389 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
390 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
391 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
392 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
393 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
394 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
395
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
396 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
397 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
398
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
399 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
400 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
401 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
402 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
403 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
404 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
405
86da9298b908 Make file objects as thread safe as the underlying libc FILE* implementation.
Gregory P. Smith <greg@mad-scientist.com>
parents: 45453
diff changeset
406 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
407 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
408 # 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
409 # 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
410 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
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 _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
413 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
414 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
415 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
416 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
417 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
418 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
419 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
420 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
421 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
422 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
423 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
424 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
425 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
426 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
427 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
428 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
429 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
430 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
431 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
432 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
433 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
434 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
435
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 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
437 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
438 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
439 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
440 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
441 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
442 ))
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 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
444 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
445 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
446 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
447 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
448 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
449 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
450 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
451 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
452 # 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
453 # 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
454 #
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 # 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
456 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
457 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
458
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 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
460 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
461 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
462 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
463
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 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
465 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
466 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
467 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
468
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 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
470 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
471 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
472 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
473
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 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
475 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
476 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
477 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
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 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
480 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
481 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
482 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
483
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 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
485 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
486 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
487 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
488
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 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
490 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
491 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
492 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
493 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
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_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
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 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
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_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
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.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
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_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
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 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
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_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
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.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
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_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
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 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
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_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
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.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
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_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
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 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
528 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
529
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
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
531 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
532
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
533 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
534 # 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
535 # 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
536 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
537
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
538 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
539 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
540 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
541 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
542
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
543 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
544 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
545 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
546 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
547 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
548
50111
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
549 def test_del_stdout_before_print(self):
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
550 # 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
551 # sys.stdout was deleted.
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
552 save_stdout = sys.stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
553 del sys.stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
554 try:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
555 print
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
556 except RuntimeError as e:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
557 self.assertEquals(str(e), "lost sys.stdout")
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
558 else:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
559 self.fail("Expected RuntimeError")
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
560 finally:
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
561 sys.stdout = save_stdout
2f8c317dff17 Merged revisions 67666,67685 via svnmerge from
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49297
diff changeset
562
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
563
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
564 def test_main():
38684
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
565 # Historically, these tests have been sloppy about removing TESTFN.
debc8d783f86 Fix grammar and reflow
Neal Norwitz <nnorwitz@gmail.com>
parents: 38682
diff changeset
566 # 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
567 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
568 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
569 FileThreadingTests, StdoutTests)
38682
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
570 finally:
eca4fe132940 testUnicodeOpen(): I have no idea why, but making this
Tim Peters <tim.peters@gmail.com>
parents: 38681
diff changeset
571 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
572 os.unlink(TESTFN)
38671
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
573
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
574 if __name__ == '__main__':
365ca04d0856 Convert test_file to unittest.
Georg Brandl <georg@python.org>
parents: 38212
diff changeset
575 test_main()