init.py: modules loaded by user scripts are now unloaded when the script finishes. No more reload() needed. Thanks to cbwhiz for the idea.

This commit is contained in:
gergely.erdelyi 2010-02-04 20:58:38 +00:00
parent e1298031ac
commit dd5273fee9

View File

@ -55,7 +55,11 @@ def runscript(script):
""" """
addscriptpath(script) addscriptpath(script)
watchdog.reset() watchdog.reset()
# Save the argv, path, I/O and base modules for later cleanup
argv = sys.argv argv = sys.argv
path = sys.path
stdio = [sys.stdin, sys.stdout, sys.stderr]
basemodules = sys.modules.copy()
sys.argv = [ script ] sys.argv = [ script ]
# Adjust the __file__ path in the globals we pass to the script # Adjust the __file__ path in the globals we pass to the script
g = globals() g = globals()
@ -66,8 +70,15 @@ def runscript(script):
except: except:
raise raise
finally: finally:
# Restore the globals to the state before the script was run
g['__file__'] = old__file__ g['__file__'] = old__file__
sys.argv = argv
sys.path = path
sys.stdin, sys.stdout, sys.stderr = stdio
# Clean up the modules loaded by the script
for module in sys.modules.keys():
if not module in basemodules:
del(sys.modules[module])
def print_banner(): def print_banner():
version1 = "Python interpreter version %d.%d.%d %s (serial %d)" % sys.version_info version1 = "Python interpreter version %d.%d.%d %s (serial %d)" % sys.version_info