Bug 1315785 - Restore environment variables after running command; r?glandium draft
authorGregory Szorc <gps@mozilla.com>
Tue, 08 Nov 2016 12:15:07 -0800
changeset 435490 4f32973b6a7ce8355e467c280790147e4c7bfb82
parent 435356 aea5b4c3d165dcde027b3b6551b146a56748e9be
child 435491 51d7cc8c5521fade2297bceb08cfabcdda7900b4
push id35066
push userbmo:gps@mozilla.com
push dateTue, 08 Nov 2016 20:22:43 +0000
reviewersglandium
bugs1315785
milestone52.0a1
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
python/mach/mach/main.py
--- 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: