Skip to content

bpo-34624: Allow regex for module passed via -W or PYTHONWARNINGS #9358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 5 additions & 3 deletions Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,18 @@ precedence over earlier ones).

Commonly used warning filters apply to either all warnings, warnings in a
particular category, or warnings raised by particular modules or packages.
Some examples::
Some examples:

.. code-block:: none

default # Show all warnings (even those ignored by default)
ignore # Ignore all warnings
error # Convert all warnings to errors
error::ResourceWarning # Treat ResourceWarning messages as errors
default::DeprecationWarning # Show DeprecationWarning messages
ignore,default:::mymodule # Only report warnings triggered by "mymodule"
error:::mymodule[.*] # Convert warnings to errors in "mymodule"
# and any subpackages of "mymodule"
error:::mymodule(\..*)? # Convert warnings to errors in "mymodule"
# and any of its submodules and subpackages


.. _default-warning-filter:
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,26 @@ def test_envvar_and_command_line(self):
self.assertEqual(stdout,
b"['ignore::DeprecationWarning', 'ignore::UnicodeWarning']")

def test_module_regex(self):
WARN_NOWHERE = 'True'
WARN_IN_MAIN = "import warnings; warnings.warn('__main__ warning');"
WARN_IN_SUBMOD = "import test.test_warnings.data.submodule_warning;"
# Check the warning is ignored by default:
assert_python_ok("-c", WARN_IN_MAIN, PYTHONWARNINGS="")
assert_python_ok("-c", WARN_IN_MAIN, PYTHONWARNINGS="ignore")
assert_python_ok("-c", WARN_IN_SUBMOD, PYTHONWARNINGS="")
assert_python_ok("-c", WARN_IN_SUBMOD, PYTHONWARNINGS="ignore")
# The submodule warning is still ignored because the pattern selects
# only to the package itself, not the submodule:
assert_python_ok("-c", WARN_IN_SUBMOD, PYTHONWARNINGS="error:::test")
# Warnings can be enabled by using wildcard regex:
assert_python_ok("-c", WARN_NOWHERE, PYTHONWARNINGS="error:::.*")
assert_python_failure("-c", WARN_IN_MAIN, PYTHONWARNINGS="error:::.*")
assert_python_failure("-c", WARN_IN_SUBMOD, PYTHONWARNINGS="error:::.*")
# Warning from submodule is raised using a more specific pattern:
assert_python_failure("-c", WARN_IN_SUBMOD,
PYTHONWARNINGS=r"error:::test.test_warnings(\..*)?")

def test_conflicting_envvar_and_command_line(self):
rc, stdout, stderr = assert_python_failure("-Werror::DeprecationWarning", "-c",
"import sys, warnings; sys.stdout.write(str(sys.warnoptions)); "
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_warnings/data/submodule_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import warnings

warnings.warn('submodule warning', DeprecationWarning)
6 changes: 1 addition & 5 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,8 @@ def _setoption(arg):
for s in parts]
action = _getaction(action)
category = _getcategory(category)
if message or module:
import re
if message:
message = re.escape(message)
if module:
module = re.escape(module) + r'\Z'
module = r'\A(' + module + r')\Z'
if lineno:
try:
lineno = int(lineno)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When passing warning filters via ``-W`` or ``PYTHONWARNINGS``, treat the
``module`` part as regex.