GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-30441: Fix bug when modifying os.environ while iterating over it by osantana · Pull Request #2409 · python/cpython

Conversation

@osantana

@osantana osantana commented Jun 26, 2017

Copy link
Copy Markdown
Contributor

Opening this pull request at Serhiy Storchaka's request to correct Bug #30441.

Question: Would not it be better to use tuple() instead of list() at Lib/os.py:700?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@osantana
Fix bug when modifying os.environ while iterating over it (Issue #30441)
375f977
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Jun 26, 2017
@osantana osantana changed the title Fix bug when modifying os.environ while iterating over it (Bug #30441) bpo-30441: Fix bug when modifying os.environ while iterating over it Jun 26, 2017
serhiy-storchaka
serhiy-storchaka reviewed Jun 27, 2017
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Please add also a NEWS entry in Misc/NEWS.d/next/Library and your name in Misc/ACKS.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/os.py Outdated

def __iter__(self):
for key in self._data:
data = list(self._data)

@serhiy-storchaka serhiy-storchaka Jun 27, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Either inline data or rename it to keys. Add an explaining comment about atomicity of list() from dict.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_os.py Outdated
self.assertTrue(cm.exception.__suppress_context__)

def test_iter_error_when_os_environ_changes(self):
def _iter_environ_change():

@serhiy-storchaka serhiy-storchaka Jun 27, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

You can just use iter(os.environ.items()).

It may be worth to test also other iterators: iter(os.environ) and iter(os.environ.values()).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_os.py Outdated

# add a new key in os.environ mapping
new_key = "__{}".format(key)
os.environ[new_key] = value

@serhiy-storchaka serhiy-storchaka Jun 27, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Don't forget to remove the added key.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka

serhiy-storchaka commented Jun 27, 2017

Copy link
Copy Markdown
Member

Question: Would not it be better to use tuple() instead of list() at Lib/os.py:700?

No. Creating a tuple from a dict is not atomic now.

osantana reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

osantana added 4 commits June 29, 2017 14:16
@osantana
Rename data->keys and explain about list() atomicity
cac586f
@osantana
Improve tests and add more use-cases
6bbfe8c
@osantana
Add entry in NEWS.d/Library and author name in Misc/ACKS
2190931
@osantana
Fix typo in test names and improve var names
3b15ce0
serhiy-storchaka
serhiy-storchaka reviewed Jun 30, 2017
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Add an entry in Misc/NEWS.d.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/ACKS Outdated
Gennadiy Zlobin
Doug Zongker
Peter Åstrand
Osvaldo Santana Neto

@serhiy-storchaka serhiy-storchaka Jun 30, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@osantana osantana Jul 1, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@osantana osantana Jul 1, 2017

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_os.py Outdated
next(iterator) # force iteration over modified mapping
self.assertEqual(os.environ[new_key], "test_environ_iteration")

del os.environ[new_key]

@serhiy-storchaka serhiy-storchaka Jun 30, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

osantana reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
serhiy-storchaka
serhiy-storchaka reviewed Jun 30, 2017
View reviewed changes
Comment thread Lib/test/test_os.py Outdated

def test_iter_error_when_changing_os_environ_values(self):
iter_environ_values = iter(os.environ.values())
self._test_environ_iteration(iter_environ_values)

@serhiy-storchaka serhiy-storchaka Jun 30, 2017

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

osantana reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
osantana added 2 commits July 1, 2017 13:27
@osantana
Refactor tests to improve readability
a061788
@osantana
Fix surname alphabetical order in Misc/ACKS
4c3f4da
serhiy-storchaka
serhiy-storchaka approved these changes Jul 1, 2017
View reviewed changes
@serhiy-storchaka serhiy-storchaka added needs backport to 3.5 type-bug An unexpected behavior, bug, or error labels Jul 1, 2017
@serhiy-storchaka
serhiy-storchaka merged commit 8a8d285 into python:master Jul 1, 2017
serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull request Jul 4, 2017
@osantana @serhiy-storchaka
[3.6] bpo-30441: Fix bug when modifying os.environ while iterating ov…
68f001e
…er it (pythonGH-2409).

(cherry picked from commit 8a8d285)
@bedevere-bot

bedevere-bot commented Jul 4, 2017

Copy link
Copy Markdown

GH-2556 is a backport of this pull request to the 3.6 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.6 label Jul 4, 2017
serhiy-storchaka pushed a commit to serhiy-storchaka/cpython that referenced this pull request Jul 4, 2017
@osantana @serhiy-storchaka
[3.5] bpo-30441: Fix bug when modifying os.environ while iterating ov…
2bef835
…er it (pythonGH-2409).

(cherry picked from commit 8a8d285)
@bedevere-bot bedevere-bot removed the needs backport to 3.5 label Jul 4, 2017
@bedevere-bot

bedevere-bot commented Jul 4, 2017

Copy link
Copy Markdown

GH-2557 is a backport of this pull request to the 3.5 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added a commit that referenced this pull request Jul 4, 2017
@serhiy-storchaka
[3.6] bpo-30441: Fix bug when modifying os.environ while iterating ov…
bebd2cf
…er it (GH-2409). (#2556)

(cherry picked from commit 8a8d285)
serhiy-storchaka added a commit that referenced this pull request Jul 4, 2017
@serhiy-storchaka
[3.5] bpo-30441: Fix bug when modifying os.environ while iterating ov…
1a3bc55
…er it (GH-2409). (#2557)

(cherry picked from commit 8a8d285)
@boegel boegel mentioned this pull request Dec 21, 2019
Python RuntimeError during sanity check with python3 easybuilders/easybuild-framework#3138
Closed
@gukoff gukoff mentioned this pull request Oct 1, 2025
os.environ.clear() has a quadratic time complexity #139482
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@osantana @serhiy-storchaka @bedevere-bot @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.