diff --git a/importlib_resources/_common.py b/importlib_resources/_common.py index 5f41c26..d825219 100644 --- a/importlib_resources/_common.py +++ b/importlib_resources/_common.py @@ -5,7 +5,6 @@ import itertools import os import pathlib -import tempfile import types import warnings from typing import Optional, Union, cast @@ -127,6 +126,9 @@ def _tempfile( *, _os_remove=os.remove, ): + # Deferred for performance. + import tempfile + # 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. @@ -180,24 +182,18 @@ def _(path): yield path -@contextlib.contextmanager -def _temp_path(dir: tempfile.TemporaryDirectory): - """ - Wrap tempfile.TemporaryDirectory to return a pathlib object. - """ - with dir as result: - yield pathlib.Path(result) - - @contextlib.contextmanager def _temp_dir(path): """ Given a traversable dir, recursively replicate the whole tree to the file system in a context manager. """ + # Deferred for performance. + import tempfile + assert path.is_dir() - with _temp_path(tempfile.TemporaryDirectory()) as temp_dir: - yield _write_contents(temp_dir, path) + with tempfile.TemporaryDirectory() as temp_dir: + yield _write_contents(pathlib.Path(temp_dir), path) def _write_contents(target, source):