Fix #8886. Use context managers throughout zipfile tests.

This was fixed in py3k SVN. Consider this a backport.
This commit is contained in:
Brian Curtin 2011-04-19 21:15:55 -05:00
parent c98556e72e
commit 0d65433568
1 changed files with 108 additions and 110 deletions

View File

@ -115,7 +115,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
zipopen1 = zipfp.open(TESTFN)
with zipfp.open(TESTFN) as zipopen1:
while True:
read_data = zipopen1.read(256)
if not read_data:
@ -123,7 +123,7 @@ class TestsWithSourceFile(unittest.TestCase):
zipdata1.append(read_data)
zipdata2 = []
zipopen2 = zipfp.open("another.name")
with zipfp.open("another.name") as zipopen2:
while True:
read_data = zipopen2.read(256)
if not read_data:
@ -147,7 +147,8 @@ class TestsWithSourceFile(unittest.TestCase):
infos = zipfp.infolist()
data = ""
for info in infos:
data += zipfp.open(info).read()
with zipfp.open(info) as f:
data += f.read()
self.assertTrue(data == "foobar" or data == "barfoo")
data = ""
for info in infos:
@ -160,7 +161,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
zipopen1 = zipfp.open(TESTFN)
with zipfp.open(TESTFN) as zipopen1:
while True:
read_data = zipopen1.read(randint(1, 1024))
if not read_data:
@ -177,16 +178,14 @@ class TestsWithSourceFile(unittest.TestCase):
f = StringIO()
data = 'a\r\n' * 16 * 1024
zipfp = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp:
zipfp.writestr(TESTFN, data)
zipfp.close()
data2 = ''
zipfp = zipfile.ZipFile(f, 'r')
zipopen = zipfp.open(TESTFN, 'rU')
with zipfile.ZipFile(f, 'r') as zipfp:
with zipfp.open(TESTFN, 'rU') as zipopen:
for line in zipopen:
data2 += line
zipfp.close()
self.assertEqual(data, data2.replace('\n', '\r\n'))
@ -194,9 +193,8 @@ class TestsWithSourceFile(unittest.TestCase):
self.make_test_archive(f, compression)
# Read the ZIP archive
zipfp = zipfile.ZipFile(f, "r")
zipopen = zipfp.open(TESTFN)
with zipfile.ZipFile(f, "r") as zipfp:
with zipfp.open(TESTFN) as zipopen:
data = ''
while True:
read = zipopen.readline()
@ -210,14 +208,13 @@ class TestsWithSourceFile(unittest.TestCase):
data += read
self.assertEqual(data, self.data)
zipfp.close()
def zip_readline_test(self, f, compression):
self.make_test_archive(f, compression)
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
zipopen = zipfp.open(TESTFN)
with zipfp.open(TESTFN) as zipopen:
for line in self.line_gen:
linedata = zipopen.readline()
self.assertEqual(linedata, line + '\n')
@ -227,7 +224,8 @@ class TestsWithSourceFile(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
ziplines = zipfp.open(TESTFN).readlines()
with zipfp.open(TESTFN) as zo:
ziplines = zo.readlines()
for line, zipline in zip(self.line_gen, ziplines):
self.assertEqual(zipline, line + '\n')
@ -301,7 +299,7 @@ class TestsWithSourceFile(unittest.TestCase):
# Get an open object for strfile
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), '1')
self.assertEqual(openobj.read(1), '2')
@ -775,8 +773,8 @@ class OtherTests(unittest.TestCase):
self.assertRaises(IOError, zipfile.ZipFile, TESTFN)
def test_empty_file_raises_BadZipFile(self):
f = open(TESTFN, 'w')
f.close()
with open(TESTFN, 'w') as f:
pass
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
with open(TESTFN, 'w') as fp:
@ -820,7 +818,7 @@ class OtherTests(unittest.TestCase):
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
# 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 xrange(FIXEDTEST_SIZE):
self.assertEqual(f.read(0), '')
@ -952,15 +950,15 @@ class OtherTests(unittest.TestCase):
def test_empty_zipfile(self):
# Check that creating a file in 'w' or 'a' mode and closing without
# adding any files to the archives creates a valid empty ZIP file
zipf = zipfile.ZipFile(TESTFN, mode="w")
zipf.close()
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
pass
try:
zipf = zipfile.ZipFile(TESTFN, mode="r")
except zipfile.BadZipfile:
self.fail("Unable to create empty ZIP file in 'w' mode")
zipf = zipfile.ZipFile(TESTFN, mode="a")
zipf.close()
with zipfile.ZipFile(TESTFN, mode="a") as zipf:
pass
try:
zipf = zipfile.ZipFile(TESTFN, mode="r")
except:
@ -970,8 +968,8 @@ class OtherTests(unittest.TestCase):
# Issue 1710703: Check that opening a file with less than 22 bytes
# raises a BadZipfile exception (rather than the previously unhelpful
# IOError)
f = open(TESTFN, 'w')
f.close()
with open(TESTFN, 'w') as f:
pass
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r')
def tearDown(self):
@ -1084,7 +1082,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
zipopen1 = zipfp.open(TESTFN)
with zipfp.open(TESTFN) as zipopen1:
while True:
read_data = zipopen1.read(256)
if not read_data:
@ -1092,7 +1090,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
zipdata1.append(read_data)
zipdata2 = []
zipopen2 = zipfp.open("another.name")
with zipfp.open("another.name") as zipopen2:
while True:
read_data = zipopen2.read(256)
if not read_data:
@ -1122,7 +1120,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
zipopen1 = zipfp.open(TESTFN)
with zipfp.open(TESTFN) as zipopen1:
while True:
read_data = zipopen1.read(randint(1, 1024))
if not read_data:
@ -1167,8 +1165,7 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
zopen1 = zipf.open('ones')
zopen2 = zipf.open('twos')
with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
data1 = zopen1.read(500)
data2 = zopen2.read(500)
data1 += zopen1.read(500)
@ -1180,9 +1177,8 @@ class TestsWithMultipleOpens(unittest.TestCase):
# Verify that (when the ZipFile is in control of creating file objects)
# multiple open() calls can be made without interfering with each other.
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)
zopen2 = zipf.open('twos')
data2 = zopen2.read(500)
data1 += zopen1.read(500)
data2 += zopen2.read(500)
@ -1244,7 +1240,8 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
for sep, fn in self.arcfiles.items():
zipdata = zipfp.open(fn, "rU").read()
with zipfp.open(fn, "rU") as fp:
zipdata = fp.read()
self.assertEqual(self.arcdata[sep], zipdata)
def readline_read_test(self, f, compression):
@ -1253,7 +1250,7 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
zipfp = zipfile.ZipFile(f, "r")
for sep, fn in self.arcfiles.items():
zipopen = zipfp.open(fn, "rU")
with zipfp.open(fn, "rU") as zipopen:
data = ''
while True:
read = zipopen.readline()
@ -1276,7 +1273,7 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
for sep, fn in self.arcfiles.items():
zipopen = zipfp.open(fn, "rU")
with zipfp.open(fn, "rU") as zipopen:
for line in self.line_gen:
linedata = zipopen.readline()
self.assertEqual(linedata, line + '\n')
@ -1287,7 +1284,8 @@ class UniversalNewlineTests(unittest.TestCase):
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
for sep, fn in self.arcfiles.items():
ziplines = zipfp.open(fn, "rU").readlines()
with zipfp.open(fn, "rU") as fp:
ziplines = fp.readlines()
for line, zipline in zip(self.line_gen, ziplines):
self.assertEqual(zipline, line + '\n')