Skip to content

Stop raises context managers from keeping caught exceptions alive - #15481

Draft
RonnyPfannschmidt wants to merge 6 commits into
mainfrom
claude/project-thread-c5c8ba
Draft

RonnyPfannschmidt wants to merge 6 commits into
mainfrom
claude/project-thread-c5c8ba

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Requested by Ronny · project thread

🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.

A pytest.raises() object used as a parametrize value kept its last caught exception, traceback and the test frame's locals alive for the whole session. Reusing one object for nested, recursive or concurrent blocks filled the wrong ExceptionInfo or crashed. Each raises object now tracks its active blocks in its own context variable. The accidentally public excinfo attribute is deprecated: it still works with a warning, and pytest clears it after teardown for parametrized tests. The raises context managers also support async with now. Tests cover the leak, every reuse case, async with and the deprecation.

Fixes #13410

🤖 Generated with Claude Code

https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9

The ExceptionInfo of an active ``with`` block now lives in a context
variable instead of on the context manager, so a pytest.raises() object
that outlives its block (e.g. a parametrize value held by the mark) no
longer pins the traceback and the test frame's locals. This also makes
nested, recursive and concurrent use of one object fill the right
ExceptionInfo.

The accidentally public ``excinfo`` attribute is deprecated (removal in
pytest 10); it still returns the last ExceptionInfo, which pytest drops
after the teardown of tests that receive the object as a parameter.

Fixes #13410

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 28, 2026
@RonnyPfannschmidt RonnyPfannschmidt self-assigned this Nov 28, 2026
Replace the module-wide tuple of (context manager, ExceptionInfo) pairs
with one ContextVar per raises object, holding a linked chain of that
object's active blocks. Exiting resets the token, so the variable leaves
the context again after the outermost block. The variable is dropped on
copy and pickle, which keeps raises objects copyable as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
A context copied inside a block (e.g. a task created there) or a block
exited from another context can keep the block object referenced after
the block is done. Empty it on exit and skip emptied blocks, so those
references no longer pin the traceback and frame locals. Use one
constant name for the per-object ContextVar.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9
Exiting a block in a thread or asyncio task other than the one that
entered it (e.g. an async generator whose setup and teardown run in
different tasks) now uses that block when it is the object's only open
block, and warns. It raises a RuntimeError that says why when the exit is
ambiguous or has no matching enter, instead of an internal AssertionError.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014GZsRDtMqtcrVaRE39xyz9

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

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The documented pattern for parametrizing conditional raising leaks a lot of memory

2 participants