0001 # We can test part of the module without zlib. 0002 try: 0003 import zlib 0004 except ImportError: 0005 zlib = None 0006 0007 import zipfile, os, unittest 0008 0009 from StringIO import StringIO 0010 from tempfile import TemporaryFile 0011 0012 from test.test_support import TESTFN, run_unittest 0013 0014 TESTFN2 = TESTFN + "2" 0015 0016 class TestsWithSourceFile(unittest.TestCase): 0017 def setUp(self): 0018 line_gen = ("Test of zipfile line %d." % i for i in range(0, 1000)) 0019 self.data = '\n'.join(line_gen) 0020 0021 # Make a source file with some lines 0022 fp = open(TESTFN, "wb") 0023 fp.write(self.data) 0024 fp.close() 0025 0026 def zipTest(self, f, compression): 0027 # Create the ZIP archive 0028 zipfp = zipfile.ZipFile(f, "w", compression) 0029 zipfp.write(TESTFN, "another"+os.extsep+"name") 0030 zipfp.write(TESTFN, TESTFN) 0031 zipfp.close() 0032 0033 # Read the ZIP archive 0034 zipfp = zipfile.ZipFile(f, "r", compression) 0035 self.assertEqual(zipfp.read(TESTFN), self.data) 0036 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) 0037 zipfp.close() 0038 0039 def testStored(self): 0040 for f in (TESTFN2, TemporaryFile(), StringIO()): 0041 self.zipTest(f, zipfile.ZIP_STORED) 0042 0043 if zlib: 0044 def testDeflated(self): 0045 for f in (TESTFN2, TemporaryFile(), StringIO()): 0046 self.zipTest(f, zipfile.ZIP_DEFLATED) 0047 0048 def tearDown(self): 0049 os.remove(TESTFN) 0050 os.remove(TESTFN2) 0051 0052 class OtherTests(unittest.TestCase): 0053 def testCloseErroneousFile(self): 0054 # This test checks that the ZipFile constructor closes the file object 0055 # it opens if there's an error in the file. If it doesn't, the traceback 0056 # holds a reference to the ZipFile object and, indirectly, the file object. 0057 # On Windows, this causes the os.unlink() call to fail because the 0058 # underlying file is still open. This is SF bug #412214. 0059 # 0060 fp = open(TESTFN, "w") 0061 fp.write("this is not a legal zip file\n") 0062 fp.close() 0063 try: 0064 zf = zipfile.ZipFile(TESTFN) 0065 except zipfile.BadZipfile: 0066 os.unlink(TESTFN) 0067 0068 def testNonExistentFileRaisesIOError(self): 0069 # make sure we don't raise an AttributeError when a partially-constructed 0070 # ZipFile instance is finalized; this tests for regression on SF tracker 0071 # bug #403871. 0072 0073 # The bug we're testing for caused an AttributeError to be raised 0074 # when a ZipFile instance was created for a file that did not 0075 # exist; the .fp member was not initialized but was needed by the 0076 # __del__() method. Since the AttributeError is in the __del__(), 0077 # it is ignored, but the user should be sufficiently annoyed by 0078 # the message on the output that regression will be noticed 0079 # quickly. 0080 self.assertRaises(IOError, zipfile.ZipFile, TESTFN) 0081 0082 def testClosedZipRaisesRuntimeError(self): 0083 # Verify that testzip() doesn't swallow inappropriate exceptions. 0084 data = StringIO() 0085 zipf = zipfile.ZipFile(data, mode="w") 0086 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 0087 zipf.close() 0088 0089 # This is correct; calling .read on a closed ZipFile should throw 0090 # a RuntimeError, and so should calling .testzip. An earlier 0091 # version of .testzip would swallow this exception (and any other) 0092 # and report that the first file in the archive was corrupt. 0093 self.assertRaises(RuntimeError, zipf.testzip) 0094 0095 def test_main(): 0096 run_unittest(TestsWithSourceFile, OtherTests) 0097 0098 if __name__ == "__main__": 0099 test_main() 0100
Generated by PyXR 0.9.4