[Python-checkins] cpython (3.3): issue #18698: ensure importlib.reload() returns the module out of sys.modules.
eric.snow
python-checkins at python.org
Thu Aug 15 02:18:31 CEST 2013
http://hg.python.org/cpython/rev/e22e7268e58a
changeset: 85173:e22e7268e58a
branch: 3.3
user: Eric Snow <ericsnowcurrently at gmail.com>
date: Wed Aug 14 18:03:34 2013 -0600
summary:
issue #18698: ensure importlib.reload() returns the module out of sys.modules.
files:
Lib/imp.py | 4 +++-
Lib/test/test_imp.py | 17 +++++++++++++++++
Misc/NEWS | 2 ++
3 files changed, 22 insertions(+), 1 deletions(-)
diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -268,7 +268,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_imp.py b/Lib/test/test_imp.py
--- a/Lib/test/test_imp.py
+++ b/Lib/test/test_imp.py
@@ -5,6 +5,7 @@
import shutil
import sys
from test import support
+from test.test_importlib import util
import unittest
import warnings
@@ -285,6 +286,22 @@
with self.assertRaisesRegex(ImportError, 'html'):
imp.reload(parser)
+ def test_module_replaced(self):
+ # see #18698
+ def code():
+ 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 = imp.reload(module)
+ actual = sys.modules['top_level']
+ self.assertEqual(actual.spam, 3)
+ self.assertEqual(reloaded.spam, 3)
+
class PEP3147Tests(unittest.TestCase):
"""Tests of PEP 3147."""
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -202,6 +202,8 @@
- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X
with port None or "0" and flags AI_NUMERICSERV.
+- Issue #18698: Ensure imp.reload() returns the module out of sys.modules.
+
- Issue #18080: When building a C extension module on OS X, if the compiler
is overriden with the CC environment variable, use the new compiler as
the default for linking if LDSHARED is not also overriden. This restores
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list