gh-135801: Add tests for filtering warnings by module (GH-140240)
Some checks failed
Tests / Change detection (push) Waiting to run
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Docs (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / Sanitizers (push) Blocked by required conditions
Tests / Ubuntu SSL tests with AWS-LC (push) Blocked by required conditions
Tests / Android (aarch64) (push) Blocked by required conditions
Tests / Android (x86_64) (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run
JIT / Interpreter (Debug) (push) Has been cancelled
JIT / aarch64-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / aarch64-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / i686-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / i686-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / aarch64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / aarch64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / x86_64-pc-windows-msvc/msvc (Release) (push) Has been cancelled
JIT / x86_64-pc-windows-msvc/msvc (Debug) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Release) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Release) (push) Has been cancelled
JIT / x86_64-apple-darwin/clang (Debug) (push) Has been cancelled
JIT / x86_64-unknown-linux-gnu/gcc (Debug) (push) Has been cancelled
JIT / Free-Threaded (Debug) (push) Has been cancelled
JIT / JIT without optimizations (Debug) (push) Has been cancelled

This commit is contained in:
Serhiy Storchaka 2025-10-17 14:21:39 +03:00 committed by GitHub
parent 67c98ad8ef
commit fbf0843e39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -241,6 +241,85 @@ class FilterTests(BaseTest):
42)
self.assertEqual(len(w), 0)
def test_filter_module(self):
MS_WINDOWS = (sys.platform == 'win32')
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module=r'package\.module\z')
self.module.warn_explicit('msg', UserWarning, 'filename', 42,
module='package.module')
self.assertEqual(len(w), 1)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, '/path/to/package/module', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, '/path/to/package/module.py', 42)
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module='package')
self.module.warn_explicit('msg', UserWarning, 'filename', 42,
module='package.module')
self.assertEqual(len(w), 1)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, 'filename', 42,
module='other.package.module')
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, '/path/to/otherpackage/module.py', 42)
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module=r'/path/to/package/module\z')
self.module.warn_explicit('msg', UserWarning, '/path/to/package/module', 42)
self.assertEqual(len(w), 1)
self.module.warn_explicit('msg', UserWarning, '/path/to/package/module.py', 42)
self.assertEqual(len(w), 2)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, '/PATH/TO/PACKAGE/MODULE', 42)
if MS_WINDOWS:
if self.module is py_warnings:
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module.PY', 42)
self.assertEqual(len(w), 3)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module/__init__.py', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module.pyw', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'\path\to\package\module', 42)
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module=r'/path/to/package/__init__\z')
self.module.warn_explicit('msg', UserWarning, '/path/to/package/__init__.py', 42)
self.assertEqual(len(w), 1)
self.module.warn_explicit('msg', UserWarning, '/path/to/package/__init__', 42)
self.assertEqual(len(w), 2)
if MS_WINDOWS:
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module=r'C:\\path\\to\\package\\module\z')
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module', 42)
self.assertEqual(len(w), 1)
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.py', 42)
self.assertEqual(len(w), 2)
if self.module is py_warnings:
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.PY', 42)
self.assertEqual(len(w), 3)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.pyw', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'C:\PATH\TO\PACKAGE\MODULE', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'C:/path/to/package/module', 42)
with self.assertRaises(UserWarning):
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module\__init__.py', 42)
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter('error')
self.module.filterwarnings('always', module=r'<unknown>\z')
self.module.warn_explicit('msg', UserWarning, '', 42)
self.assertEqual(len(w), 1)
def test_module_globals(self):
with self.module.catch_warnings(record=True) as w:
self.module.simplefilter("always", UserWarning)