Fix #8886. Use context managers throughout the test.
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index d67938f..15a8c4c 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -112,20 +112,20 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
- zipopen1 = zipfp.open(TESTFN)
- while True:
- read_data = zipopen1.read(256)
- if not read_data:
- break
- zipdata1.append(read_data)
+ with zipfp.open(TESTFN) as zipopen1:
+ while True:
+ read_data = zipopen1.read(256)
+ if not read_data:
+ break
+ zipdata1.append(read_data)
zipdata2 = []
- zipopen2 = zipfp.open("another.name")
- while True:
- read_data = zipopen2.read(256)
- if not read_data:
- break
- zipdata2.append(read_data)
+ with zipfp.open("another.name") as zipopen2:
+ while True:
+ read_data = zipopen2.read(256)
+ if not read_data:
+ break
+ zipdata2.append(read_data)
self.assertEqual(b''.join(zipdata1), self.data)
self.assertEqual(b''.join(zipdata2), self.data)
@@ -146,7 +146,8 @@
infos = zipfp.infolist()
data = b""
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")
data = b""
for info in infos:
@@ -159,12 +160,12 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
- zipopen1 = zipfp.open(TESTFN)
- while True:
- read_data = zipopen1.read(randint(1, 1024))
- if not read_data:
- break
- zipdata1.append(read_data)
+ with zipfp.open(TESTFN) as zipopen1:
+ while True:
+ read_data = zipopen1.read(randint(1, 1024))
+ if not read_data:
+ break
+ zipdata1.append(read_data)
self.assertEqual(b''.join(zipdata1), self.data)
if not isinstance(f, str):
@@ -184,9 +185,9 @@
data2 = b''
zipfp = zipfile.ZipFile(f, 'r')
- zipopen = zipfp.open(TESTFN, 'rU')
- for line in zipopen:
- data2 += line
+ with zipfp.open(TESTFN, 'rU') as zipopen:
+ for line in zipopen:
+ data2 += line
zipfp.close()
self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
@@ -196,19 +197,18 @@
# Read the ZIP archive
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''
- while True:
- read = zipopen.readline()
- if not read:
- break
- data += read
-
- read = zipopen.read(100)
- if not read:
- break
- data += read
+ read = zipopen.read(100)
+ if not read:
+ break
+ data += read
self.assertEqual(data, self.data)
zipfp.close()
@@ -220,10 +220,10 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
- zipopen = zipfp.open(TESTFN)
- for line in self.line_gen:
- linedata = zipopen.readline()
- self.assertEqual(linedata, line + '\n')
+ with zipfp.open(TESTFN) as zipopen:
+ for line in self.line_gen:
+ linedata = zipopen.readline()
+ self.assertEqual(linedata, line + '\n')
if not isinstance(f, str):
f.close()
@@ -232,7 +232,8 @@
# Read the ZIP archive
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):
self.assertEqual(zipline, line + '\n')
if not isinstance(f, str):
@@ -243,8 +244,9 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r") as zipfp:
- for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
- self.assertEqual(zipline, line + '\n')
+ with zipfp.open(TESTFN) as zipopen:
+ for line, zipline in zip(self.line_gen, zipopen):
+ self.assertEqual(zipline, line + '\n')
if not isinstance(f, str):
f.close()
@@ -311,9 +313,9 @@
# Get an open object for strfile
with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
- openobj = zipfp.open("strfile")
- self.assertEqual(openobj.read(1), b'1')
- self.assertEqual(openobj.read(1), b'2')
+ with zipfp.open("strfile") as openobj:
+ self.assertEqual(openobj.read(1), b'1')
+ self.assertEqual(openobj.read(1), b'2')
def test_absolute_arcnames(self):
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
@@ -352,7 +354,8 @@
produces the expected result."""
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
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")
def test_per_file_compression(self):
@@ -394,7 +397,8 @@
self.assertEqual(writtenfile, correctfile)
# 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)
@@ -414,7 +418,8 @@
else:
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)
@@ -674,7 +679,8 @@
def test_write_non_pyfile(self):
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)
os.remove(TESTFN)
@@ -825,7 +831,8 @@
self.assertRaises(RuntimeError, zipf.open, "foo.txt")
self.assertRaises(RuntimeError, zipf.testzip)
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)
def test_bad_constructor_mode(self):
@@ -848,11 +855,11 @@
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")
- for i in range(FIXEDTEST_SIZE):
- self.assertEqual(f.read(0), b'')
+ with zipf.open("foo.txt") as f:
+ for i in range(FIXEDTEST_SIZE):
+ 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):
"""Check that attempting to call open() for an item that doesn't
@@ -1115,20 +1122,20 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
- zipopen1 = zipfp.open(TESTFN)
- while True:
- read_data = zipopen1.read(256)
- if not read_data:
- break
- zipdata1.append(read_data)
+ with zipfp.open(TESTFN) as zipopen1:
+ while True:
+ read_data = zipopen1.read(256)
+ if not read_data:
+ break
+ zipdata1.append(read_data)
zipdata2 = []
- zipopen2 = zipfp.open("another.name")
- while True:
- read_data = zipopen2.read(256)
- if not read_data:
- break
- zipdata2.append(read_data)
+ with zipfp.open("another.name") as zipopen2:
+ while True:
+ read_data = zipopen2.read(256)
+ if not read_data:
+ break
+ zipdata2.append(read_data)
testdata1 = b''.join(zipdata1)
self.assertEqual(len(testdata1), len(self.data))
@@ -1155,12 +1162,12 @@
# Read the ZIP archive
with zipfile.ZipFile(f, "r", compression) as zipfp:
zipdata1 = []
- zipopen1 = zipfp.open(TESTFN)
- while True:
- read_data = zipopen1.read(randint(1, 1024))
- if not read_data:
- break
- zipdata1.append(read_data)
+ with zipfp.open(TESTFN) as zipopen1:
+ while True:
+ read_data = zipopen1.read(randint(1, 1024))
+ if not read_data:
+ break
+ zipdata1.append(read_data)
testdata = b''.join(zipdata1)
self.assertEqual(len(testdata), len(self.data))
@@ -1190,24 +1197,22 @@
# 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('ones')
- data1 = zopen1.read(500)
- data2 = zopen2.read(500)
- data1 += zopen1.read(500)
- data2 += zopen2.read(500)
+ with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read(500)
+ data2 += zopen2.read(500)
self.assertEqual(data1, data2)
def test_different_file(self):
# 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')
- data1 = zopen1.read(500)
- data2 = zopen2.read(500)
- data1 += zopen1.read(500)
- data2 += zopen2.read(500)
+ with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read(500)
+ data2 += zopen2.read(500)
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
@@ -1215,12 +1220,11 @@
# 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')
- data1 = zopen1.read(500)
- zopen2 = zipf.open('twos')
- data2 = zopen2.read(500)
- data1 += zopen1.read(500)
- data2 += zopen2.read(500)
+ with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
+ data1 = zopen1.read(500)
+ data2 = zopen2.read(500)
+ data1 += zopen1.read(500)
+ data2 += zopen2.read(500)
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
@@ -1294,25 +1298,23 @@
self.make_test_archive(f, compression)
# Read the ZIP archive
- zipfp = zipfile.ZipFile(f, "r")
- for sep, fn in self.arcfiles.items():
- zipopen = zipfp.open(fn, "rU")
- data = b''
- while True:
- read = zipopen.readline()
- if not read:
- break
- data += read
+ with zipfile.ZipFile(f, "r") as zipfp:
+ for sep, fn in self.arcfiles.items():
+ with zipfp.open(fn, "rU") as zipopen:
+ data = b''
+ while True:
+ read = zipopen.readline()
+ if not read:
+ break
+ data += read
- read = zipopen.read(5)
- if not read:
- break
- data += read
+ read = zipopen.read(5)
+ if not read:
+ break
+ data += read
- zipopen.close()
self.assertEqual(data, self.arcdata['\n'])
- zipfp.close()
if not isinstance(f, str):
f.close()
diff --git a/Misc/NEWS b/Misc/NEWS
index bf660cc..13cae78 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,12 @@
- 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?
=================================