Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 180510d

Browse files
committed
Merged revisions 61189-61190,61192,61194-61195,61198-61199 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r61189 | brett.cannon | 2008-03-03 01:38:58 +0100 (Mon, 03 Mar 2008) | 5 lines Refactor test_logging to use unittest. This should finally solve the flakiness issues. Thanks to Antoine Pitrou for the patch. ........ r61190 | jeffrey.yasskin | 2008-03-03 02:27:03 +0100 (Mon, 03 Mar 2008) | 3 lines compile.c always emits END_FINALLY after WITH_CLEANUP, so predict that in ceval.c. This is worth about a .03-.04us speedup on a simple with block. ........ r61192 | brett.cannon | 2008-03-03 03:41:40 +0100 (Mon, 03 Mar 2008) | 4 lines Move test_largefile over to using 'with' statements for open files. Also rename the driver function to test_main() instead of main_test(). ........ r61194 | brett.cannon | 2008-03-03 04:24:48 +0100 (Mon, 03 Mar 2008) | 3 lines Add a note in the main test class' docstring that the order of execution of the tests is important. ........ r61195 | brett.cannon | 2008-03-03 04:26:43 +0100 (Mon, 03 Mar 2008) | 3 lines Add a note in the main test class' docstring that the order of execution of the tests is important. ........ r61198 | brett.cannon | 2008-03-03 05:19:29 +0100 (Mon, 03 Mar 2008) | 4 lines Add test_main() functions to various tests where it was simple to do. Done so that regrtest can execute the test_main() directly instead of relying on import side-effects. ........ r61199 | neal.norwitz | 2008-03-03 05:37:45 +0100 (Mon, 03 Mar 2008) | 1 line Only DECREF if ret != NULL ........
1 parent e7a30f7 commit 180510d

7 files changed

Lines changed: 805 additions & 2201 deletions

File tree

Lib/test/test_audioop.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def testone(name, data):
271271
if not rv:
272272
print('Test FAILED for audioop.'+name+'()')
273273

274-
def testall():
274+
def test_main():
275275
data = [gendata1(), gendata2(), gendata4()]
276276
names = dir(audioop)
277277
# We know there is a routine 'add'
@@ -281,4 +281,8 @@ def testall():
281281
routines.append(n)
282282
for n in routines:
283283
testone(n, data)
284-
testall()
284+
285+
286+
287+
if __name__ == '__main__':
288+
test_main()

Lib/test/test_dbm.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ def test_modes():
4444
d = dbm.open(filename, 'n')
4545
d.close()
4646

47-
cleanup()
48-
try:
49-
test_keys()
50-
test_modes()
51-
except:
47+
def test_main():
5248
cleanup()
53-
raise
49+
try:
50+
test_keys()
51+
test_modes()
52+
except:
53+
cleanup()
54+
raise
5455

55-
cleanup()
56+
cleanup()
57+
58+
59+
60+
if __name__ == '__main__':
61+
test_main()

Lib/test/test_largefile.py

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
class TestCase(unittest.TestCase):
2525
"""Test that each file function works as expected for a large
2626
(i.e. > 2GB, do we have to check > 4GB) files.
27+
28+
NOTE: the order of execution of the test methods is important! test_seek
29+
must run first to create the test file. File cleanup must also be handled
30+
outside the test instances because of this.
31+
2732
"""
2833

2934
def test_seek(self):
3035
if verbose:
3136
print('create large file via seek (may be sparse file) ...')
32-
f = open(TESTFN, 'wb')
33-
try:
37+
with open(TESTFN, 'wb') as f:
3438
f.write(b'z')
3539
f.seek(0)
3640
f.seek(size)
@@ -39,8 +43,6 @@ def test_seek(self):
3943
if verbose:
4044
print('check file size with os.fstat')
4145
self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
42-
finally:
43-
f.close()
4446

4547
def test_osstat(self):
4648
if verbose:
@@ -50,8 +52,7 @@ def test_osstat(self):
5052
def test_seek_read(self):
5153
if verbose:
5254
print('play around with seek() and read() with the built largefile')
53-
f = open(TESTFN, 'rb')
54-
try:
55+
with open(TESTFN, 'rb') as f:
5556
self.assertEqual(f.tell(), 0)
5657
self.assertEqual(f.read(1), b'z')
5758
self.assertEqual(f.tell(), 1)
@@ -80,14 +81,11 @@ def test_seek_read(self):
8081
f.seek(-size-1, 1)
8182
self.assertEqual(f.read(1), b'z')
8283
self.assertEqual(f.tell(), 1)
83-
finally:
84-
f.close()
8584

8685
def test_lseek(self):
8786
if verbose:
8887
print('play around with os.lseek() with the built largefile')
89-
f = open(TESTFN, 'rb')
90-
try:
88+
with open(TESTFN, 'rb') as f:
9189
self.assertEqual(os.lseek(f.fileno(), 0, 0), 0)
9290
self.assertEqual(os.lseek(f.fileno(), 42, 0), 42)
9391
self.assertEqual(os.lseek(f.fileno(), 42, 1), 84)
@@ -98,18 +96,15 @@ def test_lseek(self):
9896
self.assertEqual(os.lseek(f.fileno(), size, 0), size)
9997
# the 'a' that was written at the end of file above
10098
self.assertEqual(f.read(1), b'a')
101-
finally:
102-
f.close()
10399

104100
def test_truncate(self):
105101
if verbose:
106102
print('try truncate')
107-
f = open(TESTFN, 'r+b')
108-
# this is already decided before start running the test suite
109-
# but we do it anyway for extra protection
110-
if not hasattr(f, 'truncate'):
111-
raise TestSkipped("open().truncate() not available on this system")
112-
try:
103+
with open(TESTFN, 'r+b') as f:
104+
# this is already decided before start running the test suite
105+
# but we do it anyway for extra protection
106+
if not hasattr(f, 'truncate'):
107+
raise TestSkipped("open().truncate() not available on this system")
113108
f.seek(0, 2)
114109
# else we've lost track of the true size
115110
self.assertEqual(f.tell(), size+1)
@@ -134,10 +129,8 @@ def test_truncate(self):
134129
f.truncate(1)
135130
self.assertEqual(f.tell(), 0) # else pointer moved
136131
self.assertEqual(len(f.read()), 1) # else wasn't truncated
137-
finally:
138-
f.close()
139132

140-
def main_test():
133+
def test_main():
141134
# On Windows and Mac OSX this test comsumes large resources; It
142135
# takes a long time to build the >2GB file and takes >2GB of disk
143136
# space therefore the resource must be enabled to run this test.
@@ -168,13 +161,14 @@ def main_test():
168161
suite.addTest(TestCase('test_osstat'))
169162
suite.addTest(TestCase('test_seek_read'))
170163
suite.addTest(TestCase('test_lseek'))
171-
f = open(TESTFN, 'w')
172-
if hasattr(f, 'truncate'):
173-
suite.addTest(TestCase('test_truncate'))
174-
f.close()
175-
unlink(TESTFN)
176-
run_unittest(suite)
164+
with open(TESTFN, 'w') as f:
165+
if hasattr(f, 'truncate'):
166+
suite.addTest(TestCase('test_truncate'))
177167
unlink(TESTFN)
168+
try:
169+
run_unittest(suite)
170+
finally:
171+
unlink(TESTFN)
178172

179173
if __name__ == '__main__':
180-
main_test()
174+
test_main()

0 commit comments

Comments
 (0)