From dd5273fee9739ed36d5a458ffb543bf278eb9762 Mon Sep 17 00:00:00 2001 From: "gergely.erdelyi" Date: Thu, 4 Feb 2010 20:58:38 +0000 Subject: [PATCH] init.py: modules loaded by user scripts are now unloaded when the script finishes. No more reload() needed. Thanks to cbwhiz for the idea. --- python/init.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/python/init.py b/python/init.py index 620b08a..64c89e5 100644 --- a/python/init.py +++ b/python/init.py @@ -55,7 +55,11 @@ def runscript(script): """ addscriptpath(script) watchdog.reset() + # Save the argv, path, I/O and base modules for later cleanup argv = sys.argv + path = sys.path + stdio = [sys.stdin, sys.stdout, sys.stderr] + basemodules = sys.modules.copy() sys.argv = [ script ] # Adjust the __file__ path in the globals we pass to the script g = globals() @@ -66,8 +70,15 @@ def runscript(script): except: raise finally: + # Restore the globals to the state before the script was run 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(): version1 = "Python interpreter version %d.%d.%d %s (serial %d)" % sys.version_info