Bug 1315785 - Restore environment variables after running command; r?glandium
Currently, environment variables set when running mach commands will
propagate after the command is finished. This can allow unwanted state
to bleed through.
This likely isn't an issue today, but isolating state during code
execution is generally a good practice. So do that.
MozReview-Commit-ID: AdaomGub5EF
--- a/python/mach/mach/main.py
+++ b/python/mach/mach/main.py
@@ -325,16 +325,18 @@ To see more help for a specific command,
orig_stdin = sys.stdin
orig_stdout = sys.stdout
orig_stderr = sys.stderr
sys.stdin = stdin
sys.stdout = stdout
sys.stderr = stderr
+ orig_env = dict(os.environ)
+
try:
if stdin.encoding is None:
sys.stdin = codecs.getreader('utf-8')(stdin)
if stdout.encoding is None:
sys.stdout = codecs.getwriter('utf-8')(stdout)
if stderr.encoding is None:
@@ -357,16 +359,19 @@ To see more help for a specific command,
exc_type, exc_value, exc_tb = sys.exc_info()
stack = traceback.extract_tb(exc_tb)
self._print_exception(sys.stdout, exc_type, exc_value, stack)
return 1
finally:
+ os.environ.clear()
+ os.environ.update(orig_env)
+
sys.stdin = orig_stdin
sys.stdout = orig_stdout
sys.stderr = orig_stderr
def _run(self, argv):
# Load settings as early as possible so things in dispatcher.py
# can use them.
for provider in Registrar.settings_providers: