summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2016-06-25 17:47:53 (GMT)
committerBrett Cannon <brett@python.org>2016-06-25 17:47:53 (GMT)
commite92dc9c23da8cb11f922f28353e6a58928aa5c71 (patch)
treee69b676b8236c0adde0f45fb2ee79c458771f178 /Lib/test/test_importlib
parent559ad5d401229399e7bc887b62a87d9f69206fa6 (diff)
downloadcpython-e92dc9c23da8cb11f922f28353e6a58928aa5c71.zip
cpython-e92dc9c23da8cb11f922f28353e6a58928aa5c71.tar.gz
cpython-e92dc9c23da8cb11f922f28353e6a58928aa5c71.tar.bz2
Fix a scoping issue where an UnboundLocalError was triggered if a
lazy-loaded module was already in sys.modules.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/test_lazy.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/Lib/test/test_importlib/test_lazy.py b/Lib/test/test_importlib/test_lazy.py
index 774b7a4..cc383c2 100644
--- a/Lib/test/test_importlib/test_lazy.py
+++ b/Lib/test/test_importlib/test_lazy.py
@@ -1,6 +1,8 @@
import importlib
from importlib import abc
from importlib import util
+import sys
+import types
import unittest
from . import util as test_util
@@ -122,12 +124,20 @@ class LazyLoaderTests(unittest.TestCase):
self.assertFalse(hasattr(module, '__name__'))
def test_module_substitution_error(self):
- source_code = 'import sys; sys.modules[__name__] = 42'
- module = self.new_module(source_code)
with test_util.uncache(TestingImporter.module_name):
- with self.assertRaises(ValueError):
+ fresh_module = types.ModuleType(TestingImporter.module_name)
+ sys.modules[TestingImporter.module_name] = fresh_module
+ module = self.new_module()
+ with self.assertRaisesRegex(ValueError, "substituted"):
module.__name__
+ def test_module_already_in_sys(self):
+ with test_util.uncache(TestingImporter.module_name):
+ module = self.new_module()
+ sys.modules[TestingImporter.module_name] = module
+ # Force the load; just care that no exception is raised.
+ module.__name__
+
if __name__ == '__main__':
unittest.main()