Skip to content

Commit 8cc546b

Browse files
committed
Defer construction of the zipfile.Path and release the handle after grabbing a temporary file. Closes #110.
1 parent 8b5dd60 commit 8cc546b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

importlib_resources/_common.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ def as_file(path):
108108
Given a Traversable object, return that object as a
109109
path on the local file system in a context manager.
110110
"""
111-
with _tempfile(path.read_bytes, suffix=path.name) as local:
111+
reader = path.read_bytes
112+
with _tempfile(reader, suffix=path.name) as local:
113+
# release the handle to the path and reader
114+
del reader
115+
del path
112116
yield local
113117

114118

importlib_resources/readers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def files(self):
2222
class ZipReader(abc.TraversableResources):
2323
def __init__(self, loader, module):
2424
_, _, name = module.rpartition('.')
25-
prefix = loader.prefix.replace('\\', '/') + name + '/'
26-
self.path = ZipPath(loader.archive, prefix)
25+
self.prefix = loader.prefix.replace('\\', '/') + name + '/'
26+
self.archive = loader.archive
2727

2828
def open_resource(self, resource):
2929
try:
@@ -38,4 +38,4 @@ def is_resource(self, path):
3838
return target.is_file() and target.exists()
3939

4040
def files(self):
41-
return self.path
41+
return ZipPath(self.archive, self.prefix)

0 commit comments

Comments
 (0)