diff options
author | Walter Dörwald <walter@livinglogic.de> | 2009-04-25 12:48:43 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2009-04-25 12:48:43 (GMT) |
commit | 4ba801360af443813233131252100c35825288c9 (patch) | |
tree | 111b95034301e21fad155bf974c8b0498a867476 /Lib/test/support.py | |
parent | 23336083d3ad050d6389d4696147e4e0c71536cb (diff) | |
download | cpython-4ba801360af443813233131252100c35825288c9.zip cpython-4ba801360af443813233131252100c35825288c9.tar.gz cpython-4ba801360af443813233131252100c35825288c9.tar.bz2 |
Merged revisions 71875 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71875 | walter.doerwald | 2009-04-25 14:15:07 +0200 (Sa, 25 Apr 2009) | 7 lines
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().
........
Diffstat (limited to 'Lib/test/support.py')
-rw-r--r-- | Lib/test/support.py | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/Lib/test/support.py b/Lib/test/support.py index ebb3495..09e8574 100644 --- a/Lib/test/support.py +++ b/Lib/test/support.py @@ -516,30 +516,31 @@ class EnvironmentVarGuard(object): 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.items(): - 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): |