Fix #8886. Use context managers throughout the test.

This commit is contained in:
Brian Curtin 2010-11-18 02:15:28 +00:00
parent a47bbf5a4b
commit 8fb9b868bd
2 changed files with 112 additions and 104 deletions

View File

@ -112,7 +112,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp: with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = [] zipdata1 = []
zipopen1 = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen1:
while True: while True:
read_data = zipopen1.read(256) read_data = zipopen1.read(256)
if not read_data: if not read_data:
@ -120,7 +120,7 @@ class TestsWithSourceFile(unittest.TestCase):
zipdata1.append(read_data) zipdata1.append(read_data)
zipdata2 = [] zipdata2 = []
zipopen2 = zipfp.open("another.name") with zipfp.open("another.name") as zipopen2:
while True: while True:
read_data = zipopen2.read(256) read_data = zipopen2.read(256)
if not read_data: if not read_data:
@ -146,7 +146,8 @@ class TestsWithSourceFile(unittest.TestCase):
infos = zipfp.infolist() infos = zipfp.infolist()
data = b"" data = b""
for info in infos: for info in infos:
data += zipfp.open(info).read() with zipfp.open(info) as zipopen:
data += zipopen.read()
self.assertTrue(data == b"foobar" or data == b"barfoo") self.assertTrue(data == b"foobar" or data == b"barfoo")
data = b"" data = b""
for info in infos: for info in infos:
@ -159,7 +160,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp: with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = [] zipdata1 = []
zipopen1 = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen1:
while True: while True:
read_data = zipopen1.read(randint(1, 1024)) read_data = zipopen1.read(randint(1, 1024))
if not read_data: if not read_data:
@ -184,7 +185,7 @@ class TestsWithSourceFile(unittest.TestCase):
data2 = b'' data2 = b''
zipfp = zipfile.ZipFile(f, 'r') zipfp = zipfile.ZipFile(f, 'r')
zipopen = zipfp.open(TESTFN, 'rU') with zipfp.open(TESTFN, 'rU') as zipopen:
for line in zipopen: for line in zipopen:
data2 += line data2 += line
zipfp.close() zipfp.close()
@ -196,8 +197,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
zipfp = zipfile.ZipFile(f, "r") zipfp = zipfile.ZipFile(f, "r")
zipopen = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen:
data = b'' data = b''
while True: while True:
read = zipopen.readline() read = zipopen.readline()
@ -220,7 +220,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp: with zipfile.ZipFile(f, "r") as zipfp:
zipopen = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen:
for line in self.line_gen: for line in self.line_gen:
linedata = zipopen.readline() linedata = zipopen.readline()
self.assertEqual(linedata, line + '\n') self.assertEqual(linedata, line + '\n')
@ -232,7 +232,8 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp: with zipfile.ZipFile(f, "r") as zipfp:
ziplines = zipfp.open(TESTFN).readlines() with zipfp.open(TESTFN) as zipopen:
ziplines = zipopen.readlines()
for line, zipline in zip(self.line_gen, ziplines): for line, zipline in zip(self.line_gen, ziplines):
self.assertEqual(zipline, line + '\n') self.assertEqual(zipline, line + '\n')
if not isinstance(f, str): if not isinstance(f, str):
@ -243,7 +244,8 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp: with zipfile.ZipFile(f, "r") as zipfp:
for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): with zipfp.open(TESTFN) as zipopen:
for line, zipline in zip(self.line_gen, zipopen):
self.assertEqual(zipline, line + '\n') self.assertEqual(zipline, line + '\n')
if not isinstance(f, str): if not isinstance(f, str):
f.close() f.close()
@ -311,7 +313,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Get an open object for strfile # Get an open object for strfile
with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
openobj = zipfp.open("strfile") with zipfp.open("strfile") as openobj:
self.assertEqual(openobj.read(1), b'1') self.assertEqual(openobj.read(1), b'1')
self.assertEqual(openobj.read(1), b'2') self.assertEqual(openobj.read(1), b'2')
@ -352,7 +354,8 @@ class TestsWithSourceFile(unittest.TestCase):
produces the expected result.""" produces the expected result."""
with zipfile.ZipFile(TESTFN2, "w") as zipfp: with zipfile.ZipFile(TESTFN2, "w") as zipfp:
zipfp.write(TESTFN) zipfp.write(TESTFN)
self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read()) with open(TESTFN, "rb") as f:
self.assertEqual(zipfp.read(TESTFN), f.read())
@skipUnless(zlib, "requires zlib") @skipUnless(zlib, "requires zlib")
def test_per_file_compression(self): def test_per_file_compression(self):
@ -394,7 +397,8 @@ class TestsWithSourceFile(unittest.TestCase):
self.assertEqual(writtenfile, correctfile) self.assertEqual(writtenfile, correctfile)
# make sure correct data is in correct file # make sure correct data is in correct file
self.assertEqual(fdata.encode(), open(writtenfile, "rb").read()) with open(writtenfile, "rb") as f:
self.assertEqual(fdata.encode(), f.read())
os.remove(writtenfile) os.remove(writtenfile)
@ -414,7 +418,8 @@ class TestsWithSourceFile(unittest.TestCase):
else: else:
outfile = os.path.join(os.getcwd(), fpath) outfile = os.path.join(os.getcwd(), fpath)
self.assertEqual(fdata.encode(), open(outfile, "rb").read()) with open(outfile, "rb") as f:
self.assertEqual(fdata.encode(), f.read())
os.remove(outfile) os.remove(outfile)
@ -674,7 +679,8 @@ class PyZipFileTests(unittest.TestCase):
def test_write_non_pyfile(self): def test_write_non_pyfile(self):
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
open(TESTFN, 'w').write('most definitely not a python file') with open(TESTFN, 'w') as f:
f.write('most definitely not a python file')
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
os.remove(TESTFN) os.remove(TESTFN)
@ -825,7 +831,8 @@ class OtherTests(unittest.TestCase):
self.assertRaises(RuntimeError, zipf.open, "foo.txt") self.assertRaises(RuntimeError, zipf.open, "foo.txt")
self.assertRaises(RuntimeError, zipf.testzip) self.assertRaises(RuntimeError, zipf.testzip)
self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
open(TESTFN, 'w').write('zipfile test data') with open(TESTFN, 'w') as f:
f.write('zipfile test data')
self.assertRaises(RuntimeError, zipf.write, TESTFN) self.assertRaises(RuntimeError, zipf.write, TESTFN)
def test_bad_constructor_mode(self): def test_bad_constructor_mode(self):
@ -848,7 +855,7 @@ class OtherTests(unittest.TestCase):
with zipfile.ZipFile(TESTFN, mode="w") as zipf: with zipfile.ZipFile(TESTFN, mode="w") as zipf:
zipf.writestr("foo.txt", "O, for a Muse of Fire!") zipf.writestr("foo.txt", "O, for a Muse of Fire!")
# read the data to make sure the file is there # read the data to make sure the file is there
f = zipf.open("foo.txt") with zipf.open("foo.txt") as f:
for i in range(FIXEDTEST_SIZE): for i in range(FIXEDTEST_SIZE):
self.assertEqual(f.read(0), b'') self.assertEqual(f.read(0), b'')
@ -1115,7 +1122,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp: with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = [] zipdata1 = []
zipopen1 = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen1:
while True: while True:
read_data = zipopen1.read(256) read_data = zipopen1.read(256)
if not read_data: if not read_data:
@ -1123,7 +1130,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
zipdata1.append(read_data) zipdata1.append(read_data)
zipdata2 = [] zipdata2 = []
zipopen2 = zipfp.open("another.name") with zipfp.open("another.name") as zipopen2:
while True: while True:
read_data = zipopen2.read(256) read_data = zipopen2.read(256)
if not read_data: if not read_data:
@ -1155,7 +1162,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive # Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp: with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = [] zipdata1 = []
zipopen1 = zipfp.open(TESTFN) with zipfp.open(TESTFN) as zipopen1:
while True: while True:
read_data = zipopen1.read(randint(1, 1024)) read_data = zipopen1.read(randint(1, 1024))
if not read_data: if not read_data:
@ -1190,8 +1197,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects) # Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other. # multiple open() calls can be made without interfering with each other.
with zipfile.ZipFile(TESTFN2, mode="r") as zipf: with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
zopen1 = zipf.open('ones') with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
zopen2 = zipf.open('ones')
data1 = zopen1.read(500) data1 = zopen1.read(500)
data2 = zopen2.read(500) data2 = zopen2.read(500)
data1 += zopen1.read(500) data1 += zopen1.read(500)
@ -1202,8 +1208,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects) # Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other. # multiple open() calls can be made without interfering with each other.
with zipfile.ZipFile(TESTFN2, mode="r") as zipf: with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
zopen1 = zipf.open('ones') with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
zopen2 = zipf.open('twos')
data1 = zopen1.read(500) data1 = zopen1.read(500)
data2 = zopen2.read(500) data2 = zopen2.read(500)
data1 += zopen1.read(500) data1 += zopen1.read(500)
@ -1215,9 +1220,8 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects) # Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other. # multiple open() calls can be made without interfering with each other.
with zipfile.ZipFile(TESTFN2, mode="r") as zipf: with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
zopen1 = zipf.open('ones') with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
data1 = zopen1.read(500) data1 = zopen1.read(500)
zopen2 = zipf.open('twos')
data2 = zopen2.read(500) data2 = zopen2.read(500)
data1 += zopen1.read(500) data1 += zopen1.read(500)
data2 += zopen2.read(500) data2 += zopen2.read(500)
@ -1294,9 +1298,9 @@ class UniversalNewlineTests(unittest.TestCase):
self.make_test_archive(f, compression) self.make_test_archive(f, compression)
# Read the ZIP archive # Read the ZIP archive
zipfp = zipfile.ZipFile(f, "r") with zipfile.ZipFile(f, "r") as zipfp:
for sep, fn in self.arcfiles.items(): for sep, fn in self.arcfiles.items():
zipopen = zipfp.open(fn, "rU") with zipfp.open(fn, "rU") as zipopen:
data = b'' data = b''
while True: while True:
read = zipopen.readline() read = zipopen.readline()
@ -1309,10 +1313,8 @@ class UniversalNewlineTests(unittest.TestCase):
break break
data += read data += read
zipopen.close()
self.assertEqual(data, self.arcdata['\n']) self.assertEqual(data, self.arcdata['\n'])
zipfp.close()
if not isinstance(f, str): if not isinstance(f, str):
f.close() f.close()

View File

@ -31,6 +31,12 @@ C-API
- Loosen PyArg_ValidateKeywordArguments to allow dict subclasses. - Loosen PyArg_ValidateKeywordArguments to allow dict subclasses.
Tests
-----
- Issue #8886: Use context managers throughout test_zipfile. Patch by
Eric Carstensen.
What's New in Python 3.2 Alpha 4? What's New in Python 3.2 Alpha 4?
================================= =================================