[Python-checkins] cpython: Closes issue #18698: ensure importlib.reload() returns the module out of
eric.snow
python-checkins at python.org
Thu Aug 15 02:18:34 CEST 2013
http://hg.python.org/cpython/rev/1af087712e69
changeset: 85175:1af087712e69
user: Eric Snow <ericsnowcurrently at gmail.com>
date: Wed Aug 14 18:11:09 2013 -0600
summary:
Closes issue #18698: ensure importlib.reload() returns the module out of sys.modules.
files:
Lib/importlib/__init__.py | 4 +++-
Lib/test/test_importlib/test_api.py | 16 ++++++++++++++++
Misc/NEWS | 2 ++
3 files changed, 21 insertions(+), 1 deletions(-)
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -118,7 +118,9 @@
if parent_name and parent_name not in sys.modules:
msg = "parent {!r} not in sys.modules"
raise ImportError(msg.format(parent_name), name=parent_name)
- return module.__loader__.load_module(name)
+ module.__loader__.load_module(name)
+ # The module may have replaced itself in sys.modules!
+ return sys.modules[module.__name__]
finally:
try:
del _RELOADING[name]
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -162,6 +162,22 @@
module = importlib.import_module(mod)
importlib.reload(module)
+ def test_module_replaced(self):
+ def code():
+ import sys
+ module = type(sys)('top_level')
+ module.spam = 3
+ sys.modules['top_level'] = module
+ mock = util.mock_modules('top_level',
+ module_code={'top_level': code})
+ with mock:
+ with util.import_state(meta_path=[mock]):
+ module = importlib.import_module('top_level')
+ reloaded = importlib.reload(module)
+ actual = sys.modules['top_level']
+ self.assertEqual(actual.spam, 3)
+ self.assertEqual(reloaded.spam, 3)
+
class InvalidateCacheTests(unittest.TestCase):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,8 @@
- Issue #17867: Raise an ImportError if __import__ is not found in __builtins__.
+- Issue #18698: Ensure importlib.reload() returns the module out of sys.modules.
+
- Issue #17857: Prevent build failures with pre-3.5.0 versions of sqlite3,
such as was shipped with Centos 5 and Mac OS X 10.4.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list