From 710cf0b8352b9e17646164f8178b0843df854e83 Mon Sep 17 00:00:00 2001 From: David Sveningsson Date: Mon, 13 Jun 2011 21:29:30 +0200 Subject: [PATCH] Restore __main__ module after loading and running the plugin. If the plugin raises an exception (including SystemExit) the __main__ module is not properly loaded and it's __dict__ is lost. E.g. this causes errors when using any global variable in planet.py. I don't think is is a good idea to keep the plugin in memory (as __main__) anyway. --- planet/shell/plugin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/planet/shell/plugin.py b/planet/shell/plugin.py index dd943801..2bd8f5c1 100644 --- a/planet/shell/plugin.py +++ b/planet/shell/plugin.py @@ -4,6 +4,7 @@ def run(script, doc, output_file=None, options={}): """ process an Python script using imp """ save_sys = (sys.stdin, sys.stdout, sys.stderr, sys.argv) + save_main = sys.modules['__main__'] plugin_stdout = StringIO() plugin_stderr = StringIO() @@ -54,6 +55,7 @@ def run(script, doc, output_file=None, options={}): finally: # restore system state sys.stdin, sys.stdout, sys.stderr, sys.argv = save_sys + sys.modules['__main__'] = save_main # log anything sent to stderr if plugin_stderr.getvalue():