summaryrefslogtreecommitdiffstats
path: root/Lib/imp.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2012-04-29 18:38:11 (GMT)
committerBrett Cannon <brett@python.org>2012-04-29 18:38:11 (GMT)
commit62228dbd6c3b7be4199756c42a21429664c053e6 (patch)
tree3d63ba456c809bd1f0eae2d9dee4bf8ae7dad31b /Lib/imp.py
parent1fc3ec91cc3e86035179db3e476ae7cd36ca6716 (diff)
downloadcpython-62228dbd6c3b7be4199756c42a21429664c053e6.zip
cpython-62228dbd6c3b7be4199756c42a21429664c053e6.tar.gz
cpython-62228dbd6c3b7be4199756c42a21429664c053e6.tar.bz2
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Thanks to Eric Snow for the patch.
Diffstat (limited to 'Lib/imp.py')
-rw-r--r--Lib/imp.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/Lib/imp.py b/Lib/imp.py
index 2abd7af..03077b6 100644
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -6,7 +6,7 @@ functionality over this module.
"""
# (Probably) need to stay in _imp
-from _imp import (lock_held, acquire_lock, release_lock, reload,
+from _imp import (lock_held, acquire_lock, release_lock,
load_dynamic, get_frozen_object, is_frozen_package,
init_builtin, init_frozen, is_builtin, is_frozen,
_fix_co_filename)
@@ -207,3 +207,34 @@ def find_module(name, path=None):
encoding = tokenize.detect_encoding(file.readline)[0]
file = open(file_path, mode, encoding=encoding)
return file, file_path, (suffix, mode, type_)
+
+
+_RELOADING = {}
+
+def reload(module):
+ """Reload the module and return it.
+
+ The module must have been successfully imported before.
+
+ """
+ if not module or type(module) != type(sys):
+ 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
+