summaryrefslogtreecommitdiff
path: root/Lib/importlib/_common.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2020-10-25 14:21:46 -0400
committerGitHub <noreply@github.com>2020-10-25 14:21:46 -0400
commitdf8d4c83a6e1727e766191896aeabde886979587 (patch)
treea286f28927d1cd736787880784d2c1af908b45b8 /Lib/importlib/_common.py
parentc32f2976b8f4034724c3270397aa16f38daf470f (diff)
downloadcpython-git-df8d4c83a6e1727e766191896aeabde886979587.tar.gz
bpo-41490: ``path`` and ``contents`` to aggressively close handles (#22915)
* bpo-41490: ``path`` method to aggressively close handles * Add blurb * In ZipReader.contents, eagerly evaluate the contents to release references to the zipfile. * Instead use _ensure_sequence to ensure any iterable from a reader is eagerly converted to a list if it's not already a sequence.
Diffstat (limited to 'Lib/importlib/_common.py')
-rw-r--r--Lib/importlib/_common.py5
1 files changed, 2 insertions, 3 deletions
diff --git a/Lib/importlib/_common.py b/Lib/importlib/_common.py
index b15c59eb9c..71ce6af8cc 100644
--- a/Lib/importlib/_common.py
+++ b/Lib/importlib/_common.py
@@ -88,6 +88,7 @@ def _tempfile(reader, suffix=''):
try:
os.write(fd, reader())
os.close(fd)
+ del reader
yield pathlib.Path(raw_path)
finally:
try:
@@ -97,14 +98,12 @@ def _tempfile(reader, suffix=''):
@functools.singledispatch
-@contextlib.contextmanager
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, suffix=path.name) as local:
- yield local
+ return _tempfile(path.read_bytes, suffix=path.name)
@as_file.register(pathlib.Path)