diff --git a/Doc/library/fnmatch.rst b/Doc/library/fnmatch.rst index fda44923f204fc..6de1ea98dfe71f 100644 --- a/Doc/library/fnmatch.rst +++ b/Doc/library/fnmatch.rst @@ -52,7 +52,7 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`, .. function:: fnmatch(name, pat) - Test whether the filename string *name* matches the pattern string *pat*, + Test whether the filename *name* matches the pattern string *pat*, returning ``True`` or ``False``. Both parameters are case-normalized using :func:`os.path.normcase`. :func:`fnmatchcase` can be used to perform a case-sensitive comparison, regardless of whether that's standard for the @@ -71,10 +71,14 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`, .. function:: fnmatchcase(name, pat) - Test whether the filename string *name* matches the pattern string *pat*, + Test whether the filename *name* matches the pattern string *pat*, returning ``True`` or ``False``; the comparison is case-sensitive and does not apply :func:`os.path.normcase`. + .. versionchanged:: 3.14 + Added support for :term:`path-like objects ` for + the *name* parameter. + .. function:: filter(names, pat) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index a34dc639ad2a94..705b388d6bfd3a 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -126,6 +126,14 @@ dis (Contributed by Bénédikt Tran in :gh:`123165`.) +fnmatch +------- + +* Added support for :term:`path-like objects ` for + the *name* parameter of :func:`fnmatch.fnmatchcase`. + + (Contributed by Bénédikt Tran in :gh:`123215`.) + fractions --------- diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index 73acb1fe8d4106..16dfb265ab58f7 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -68,7 +68,7 @@ def fnmatchcase(name, pat): its arguments. """ match = _compile_pattern(pat) - return match(name) is not None + return match(os.fspath(name)) is not None def translate(pat): diff --git a/Lib/test/test_fnmatch.py b/Lib/test/test_fnmatch.py index 10ed496d4e2f37..819a0ba42e5fbb 100644 --- a/Lib/test/test_fnmatch.py +++ b/Lib/test/test_fnmatch.py @@ -4,6 +4,8 @@ import os import string import warnings +from pathlib import Path +from test.support.os_helper import FakePath from fnmatch import fnmatch, fnmatchcase, translate, filter @@ -12,12 +14,12 @@ class FnmatchTestCase(unittest.TestCase): def check_match(self, filename, pattern, should_match=True, fn=fnmatch): if should_match: self.assertTrue(fn(filename, pattern), - "expected %r to match pattern %r" - % (filename, pattern)) + "expected %r (%r) to match pattern %r" + % (filename, os.fspath(filename), pattern)) else: self.assertFalse(fn(filename, pattern), - "expected %r not to match pattern %r" - % (filename, pattern)) + "expected %r (%r) not to match pattern %r" + % (filename, os.fspath(filename), pattern)) def test_fnmatch(self): check = self.check_match @@ -61,15 +63,17 @@ def test_mix_bytes_str(self): def test_fnmatchcase(self): check = self.check_match - check('abc', 'abc', True, fnmatchcase) - check('AbC', 'abc', False, fnmatchcase) - check('abc', 'AbC', False, fnmatchcase) - check('AbC', 'AbC', True, fnmatchcase) - - check('usr/bin', 'usr/bin', True, fnmatchcase) - check('usr\\bin', 'usr/bin', False, fnmatchcase) - check('usr/bin', 'usr\\bin', False, fnmatchcase) - check('usr\\bin', 'usr\\bin', True, fnmatchcase) + for cls in (str, Path, FakePath): + with self.subTest(cls=cls): + check(cls('abc'), 'abc', True, fnmatchcase) + check(cls('AbC'), 'abc', False, fnmatchcase) + check(cls('abc'), 'AbC', False, fnmatchcase) + check(cls('AbC'), 'AbC', True, fnmatchcase) + + check(cls('usr/bin'), 'usr/bin', os.fspath(cls('/')) == '/', fnmatchcase) + check(cls('usr\\bin'), 'usr/bin', os.fspath(cls('\\')) == '/', fnmatchcase) + check(cls('usr/bin'), 'usr\\bin', os.fspath(cls('/')) == '\\', fnmatchcase) + check(cls('usr\\bin'), 'usr\\bin', os.fspath(cls('\\')) == '\\', fnmatchcase) def test_bytes(self): self.check_match(b'test', b'te*') diff --git a/Misc/NEWS.d/next/Library/2024-08-22-09-33-17.gh-issue-123215.MpDzeC.rst b/Misc/NEWS.d/next/Library/2024-08-22-09-33-17.gh-issue-123215.MpDzeC.rst new file mode 100644 index 00000000000000..bd5a211c614c09 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-22-09-33-17.gh-issue-123215.MpDzeC.rst @@ -0,0 +1,2 @@ +Added support for :term:`path-like objects ` for the +*name* parameter of :func:`fnmatch.fnmatchcase`. Patch by Bénédikt Tran.