diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2013-12-10 02:59:10 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2013-12-10 02:59:10 (GMT) |
commit | c1e7c747f92a27f1f2bb392dbbfbd2817591f44f (patch) | |
tree | 5b704ab3437269d9811aee36233d897500a24f76 /Lib/test | |
parent | fc25d629aba73a0da3fa0dd9de640a117fd76c29 (diff) | |
download | cpython-c1e7c747f92a27f1f2bb392dbbfbd2817591f44f.zip cpython-c1e7c747f92a27f1f2bb392dbbfbd2817591f44f.tar.gz cpython-c1e7c747f92a27f1f2bb392dbbfbd2817591f44f.tar.bz2 |
Issue 19851: Fix a regression in reloading submodules.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_importlib/test_api.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index c6c2d47..792c82d 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -4,6 +4,7 @@ frozen_init, source_init = util.import_importlib('importlib') frozen_util, source_util = util.import_importlib('importlib.util') frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') +from contextlib import contextmanager import os.path import sys from test import support @@ -11,6 +12,37 @@ import types import unittest +@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 util.uncache(name, *conflicts): + with support.DirsOnSysPath(cwd): + frozen_init.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 + + +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 + + class ImportModuleTests: """Test importlib.import_module.""" @@ -246,6 +278,32 @@ class FindSpecTests: # None is returned upon failure to find a loader. self.assertIsNone(self.init.find_spec('nevergoingtofindthismodule')) + def test_find_submodule(self): + name = 'spam' + subname = 'ham' + with temp_module(name, pkg=True) as pkg_dir: + fullname, _ = submodule(name, subname, pkg_dir) + spec = self.init.find_spec(fullname, [pkg_dir]) + self.assertIsNot(spec, None) + self.assertNotIn(name, sorted(sys.modules)) + # Ensure successive calls behave the same. + spec_again = self.init.find_spec(fullname, [pkg_dir]) + # XXX Once #19927 is resolved, uncomment this line. + #self.assertEqual(spec_again, spec) + + def test_find_submodule_missing_path(self): + name = 'spam' + subname = 'ham' + with temp_module(name, pkg=True) as pkg_dir: + fullname, _ = submodule(name, subname, pkg_dir) + spec = self.init.find_spec(fullname) + self.assertIs(spec, None) + self.assertNotIn(name, sorted(sys.modules)) + # Ensure successive calls behave the same. + spec = self.init.find_spec(fullname) + self.assertIs(spec, None) + + class Frozen_FindSpecTests(FindSpecTests, unittest.TestCase): init = frozen_init machinery = frozen_machinery @@ -410,6 +468,16 @@ class ReloadTests: self.assertEqual(loader.path, init_path) self.assertEqual(ns, expected) + def test_reload_submodule(self): + # See #19851. + name = 'spam' + subname = 'ham' + with temp_module(name, pkg=True) as pkg_dir: + fullname, _ = submodule(name, subname, pkg_dir) + ham = self.init.import_module(fullname) + reloaded = self.init.reload(ham) + self.assertIs(reloaded, ham) + class Frozen_ReloadTests(ReloadTests, unittest.TestCase): init = frozen_init |