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

This commit is contained in:
Osvaldo Santana Neto 2017-07-01 14:34:45 -03:00 committed by Serhiy Storchaka
parent 85f643023f
commit 8a8d28501f
4 changed files with 29 additions and 1 deletions

View file

@ -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()."""