Skip to content

Fix nested caplog.filtering early removal - #14284

Merged
RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
oaksprout:fix-nested-caplog-filtering
Aug 26, 2026
Merged

Fix nested caplog.filtering early removal#14284
RonnyPfannschmidt merged 11 commits into
pytest-dev:mainfrom
oaksprout:fix-nested-caplog-filtering

Conversation

@oaksprout

Copy link
Copy Markdown
Contributor

Closes #14189. Nested caplog.filtering usage was eagerly removing filters when the inner context manager exited, even if the filter was already present from the outer context manager. This PR ensures the filter is only removed if it wasn't already present when the context manager was entered.

Closes pytest-dev#14189 by checking if the filter is already present before adding or removing it.
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Mar 13, 2026
themavik

This comment was marked as spam.

Comment thread testing/logging/test_fixture.py Outdated
@bluetech bluetech added backport 9.1.x apply to PRs at any point; backports the changes to the 9.1.x branch and removed backport 9.0.x labels Jun 14, 2026
Comment thread src/_pytest/logging.py Outdated
RonnyPfannschmidt and others added 3 commits August 26, 2026 10:03
The makepyfile/makeini docstring reflow is not this repo's style: nothing
in pyproject.toml asks for it and ruff format accepts the original layout.
It doubled the diff and would conflict on backport.

Co-Authored-By: Claude Opus 5 <ai@anthropic.com>
Co-Authored-By: Claude Code <ai@anthropic.com>
cast(logging.Filter, some_function) lies to the type checker. Subclass
logging.Filter like test_with_statement_filtering right above does.

Co-Authored-By: Claude Opus 5 <ai@anthropic.com>
Co-Authored-By: Claude Code <ai@anthropic.com>
Per @ncoghlan's review suggestion: skip the try/finally entirely when the
filter is already present, so the add and its matching remove live in the
same branch instead of behind two separate guards. No behaviour change.

Co-Authored-By: Claude Opus 5 <ai@anthropic.com>
Co-Authored-By: Claude Code <ai@anthropic.com>
@RonnyPfannschmidt
RonnyPfannschmidt merged commit c7f9e75 into pytest-dev:main Aug 26, 2026
36 checks passed
@patchback

patchback Bot commented Aug 26, 2026

Copy link
Copy Markdown

Backport to 9.1.x: 💚 backport PR created

✅ Backport PR branch: patchback/backports/9.1.x/c7f9e75c5d3b04bf96e8389a9eac6d98da2be8f4/pr-14284

Backported as #14939

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

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

Labels

backport 9.1.x apply to PRs at any point; backports the changes to the 9.1.x branch bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested caplog.filtering usage may remove filters early

5 participants