1 # We can test part of the module without zlib.
7 import zipfile
, os
, unittest
9 from StringIO
import StringIO
10 from tempfile
import TemporaryFile
12 from test
.test_support
import TESTFN
, run_unittest
14 TESTFN2
= TESTFN
+ "2"
16 class TestsWithSourceFile(unittest
.TestCase
):
18 line_gen
= ("Test of zipfile line %d." % i
for i
in range(0, 1000))
19 self
.data
= '\n'.join(line_gen
)
21 # Make a source file with some lines
22 fp
= open(TESTFN
, "wb")
26 def zipTest(self
, f
, compression
):
27 # Create the ZIP archive
28 zipfp
= zipfile
.ZipFile(f
, "w", compression
)
29 zipfp
.write(TESTFN
, "another"+os
.extsep
+"name")
30 zipfp
.write(TESTFN
, TESTFN
)
33 # Read the ZIP archive
34 zipfp
= zipfile
.ZipFile(f
, "r", compression
)
35 self
.assertEqual(zipfp
.read(TESTFN
), self
.data
)
36 self
.assertEqual(zipfp
.read("another"+os
.extsep
+"name"), self
.data
)
40 for f
in (TESTFN2
, TemporaryFile(), StringIO()):
41 self
.zipTest(f
, zipfile
.ZIP_STORED
)
44 def testDeflated(self
):
45 for f
in (TESTFN2
, TemporaryFile(), StringIO()):
46 self
.zipTest(f
, zipfile
.ZIP_DEFLATED
)
48 def testAbsoluteArcnames(self
):
49 zipfp
= zipfile
.ZipFile(TESTFN2
, "w", zipfile
.ZIP_STORED
)
50 zipfp
.write(TESTFN
, "/absolute")
53 zipfp
= zipfile
.ZipFile(TESTFN2
, "r", zipfile
.ZIP_STORED
)
54 self
.assertEqual(zipfp
.namelist(), ["absolute"])
62 class OtherTests(unittest
.TestCase
):
63 def testCloseErroneousFile(self
):
64 # This test checks that the ZipFile constructor closes the file object
65 # it opens if there's an error in the file. If it doesn't, the traceback
66 # holds a reference to the ZipFile object and, indirectly, the file object.
67 # On Windows, this causes the os.unlink() call to fail because the
68 # underlying file is still open. This is SF bug #412214.
70 fp
= open(TESTFN
, "w")
71 fp
.write("this is not a legal zip file\n")
74 zf
= zipfile
.ZipFile(TESTFN
)
75 except zipfile
.BadZipfile
:
78 def testNonExistentFileRaisesIOError(self
):
79 # make sure we don't raise an AttributeError when a partially-constructed
80 # ZipFile instance is finalized; this tests for regression on SF tracker
83 # The bug we're testing for caused an AttributeError to be raised
84 # when a ZipFile instance was created for a file that did not
85 # exist; the .fp member was not initialized but was needed by the
86 # __del__() method. Since the AttributeError is in the __del__(),
87 # it is ignored, but the user should be sufficiently annoyed by
88 # the message on the output that regression will be noticed
90 self
.assertRaises(IOError, zipfile
.ZipFile
, TESTFN
)
92 def testClosedZipRaisesRuntimeError(self
):
93 # Verify that testzip() doesn't swallow inappropriate exceptions.
95 zipf
= zipfile
.ZipFile(data
, mode
="w")
96 zipf
.writestr("foo.txt", "O, for a Muse of Fire!")
99 # This is correct; calling .read on a closed ZipFile should throw
100 # a RuntimeError, and so should calling .testzip. An earlier
101 # version of .testzip would swallow this exception (and any other)
102 # and report that the first file in the archive was corrupt.
103 self
.assertRaises(RuntimeError, zipf
.testzip
)
106 run_unittest(TestsWithSourceFile
, OtherTests
)
108 if __name__
== "__main__":