This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ZipFile constructor leaves files open
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: fdrake, loewis, snejsource, tim.peters
Priority: normal Keywords:

Created on 2001-03-29 15:40 by snejsource, last changed 2022-04-10 16:03 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
zipfile.patch fdrake, 2001-04-04 18:41 proposed fix
Messages (5)
msg4109 - (view) Author: Jens Quade (snejsource) Date: 2001-03-29 15:40
During the construction of a ZipFile object, a 
file is opened and assigned to self.fp.

If anything goes wrong, i.e. the file is not a zipfile,
it is not closed explicitly.  

On Windows, this does not work:

import zipfile, os

filename="test.zip" # it's a broken one
try:
  zf=zipfile.ZipFile(filename)
  zf.close()
finally:
  os.unlink(zipfile)

=> OSError: [errno 13] Permission denied "test.zip"

(on Unix, the file stays open too, but unlink doesn't
fail)
msg4110 - (view) Author: Jens Quade (snejsource) Date: 2001-03-29 16:27
Logged In: YES 
user_id=137089

os.unlink(zipfile) should read os.unlink(filename). 
msg4111 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2001-03-30 07:27
Logged In: YES 
user_id=21627

Normally, the file should be closed when the last reference
to the zipfile is dropped. In this case, the traceback holds
onto the innermost frame (i.e. the one of __init__), which
holds onto self, which is the ZipFile. I agree this should
be fixed in __init__.
msg4112 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2001-04-04 18:41
Logged In: YES 
user_id=3066

I've attached a patch and a test case, but I'm not running Windows.

Tim, can you test the patch and assign it back to me with your comments?
msg4113 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-04-04 18:57
Logged In: YES 
user_id=31435

Since I had to apply the patch to test it, and it worked 
fine, I checked it in too:

Lib/zipfile.py rev 1.11
Lib/test/test_zipfile.py rev 1.6
History
Date User Action Args
2022-04-10 16:03:54adminsetgithub: 34249
2001-03-29 15:40:36snejsourcecreate