Skip to content

Commit 638baca

Browse files
Only test against filenames with ".py" stripped.
1 parent 9cbe5ea commit 638baca

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

Doc/library/warnings.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,13 @@ Available Functions
488488

489489
*module*, if supplied, should be the module name.
490490
If no module is passed, the module regular expression in
491-
:ref:`warnings filter <warning-filter>` will be tested against the filename
492-
with ``/__init__.py`` and ``.py`` (and ``.pyw`` on Windows) stripped and
493-
against the module names constructed from the path components starting
494-
from all parent directories.
495-
For example, when filename is ``'/path/to/package/module.py'``, it will
496-
be tested against ``'/path/to/package/module'``,
497-
``'path.to.package.module'``, ``'to.package.module'``
498-
``'package.module'`` and ``'module'``.
491+
:ref:`warnings filter <warning-filter>` will be tested against the module
492+
names constructed from the path components starting from all parent
493+
directories (with ``/__init__.py``, ``.py`` and, on Windows, ``.pyw``
494+
stripped) and against the filename with ``.py`` stripped.
495+
For example, when the filename is ``'/path/to/package/module.py'``, it will
496+
be tested against ``'path.to.package.module'``, ``'to.package.module'``
497+
``'package.module'``, ``'module'``, and ``'/path/to/package/module'``.
499498

500499
*registry*, if supplied, should be the ``__warningregistry__`` dictionary
501500
of the module.

Doc/whatsnew/3.15.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ warnings
608608
argument is passed.
609609
It now tests the module regular expression in the warnings filter not only
610610
against the filename with ``.py`` stripped, but also against module names
611-
constructed starting from different parent directories of the filename.
612-
Strip also ``.pyw`` (on Windows) and ``/__init__.py``.
611+
constructed starting from different parent directories of the filename
612+
(with ``/__init__.py``, ``.py`` and, on Windows, ``.pyw`` stripped).
613613
(Contributed by Serhiy Storchaka in :gh:`135801`.)
614614

615615

Lib/_py_warnings.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -526,27 +526,22 @@ def _match_filename(pattern, filename, *, MS_WINDOWS=(sys.platform == 'win32')):
526526
if filename[0] == '<' and filename[-1] == '>':
527527
return pattern.match(filename) is not None
528528

529+
is_py = (filename[-3:].lower() == '.py'
530+
if MS_WINDOWS else
531+
filename.endswith('.py'))
532+
if is_py:
533+
filename = filename[:-3]
534+
if pattern.match(filename): # for backward compatibility
535+
return True
529536
if MS_WINDOWS:
530-
if filename[-12:].lower() in (r'\__init__.py', '/__init__.py'):
531-
if pattern.match(filename[:-3]): # without '.py'
532-
return True
533-
filename = filename[:-12]
534-
elif filename[-3:].lower() == '.py':
535-
filename = filename[:-3]
536-
elif filename[-4:].lower() == '.pyw':
537+
if is_py and filename[-9:].lower() in (r'\__init__', '/__init__'):
538+
filename = filename[:-9]
539+
elif not is_py and filename[-4:].lower() == '.pyw':
537540
filename = filename[:-4]
538-
if pattern.match(filename):
539-
return True
540-
filename = filename.replace('\\', '/')
541+
filename = filename.replace('\\', '.')
541542
else:
542-
if filename.endswith('/__init__.py'):
543-
if pattern.match(filename[:-3]): # without '.py'
544-
return True
545-
filename = filename[:-12]
546-
elif filename.endswith('.py'):
547-
filename = filename[:-3]
548-
if pattern.match(filename):
549-
return True
543+
if is_py and filename.endswith('/__init__'):
544+
filename = filename[:-9]
550545
filename = filename.replace('/', '.')
551546
i = 0
552547
while True:

Lib/test/test_warnings/__init__.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ def test_filter_module(self):
289289
if MS_WINDOWS:
290290
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module.PY', 42)
291291
self.assertEqual(len(w), 3)
292-
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module/__INIT__.PY', 42)
293-
self.assertEqual(len(w), 4)
294-
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module.PYW', 42)
295-
self.assertEqual(len(w), 5)
292+
with self.assertRaises(UserWarning):
293+
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module/__init__.py', 42)
294+
with self.assertRaises(UserWarning):
295+
self.module.warn_explicit('msg', UserWarning, r'/path/to/package/module.pyw', 42)
296296
with self.assertRaises(UserWarning):
297297
self.module.warn_explicit('msg', UserWarning, r'\path\to\package\module', 42)
298298

@@ -314,16 +314,14 @@ def test_filter_module(self):
314314
self.assertEqual(len(w), 2)
315315
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.PY', 42)
316316
self.assertEqual(len(w), 3)
317-
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module\__INIT__.PY', 42)
318-
self.assertEqual(len(w), 4)
319-
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.PYW', 42)
320-
self.assertEqual(len(w), 5)
317+
with self.assertRaises(UserWarning):
318+
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module.pyw', 42)
321319
with self.assertRaises(UserWarning):
322320
self.module.warn_explicit('msg', UserWarning, r'C:\PATH\TO\PACKAGE\MODULE', 42)
323321
with self.assertRaises(UserWarning):
324322
self.module.warn_explicit('msg', UserWarning, r'C:/path/to/package/module', 42)
325323
with self.assertRaises(UserWarning):
326-
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module\__init__', 42)
324+
self.module.warn_explicit('msg', UserWarning, r'C:\path\to\package\module\__init__.py', 42)
327325

328326
with self.module.catch_warnings(record=True) as w:
329327
self.module.simplefilter('error')

Misc/NEWS.d/next/Library/2025-10-16-17-17-20.gh-issue-135801.faH3fa.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ Improve filtering by module in :func:`warnings.warn_explicit` if no *module*
22
argument is passed. It now tests the module regular expression in the
33
warnings filter not only against the filename with ``.py`` stripped, but
44
also against module names constructed starting from different parent
5-
directories of the filename. Strip also ``.pyw`` (on Windows) and
5+
directories of the filename (with ``/__init__.py``, ``.py`` and, on Windows,
6+
``.pyw`` stripped).
67
``/__init__.py``.

0 commit comments

Comments
 (0)