diff options
| author | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-27 15:50:40 (GMT) |
|---|---|---|
| committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-27 15:50:40 (GMT) |
| commit | db5fe667311745a2bc86355620354449721545f3 (patch) | |
| tree | a33cb5639ac9b87911c0a934ba5a42737c3df671 /Lib/test/test_zipfile.py | |
| parent | d88e8fab148359a85f8e2a110a850c74e8ed5244 (diff) | |
| download | cpython-db5fe667311745a2bc86355620354449721545f3.zip cpython-db5fe667311745a2bc86355620354449721545f3.tar.gz cpython-db5fe667311745a2bc86355620354449721545f3.tar.bz2 | |
Merged revisions 67946 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67946 | antoine.pitrou | 2008-12-27 16:43:12 +0100 (sam., 27 déc. 2008) | 4 lines
Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Patch by Gabriel Genellina.
........
Diffstat (limited to 'Lib/test/test_zipfile.py')
| -rw-r--r-- | Lib/test/test_zipfile.py | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 9e565fb..678ac9f 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -621,20 +621,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 = io.BytesIO() + fp.write(b"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", b"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 = io.BytesIO() + 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 |
