# 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)
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:
# 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):
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'))
# Read the ZIP archive
zipfp = zipfile.ZipFile(f, "r")
- zipopen = zipfp.open(TESTFN)
-
- data = b''
- while True:
- read = zipopen.readline()
- if not read:
- break
- data += read
+ with zipfp.open(TESTFN) as zipopen:
+ 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()
# 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()
# 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):
# 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()
# 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:
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):
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)
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)
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)
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):
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
# 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))
# 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))
# 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)
# 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)
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
-
- read = zipopen.read(5)
- 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
- zipopen.close()
self.assertEqual(data, self.arcdata['\n'])
- zipfp.close()
if not isinstance(f, str):
f.close()