Commit 063606a0 authored by Jason Tishler's avatar Jason Tishler

test_largefile can leave its temp file open if one of many tests fail. On

platforms (e.g., Cygwin) that are "particular" about open files, this will
cause other regression tests that use the same temp file to fail:

    $ ./python.exe -E -tt Lib/test/regrtest.py -l
    test_largefile test_mmap test_mutants
    test_largefile
    test test_largefile failed -- got -1794967295L, but expected 2500000001L
    test_mmap
    test test_mmap crashed -- exceptions.IOError: [Errno 13] Permission denied: '@test'
    test_mutants
    test test_mutants crashed -- exceptions.IOError: [Errno 13] Permission denied: '@test'

This patch solves the problem by adding missing "try/finally" blocks. Note
that the "large" size of this patch is due to many white space changes --
otherwise, the patch is small.

I tested this patch under Red Hat Linux 8.0 too.
parent 9e386419
...@@ -69,15 +69,17 @@ def expect(got_this, expect_this): ...@@ -69,15 +69,17 @@ def expect(got_this, expect_this):
if test_support.verbose: if test_support.verbose:
print 'create large file via seek (may be sparse file) ...' print 'create large file via seek (may be sparse file) ...'
f = open(name, 'wb') f = open(name, 'wb')
f.write('z') try:
f.seek(0) f.write('z')
f.seek(size) f.seek(0)
f.write('a') f.seek(size)
f.flush() f.write('a')
if test_support.verbose: f.flush()
print 'check file size with os.fstat' if test_support.verbose:
expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1) print 'check file size with os.fstat'
f.close() expect(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
finally:
f.close()
if test_support.verbose: if test_support.verbose:
print 'check file size with os.stat' print 'check file size with os.stat'
expect(os.stat(name)[stat.ST_SIZE], size+1) expect(os.stat(name)[stat.ST_SIZE], size+1)
...@@ -85,78 +87,84 @@ expect(os.stat(name)[stat.ST_SIZE], size+1) ...@@ -85,78 +87,84 @@ expect(os.stat(name)[stat.ST_SIZE], size+1)
if test_support.verbose: if test_support.verbose:
print 'play around with seek() and read() with the built largefile' print 'play around with seek() and read() with the built largefile'
f = open(name, 'rb') f = open(name, 'rb')
expect(f.tell(), 0) try:
expect(f.read(1), 'z') expect(f.tell(), 0)
expect(f.tell(), 1) expect(f.read(1), 'z')
f.seek(0) expect(f.tell(), 1)
expect(f.tell(), 0) f.seek(0)
f.seek(0, 0) expect(f.tell(), 0)
expect(f.tell(), 0) f.seek(0, 0)
f.seek(42) expect(f.tell(), 0)
expect(f.tell(), 42) f.seek(42)
f.seek(42, 0) expect(f.tell(), 42)
expect(f.tell(), 42) f.seek(42, 0)
f.seek(42, 1) expect(f.tell(), 42)
expect(f.tell(), 84) f.seek(42, 1)
f.seek(0, 1) expect(f.tell(), 84)
expect(f.tell(), 84) f.seek(0, 1)
f.seek(0, 2) # seek from the end expect(f.tell(), 84)
expect(f.tell(), size + 1 + 0) f.seek(0, 2) # seek from the end
f.seek(-10, 2) expect(f.tell(), size + 1 + 0)
expect(f.tell(), size + 1 - 10) f.seek(-10, 2)
f.seek(-size-1, 2) expect(f.tell(), size + 1 - 10)
expect(f.tell(), 0) f.seek(-size-1, 2)
f.seek(size) expect(f.tell(), 0)
expect(f.tell(), size) f.seek(size)
expect(f.read(1), 'a') # the 'a' that was written at the end of the file above expect(f.tell(), size)
f.seek(-size-1, 1) expect(f.read(1), 'a') # the 'a' that was written at the end of file above
expect(f.read(1), 'z') f.seek(-size-1, 1)
expect(f.tell(), 1) expect(f.read(1), 'z')
f.close() expect(f.tell(), 1)
finally:
f.close()
if test_support.verbose: if test_support.verbose:
print 'play around with os.lseek() with the built largefile' print 'play around with os.lseek() with the built largefile'
f = open(name, 'rb') f = open(name, 'rb')
expect(os.lseek(f.fileno(), 0, 0), 0) try:
expect(os.lseek(f.fileno(), 42, 0), 42) expect(os.lseek(f.fileno(), 0, 0), 0)
expect(os.lseek(f.fileno(), 42, 1), 84) expect(os.lseek(f.fileno(), 42, 0), 42)
expect(os.lseek(f.fileno(), 0, 1), 84) expect(os.lseek(f.fileno(), 42, 1), 84)
expect(os.lseek(f.fileno(), 0, 2), size+1+0) expect(os.lseek(f.fileno(), 0, 1), 84)
expect(os.lseek(f.fileno(), -10, 2), size+1-10) expect(os.lseek(f.fileno(), 0, 2), size+1+0)
expect(os.lseek(f.fileno(), -size-1, 2), 0) expect(os.lseek(f.fileno(), -10, 2), size+1-10)
expect(os.lseek(f.fileno(), size, 0), size) expect(os.lseek(f.fileno(), -size-1, 2), 0)
expect(f.read(1), 'a') # the 'a' that was written at the end of the file above expect(os.lseek(f.fileno(), size, 0), size)
f.close() expect(f.read(1), 'a') # the 'a' that was written at the end of file above
finally:
f.close()
if hasattr(f, 'truncate'): if hasattr(f, 'truncate'):
if test_support.verbose: if test_support.verbose:
print 'try truncate' print 'try truncate'
f = open(name, 'r+b') f = open(name, 'r+b')
f.seek(0, 2) try:
expect(f.tell(), size+1) # else we've lost track of the true size f.seek(0, 2)
# Cut it back via seek + truncate with no argument. expect(f.tell(), size+1) # else we've lost track of the true size
newsize = size - 10 # Cut it back via seek + truncate with no argument.
f.seek(newsize) newsize = size - 10
f.truncate() f.seek(newsize)
expect(f.tell(), newsize) # else pointer moved f.truncate()
f.seek(0, 2) expect(f.tell(), newsize) # else pointer moved
expect(f.tell(), newsize) # else wasn't truncated f.seek(0, 2)
# Ensure that truncate(smaller than true size) shrinks the file. expect(f.tell(), newsize) # else wasn't truncated
newsize -= 1 # Ensure that truncate(smaller than true size) shrinks the file.
f.seek(42) newsize -= 1
f.truncate(newsize) f.seek(42)
expect(f.tell(), 42) # else pointer moved f.truncate(newsize)
f.seek(0, 2) expect(f.tell(), 42) # else pointer moved
expect(f.tell(), newsize) # else wasn't truncated f.seek(0, 2)
expect(f.tell(), newsize) # else wasn't truncated
# XXX truncate(larger than true size) is ill-defined across platforms
# XXX truncate(larger than true size) is ill-defined across platforms
# cut it waaaaay back
f.seek(0) # cut it waaaaay back
f.truncate(1) f.seek(0)
expect(f.tell(), 0) # else pointer moved f.truncate(1)
expect(len(f.read()), 1) # else wasn't truncated expect(f.tell(), 0) # else pointer moved
expect(len(f.read()), 1) # else wasn't truncated
f.close()
finally:
f.close()
os.unlink(name) os.unlink(name)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment