Repository navigation
Restore the test environment on exceptional exit - #11267
Open
akoroderdeniteenfeld-sudo wants to merge 2 commits into
Open
akoroderdeniteenfeld-sudo wants to merge 2 commits into
akoroderdeniteenfeld-sudo wants to merge 2 commits into
Conversation
…nment Signed-off-by: akoroderdeniteenfeld-sudo <360661402+akoroderdeniteenfeld-sudo@users.noreply.github.com>
akoroderdeniteenfeld-sudo
marked this pull request as ready for review
November 24, 2026 12:42
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/helpers.py" line_range="251" />
<code_context>
-
- os.environ.clear()
- os.environ.update(original_environ)
+ try:
+ yield
+ finally:
</code_context>
<issue_to_address>
**Failed setup corrupts later tests**
When `clear=True` and `environ` contains a value that `os.environ` rejects, `os.environ.clear()` and `os.environ.update(environ)` run before the `try`, so a rejected value leaves the environment cleared or partially updated and later tests see corrupted state.
Include the clear/update setup inside the `try`/`finally` so the original environment is restored even when applying `environ` raises.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: tests/helpers.py:251
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Relates-to: #3155
When a block inside
isolated_environmentraises, the exception skips the cleanup afteryield. An error while applying environment variables can also leave the environment partially changed.The
finallyclause now restores the original environment if either setup or the context body raises. The regression tests check that the original exception propagates and the environment is restored. The two setup-error cases fail before the follow-up change and pass with it, both with and withoutclear=True.Validation on macOS with Python 3.11: 3,369 passed, 33 skipped, 2 xfailed. The focused suite passed all 74 tests. The 7
isolated_environmentcases also passed on Windows Python 3.11 under CrossOver. mypy, Ruff lint and formatting, andgit diff --checkpassed.Pull Request Check List