[Python-checkins] r71875 - in python/trunk: Lib/test/test_support.py Misc/NEWS

walter.doerwald python-checkins at python.org
Sat Apr 25 14:15:07 CEST 2009


Author: walter.doerwald
Date: Sat Apr 25 14:15:07 2009
New Revision: 71875

Log:
Issue #5837: Certain sequences of calls to set() and unset() for
support.EnvironmentVarGuard objects restored the environment variables
incorrectly on __exit__.

Fix this by recording the initial value of each environment variable on the
first access in set() or unset().


Modified:
   python/trunk/Lib/test/test_support.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Lib/test/test_support.py
==============================================================================
--- python/trunk/Lib/test/test_support.py	(original)
+++ python/trunk/Lib/test/test_support.py	Sat Apr 25 14:15:07 2009
@@ -532,30 +532,32 @@
     a context manager."""
 
     def __init__(self):
-        self._environ = os.environ
-        self._unset = set()
-        self._reset = dict()
+        self._changed = {}
 
     def set(self, envvar, value):
-        if envvar not in self._environ:
-            self._unset.add(envvar)
-        else:
-            self._reset[envvar] = self._environ[envvar]
-        self._environ[envvar] = value
+        # Remember the initial value on the first access
+        if envvar not in self._changed:
+            self._changed[envvar] = os.environ.get(envvar)
+        os.environ[envvar] = value
 
     def unset(self, envvar):
-        if envvar in self._environ:
-            self._reset[envvar] = self._environ[envvar]
-            del self._environ[envvar]
+        # Remember the initial value on the first access
+        if envvar not in self._changed:
+            self._changed[envvar] = os.environ.get(envvar)
+        if envvar in os.environ:
+            del os.environ[envvar]
 
     def __enter__(self):
         return self
 
     def __exit__(self, *ignore_exc):
-        for envvar, value in self._reset.iteritems():
-            self._environ[envvar] = value
-        for unset in self._unset:
-            del self._environ[unset]
+        for (k, v) in self._changed.items():
+            if v is None:
+                if k in os.environ:
+                    del os.environ[k]
+            else:
+                os.environ[k] = v
+
 
 class TransientResource(object):
 

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Apr 25 14:15:07 2009
@@ -875,6 +875,10 @@
 
 - Issue #5083: New 'gui' resource for regrtest.
 
+- Issue #5837: Certain sequences of calls to set() and unset() for
+  support.EnvironmentVarGuard objects restored the environment variables
+  incorrectly on __exit__.
+
 
 What's New in Python 2.6 final
 ==============================


More information about the Python-checkins mailing list