Skip to content

Commit bce7564

Browse files
committed
This approach begins to work, but it requires as_file to be aware of the internals of zipfile.Path.
1 parent 45a160c commit bce7564

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

importlib_resources/_common.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def _tempfile(reader, suffix=''):
9393
try:
9494
os.write(fd, reader())
9595
os.close(fd)
96-
del reader
9796
yield Path(raw_path)
9897
finally:
9998
try:
@@ -109,11 +108,15 @@ def as_file(path):
109108
Given a Traversable object, return that object as a
110109
path on the local file system in a context manager.
111110
"""
112-
reader = path.read_bytes
113-
with _tempfile(reader, suffix=path.name) as local:
114-
# release the handle to the path and reader
115-
del reader
116-
del path
111+
content = path.read_bytes()
112+
name = path.name
113+
114+
def reader():
115+
return content
116+
117+
del path.root
118+
del path
119+
with _tempfile(reader, suffix=name) as local:
117120
yield local
118121

119122

importlib_resources/_py3.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,18 @@ def path(
9696
return (
9797
_path_from_reader(reader, resource)
9898
if reader else
99-
_common.as_file(
100-
_common.files(package).joinpath(_common.normalize_path(resource)))
99+
_fallback_path(package, resource)
101100
)
102101

103102

103+
@contextmanager
104+
def _fallback_path(package, resource):
105+
files = _common.files(package).joinpath(_common.normalize_path(resource))
106+
with _common.as_file(files) as res:
107+
del files
108+
yield res
109+
110+
104111
@contextmanager
105112
def _path_from_reader(reader, resource):
106113
norm_resource = _common.normalize_path(resource)

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ skip_missing_interpreters = True
66

77
[testenv]
88
commands =
9-
!cov,!diffcov: python -m unittest discover
10-
cov,diffcov: python -m coverage run {[coverage]rc} -m unittest discover {posargs}
9+
!cov,!diffcov: python -m unittest {posargs:discover}
10+
cov,diffcov: python -m coverage run {[coverage]rc} -m unittest {posargs:posargs}
1111
cov,diffcov: python -m coverage combine {[coverage]rc}
1212
cov: python -m coverage html {[coverage]rc}
1313
cov: python -m coverage xml {[coverage]rc}

0 commit comments

Comments
 (0)