diff options
Diffstat (limited to 'Lib/importlib/test')
-rw-r--r-- | Lib/importlib/test/source/util.py | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/Lib/importlib/test/source/util.py b/Lib/importlib/test/source/util.py index ca04edf..2b945c5 100644 --- a/Lib/importlib/test/source/util.py +++ b/Lib/importlib/test/source/util.py @@ -42,8 +42,8 @@ def create_modules(*names): that contains the name passed into the context manager that caused the creation of the module. - All files are created in a temporary directory specified by - tempfile.gettempdir(). This directory is inserted at the beginning of + All files are created in a temporary directory returned by + tempfile.mkdtemp(). This directory is inserted at the beginning of sys.path. When the context manager exits all created files (source and bytecode) are explicitly deleted. @@ -55,8 +55,10 @@ def create_modules(*names): source = 'attr = {0!r}' created_paths = [] mapping = {} + state_manager = None + uncache_manager = None try: - temp_dir = tempfile.gettempdir() + temp_dir = tempfile.mkdtemp() mapping['.root'] = temp_dir import_names = set() for name in names: @@ -85,13 +87,8 @@ def create_modules(*names): state_manager.__enter__() yield mapping finally: - state_manager.__exit__(None, None, None) - uncache_manager.__exit__(None, None, None) - # Reverse the order for path removal to unroll directory creation. - for path in reversed(created_paths): - if file_path.endswith('.py'): - support.unlink(path) - support.unlink(path + 'c') - support.unlink(path + 'o') - else: - os.rmdir(path) + if state_manager is not None: + state_manager.__exit__(None, None, None) + if uncache_manager is not None: + uncache_manager.__exit__(None, None, None) + support.rmtree(temp_dir) |