Skip to content

Support regular expressions in -W and PYTHONWARNINGS #134716

Open
@garyaviv

Description

@garyaviv

Bug report

Bug description:

When the PYTHONWARNINGS message field contains a special character such as . or * it gets quoted as \. or \* so it looses it's regex meaning and never matches. I tested this in 3.8 and 3.13.

PYTHONWARNINGS='ignore:This.*deadlocks:DeprecationWarning' python -c 'import warnings; print(warnings.filters[0])'

yields:

('ignore', re.compile('This\\.\\*deadlocks', re.IGNORECASE), <class 'DeprecationWarning'>, None, 0)

(note the extra backslash is a result of the print statement, doing a repr.)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Activity

picnixz

picnixz commented on May 26, 2025

@picnixz
Member

The docs say:

In -W and PYTHONWARNINGS, message is a literal string that the start of the warning message must contain (case-insensitively), ignoring any whitespace at the start or end of message

So I don't think we're meant to support regexes when using -W and PYTHONWARNINGS (but we can when we use the module directly)

added
type-featureA feature request or enhancement
and removed
type-bugAn unexpected behavior, bug, or error
on May 26, 2025
changed the title [-]regular expressions in symbol PYTHONWARNINGS don't work[/-] [+]Support regular expressions in `-W` and `PYTHONWARNINGS`[/+] on May 26, 2025
garyaviv

garyaviv commented on May 26, 2025

@garyaviv
Author

Thank you for clarification.
My fault. The docs 3.10.7 indeed says -W and PYTHONWARNINGS is a literal string. Prior versions seem to lack this language:

The warnings filter is initialized by -W options passed to the Python interpreter command line and the PYTHONWARNINGS environment variable.
<snip>
Individual warnings filters are specified as a sequence of fields separated by colons:
action:message:category:module:line
The meaning of each of these fields is as described in The Warnings Filter.

serhiy-storchaka

serhiy-storchaka commented on May 30, 2025

@serhiy-storchaka
Member

See also #78805, #79101, #86438, #88028. There may be other duplicate issues.

picnixz

picnixz commented on May 30, 2025

@picnixz
Member

Arf, my bad. I'll triage them tomorrow.

serhiy-storchaka

serhiy-storchaka commented on May 30, 2025

@serhiy-storchaka
Member

All of them were closed. AFAIK. only the documentation was updated.

But I had ideas on how to solve this problem differently, so we can leave this issue for that or reopen one of old issues if they contain more information.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Support regular expressions in `-W` and `PYTHONWARNINGS` · Issue #134716 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant