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,20 +112,20 @@ 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:
break break
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:
break break
zipdata2.append(read_data) zipdata2.append(read_data)
self.assertEqual(b''.join(zipdata1), self.data) self.assertEqual(b''.join(zipdata1), self.data)
self.assertEqual(b''.join(zipdata2), self.data) self.assertEqual(b''.join(zipdata2), self.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,12 +160,12 @@ 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:
break break
zipdata1.append(read_data) zipdata1.append(read_data)
self.assertEqual(b''.join(zipdata1), self.data) self.assertEqual(b''.join(zipdata1), self.data)
if not isinstance(f, str): if not isinstance(f, str):
@ -184,9 +185,9 @@ 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()
self.assertEqual(data, data2.replace(b'\n', b'\r\n')) self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
@ -196,19 +197,18 @@ 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''
while True:
read = zipopen.readline()
if not read:
break
data += read
data = b'' read = zipopen.read(100)
while True: if not read:
read = zipopen.readline() break
if not read: data += read
break
data += read
read = zipopen.read(100)
if not read:
break
data += read
self.assertEqual(data, self.data) self.assertEqual(data, self.data)
zipfp.close() zipfp.close()
@ -220,10 +220,10 @@ 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')
if not isinstance(f, str): if not isinstance(f, str):
f.close() f.close()
@ -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,8 +244,9 @@ 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:
self.assertEqual(zipline, line + '\n') for line, zipline in zip(self.line_gen, zipopen):
self.assertEqual(zipline, line + '\n')
if not isinstance(f, str): if not isinstance(f, str):
f.close() f.close()
@ -311,9 +313,9 @@ 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')
def test_absolute_arcnames(self): def test_absolute_arcnames(self):
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
@ -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,11 +855,11 @@ 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'')
self.assertEqual(f.read(), b"O, for a Muse of Fire!") self.assertEqual(f.read(), b"O, for a Muse of Fire!")
def test_open_non_existent_item(self): def test_open_non_existent_item(self):
"""Check that attempting to call open() for an item that doesn't """Check that attempting to call open() for an item that doesn't
@ -1115,20 +1122,20 @@ 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:
break break
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:
break break
zipdata2.append(read_data) zipdata2.append(read_data)
testdata1 = b''.join(zipdata1) testdata1 = b''.join(zipdata1)
self.assertEqual(len(testdata1), len(self.data)) self.assertEqual(len(testdata1), len(self.data))
@ -1155,12 +1162,12 @@ 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:
break break
zipdata1.append(read_data) zipdata1.append(read_data)
testdata = b''.join(zipdata1) testdata = b''.join(zipdata1)
self.assertEqual(len(testdata), len(self.data)) self.assertEqual(len(testdata), len(self.data))
@ -1190,24 +1197,22 @@ 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) data2 += zopen2.read(500)
data2 += zopen2.read(500)
self.assertEqual(data1, data2) self.assertEqual(data1, data2)
def test_different_file(self): def test_different_file(self):
# 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) data2 += zopen2.read(500)
data2 += zopen2.read(500)
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
@ -1215,12 +1220,11 @@ 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)
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE) self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE) self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
@ -1294,25 +1298,23 @@ 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()
if not read: if not read:
break break
data += read data += read
read = zipopen.read(5) read = zipopen.read(5)
if not read: if not read:
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?
================================= =================================