GeezerVison is showing github.com — Support regular expressions in `-W` and `PYTHONWARNINGS` · Issue #134716 · python/cpython
Support regular expressions in -W and PYTHONWARNINGS #134716DescriptionBug 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
Linked PRs
Activitypicnixz commented on May 26, 2025 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) changed the title [-]regular expressions in symbol PYTHONWARNINGS don't work[/-] [+]Support regular expressions in `-W` and `PYTHONWARNINGS`[/+] on May 26, 2025garyaviv commented on May 26, 2025 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 commented on May 30, 2025 picnixz commented on May 30, 2025 Arf, my bad. I'll triage them tomorrow. serhiy-storchaka commented on May 30, 2025 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. serhiy-storchaka commented on Aug 25, 2025 #138149 implements support of regular expressions. To differentiate them from literal strings, you must surround them by forward slashes.
Using the original example:
$ PYTHONWARNINGS='ignore:This.*deadlocks:DeprecationWarning' ./python -c 'import warnings; print(warnings.filters[0])'
('ignore', re.compile('This\\.\\*deadlocks', re.IGNORECASE), <class 'DeprecationWarning'>, None, 0)
$ PYTHONWARNINGS='ignore:/This.*deadlocks/:DeprecationWarning' ./python -c 'import warnings; print(warnings.filters[0])'
('ignore', re.compile('This.*deadlocks', re.IGNORECASE), <class 'DeprecationWarning'>, None, 0)serhiy-storchaka commented on Sep 15, 2025 Previous issues which were closed after changing the documentation:
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|