summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/__init__.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-14 19:04:26 (GMT)
committerBrett Cannon <brett@python.org>2013-06-14 19:04:26 (GMT)
commit3fe35e65034de82c45e2d8fe1ebe4a2929c68453 (patch)
tree5417c105714337f251daf3eb8be5f6762d273d99 /Lib/importlib/__init__.py
parent6f1057605b26c34b30c562a1b620984ed1211f39 (diff)
downloadcpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.zip
cpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.tar.gz
cpython-3fe35e65034de82c45e2d8fe1ebe4a2929c68453.tar.bz2
Issue #18193: Add importlib.reload(), documenting (but not
implementing in code) the deprecation of imp.reload(). Thanks to Berker Peksag for the patch.
Diffstat (limited to 'Lib/importlib/__init__.py')
-rw-r--r--Lib/importlib/__init__.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index df893cc..83de028 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -1,5 +1,5 @@
"""A pure Python implementation of import."""
-__all__ = ['__import__', 'import_module', 'invalidate_caches']
+__all__ = ['__import__', 'import_module', 'invalidate_caches', 'reload']
# Bootstrap help #####################################################
@@ -11,6 +11,7 @@ __all__ = ['__import__', 'import_module', 'invalidate_caches']
# initialised below if the frozen one is not available).
import _imp # Just the builtin component, NOT the full Python module
import sys
+import types
try:
import _frozen_importlib as _bootstrap
@@ -90,3 +91,34 @@ def import_module(name, package=None):
break
level += 1
return _bootstrap._gcd_import(name[level:], package, level)
+
+
+_RELOADING = {}
+
+
+def reload(module):
+ """Reload the module and return it.
+
+ The module must have been successfully imported before.
+
+ """
+ if not module or not isinstance(module, types.ModuleType):
+ raise TypeError("reload() argument must be module")
+ name = module.__name__
+ if name not in sys.modules:
+ msg = "module {} not in sys.modules"
+ raise ImportError(msg.format(name), name=name)
+ if name in _RELOADING:
+ return _RELOADING[name]
+ _RELOADING[name] = module
+ try:
+ parent_name = name.rpartition('.')[0]
+ if parent_name and parent_name not in sys.modules:
+ msg = "parent {!r} not in sys.modules"
+ raise ImportError(msg.format(parentname), name=parent_name)
+ return module.__loader__.load_module(name)
+ finally:
+ try:
+ del _RELOADING[name]
+ except KeyError:
+ pass