From c9451566a7d7e45859d0f8018b5c1e6e57ed52c3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 17 Oct 2025 14:21:39 +0300 Subject: [PATCH 1/2] gh-135801: Add tests for filtering warnings by module (GH-140240) (cherry picked from commit fbf0843e39e01ec8a8295f6475065b08053f13dd) Co-authored-by: Serhiy Storchaka --- Lib/test/test_warnings/__init__.py | 79 ++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 57016d5a292cd9..7d1b599b7244cc 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -237,6 +237,85 @@ def test_once(self): 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'\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: From 99f0271b07fabbc919c7e1bef37153391a4845f6 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 21 Oct 2025 20:55:56 +0300 Subject: [PATCH 2/2] Fix regexps. --- Lib/test/test_warnings/__init__.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py index 7d1b599b7244cc..bcbea1b2932027 100644 --- a/Lib/test/test_warnings/__init__.py +++ b/Lib/test/test_warnings/__init__.py @@ -241,7 +241,7 @@ 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.filterwarnings('always', module=r'package\.module\Z') self.module.warn_explicit('msg', UserWarning, 'filename', 42, module='package.module') self.assertEqual(len(w), 1) @@ -264,7 +264,7 @@ def test_filter_module(self): 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.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) @@ -284,7 +284,7 @@ def test_filter_module(self): 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.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) @@ -293,7 +293,7 @@ def test_filter_module(self): 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.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) @@ -312,7 +312,7 @@ def test_filter_module(self): with self.module.catch_warnings(record=True) as w: self.module.simplefilter('error') - self.module.filterwarnings('always', module=r'\z') + self.module.filterwarnings('always', module=r'\Z') self.module.warn_explicit('msg', UserWarning, '', 42) self.assertEqual(len(w), 1)