Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 1 | import zipfile, os |
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 | |
| 7 | try: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 8 | fp = open(srcname, "w") # Make a source file with some lines |
| 9 | for i in range(0, 1000): |
| 10 | fp.write("Test of zipfile line %d.\n" % i) |
| 11 | fp.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 12 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 13 | zip = zipfile.ZipFile(zipname, "w") # Create the ZIP archive |
| 14 | zip.write(srcname, srcname) |
| 15 | zip.write(srcname, "another.name") |
| 16 | zip.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 17 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 18 | zip = zipfile.ZipFile(zipname, "r") # Read the ZIP archive |
| 19 | zip.read("another.name") |
| 20 | zip.read(srcname) |
| 21 | zip.close() |
Guido van Rossum | 368f04a | 2000-04-10 13:23:04 +0000 | [diff] [blame] | 22 | finally: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 23 | if os.path.isfile(srcname): # Remove temporary files |
| 24 | os.unlink(srcname) |
| 25 | if os.path.isfile(zipname): |
| 26 | os.unlink(zipname) |
Fred Drake | 6e7e485 | 2001-02-28 05:34:16 +0000 | [diff] [blame] | 27 | |
| 28 | # make sure we don't raise an AttributeError when a partially-constructed |
| 29 | # ZipFile instance is finalized; this tests for regression on SF tracker |
| 30 | # bug #403871. |
| 31 | try: |
| 32 | zipfile.ZipFile(srcname) |
| 33 | except IOError: |
| 34 | # The bug we're testing for caused an AttributeError to be raised |
| 35 | # when a ZipFile instance was created for a file that did not |
| 36 | # exist; the .fp member was not initialized but was needed by the |
| 37 | # __del__() method. Since the AttributeError is in the __del__(), |
| 38 | # it is ignored, but the user should be sufficiently annoyed by |
| 39 | # the message on the output that regression will be noticed |
| 40 | # quickly. |
| 41 | pass |
| 42 | else: |
| 43 | raise TestFailed("expected creation of readable ZipFile without\n" |
| 44 | " a file to raise an IOError.") |