Skip to content
Snippets Groups Projects
Commit 9d0270ae authored by Stefaan's avatar Stefaan
Browse files

Issue #79 use original resource filename in temp filename

parent 1a14b39b
No related branches found
No related tags found
1 merge request!94Use original resource filename in temp filename
......@@ -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)
......@@ -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
......
......@@ -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
......
......@@ -19,6 +19,7 @@ class PathTests:
# 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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment