diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2014-01-25 22:32:46 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2014-01-25 22:32:46 (GMT) |
commit | 6029e086911be873b2ebacb933e3df08c23084e4 (patch) | |
tree | d91555f72eacd791ecf1bdd9d169bf82ea28a49d /Lib/test/test_importlib/util.py | |
parent | 128ee220e21bd4e7eb152396ff24d879f38c5d91 (diff) | |
download | cpython-6029e086911be873b2ebacb933e3df08c23084e4.zip cpython-6029e086911be873b2ebacb933e3df08c23084e4.tar.gz cpython-6029e086911be873b2ebacb933e3df08c23084e4.tar.bz2 |
Issue 19944: Fix importlib.find_spec() so it imports parents as needed.
The function is also moved to importlib.util.
Diffstat (limited to 'Lib/test/test_importlib/util.py')
-rw-r--r-- | Lib/test/test_importlib/util.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py index a0dee6e..885cec3 100644 --- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -1,5 +1,5 @@ from contextlib import contextmanager -from importlib import util +from importlib import util, invalidate_caches import os.path from test import support import unittest @@ -46,6 +46,13 @@ def case_insensitive_tests(test): "requires a case-insensitive filesystem")(test) +def submodule(parent, name, pkg_dir, content=''): + path = os.path.join(pkg_dir, name + '.py') + with open(path, 'w') as subfile: + subfile.write(content) + return '{}.{}'.format(parent, name), path + + @contextmanager def uncache(*names): """Uncache a module from sys.modules. @@ -71,6 +78,31 @@ def uncache(*names): except KeyError: pass + +@contextmanager +def temp_module(name, content='', *, pkg=False): + conflicts = [n for n in sys.modules if n.partition('.')[0] == name] + with support.temp_cwd(None) as cwd: + with uncache(name, *conflicts): + with support.DirsOnSysPath(cwd): + invalidate_caches() + + location = os.path.join(cwd, name) + if pkg: + modpath = os.path.join(location, '__init__.py') + os.mkdir(name) + else: + modpath = location + '.py' + if content is None: + # Make sure the module file gets created. + content = '' + if content is not None: + # not a namespace package + with open(modpath, 'w') as modfile: + modfile.write(content) + yield location + + @contextmanager def import_state(**kwargs): """Context manager to manage the various importers and stored state in the |