Guido van Rossum | bfce016 | 2001-04-10 14:46:39 +0000 | [diff] [blame] | 1 | import zlib # implied prerequisite |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 2 | import zipfile, os, StringIO, tempfile |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test.test_support import TestFailed |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 5 | srcname = "junk9630"+os.extsep+"tmp" |
| 6 | zipname = "junk9708"+os.extsep+"tmp" |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 7 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 8 | |
| 9 | def zipTest(f, compression, srccontents): |
| 10 | zip = zipfile.ZipFile(f, "w", compression) # Create the ZIP archive |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 11 | zip.write(srcname, "another"+os.extsep+"name") |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 12 | zip.write(srcname, srcname) |
| 13 | zip.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 14 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 15 | zip = zipfile.ZipFile(f, "r", compression) # Read the ZIP archive |
| 16 | readData2 = zip.read(srcname) |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 17 | readData1 = zip.read("another"+os.extsep+"name") |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 18 | zip.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 19 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 20 | if readData1 != srccontents or readData2 != srccontents: |
| 21 | raise TestFailed, "Written data doesn't equal read data." |
| 22 | |
| 23 | |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 24 | try: |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 25 | fp = open(srcname, "wb") # Make a source file with some lines |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 26 | for i in range(0, 1000): |
| 27 | fp.write("Test of zipfile line %d.\n" % i) |
| 28 | fp.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 29 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 30 | fp = open(srcname, "rb") |
| 31 | writtenData = fp.read() |
| 32 | fp.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame] | 33 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 34 | for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()): |
| 35 | zipTest(file, zipfile.ZIP_STORED, writtenData) |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 36 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 37 | for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()): |
| 38 | zipTest(file, zipfile.ZIP_DEFLATED, writtenData) |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 40 | finally: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 41 | if os.path.isfile(srcname): # Remove temporary files |
| 42 | os.unlink(srcname) |
| 43 | if os.path.isfile(zipname): |
| 44 | os.unlink(zipname) |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 45 | |
Tim Peters | 7d3bad6 | 2001-04-04 18:56:49 +0000 | [diff] [blame] | 46 | |
| 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 | # |
| 53 | fp = open(srcname, "w") |
| 54 | fp.write("this is not a legal zip file\n") |
| 55 | fp.close() |
| 56 | try: |
| 57 | zf = zipfile.ZipFile(srcname) |
| 58 | except zipfile.BadZipfile: |
| 59 | os.unlink(srcname) |
| 60 | |
| 61 | |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 62 | # 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. |
| 65 | try: |
| 66 | zipfile.ZipFile(srcname) |
| 67 | except 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 |
| 76 | else: |
| 77 | raise TestFailed("expected creation of readable ZipFile without\n" |
| 78 | " a file to raise an IOError.") |
Raymond Hettinger | c0fac96 | 2003-06-27 22:25:03 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | # Verify that testzip() doesn't swallow inappropriate exceptions. |
| 82 | data = StringIO.StringIO() |
| 83 | zipf = zipfile.ZipFile(data, mode="w") |
| 84 | zipf.writestr("foo.txt", "O, for a Muse of Fire!") |
| 85 | zipf.close() |
| 86 | zipf = zipfile.ZipFile(data, mode="r") |
| 87 | zipf.close() |
| 88 | try: |
| 89 | zipf.testzip() |
| 90 | except RuntimeError: |
| 91 | # This is correct; calling .read on a closed ZipFile should throw |
| 92 | # a RuntimeError, and so should calling .testzip. An earlier |
| 93 | # version of .testzip would swallow this exception (and any other) |
| 94 | # and report that the first file in the archive was corrupt. |
| 95 | pass |
| 96 | else: |
| 97 | raise TestFailed("expected calling .testzip on a closed ZipFile" |
| 98 | " to raise a RuntimeError") |
| 99 | del data, zipf |