Skip to content

Commit

Permalink
Issue python#79 use original resource filename in temp filename
Browse files Browse the repository at this point in the history
  • Loading branch information
soxofaan committed Mar 15, 2020
1 parent 1a14b39 commit 9d0270a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
6 changes: 3 additions & 3 deletions importlib_resources/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def fallback_resources(spec):


@contextlib.contextmanager
def _tempfile(reader):
def _tempfile(reader, suffix=''):
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
# blocks due to the need to close the temporary file to work on Windows
# properly.
fd, raw_path = tempfile.mkstemp()
fd, raw_path = tempfile.mkstemp(suffix=suffix)
try:
os.write(fd, reader())
os.close(fd)
Expand All @@ -63,7 +63,7 @@ def as_file(path):
Given a Traversable object, return that object as a
path on the local file system in a context manager.
"""
with _tempfile(path.read_bytes) as local:
with _tempfile(path.read_bytes, suffix=path.name) as local:
yield local


Expand Down
2 changes: 1 addition & 1 deletion importlib_resources/_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def _path_from_reader(reader, resource):
yield Path(reader.resource_path(norm_resource))
return
opener_reader = reader.open_resource(norm_resource)
with _common._tempfile(opener_reader.read) as res:
with _common._tempfile(opener_reader.read, suffix=norm_resource) as res:
yield res


Expand Down
1 change: 1 addition & 0 deletions importlib_resources/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_reading(self):
# Test also implicitly verifies the returned object is a pathlib.Path
# instance.
with resources.path(self.data, 'utf-8.file') as path:
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
# pathlib.Path.read_text() was introduced in Python 3.5.
with path.open('r', encoding='utf-8') as file:
text = file.read()
Expand Down

0 comments on commit 9d0270a

Please sign in to comment.