Kaydet (Commit) 8a8d2850 authored tarafından Osvaldo Santana Neto's avatar Osvaldo Santana Neto Kaydeden (comit) Serhiy Storchaka

bpo-30441: Fix bug when modifying os.environ while iterating over it (#2409)

üst 85f64302
......@@ -697,7 +697,9 @@ class _Environ(MutableMapping):
raise KeyError(key) from None
def __iter__(self):
for key in self._data:
# list() from dict object is an atomic operation
keys = list(self._data)
for key in keys:
yield self.decodekey(key)
def __len__(self):
......
......@@ -835,6 +835,30 @@ class EnvironTests(mapping_tests.BasicTestMappingProtocol):
self.assertIs(cm.exception.args[0], missing)
self.assertTrue(cm.exception.__suppress_context__)
def _test_environ_iteration(self, collection):
iterator = iter(collection)
new_key = "__new_key__"
next(iterator) # start iteration over os.environ.items
# add a new key in os.environ mapping
os.environ[new_key] = "test_environ_iteration"
try:
next(iterator) # force iteration over modified mapping
self.assertEqual(os.environ[new_key], "test_environ_iteration")
finally:
del os.environ[new_key]
def test_iter_error_when_changing_os_environ(self):
self._test_environ_iteration(os.environ)
def test_iter_error_when_changing_os_environ_items(self):
self._test_environ_iteration(os.environ.items())
def test_iter_error_when_changing_os_environ_values(self):
self._test_environ_iteration(os.environ.values())
class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
......
......@@ -1088,6 +1088,7 @@ Fredrik Nehr
Tony Nelson
Trent Nelson
Andrew Nester
Osvaldo Santana Neto
Chad Netzer
Max Neunhöffer
Anthon van der Neut
......
Fix bug when modifying os.environ while iterating over it
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment