mirror of
https://github.com/python/cpython.git
synced 2024-11-30 18:51:15 +01:00
df8d4c83a6
* 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.
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import zipfile
|
|
import pathlib
|
|
from . import abc
|
|
|
|
|
|
class FileReader(abc.TraversableResources):
|
|
def __init__(self, loader):
|
|
self.path = pathlib.Path(loader.path).parent
|
|
|
|
def resource_path(self, resource):
|
|
"""
|
|
Return the file system path to prevent
|
|
`resources.path()` from creating a temporary
|
|
copy.
|
|
"""
|
|
return str(self.path.joinpath(resource))
|
|
|
|
def files(self):
|
|
return self.path
|
|
|
|
|
|
class ZipReader(abc.TraversableResources):
|
|
def __init__(self, loader, module):
|
|
_, _, name = module.rpartition('.')
|
|
self.prefix = loader.prefix.replace('\\', '/') + name + '/'
|
|
self.archive = loader.archive
|
|
|
|
def open_resource(self, resource):
|
|
try:
|
|
return super().open_resource(resource)
|
|
except KeyError as exc:
|
|
raise FileNotFoundError(exc.args[0])
|
|
|
|
def is_resource(self, path):
|
|
# workaround for `zipfile.Path.is_file` returning true
|
|
# for non-existent paths.
|
|
target = self.files().joinpath(path)
|
|
return target.is_file() and target.exists()
|
|
|
|
def files(self):
|
|
return zipfile.Path(self.archive, self.prefix)
|