Skip to content

Commit f077169

Browse files
committed
Remove unnecessary context managers.
1 parent bce7564 commit f077169

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

importlib_resources/_common.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def _tempfile(reader, suffix=''):
9393
try:
9494
os.write(fd, reader())
9595
os.close(fd)
96+
del reader
9697
yield Path(raw_path)
9798
finally:
9899
try:
@@ -102,22 +103,12 @@ def _tempfile(reader, suffix=''):
102103

103104

104105
@singledispatch
105-
@contextlib.contextmanager
106106
def as_file(path):
107107
"""
108108
Given a Traversable object, return that object as a
109109
path on the local file system in a context manager.
110110
"""
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:
120-
yield local
111+
return _tempfile(path.read_bytes, suffix=path.name)
121112

122113

123114
@as_file.register(Path)

importlib_resources/_py3.py

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

102103

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-
111104
@contextmanager
112105
def _path_from_reader(reader, resource):
113106
norm_resource = _common.normalize_path(resource)

0 commit comments

Comments
 (0)