bpo-41490: `path and contents` to aggressively close handles (#22915)

* bpo-41490: ``path`` method to aggressively close handles

* Add blurb

* In ZipReader.contents, eagerly evaluate the contents to release references to the zipfile.

* Instead use _ensure_sequence to ensure any iterable from a reader is eagerly converted to a list if it's not already a sequence.
This commit is contained in:
Jason R. Coombs 2020-10-25 14:21:46 -04:00 committed by GitHub
parent c32f2976b8
commit df8d4c83a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 16 deletions

View file

@ -22,8 +22,8 @@ class FileReader(abc.TraversableResources):
class ZipReader(abc.TraversableResources):
def __init__(self, loader, module):
_, _, name = module.rpartition('.')
prefix = loader.prefix.replace('\\', '/') + name + '/'
self.path = zipfile.Path(loader.archive, prefix)
self.prefix = loader.prefix.replace('\\', '/') + name + '/'
self.archive = loader.archive
def open_resource(self, resource):
try:
@ -38,4 +38,4 @@ class ZipReader(abc.TraversableResources):
return target.is_file() and target.exists()
def files(self):
return self.path
return zipfile.Path(self.archive, self.prefix)