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