Skip to content

Restore the test environment on exceptional exit - #11267

Open
akoroderdeniteenfeld-sudo wants to merge 2 commits into
python-poetry:mainfrom
akoroderdeniteenfeld-sudo:fix/restore-test-environment
Open

akoroderdeniteenfeld-sudo wants to merge 2 commits into
python-poetry:mainfrom
akoroderdeniteenfeld-sudo:fix/restore-test-environment

Conversation

@akoroderdeniteenfeld-sudo

@akoroderdeniteenfeld-sudo akoroderdeniteenfeld-sudo commented Nov 24, 2026 •

Copy link
Copy Markdown

Relates-to: #3155

When a block inside isolated_environment raises, the exception skips the cleanup after yield. An error while applying environment variables can also leave the environment partially changed.

The finally clause 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 without clear=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_environment cases also passed on Windows Python 3.11 under CrossOver. mypy, Ruff lint and formatting, and git diff --check passed.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. No user-facing change.

…nment

Signed-off-by: akoroderdeniteenfeld-sudo <360661402+akoroderdeniteenfeld-sudo@users.noreply.github.com>
@akoroderdeniteenfeld-sudo
akoroderdeniteenfeld-sudo marked this pull request as ready for review November 24, 2026 12:42

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/helpers.py Resolved
chrikrah

This comment was marked as spam.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants