blob: 0dc080b2ffdca692f8085605ffb2050dbd5f383c [file] [log] [blame]
Guido van Rossum368f04a2000-04-10 13:23:04 +00001import zipfile, os
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
7try:
Fred Drake004d5e62000-10-23 17:22:08 +00008 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 Rossum368f04a2000-04-10 13:23:04 +000012
Fred Drake004d5e62000-10-23 17:22:08 +000013 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 Rossum368f04a2000-04-10 13:23:04 +000017
Fred Drake004d5e62000-10-23 17:22:08 +000018 zip = zipfile.ZipFile(zipname, "r") # Read the ZIP archive
19 zip.read("another.name")
20 zip.read(srcname)
21 zip.close()
Guido van Rossum368f04a2000-04-10 13:23:04 +000022finally:
Fred Drake004d5e62000-10-23 17:22:08 +000023 if os.path.isfile(srcname): # Remove temporary files
24 os.unlink(srcname)
25 if os.path.isfile(zipname):
26 os.unlink(zipname)
Fred Drake6e7e4852001-02-28 05:34:16 +000027
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.
31try:
32 zipfile.ZipFile(srcname)
33except 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
42else:
43 raise TestFailed("expected creation of readable ZipFile without\n"
44 " a file to raise an IOError.")