Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14189.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed nested :meth:`caplog.filtering() <pytest.LogCaptureFixture.filtering>` calls with the same filter removing the filter too early when the inner context exits.
11 changes: 9 additions & 2 deletions src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,15 +581,22 @@ def filtering(self, filter_: logging.Filter) -> Generator[None]:
:meth:`handler` for the 'with' statement block, and removes that filter at the
end of the block.

If the filter is already attached to the handler (e.g. from an outer
nested ``filtering()`` call), it is not added again, and will not be
removed when the inner block exits.

:param filter_: A custom :class:`logging.Filter` object.

.. versionadded:: 7.5
"""
self.handler.addFilter(filter_)
already_present = filter_ in self.handler.filters
if not already_present:
self.handler.addFilter(filter_)
try:
yield
finally:
self.handler.removeFilter(filter_)
if not already_present:
self.handler.removeFilter(filter_)


@fixture
Expand Down
21 changes: 21 additions & 0 deletions testing/logging/test_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ def filter(self, record: logging.LogRecord) -> bool:
assert unfiltered_tuple == ("test_fixture", 20, "handler call")


def test_nested_filtering_same_filter(caplog: pytest.LogCaptureFixture) -> None:
"""Nested ``caplog.filtering()`` with the same filter should not remove
the filter when the inner context exits (#14189)."""

class NoCaptureFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
return False

filter_instance = NoCaptureFilter()
with caplog.at_level(logging.INFO):
with caplog.filtering(filter_instance):
logger.info("outer before inner")
with caplog.filtering(filter_instance):
logger.info("inside inner")
logger.info("outer after inner")
logger.info("outside both")

assert len(caplog.records) == 1
assert caplog.records[0].message == "outside both"


@pytest.mark.parametrize(
"level_str,expected_disable_level",
[
Expand Down