Fix a scoping issue where an UnboundLocalError was triggered if a

lazy-loaded module was already in sys.modules.
This commit is contained in:
Brett Cannon 2016-06-25 10:47:53 -07:00
parent 559ad5d401
commit e92dc9c23d
3 changed files with 18 additions and 4 deletions

View File

@ -1,6 +1,8 @@
import importlib import importlib
from importlib import abc from importlib import abc
from importlib import util from importlib import util
import sys
import types
import unittest import unittest
from . import util as test_util from . import util as test_util
@ -122,10 +124,18 @@ class LazyLoaderTests(unittest.TestCase):
self.assertFalse(hasattr(module, '__name__')) self.assertFalse(hasattr(module, '__name__'))
def test_module_substitution_error(self): 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 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__ module.__name__

View File

@ -13,6 +13,10 @@ Core and Builtins
Library Library
------- -------
- Fix a scoping issue in importlib.util.LazyLoader which triggered an
UnboundLocalError when lazy-loading a module that was already put into
sys.modules.
- Issue #27079: Fixed curses.ascii functions isblank(), iscntrl() and ispunct(). - Issue #27079: Fixed curses.ascii functions isblank(), iscntrl() and ispunct().
- Issue #26754: Some functions (compile() etc) accepted a filename argument - Issue #26754: Some functions (compile() etc) accepted a filename argument