mirror of https://github.com/python/cpython
cleanup and refactoring
This commit is contained in:
parent
2c19674b51
commit
6d6b53cab8
|
@ -6,6 +6,7 @@ except ImportError:
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import shutil
|
import shutil
|
||||||
import struct
|
import struct
|
||||||
import zipfile
|
import zipfile
|
||||||
|
@ -35,14 +36,13 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
self.data = '\n'.join(self.line_gen) + '\n'
|
self.data = '\n'.join(self.line_gen) + '\n'
|
||||||
|
|
||||||
# Make a source file with some lines
|
# Make a source file with some lines
|
||||||
fp = open(TESTFN, "wb")
|
with open(TESTFN, "wb") as fp:
|
||||||
fp.write(self.data)
|
fp.write(self.data)
|
||||||
fp.close()
|
|
||||||
|
|
||||||
def make_test_archive(self, f, compression):
|
def make_test_archive(self, f, compression):
|
||||||
# Create the ZIP archive
|
# Create the ZIP archive
|
||||||
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
||||||
zipfp.write(TESTFN, "another"+os.extsep+"name")
|
zipfp.write(TESTFN, "another.name")
|
||||||
zipfp.write(TESTFN, TESTFN)
|
zipfp.write(TESTFN, TESTFN)
|
||||||
zipfp.writestr("strfile", self.data)
|
zipfp.writestr("strfile", self.data)
|
||||||
|
|
||||||
|
@ -52,7 +52,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:
|
||||||
self.assertEqual(zipfp.read(TESTFN), self.data)
|
self.assertEqual(zipfp.read(TESTFN), self.data)
|
||||||
self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
|
self.assertEqual(zipfp.read("another.name"), self.data)
|
||||||
self.assertEqual(zipfp.read("strfile"), self.data)
|
self.assertEqual(zipfp.read("strfile"), self.data)
|
||||||
|
|
||||||
# Print the ZIP directory
|
# Print the ZIP directory
|
||||||
|
@ -66,39 +66,40 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
|
|
||||||
directory = fp.getvalue()
|
directory = fp.getvalue()
|
||||||
lines = directory.splitlines()
|
lines = directory.splitlines()
|
||||||
self.assertEquals(len(lines), 4) # Number of files + header
|
self.assertEqual(len(lines), 4) # Number of files + header
|
||||||
|
|
||||||
self.assertTrue('File Name' in lines[0])
|
self.assertTrue('File Name' in lines[0])
|
||||||
self.assertTrue('Modified' in lines[0])
|
self.assertTrue('Modified' in lines[0])
|
||||||
self.assertTrue('Size' in lines[0])
|
self.assertTrue('Size' in lines[0])
|
||||||
|
|
||||||
fn, date, time, size = lines[1].split()
|
fn, date, time_, size = lines[1].split()
|
||||||
self.assertEquals(fn, 'another.name')
|
self.assertEqual(fn, 'another.name')
|
||||||
# XXX: timestamp is not tested
|
self.assertTrue(time.strptime(date, '%Y-%m-%d'))
|
||||||
self.assertEquals(size, str(len(self.data)))
|
self.assertTrue(time.strptime(time_, '%H:%M:%S'))
|
||||||
|
self.assertEqual(size, str(len(self.data)))
|
||||||
|
|
||||||
# Check the namelist
|
# Check the namelist
|
||||||
names = zipfp.namelist()
|
names = zipfp.namelist()
|
||||||
self.assertEquals(len(names), 3)
|
self.assertEqual(len(names), 3)
|
||||||
self.assertTrue(TESTFN in names)
|
self.assertTrue(TESTFN in names)
|
||||||
self.assertTrue("another"+os.extsep+"name" in names)
|
self.assertTrue("another.name" in names)
|
||||||
self.assertTrue("strfile" in names)
|
self.assertTrue("strfile" in names)
|
||||||
|
|
||||||
# Check infolist
|
# Check infolist
|
||||||
infos = zipfp.infolist()
|
infos = zipfp.infolist()
|
||||||
names = [ i.filename for i in infos ]
|
names = [i.filename for i in infos]
|
||||||
self.assertEquals(len(names), 3)
|
self.assertEqual(len(names), 3)
|
||||||
self.assertTrue(TESTFN in names)
|
self.assertTrue(TESTFN in names)
|
||||||
self.assertTrue("another"+os.extsep+"name" in names)
|
self.assertTrue("another.name" in names)
|
||||||
self.assertTrue("strfile" in names)
|
self.assertTrue("strfile" in names)
|
||||||
for i in infos:
|
for i in infos:
|
||||||
self.assertEquals(i.file_size, len(self.data))
|
self.assertEqual(i.file_size, len(self.data))
|
||||||
|
|
||||||
# check getinfo
|
# check getinfo
|
||||||
for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
|
for nm in (TESTFN, "another.name", "strfile"):
|
||||||
info = zipfp.getinfo(nm)
|
info = zipfp.getinfo(nm)
|
||||||
self.assertEquals(info.filename, nm)
|
self.assertEqual(info.filename, nm)
|
||||||
self.assertEquals(info.file_size, len(self.data))
|
self.assertEqual(info.file_size, len(self.data))
|
||||||
|
|
||||||
# Check that testzip doesn't raise an exception
|
# Check that testzip doesn't raise an exception
|
||||||
zipfp.testzip()
|
zipfp.testzip()
|
||||||
|
@ -121,7 +122,7 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
zipdata1.append(read_data)
|
zipdata1.append(read_data)
|
||||||
|
|
||||||
zipdata2 = []
|
zipdata2 = []
|
||||||
zipopen2 = zipfp.open("another"+os.extsep+"name")
|
zipopen2 = zipfp.open("another.name")
|
||||||
while True:
|
while True:
|
||||||
read_data = zipopen2.read(256)
|
read_data = zipopen2.read(256)
|
||||||
if not read_data:
|
if not read_data:
|
||||||
|
@ -242,7 +243,7 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
|
|
||||||
@skipUnless(zlib, "requires zlib")
|
@skipUnless(zlib, "requires zlib")
|
||||||
def test_low_compression(self):
|
def test_low_compression(self):
|
||||||
# Checks for cases where compressed data is larger than original
|
"""Check for cases where compressed data is larger than original."""
|
||||||
# Create the ZIP archive
|
# Create the ZIP archive
|
||||||
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp:
|
||||||
zipfp.writestr("strfile", '12')
|
zipfp.writestr("strfile", '12')
|
||||||
|
@ -261,7 +262,7 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
self.assertEqual(zipfp.namelist(), ["absolute"])
|
self.assertEqual(zipfp.namelist(), ["absolute"])
|
||||||
|
|
||||||
def test_append_to_zip_file(self):
|
def test_append_to_zip_file(self):
|
||||||
# Test appending to an existing zipfile
|
"""Test appending to an existing zipfile."""
|
||||||
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
|
||||||
zipfp.write(TESTFN, TESTFN)
|
zipfp.write(TESTFN, TESTFN)
|
||||||
|
|
||||||
|
@ -270,31 +271,32 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
|
self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
|
||||||
|
|
||||||
def test_append_to_non_zip_file(self):
|
def test_append_to_non_zip_file(self):
|
||||||
# Test appending to an existing file that is not a zipfile
|
"""Test appending to an existing file that is not a zipfile."""
|
||||||
# NOTE: this test fails if len(d) < 22 because of the first
|
# NOTE: this test fails if len(d) < 22 because of the first
|
||||||
# line "fpin.seek(-22, 2)" in _EndRecData
|
# line "fpin.seek(-22, 2)" in _EndRecData
|
||||||
d = 'I am not a ZipFile!'*10
|
data = 'I am not a ZipFile!'*10
|
||||||
f = file(TESTFN2, 'wb')
|
with open(TESTFN2, 'wb') as f:
|
||||||
f.write(d)
|
f.write(data)
|
||||||
f.close()
|
|
||||||
with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
|
with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
|
||||||
zipfp.write(TESTFN, TESTFN)
|
zipfp.write(TESTFN, TESTFN)
|
||||||
|
|
||||||
f = file(TESTFN2, 'rb')
|
with open(TESTFN2, 'rb') as f:
|
||||||
f.seek(len(d))
|
f.seek(len(data))
|
||||||
with zipfile.ZipFile(f, "r") as zipfp:
|
with zipfile.ZipFile(f, "r") as zipfp:
|
||||||
self.assertEqual(zipfp.namelist(), [TESTFN])
|
self.assertEqual(zipfp.namelist(), [TESTFN])
|
||||||
f.close()
|
|
||||||
|
|
||||||
def test_write_default_name(self):
|
def test_write_default_name(self):
|
||||||
# Check that calling ZipFile.write without arcname specified produces the expected result
|
"""Check that calling ZipFile.write without arcname specified
|
||||||
|
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), file(TESTFN).read())
|
self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read())
|
||||||
|
|
||||||
@skipUnless(zlib, "requires zlib")
|
@skipUnless(zlib, "requires zlib")
|
||||||
def test_per_file_compression(self):
|
def test_per_file_compression(self):
|
||||||
# Check that files within a Zip archive can have different compression options
|
"""Check that files within a Zip archive can have different
|
||||||
|
compression options."""
|
||||||
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
|
||||||
zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
|
zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
|
||||||
zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
|
zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
|
||||||
|
@ -304,8 +306,8 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
|
self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
|
||||||
|
|
||||||
def test_write_to_readonly(self):
|
def test_write_to_readonly(self):
|
||||||
# Check that trying to call write() on a readonly ZipFile object
|
"""Check that trying to call write() on a readonly ZipFile object
|
||||||
# raises a RuntimeError
|
raises a RuntimeError."""
|
||||||
with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
|
with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
|
||||||
zipfp.writestr("somefile.txt", "bogus")
|
zipfp.writestr("somefile.txt", "bogus")
|
||||||
|
|
||||||
|
@ -331,8 +333,7 @@ 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, file(writtenfile, "rb").read())
|
self.assertEqual(fdata, open(writtenfile, "rb").read())
|
||||||
|
|
||||||
os.remove(writtenfile)
|
os.remove(writtenfile)
|
||||||
|
|
||||||
# remove the test file subdirectories
|
# remove the test file subdirectories
|
||||||
|
@ -351,8 +352,7 @@ class TestsWithSourceFile(unittest.TestCase):
|
||||||
else:
|
else:
|
||||||
outfile = os.path.join(os.getcwd(), fpath)
|
outfile = os.path.join(os.getcwd(), fpath)
|
||||||
|
|
||||||
self.assertEqual(fdata, file(outfile, "rb").read())
|
self.assertEqual(fdata, open(outfile, "rb").read())
|
||||||
|
|
||||||
os.remove(outfile)
|
os.remove(outfile)
|
||||||
|
|
||||||
# remove the test file subdirectories
|
# remove the test file subdirectories
|
||||||
|
@ -409,23 +409,23 @@ class TestZip64InSmallFiles(unittest.TestCase):
|
||||||
self._limit = zipfile.ZIP64_LIMIT
|
self._limit = zipfile.ZIP64_LIMIT
|
||||||
zipfile.ZIP64_LIMIT = 5
|
zipfile.ZIP64_LIMIT = 5
|
||||||
|
|
||||||
line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE))
|
line_gen = ("Test of zipfile line %d." % i
|
||||||
|
for i in range(0, FIXEDTEST_SIZE))
|
||||||
self.data = '\n'.join(line_gen)
|
self.data = '\n'.join(line_gen)
|
||||||
|
|
||||||
# Make a source file with some lines
|
# Make a source file with some lines
|
||||||
fp = open(TESTFN, "wb")
|
with open(TESTFN, "wb") as fp:
|
||||||
fp.write(self.data)
|
fp.write(self.data)
|
||||||
fp.close()
|
|
||||||
|
|
||||||
def large_file_exception_test(self, f, compression):
|
def large_file_exception_test(self, f, compression):
|
||||||
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
||||||
self.assertRaises(zipfile.LargeZipFile,
|
self.assertRaises(zipfile.LargeZipFile,
|
||||||
zipfp.write, TESTFN, "another"+os.extsep+"name")
|
zipfp.write, TESTFN, "another.name")
|
||||||
|
|
||||||
def large_file_exception_test2(self, f, compression):
|
def large_file_exception_test2(self, f, compression):
|
||||||
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
||||||
self.assertRaises(zipfile.LargeZipFile,
|
self.assertRaises(zipfile.LargeZipFile,
|
||||||
zipfp.writestr, "another"+os.extsep+"name", self.data)
|
zipfp.writestr, "another.name", self.data)
|
||||||
|
|
||||||
def test_large_file_exception(self):
|
def test_large_file_exception(self):
|
||||||
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
||||||
|
@ -435,14 +435,14 @@ class TestZip64InSmallFiles(unittest.TestCase):
|
||||||
def zip_test(self, f, compression):
|
def zip_test(self, f, compression):
|
||||||
# Create the ZIP archive
|
# Create the ZIP archive
|
||||||
with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
|
with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
|
||||||
zipfp.write(TESTFN, "another"+os.extsep+"name")
|
zipfp.write(TESTFN, "another.name")
|
||||||
zipfp.write(TESTFN, TESTFN)
|
zipfp.write(TESTFN, TESTFN)
|
||||||
zipfp.writestr("strfile", self.data)
|
zipfp.writestr("strfile", self.data)
|
||||||
|
|
||||||
# Read the ZIP archive
|
# Read the ZIP archive
|
||||||
with zipfile.ZipFile(f, "r", compression) as zipfp:
|
with zipfile.ZipFile(f, "r", compression) as zipfp:
|
||||||
self.assertEqual(zipfp.read(TESTFN), self.data)
|
self.assertEqual(zipfp.read(TESTFN), self.data)
|
||||||
self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
|
self.assertEqual(zipfp.read("another.name"), self.data)
|
||||||
self.assertEqual(zipfp.read("strfile"), self.data)
|
self.assertEqual(zipfp.read("strfile"), self.data)
|
||||||
|
|
||||||
# Print the ZIP directory
|
# Print the ZIP directory
|
||||||
|
@ -456,39 +456,40 @@ class TestZip64InSmallFiles(unittest.TestCase):
|
||||||
|
|
||||||
directory = fp.getvalue()
|
directory = fp.getvalue()
|
||||||
lines = directory.splitlines()
|
lines = directory.splitlines()
|
||||||
self.assertEquals(len(lines), 4) # Number of files + header
|
self.assertEqual(len(lines), 4) # Number of files + header
|
||||||
|
|
||||||
self.assertTrue('File Name' in lines[0])
|
self.assertTrue('File Name' in lines[0])
|
||||||
self.assertTrue('Modified' in lines[0])
|
self.assertTrue('Modified' in lines[0])
|
||||||
self.assertTrue('Size' in lines[0])
|
self.assertTrue('Size' in lines[0])
|
||||||
|
|
||||||
fn, date, time, size = lines[1].split()
|
fn, date, time_, size = lines[1].split()
|
||||||
self.assertEquals(fn, 'another.name')
|
self.assertEqual(fn, 'another.name')
|
||||||
# XXX: timestamp is not tested
|
self.assertTrue(time.strptime(date, '%Y-%m-%d'))
|
||||||
self.assertEquals(size, str(len(self.data)))
|
self.assertTrue(time.strptime(time_, '%H:%M:%S'))
|
||||||
|
self.assertEqual(size, str(len(self.data)))
|
||||||
|
|
||||||
# Check the namelist
|
# Check the namelist
|
||||||
names = zipfp.namelist()
|
names = zipfp.namelist()
|
||||||
self.assertEquals(len(names), 3)
|
self.assertEqual(len(names), 3)
|
||||||
self.assertTrue(TESTFN in names)
|
self.assertTrue(TESTFN in names)
|
||||||
self.assertTrue("another"+os.extsep+"name" in names)
|
self.assertTrue("another.name" in names)
|
||||||
self.assertTrue("strfile" in names)
|
self.assertTrue("strfile" in names)
|
||||||
|
|
||||||
# Check infolist
|
# Check infolist
|
||||||
infos = zipfp.infolist()
|
infos = zipfp.infolist()
|
||||||
names = [ i.filename for i in infos ]
|
names = [i.filename for i in infos]
|
||||||
self.assertEquals(len(names), 3)
|
self.assertEqual(len(names), 3)
|
||||||
self.assertTrue(TESTFN in names)
|
self.assertTrue(TESTFN in names)
|
||||||
self.assertTrue("another"+os.extsep+"name" in names)
|
self.assertTrue("another.name" in names)
|
||||||
self.assertTrue("strfile" in names)
|
self.assertTrue("strfile" in names)
|
||||||
for i in infos:
|
for i in infos:
|
||||||
self.assertEquals(i.file_size, len(self.data))
|
self.assertEqual(i.file_size, len(self.data))
|
||||||
|
|
||||||
# check getinfo
|
# check getinfo
|
||||||
for nm in (TESTFN, "another"+os.extsep+"name", "strfile"):
|
for nm in (TESTFN, "another.name", "strfile"):
|
||||||
info = zipfp.getinfo(nm)
|
info = zipfp.getinfo(nm)
|
||||||
self.assertEquals(info.filename, nm)
|
self.assertEqual(info.filename, nm)
|
||||||
self.assertEquals(info.file_size, len(self.data))
|
self.assertEqual(info.file_size, len(self.data))
|
||||||
|
|
||||||
# Check that testzip doesn't raise an exception
|
# Check that testzip doesn't raise an exception
|
||||||
zipfp.testzip()
|
zipfp.testzip()
|
||||||
|
@ -532,12 +533,12 @@ class PyZipFileTests(unittest.TestCase):
|
||||||
|
|
||||||
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
|
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
|
||||||
fn = __file__
|
fn = __file__
|
||||||
if fn.endswith('.pyc') or fn.endswith('.pyo'):
|
if fn.endswith(('.pyc', '.pyo')):
|
||||||
fn = fn[:-1]
|
fn = fn[:-1]
|
||||||
|
|
||||||
zipfp.writepy(fn, "testpackage")
|
zipfp.writepy(fn, "testpackage")
|
||||||
|
|
||||||
bn = "%s/%s"%("testpackage", os.path.basename(fn))
|
bn = "%s/%s" % ("testpackage", os.path.basename(fn))
|
||||||
self.assertTrue(bn not in zipfp.namelist())
|
self.assertTrue(bn not in zipfp.namelist())
|
||||||
self.assertTrue(bn + 'o' in zipfp.namelist() or
|
self.assertTrue(bn + 'o' in zipfp.namelist() or
|
||||||
bn + 'c' in zipfp.namelist())
|
bn + 'c' in zipfp.namelist())
|
||||||
|
@ -549,7 +550,8 @@ class PyZipFileTests(unittest.TestCase):
|
||||||
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
|
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
|
||||||
zipfp.writepy(packagedir)
|
zipfp.writepy(packagedir)
|
||||||
|
|
||||||
# Check for a couple of modules at different levels of the hieararchy
|
# Check for a couple of modules at different levels of the
|
||||||
|
# hierarchy
|
||||||
names = zipfp.namelist()
|
names = zipfp.namelist()
|
||||||
self.assertTrue('email/__init__.pyo' in names or
|
self.assertTrue('email/__init__.pyo' in names or
|
||||||
'email/__init__.pyc' in names)
|
'email/__init__.pyc' in names)
|
||||||
|
@ -559,17 +561,14 @@ class PyZipFileTests(unittest.TestCase):
|
||||||
def test_write_python_directory(self):
|
def test_write_python_directory(self):
|
||||||
os.mkdir(TESTFN2)
|
os.mkdir(TESTFN2)
|
||||||
try:
|
try:
|
||||||
fp = open(os.path.join(TESTFN2, "mod1.py"), "w")
|
with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp:
|
||||||
fp.write("print 42\n")
|
fp.write("print 42\n")
|
||||||
fp.close()
|
|
||||||
|
|
||||||
fp = open(os.path.join(TESTFN2, "mod2.py"), "w")
|
with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp:
|
||||||
fp.write("print 42 * 42\n")
|
fp.write("print 42 * 42\n")
|
||||||
fp.close()
|
|
||||||
|
|
||||||
fp = open(os.path.join(TESTFN2, "mod2.txt"), "w")
|
with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp:
|
||||||
fp.write("bla bla bla\n")
|
fp.write("bla bla bla\n")
|
||||||
fp.close()
|
|
||||||
|
|
||||||
zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
|
zipfp = zipfile.PyZipFile(TemporaryFile(), "w")
|
||||||
zipfp.writepy(TESTFN2)
|
zipfp.writepy(TESTFN2)
|
||||||
|
@ -584,7 +583,7 @@ 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:
|
||||||
file(TESTFN, 'w').write('most definitely not a python file')
|
open(TESTFN, 'w').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)
|
||||||
|
|
||||||
|
@ -620,28 +619,26 @@ class OtherTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_close_erroneous_file(self):
|
def test_close_erroneous_file(self):
|
||||||
# This test checks that the ZipFile constructor closes the file object
|
# This test checks that the ZipFile constructor closes the file object
|
||||||
# it opens if there's an error in the file. If it doesn't, the traceback
|
# it opens if there's an error in the file. If it doesn't, the
|
||||||
# holds a reference to the ZipFile object and, indirectly, the file object.
|
# traceback holds a reference to the ZipFile object and, indirectly,
|
||||||
|
# the file object.
|
||||||
# On Windows, this causes the os.unlink() call to fail because the
|
# On Windows, this causes the os.unlink() call to fail because the
|
||||||
# underlying file is still open. This is SF bug #412214.
|
# underlying file is still open. This is SF bug #412214.
|
||||||
#
|
#
|
||||||
fp = open(TESTFN, "w")
|
with open(TESTFN, "w") as fp:
|
||||||
fp.write("this is not a legal zip file\n")
|
fp.write("this is not a legal zip file\n")
|
||||||
fp.close()
|
|
||||||
try:
|
try:
|
||||||
zf = zipfile.ZipFile(TESTFN)
|
zf = zipfile.ZipFile(TESTFN)
|
||||||
except zipfile.BadZipfile:
|
except zipfile.BadZipfile:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def test_is_zip_erroneous_file(self):
|
def test_is_zip_erroneous_file(self):
|
||||||
# This test checks that the is_zipfile function correctly identifies
|
"""Check that is_zipfile() correctly identifies non-zip files."""
|
||||||
# a file that is not a zip file
|
|
||||||
|
|
||||||
# - passing a filename
|
# - passing a filename
|
||||||
with open(TESTFN, "w") as fp:
|
with open(TESTFN, "w") as fp:
|
||||||
fp.write("this is not a legal zip file\n")
|
fp.write("this is not a legal zip file\n")
|
||||||
chk = zipfile.is_zipfile(TESTFN)
|
chk = zipfile.is_zipfile(TESTFN)
|
||||||
self.assertTrue(not chk)
|
self.assertFalse(chk)
|
||||||
# - passing a file object
|
# - passing a file object
|
||||||
with open(TESTFN, "rb") as fp:
|
with open(TESTFN, "rb") as fp:
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
|
@ -651,14 +648,12 @@ class OtherTests(unittest.TestCase):
|
||||||
fp.write("this is not a legal zip file\n")
|
fp.write("this is not a legal zip file\n")
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
self.assertTrue(not chk)
|
self.assertTrue(not chk)
|
||||||
fp.seek(0,0)
|
fp.seek(0, 0)
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
self.assertTrue(not chk)
|
self.assertTrue(not chk)
|
||||||
|
|
||||||
def test_is_zip_valid_file(self):
|
def test_is_zip_valid_file(self):
|
||||||
# This test checks that the is_zipfile function correctly identifies
|
"""Check that is_zipfile() correctly identifies zip files."""
|
||||||
# a file that is a zip file
|
|
||||||
|
|
||||||
# - passing a filename
|
# - passing a filename
|
||||||
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!")
|
||||||
|
@ -668,14 +663,14 @@ class OtherTests(unittest.TestCase):
|
||||||
with open(TESTFN, "rb") as fp:
|
with open(TESTFN, "rb") as fp:
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
self.assertTrue(chk)
|
self.assertTrue(chk)
|
||||||
fp.seek(0,0)
|
fp.seek(0, 0)
|
||||||
zip_contents = fp.read()
|
zip_contents = fp.read()
|
||||||
# - passing a file-like object
|
# - passing a file-like object
|
||||||
fp = StringIO()
|
fp = StringIO()
|
||||||
fp.write(zip_contents)
|
fp.write(zip_contents)
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
self.assertTrue(chk)
|
self.assertTrue(chk)
|
||||||
fp.seek(0,0)
|
fp.seek(0, 0)
|
||||||
chk = zipfile.is_zipfile(fp)
|
chk = zipfile.is_zipfile(fp)
|
||||||
self.assertTrue(chk)
|
self.assertTrue(chk)
|
||||||
|
|
||||||
|
@ -698,13 +693,12 @@ class OtherTests(unittest.TestCase):
|
||||||
f.close()
|
f.close()
|
||||||
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
|
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
|
||||||
|
|
||||||
f = open(TESTFN, 'w')
|
with open(TESTFN, 'w') as fp:
|
||||||
f.write("short file")
|
fp.write("short file")
|
||||||
f.close()
|
|
||||||
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
|
self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN)
|
||||||
|
|
||||||
def test_closed_zip_raises_RuntimeError(self):
|
def test_closed_zip_raises_RuntimeError(self):
|
||||||
# Verify that testzip() doesn't swallow inappropriate exceptions.
|
"""Verify that testzip() doesn't swallow inappropriate exceptions."""
|
||||||
data = StringIO()
|
data = StringIO()
|
||||||
with zipfile.ZipFile(data, mode="w") as zipf:
|
with zipfile.ZipFile(data, mode="w") as zipf:
|
||||||
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
|
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
|
||||||
|
@ -717,15 +711,15 @@ 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")
|
||||||
file(TESTFN, 'w').write('zipfile test data')
|
open(TESTFN, 'w').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):
|
||||||
# Check that bad modes passed to ZipFile constructor are caught
|
"""Check that bad modes passed to ZipFile constructor are caught."""
|
||||||
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
|
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q")
|
||||||
|
|
||||||
def test_bad_open_mode(self):
|
def test_bad_open_mode(self):
|
||||||
# Check that bad modes passed to ZipFile.open are caught
|
"""Check that bad modes passed to ZipFile.open are caught."""
|
||||||
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!")
|
||||||
|
|
||||||
|
@ -735,8 +729,8 @@ class OtherTests(unittest.TestCase):
|
||||||
self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
|
self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q")
|
||||||
|
|
||||||
def test_read0(self):
|
def test_read0(self):
|
||||||
# Check that calling read(0) on a ZipExtFile object returns an empty
|
"""Check that calling read(0) on a ZipExtFile object returns an empty
|
||||||
# string and doesn't advance file pointer
|
string and doesn't advance file pointer."""
|
||||||
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
|
||||||
|
@ -747,30 +741,32 @@ class OtherTests(unittest.TestCase):
|
||||||
self.assertEqual(f.read(), "O, for a Muse of Fire!")
|
self.assertEqual(f.read(), "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
|
||||||
# exist in the archive raises a RuntimeError
|
exist in the archive raises a RuntimeError."""
|
||||||
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
||||||
self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
|
self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
|
||||||
|
|
||||||
def test_bad_compression_mode(self):
|
def test_bad_compression_mode(self):
|
||||||
# Check that bad compression methods passed to ZipFile.open are caught
|
"""Check that bad compression methods passed to ZipFile.open are
|
||||||
|
caught."""
|
||||||
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
|
self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1)
|
||||||
|
|
||||||
def test_null_byte_in_filename(self):
|
def test_null_byte_in_filename(self):
|
||||||
# Check that a filename containing a null byte is properly terminated
|
"""Check that a filename containing a null byte is properly
|
||||||
|
terminated."""
|
||||||
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
||||||
zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
|
zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!")
|
||||||
self.assertEqual(zipf.namelist(), ['foo.txt'])
|
self.assertEqual(zipf.namelist(), ['foo.txt'])
|
||||||
|
|
||||||
def test_struct_sizes(self):
|
def test_struct_sizes(self):
|
||||||
# check that ZIP internal structure sizes are calculated correctly
|
"""Check that ZIP internal structure sizes are calculated correctly."""
|
||||||
self.assertEqual(zipfile.sizeEndCentDir, 22)
|
self.assertEqual(zipfile.sizeEndCentDir, 22)
|
||||||
self.assertEqual(zipfile.sizeCentralDir, 46)
|
self.assertEqual(zipfile.sizeCentralDir, 46)
|
||||||
self.assertEqual(zipfile.sizeEndCentDir64, 56)
|
self.assertEqual(zipfile.sizeEndCentDir64, 56)
|
||||||
self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
|
self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
|
||||||
|
|
||||||
def test_comments(self):
|
def test_comments(self):
|
||||||
# This test checks that comments on the archive are handled properly
|
"""Check that comments on the archive are handled properly."""
|
||||||
|
|
||||||
# check default comment is empty
|
# check default comment is empty
|
||||||
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
|
||||||
|
@ -810,9 +806,9 @@ class OtherTests(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
class DecryptionTests(unittest.TestCase):
|
class DecryptionTests(unittest.TestCase):
|
||||||
# This test checks that ZIP decryption works. Since the library does not
|
"""Check that ZIP decryption works. Since the library does not
|
||||||
# support encryption at the moment, we use a pre-generated encrypted
|
support encryption at the moment, we use a pre-generated encrypted
|
||||||
# ZIP file
|
ZIP file."""
|
||||||
|
|
||||||
data = (
|
data = (
|
||||||
'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
|
'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
|
||||||
|
@ -836,13 +832,11 @@ class DecryptionTests(unittest.TestCase):
|
||||||
plain2 = '\x00'*512
|
plain2 = '\x00'*512
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
fp = open(TESTFN, "wb")
|
with open(TESTFN, "wb") as fp:
|
||||||
fp.write(self.data)
|
fp.write(self.data)
|
||||||
fp.close()
|
|
||||||
self.zip = zipfile.ZipFile(TESTFN, "r")
|
self.zip = zipfile.ZipFile(TESTFN, "r")
|
||||||
fp = open(TESTFN2, "wb")
|
with open(TESTFN2, "wb") as fp:
|
||||||
fp.write(self.data2)
|
fp.write(self.data2)
|
||||||
fp.close()
|
|
||||||
self.zip2 = zipfile.ZipFile(TESTFN2, "r")
|
self.zip2 = zipfile.ZipFile(TESTFN2, "r")
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
@ -866,20 +860,20 @@ class DecryptionTests(unittest.TestCase):
|
||||||
@skipUnless(zlib, "requires zlib")
|
@skipUnless(zlib, "requires zlib")
|
||||||
def test_good_password(self):
|
def test_good_password(self):
|
||||||
self.zip.setpassword("python")
|
self.zip.setpassword("python")
|
||||||
self.assertEquals(self.zip.read("test.txt"), self.plain)
|
self.assertEqual(self.zip.read("test.txt"), self.plain)
|
||||||
self.zip2.setpassword("12345")
|
self.zip2.setpassword("12345")
|
||||||
self.assertEquals(self.zip2.read("zero"), self.plain2)
|
self.assertEqual(self.zip2.read("zero"), self.plain2)
|
||||||
|
|
||||||
|
|
||||||
class TestsWithRandomBinaryFiles(unittest.TestCase):
|
class TestsWithRandomBinaryFiles(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
datacount = randint(16, 64)*1024 + randint(1, 1024)
|
datacount = randint(16, 64)*1024 + randint(1, 1024)
|
||||||
self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount)))
|
self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000))
|
||||||
|
for i in xrange(datacount))
|
||||||
|
|
||||||
# Make a source file with some lines
|
# Make a source file with some lines
|
||||||
fp = open(TESTFN, "wb")
|
with open(TESTFN, "wb") as fp:
|
||||||
fp.write(self.data)
|
fp.write(self.data)
|
||||||
fp.close()
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
unlink(TESTFN)
|
unlink(TESTFN)
|
||||||
|
@ -888,7 +882,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
|
||||||
def make_test_archive(self, f, compression):
|
def make_test_archive(self, f, compression):
|
||||||
# Create the ZIP archive
|
# Create the ZIP archive
|
||||||
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
with zipfile.ZipFile(f, "w", compression) as zipfp:
|
||||||
zipfp.write(TESTFN, "another"+os.extsep+"name")
|
zipfp.write(TESTFN, "another.name")
|
||||||
zipfp.write(TESTFN, TESTFN)
|
zipfp.write(TESTFN, TESTFN)
|
||||||
|
|
||||||
def zip_test(self, f, compression):
|
def zip_test(self, f, compression):
|
||||||
|
@ -899,7 +893,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
|
||||||
testdata = zipfp.read(TESTFN)
|
testdata = zipfp.read(TESTFN)
|
||||||
self.assertEqual(len(testdata), len(self.data))
|
self.assertEqual(len(testdata), len(self.data))
|
||||||
self.assertEqual(testdata, self.data)
|
self.assertEqual(testdata, self.data)
|
||||||
self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data)
|
self.assertEqual(zipfp.read("another.name"), self.data)
|
||||||
|
|
||||||
def test_stored(self):
|
def test_stored(self):
|
||||||
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
||||||
|
@ -919,7 +913,7 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
|
||||||
zipdata1.append(read_data)
|
zipdata1.append(read_data)
|
||||||
|
|
||||||
zipdata2 = []
|
zipdata2 = []
|
||||||
zipopen2 = zipfp.open("another"+os.extsep+"name")
|
zipopen2 = zipfp.open("another.name")
|
||||||
while True:
|
while True:
|
||||||
read_data = zipopen2.read(256)
|
read_data = zipopen2.read(256)
|
||||||
if not read_data:
|
if not read_data:
|
||||||
|
@ -931,8 +925,8 @@ class TestsWithRandomBinaryFiles(unittest.TestCase):
|
||||||
self.assertEqual(testdata1, self.data)
|
self.assertEqual(testdata1, self.data)
|
||||||
|
|
||||||
testdata2 = ''.join(zipdata2)
|
testdata2 = ''.join(zipdata2)
|
||||||
self.assertEqual(len(testdata1), len(self.data))
|
self.assertEqual(len(testdata2), len(self.data))
|
||||||
self.assertEqual(testdata1, self.data)
|
self.assertEqual(testdata2, self.data)
|
||||||
|
|
||||||
def test_open_stored(self):
|
def test_open_stored(self):
|
||||||
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
for f in (TESTFN2, TemporaryFile(), StringIO()):
|
||||||
|
@ -1040,7 +1034,8 @@ class TestWithDirectory(unittest.TestCase):
|
||||||
|
|
||||||
class UniversalNewlineTests(unittest.TestCase):
|
class UniversalNewlineTests(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)]
|
self.line_gen = ["Test of zipfile line %d." % i
|
||||||
|
for i in xrange(FIXEDTEST_SIZE)]
|
||||||
self.seps = ('\r', '\r\n', '\n')
|
self.seps = ('\r', '\r\n', '\n')
|
||||||
self.arcdata, self.arcfiles = {}, {}
|
self.arcdata, self.arcfiles = {}, {}
|
||||||
for n, s in enumerate(self.seps):
|
for n, s in enumerate(self.seps):
|
||||||
|
|
Loading…
Reference in New Issue