Commit a65f2ba9 authored by Ka-Ping Yee's avatar Ka-Ping Yee

Fix SF bug #417833 (pydoc HTTP reload failure) by removing from

sys.modules all submodules of a the given module/package path
when trying to reload a module.
parent 4c809b14
...@@ -254,20 +254,24 @@ def safeimport(path, forceload=0, cache={}): ...@@ -254,20 +254,24 @@ def safeimport(path, forceload=0, cache={}):
package path is specified, the module at the end of the path is returned, package path is specified, the module at the end of the path is returned,
not the package at the beginning. If the optional 'forceload' argument not the package at the beginning. If the optional 'forceload' argument
is 1, we reload the module from disk (unless it's a dynamic extension).""" is 1, we reload the module from disk (unless it's a dynamic extension)."""
try:
# If forceload is 1 and the module has been previously loaded from
# disk, we always have to reload the module. Checking the file's
# mtime isn't good enough (e.g. the module could contain a class
# that inherits from another module that has changed).
if forceload and path in sys.modules: if forceload and path in sys.modules:
# This is the only way to be sure. Checking the mtime of the file
# isn't good enough (e.g. what if the module contains a class that
# inherits from another module that has changed?).
if path not in sys.builtin_module_names: if path not in sys.builtin_module_names:
# Python never loads a dynamic extension a second time from the # Avoid simply calling reload() because it leaves names in
# same path, even if the file is changed or missing. Deleting # the currently loaded module lying around if they're not
# the entry in sys.modules doesn't help for dynamic extensions, # defined in the new source file. Instead, remove the
# so we're not even going to try to keep them up to date. # module from sys.modules and re-import. Also remove any
info = inspect.getmoduleinfo(sys.modules[path].__file__) # submodules because they won't appear in the newly loaded
if info[3] != imp.C_EXTENSION: # module's namespace if they're already in sys.modules.
cache[path] = sys.modules[path] # prevent module from clearing subs = [m for m in sys.modules if m.startswith(path + '.')]
del sys.modules[path] for key in [path] + subs:
try: # Prevent garbage collection.
cache[key] = sys.modules[key]
del sys.modules[key]
module = __import__(path) module = __import__(path)
except: except:
# Did the error occur before or after the module was found? # Did the error occur before or after the module was found?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment