Skip to content

Commit 1471d9b

Browse files
committed
Merge branch 'feature/79-original-suffix' of https://gitlab.com/Stefaan/importlib_resources
2 parents c2ad098 + 9d0270a commit 1471d9b

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

importlib_resources/_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def fallback_resources(spec):
4040

4141

4242
@contextlib.contextmanager
43-
def _tempfile(reader):
43+
def _tempfile(reader, suffix=''):
4444
# Not using tempfile.NamedTemporaryFile as it leads to deeper 'try'
4545
# blocks due to the need to close the temporary file to work on Windows
4646
# properly.
47-
fd, raw_path = tempfile.mkstemp()
47+
fd, raw_path = tempfile.mkstemp(suffix=suffix)
4848
try:
4949
os.write(fd, reader())
5050
os.close(fd)
@@ -63,7 +63,7 @@ def as_file(path):
6363
Given a Traversable object, return that object as a
6464
path on the local file system in a context manager.
6565
"""
66-
with _tempfile(path.read_bytes) as local:
66+
with _tempfile(path.read_bytes, suffix=path.name) as local:
6767
yield local
6868

6969

importlib_resources/_py3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _path_from_reader(reader, resource):
161161
yield Path(reader.resource_path(norm_resource))
162162
return
163163
opener_reader = reader.open_resource(norm_resource)
164-
with _common._tempfile(opener_reader.read) as res:
164+
with _common._tempfile(opener_reader.read, suffix=norm_resource) as res:
165165
yield res
166166

167167

importlib_resources/tests/test_path.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def test_reading(self):
1919
# Test also implicitly verifies the returned object is a pathlib.Path
2020
# instance.
2121
with resources.path(self.data, 'utf-8.file') as path:
22+
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
2223
# pathlib.Path.read_text() was introduced in Python 3.5.
2324
with path.open('r', encoding='utf-8') as file:
2425
text = file.read()

0 commit comments

Comments
 (0)