Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 1 | import zipfile, os, StringIO, tempfile |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 2 | from test_support import TestFailed |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 3 | |
| 4 | srcname = "junk9630.tmp" |
| 5 | zipname = "junk9708.tmp" |
| 6 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 7 | |
| 8 | def 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 Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame^] | 13 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 14 | 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 Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame^] | 18 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 19 | if readData1 != srccontents or readData2 != srccontents: |
| 20 | raise TestFailed, "Written data doesn't equal read data." |
| 21 | |
| 22 | |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 23 | try: |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 24 | fp = open(srcname, "wb") # Make a source file with some lines |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 25 | for i in range(0, 1000): |
| 26 | fp.write("Test of zipfile line %d.\n" % i) |
| 27 | fp.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame^] | 28 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 29 | fp = open(srcname, "rb") |
| 30 | writtenData = fp.read() |
| 31 | fp.close() |
Tim Peters | a19a168 | 2001-03-29 04:36:09 +0000 | [diff] [blame^] | 32 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 33 | for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()): |
| 34 | zipTest(file, zipfile.ZIP_STORED, writtenData) |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 35 | |
Fred Drake | 3d9091e | 2001-03-26 15:49:24 +0000 | [diff] [blame] | 36 | for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()): |
| 37 | zipTest(file, zipfile.ZIP_DEFLATED, writtenData) |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 39 | finally: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 40 | if os.path.isfile(srcname): # Remove temporary files |
| 41 | os.unlink(srcname) |
| 42 | if os.path.isfile(zipname): |
| 43 | os.unlink(zipname) |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 44 | |
| 45 | # make sure we don't raise an AttributeError when a partially-constructed |
| 46 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 47 | # bug #403871. |
| 48 | try: |
| 49 | zipfile.ZipFile(srcname) |
| 50 | except IOError: |
| 51 | # The bug we're testing for caused an AttributeError to be raised |
| 52 | # when a ZipFile instance was created for a file that did not |
| 53 | # exist; the .fp member was not initialized but was needed by the |
| 54 | # __del__() method. Since the AttributeError is in the __del__(), |
| 55 | # it is ignored, but the user should be sufficiently annoyed by |
| 56 | # the message on the output that regression will be noticed |
| 57 | # quickly. |
| 58 | pass |
| 59 | else: |
| 60 | raise TestFailed("expected creation of readable ZipFile without\n" |
| 61 | " a file to raise an IOError.") |