Created
October 20, 2014 23:02
-
-
Save teeranan/46ccdd3f8dacbc061bde to your computer and use it in GitHub Desktop.
email_test1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fernando Perez wrote: | |
> | |
> Great! Many thanks for this. Please give it a bit more pounding, and | |
> I'd encourage other users of dreload to also try it out. The code is | |
> definitely far simpler than the original dreload, but since I don't | |
> understand that code too well, I'd like to tiptoe a bit on this | |
> issue. If it survives a bit of pounding and discussion, I'll | |
> definitely be glad to put it in. | |
One of the things where it differ's from the old deep_reload is that | |
when importing a submodule, say ipbug.vm, it will not reload | |
ipbug/__init__.py. I've attached another version, which tries to do just | |
that and I'm using that version currently without problems. | |
However I think there must be an even less complicated version. | |
Clearing sys.modules and 'reimporting' the module like in the following | |
code seems to work ok. | |
==== | |
import sys, bbutils.textproc.spellcheck | |
m=sys.modules.copy() | |
sys.modules.clear() | |
sys.modules['sys'] = sys | |
import bbutils.textproc.spellcheck | |
==== | |
Currently I don't have the time to investigate this further, but in a | |
week or two, I'll have another look at this. | |
- Ralf | |
-------------- next part -------------- | |
import sys | |
import __builtin__ | |
builtin_import = None # will be set to __builtin__.__import__ by reload function | |
old_modules = {} # will be set to sys.modules by reload function | |
reloaded = {} # names of reloaded modules, uses same keys as sys.modules | |
def determineParentName(globals): | |
"""determine name of the module which has called the import statement""" | |
if not globals or not globals.has_key("__name__"): | |
return None | |
pname = globals['__name__'] | |
if globals.has_key("__path__"): | |
return pname | |
if '.' in pname: | |
i = pname.rfind('.') | |
pname = pname[:i] | |
return pname | |
return None | |
def reloadModuleAndParents(name, globals=None): | |
"""for name='some.module.bar', reload some, some.module, some.module.bar. | |
Determines module, which has called the import statement, and prefers | |
module relative paths, i.e. 'import os' in module m, from m/__init__.py | |
imports m/os.py if it's there. | |
""" | |
global reloaded | |
def reloadByName(n): | |
if old_modules.has_key(n): | |
if reloaded.has_key(n): | |
return reloaded[n] | |
reloaded[mname]=1 | |
sys.modules[mname] = old_modules[mname] | |
if mname != old_modules[mname].__name__: | |
# module changed it's name. otherwise dreload(xml.sax) fails. | |
print "Module changed name:", mname, old_modules[mname].__name__ | |
return reloadModuleAndParents(old_modules[mname].__name__) | |
else: | |
print "Reloading", mname | |
return __builtin__.reload(old_modules[mname]) | |
return None | |
mods = name.split(".") | |
parent = determineParentName(globals) | |
retval = None | |
for i in range(len(mods)): | |
mname = ".".join(mods[:i+1]) | |
if parent: | |
relative = "%s.%s" % (parent, mname) | |
if old_modules.has_key(relative) and old_modules[relative]: | |
retval=reloadModuleAndParents(relative) | |
continue | |
retval=reloadByName(mname) | |
return retval | |
def my_import_hook(name, globals=None, locals=None, fromlist=None): | |
"""replacement for __builtin__.__import__ | |
reloads module 'name' if it hasn't been already reloaded | |
and then calls original __builtin__.__import__. | |
""" | |
## if fromlist: | |
## print 'Importing', fromlist, 'from module', name | |
## else: | |
## print 'Importing module', name | |
reloadModuleAndParents(name, globals) | |
return builtin_import(name, globals, locals, fromlist) | |
# Replacement for reload() | |
def reload(module, exclude=['sys', '__builtin__', '__main__']): | |
"""Recursively reload all modules used in the given module. Optionally | |
takes a list of modules to exclude from reloading. The default exclude | |
list contains sys, __main__, and __builtin__, to prevent, e.g., resetting | |
display, exception, and io hooks. | |
""" | |
global builtin_import | |
global old_modules | |
global reloaded | |
reloaded = {} | |
old_modules = sys.modules.copy() | |
sys.modules.clear() | |
for ex in exclude+list(sys.builtin_module_names): | |
if old_modules.has_key(ex) and not sys.modules.has_key(ex): | |
print "EXCLUDING", ex | |
sys.modules[ex] = old_modules[ex] | |
reloaded[ex]=1 | |
builtin_import = __builtin__.__import__ | |
__builtin__.__import__ = my_import_hook | |
try: | |
return reloadModuleAndParents(module.__name__) | |
finally: | |
# restore old values | |
__builtin__.__import__ = builtin_import | |
for m in old_modules: | |
if not sys.modules.has_key(m): | |
sys.modules[m] = old_modules[m] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment