Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Doc/library/fnmatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <path-like object>` for
the *name* parameter.


.. function:: filter(names, pat)

Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ dis

(Contributed by Bénédikt Tran in :gh:`123165`.)

fnmatch
-------

* Added support for :term:`path-like objects <path-like object>` for
the *name* parameter of :func:`fnmatch.fnmatchcase`.

(Contributed by Bénédikt Tran in :gh:`123215`.)

fractions
---------

Expand Down
2 changes: 1 addition & 1 deletion Lib/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
30 changes: 17 additions & 13 deletions Lib/test/test_fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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*')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added support for :term:`path-like objects <path-like object>` for the
*name* parameter of :func:`fnmatch.fnmatchcase`. Patch by Bénédikt Tran.