Index: Lib/zipfile.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/zipfile.py,v retrieving revision 1.10 diff -c -r1.10 zipfile.py *** Lib/zipfile.py 2001/03/29 04:36:08 1.10 --- Lib/zipfile.py 2001/04/04 18:38:05 *************** *** 186,194 **** --- 186,208 ---- else: # file is not a zip file, just append fp.seek(0, 2) else: + if not self._filePassed: + self.fp.close() + self.fp = None raise RuntimeError, 'Mode must be "r", "w" or "a"' def _GetContents(self): + """Read the directory, making sure we close the file if the format + is bad.""" + try: + self._RealGetContents() + except BadZipfile: + if not self._filePassed: + self.fp.close() + self.fp = None + raise + + def _RealGetContents(self): """Read in the table of contents for the ZIP file.""" fp = self.fp fp.seek(-22, 2) # Start of end-of-archive record Index: Lib/test/test_zipfile.py =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/test_zipfile.py,v retrieving revision 1.5 diff -c -r1.5 test_zipfile.py *** Lib/test/test_zipfile.py 2001/03/29 04:36:09 1.5 --- Lib/test/test_zipfile.py 2001/04/04 18:38:05 *************** *** 42,47 **** --- 42,63 ---- if os.path.isfile(zipname): os.unlink(zipname) + + # This test checks that the ZipFile constructor closes the file object + # it opens if there's an error in the file. If it doesn't, the traceback + # holds a reference to the ZipFile object and, indirectly, the file object. + # On Windows, this causes the os.unlink() call to fail because the + # underlying file is still open. This is SF bug #412214. + # + fp = open(srcname, "w") + fp.write("this is not a legal zip file\n") + fp.close() + try: + zf = zipfile.ZipFile(srcname) + except zipfile.BadZipfile: + os.unlink(srcname) + + # make sure we don't raise an AttributeError when a partially-constructed # ZipFile instance is finalized; this tests for regression on SF tracker # bug #403871.