Skip to content

warnings: adding new filter resets prior existing "once" filters #153128

Description

@clark165

Bug report

Bug description:

If a filter is defined with action="once", the occurrence tracking is reset each time a new filter is defined. This can override the ability to effectively use action="once" if the filter manipulation occurs in a library which the user cannot control (e.g. matplotlib).

For example, the following code will only output a warning once (i.e. works as intended):

import warnings

warnings.filterwarnings("once", "test warning", UserWarning)
warnings.warn("test warning", UserWarning)
warnings.warn("test warning", UserWarning)

However, simply inserting an unrelated filter prior to the second warning will cause both to be output:

warnings.filterwarnings("once", "test warning", UserWarning)
warnings.warn("test warning", UserWarning)

warnings.filterwarnings("ignore", "something unrelated")
warnings.warn("test warning", UserWarning)

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions