Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,9 @@ def __delitem__(self, key):
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):
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,31 @@ def test_key_type(self):
self.assertIs(cm.exception.args[0], missing)
self.assertTrue(cm.exception.__suppress_context__)

def _test_environ_iteration(self, iterator):
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"

next(iterator) # force iteration over modified mapping
self.assertEqual(os.environ[new_key], "test_environ_iteration")

del os.environ[new_key]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new key should be removed even if the test is failed, otherwise it could affect other tests. Use try...finally.


def test_iter_error_when_changing_os_environ(self):
iter_environ = iter(os.environ)
self._test_environ_iteration(iter_environ)

def test_iter_error_when_changing_os_environ_items(self):
iter_environ_items = iter(os.environ.items())
self._test_environ_iteration(iter_environ_items)

def test_iter_error_when_changing_os_environ_values(self):
iter_environ_values = iter(os.environ.values())
self._test_environ_iteration(iter_environ_values)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why repeat "iter environ values" so much times? Why not inline the iter_environ_values variable?



class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1760,3 +1760,4 @@ Jelle Zijlstra
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Osvaldo Santana Neto
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Names are ordered in alphabetical order. You name presumably should be under the letter N.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure? I didn't see an alphabetical order, so, I assumed I should put my name at the end of file...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! I see now!... It's alphabetical order of surname (not the first name)

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug when modifying os.environ while iterating over it