mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
[3.13] gh-135801: Add tests for filtering warnings by module (GH-140240) (GH-140247)
(cherry picked from commit fbf0843e39)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
05405152a9
commit
cd6b272f7a
1 changed files with 79 additions and 0 deletions
|
|
@ -237,6 +237,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 original_warnings.catch_warnings(record=True,
|
||||
module=self.module) as w:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue