blob: bf7770b8cb9fbf1f242043a488bf0cfe3c481587 [file] [log] [blame]
Fred Drake3d9091e2001-03-26 15:49:24 +00001import zipfile, os, StringIO, tempfile
Fred Drake6e7e4852001-02-28 05:34:16 +00002from test_support import TestFailed
Guido van Rossum368f04a2000-04-10 13:23:04 +00003
4srcname = "junk9630.tmp"
5zipname = "junk9708.tmp"
6
Fred Drake3d9091e2001-03-26 15:49:24 +00007
8def zipTest(f, compression, srccontents):
9 zip = zipfile.ZipFile(f, "w", compression) # Create the ZIP archive
10 zip.write(srcname, "another.name")
11 zip.write(srcname, srcname)
12 zip.close()
Tim Petersa19a1682001-03-29 04:36:09 +000013
Fred Drake3d9091e2001-03-26 15:49:24 +000014 zip = zipfile.ZipFile(f, "r", compression) # Read the ZIP archive
15 readData2 = zip.read(srcname)
16 readData1 = zip.read("another.name")
17 zip.close()
Tim Petersa19a1682001-03-29 04:36:09 +000018
Fred Drake3d9091e2001-03-26 15:49:24 +000019 if readData1 != srccontents or readData2 != srccontents:
20 raise TestFailed, "Written data doesn't equal read data."
21
22
Guido van Rossum368f04a2000-04-10 13:23:04 +000023try:
Fred Drake3d9091e2001-03-26 15:49:24 +000024 fp = open(srcname, "wb") # Make a source file with some lines
Fred Drake004d5e62000-10-23 17:22:08 +000025 for i in range(0, 1000):
26 fp.write("Test of zipfile line %d.\n" % i)
27 fp.close()
Tim Petersa19a1682001-03-29 04:36:09 +000028
Fred Drake3d9091e2001-03-26 15:49:24 +000029 fp = open(srcname, "rb")
30 writtenData = fp.read()
31 fp.close()
Tim Petersa19a1682001-03-29 04:36:09 +000032
Fred Drake3d9091e2001-03-26 15:49:24 +000033 for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
34 zipTest(file, zipfile.ZIP_STORED, writtenData)
Guido van Rossum368f04a2000-04-10 13:23:04 +000035
Fred Drake3d9091e2001-03-26 15:49:24 +000036 for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
37 zipTest(file, zipfile.ZIP_DEFLATED, writtenData)
Guido van Rossum368f04a2000-04-10 13:23:04 +000038
Guido van Rossum368f04a2000-04-10 13:23:04 +000039finally:
Fred Drake004d5e62000-10-23 17:22:08 +000040 if os.path.isfile(srcname): # Remove temporary files
41 os.unlink(srcname)
42 if os.path.isfile(zipname):
43 os.unlink(zipname)
Fred Drake6e7e4852001-02-28 05:34:16 +000044
Tim Peters7d3bad62001-04-04 18:56:49 +000045
46# This test checks that the ZipFile constructor closes the file object
47# it opens if there's an error in the file. If it doesn't, the traceback
48# holds a reference to the ZipFile object and, indirectly, the file object.
49# On Windows, this causes the os.unlink() call to fail because the
50# underlying file is still open. This is SF bug #412214.
51#
52fp = open(srcname, "w")
53fp.write("this is not a legal zip file\n")
54fp.close()
55try:
56 zf = zipfile.ZipFile(srcname)
57except zipfile.BadZipfile:
58 os.unlink(srcname)
59
60
Fred Drake6e7e4852001-02-28 05:34:16 +000061# make sure we don't raise an AttributeError when a partially-constructed
62# ZipFile instance is finalized; this tests for regression on SF tracker
63# bug #403871.
64try:
65 zipfile.ZipFile(srcname)
66except IOError:
67 # The bug we're testing for caused an AttributeError to be raised
68 # when a ZipFile instance was created for a file that did not
69 # exist; the .fp member was not initialized but was needed by the
70 # __del__() method. Since the AttributeError is in the __del__(),
71 # it is ignored, but the user should be sufficiently annoyed by
72 # the message on the output that regression will be noticed
73 # quickly.
74 pass
75else:
76 raise TestFailed("expected creation of readable ZipFile without\n"
77 " a file to raise an IOError.")