Issue #4756: zipfile.is_zipfile() now supports file-like objects.

Patch by Gabriel Genellina.
This commit is contained in:
Antoine Pitrou 2008-12-27 15:43:12 +00:00
parent e57e9990e7
commit 6f193e0e95
4 changed files with 60 additions and 14 deletions

View file

@ -634,20 +634,49 @@ class OtherTests(unittest.TestCase):
def testIsZipErroneousFile(self):
# This test checks that the is_zipfile function correctly identifies
# a file that is not a zip file
fp = open(TESTFN, "w")
fp.write("this is not a legal zip file\n")
fp.close()
# - passing a filename
with open(TESTFN, "w") as fp:
fp.write("this is not a legal zip file\n")
chk = zipfile.is_zipfile(TESTFN)
self.assert_(chk is False)
self.assert_(not chk)
# - passing a file object
with open(TESTFN, "rb") as fp:
chk = zipfile.is_zipfile(fp)
self.assert_(not chk)
# - passing a file-like object
fp = StringIO()
fp.write("this is not a legal zip file\n")
chk = zipfile.is_zipfile(fp)
self.assert_(not chk)
fp.seek(0,0)
chk = zipfile.is_zipfile(fp)
self.assert_(not chk)
def testIsZipValidFile(self):
# This test checks that the is_zipfile function correctly identifies
# a file that is a zip file
# - passing a filename
zipf = zipfile.ZipFile(TESTFN, mode="w")
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
zipf.close()
chk = zipfile.is_zipfile(TESTFN)
self.assert_(chk is True)
self.assert_(chk)
# - passing a file object
with open(TESTFN, "rb") as fp:
chk = zipfile.is_zipfile(fp)
self.assert_(chk)
fp.seek(0,0)
zip_contents = fp.read()
# - passing a file-like object
fp = StringIO()
fp.write(zip_contents)
chk = zipfile.is_zipfile(fp)
self.assert_(chk)
fp.seek(0,0)
chk = zipfile.is_zipfile(fp)
self.assert_(chk)
def testNonExistentFileRaisesIOError(self):
# make sure we don't raise an AttributeError when a partially-constructed