diff options
author | Walter Dörwald <walter@livinglogic.de> | 2009-04-25 12:15:07 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2009-04-25 12:15:07 (GMT) |
commit | a145317be9989bcb17f9631af7e2fc3d0e628669 (patch) | |
tree | 372cc24098212e52da14b58282b889b126fa149b /Lib | |
parent | 162641a24cf2228246a9b8d5e0ca415a32444d5f (diff) | |
download | cpython-a145317be9989bcb17f9631af7e2fc3d0e628669.zip cpython-a145317be9989bcb17f9631af7e2fc3d0e628669.tar.gz cpython-a145317be9989bcb17f9631af7e2fc3d0e628669.tar.bz2 |
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')
-rw-r--r-- | Lib/test/test_support.py | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 5973e07..c1b1c69 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -532,30 +532,32 @@ 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.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): |