Skip to content

Fix nested caplog.filtering early removal#4

Open
yashwant86 wants to merge 8 commits intomainfrom
pr-14284
Open

Fix nested caplog.filtering early removal#4
yashwant86 wants to merge 8 commits intomainfrom
pr-14284

Conversation

@yashwant86
Copy link
Copy Markdown

@yashwant86 yashwant86 commented Apr 7, 2026

Mirror of pytest-dev#14284


Summary by MergeMonkey

  • Resolved Issues:
    • Nested caplog.filtering context managers no longer prematurely remove filters that were already present before entering the context.
  • Housekeeping:
    • Code formatting: consolidated multi-line makepyfile and makeini calls to single-line syntax.

@bot-mergemonkey
Copy link
Copy Markdown

bot-mergemonkey Bot commented Apr 23, 2026

Risk AssessmentNEEDS-TESTING · ~8 min review

Focus areas: Filter membership detection logic · Nested context manager behavior · Pre-existing filter preservation

Assessment: Fixes filter lifecycle bug in logging fixture; requires validation of nested and pre-existing filter scenarios.

Walkthrough

When caplog.filtering is used as a nested context manager or when a filter is already present on the handler, the original code unconditionally removed the filter on exit, breaking the outer context. The fix checks whether the filter was already present before entry and only removes it if it was added by the context manager itself.

Changes

Files Summary
Nested Filter Removal Fix
src/_pytest/logging.py
Fixes caplog.filtering context manager to detect and preserve pre-existing filters, preventing premature removal when nesting contexts or when the same filter is already registered.
Test Coverage & Formatting
testing/logging/test_fixture.py
Adds two new test cases for nested filtering and pre-existing filter scenarios; reformats multi-line makepyfile/makeini calls to single-line syntax for consistency.
Changelog
changelog/14189.bugfix.rst
Documents bugfix for nested caplog.filtering filter removal issue.

Sequence Diagram

sequenceDiagram
    participant Test
    participant Handler
    participant Filter
    Test->>Handler: addFilter(F) [outer context]
    Handler->>Filter: register
    Test->>Handler: addFilter(F) [inner context]
    Note over Handler: F already in filters
    Test->>Handler: removeFilter(F) [inner exit]
    Note over Handler: F still present (not removed)
    Test->>Handler: removeFilter(F) [outer exit]
    Handler->>Filter: unregister
Loading

Dig Deeper With Commands

  • /review <file-path> <function-optional>
  • /chat <file-path> "<question>"
  • /roast <file-path>

Runs only when explicitly triggered.

@yashwant86
Copy link
Copy Markdown
Author

/review --force

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