mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
Sf bug [ #412214 ] ZipFile constructor leaves files open.
This applies the patch Fred Drake created to fix it. I'm checking it in since I had to apply the patch anyway in order to test its behavior on Windows.
This commit is contained in:
parent
42fc7ccdac
commit
7d3bad66e4
2 changed files with 30 additions and 0 deletions
|
@ -42,6 +42,22 @@ finally:
|
||||||
if os.path.isfile(zipname):
|
if os.path.isfile(zipname):
|
||||||
os.unlink(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
|
# make sure we don't raise an AttributeError when a partially-constructed
|
||||||
# ZipFile instance is finalized; this tests for regression on SF tracker
|
# ZipFile instance is finalized; this tests for regression on SF tracker
|
||||||
# bug #403871.
|
# bug #403871.
|
||||||
|
|
|
@ -186,9 +186,23 @@ class ZipFile:
|
||||||
else: # file is not a zip file, just append
|
else: # file is not a zip file, just append
|
||||||
fp.seek(0, 2)
|
fp.seek(0, 2)
|
||||||
else:
|
else:
|
||||||
|
if not self._filePassed:
|
||||||
|
self.fp.close()
|
||||||
|
self.fp = None
|
||||||
raise RuntimeError, 'Mode must be "r", "w" or "a"'
|
raise RuntimeError, 'Mode must be "r", "w" or "a"'
|
||||||
|
|
||||||
def _GetContents(self):
|
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."""
|
"""Read in the table of contents for the ZIP file."""
|
||||||
fp = self.fp
|
fp = self.fp
|
||||||
fp.seek(-22, 2) # Start of end-of-archive record
|
fp.seek(-22, 2) # Start of end-of-archive record
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue