Skip to content

Commit efd7aac

Browse files
gh-160900: support path-like objects in fnmatch filters (#160980)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 233b95c commit efd7aac

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

‎Doc/library/fnmatch.rst‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
4646
a period are not special for this module, and are matched by the ``*`` and ``?``
4747
patterns.
4848

49-
Unless stated otherwise, "filename string" and "pattern string" either refer to
50-
:class:`str` or ``ISO-8859-1`` encoded :class:`bytes` objects. Note that the
49+
Unless stated otherwise, "filename string" and "pattern string" refer to
50+
:class:`str` or ``ISO-8859-1`` encoded :class:`bytes` objects.
51+
In addition to filename strings, :func:`.filter` and :func:`.filterfalse`
52+
accept :term:`path-like objects <path-like object>` in *names*. Note that the
5153
functions documented below do not allow to mix a :class:`!bytes` pattern with
5254
a :class:`!str` filename, and vice-versa.
5355

@@ -90,6 +92,9 @@ functions: :func:`~fnmatch.fnmatch`, :func:`fnmatchcase`, :func:`.filter`,
9092
It is the same as ``[n for n in names if fnmatch(n, pat)]``,
9193
but implemented more efficiently.
9294

95+
.. versionchanged:: next
96+
Added support for path-like objects in *names*.
97+
9398

9499
.. function:: filterfalse(names, pat)
95100

@@ -100,6 +105,9 @@ functions: :func:`~fnmatch.fnmatch`, :func:`fnmatchcase`, :func:`.filter`,
100105

101106
.. versionadded:: 3.14
102107

108+
.. versionchanged:: next
109+
Added support for path-like objects in *names*.
110+
103111

104112
.. function:: translate(pat)
105113

‎Lib/fnmatch.py‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
"""
1212

1313
import functools
14-
import itertools
1514
import os
16-
import posixpath
1715
import re
1816

1917
__all__ = ["filter", "filterfalse", "fnmatch", "fnmatchcase", "translate"]
@@ -52,34 +50,22 @@ def _compile_pattern(pat):
5250

5351
def filter(names, pat):
5452
"""Construct a list from those elements of the iterable NAMES that match PAT."""
55-
result = []
5653
pat = os.path.normcase(pat)
5754
match = _compile_pattern(pat)
58-
if os.path is posixpath:
59-
# normcase on posix is NOP. Optimize it away from the loop.
60-
for name in names:
61-
if match(name):
62-
result.append(name)
63-
else:
64-
for name in names:
65-
if match(os.path.normcase(name)):
66-
result.append(name)
67-
return result
55+
return [
56+
name for name in names
57+
if match(os.path.normcase(name))
58+
]
6859

6960

7061
def filterfalse(names, pat):
7162
"""Construct a list from those elements of the iterable NAMES that do not match PAT."""
7263
pat = os.path.normcase(pat)
7364
match = _compile_pattern(pat)
74-
if os.path is posixpath:
75-
# normcase on posix is NOP. Optimize it away from the loop.
76-
return list(itertools.filterfalse(match, names))
77-
78-
result = []
79-
for name in names:
80-
if match(os.path.normcase(name)) is None:
81-
result.append(name)
82-
return result
65+
return [
66+
name for name in names
67+
if not match(os.path.normcase(name))
68+
]
8369

8470

8571
def fnmatchcase(name, pat):

‎Lib/test/test_fnmatch.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test cases for the fnmatch module."""
22

33
import os
4+
import pathlib
45
import string
56
import unittest
67
import warnings
@@ -324,10 +325,23 @@ def test_filter(self):
324325
self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'),
325326
[b'Python', b'Perl'])
326327

328+
def test_filter_pathlike(self):
329+
names = [pathlib.Path("Python"), pathlib.Path("Ruby")]
330+
self.assertEqual(filter(names, "P*"), [names[0]])
331+
327332
def test_mix_bytes_str(self):
328333
self.assertRaises(TypeError, filter, ['test'], b'*')
329334
self.assertRaises(TypeError, filter, [b'test'], '*')
330335

336+
def test_mix_bytes_str_pathlike(self):
337+
path = pathlib.Path("test")
338+
self.assertRaises(TypeError, filter, [path], b'*')
339+
self.assertRaises(TypeError, filter, [path, b'test'], '*')
340+
self.assertRaises(TypeError, filter, [b'test', path], '*')
341+
self.assertRaises(TypeError, filter, [path, 'test'], b'*')
342+
self.assertRaises(TypeError, filter, ['test', path], b'*')
343+
self.assertEqual(filter([path, 'other'], 'te*'), [path])
344+
331345
def test_case(self):
332346
self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'),
333347
['Test.py', 'Test.PL'] if IGNORECASE else ['Test.py'])
@@ -349,10 +363,23 @@ def test_filterfalse(self):
349363
actual = filterfalse([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*')
350364
self.assertListEqual(actual, [b'Ruby', b'Tcl'])
351365

366+
def test_filterfalse_pathlike(self):
367+
names = [pathlib.Path("Python"), pathlib.Path("Ruby")]
368+
self.assertEqual(filterfalse(names, "P*"), [names[1]])
369+
352370
def test_mix_bytes_str(self):
353371
self.assertRaises(TypeError, filterfalse, ['test'], b'*')
354372
self.assertRaises(TypeError, filterfalse, [b'test'], '*')
355373

374+
def test_mix_bytes_str_pathlike(self):
375+
path = pathlib.Path("test")
376+
self.assertRaises(TypeError, filterfalse, [path], b'*')
377+
self.assertRaises(TypeError, filterfalse, [path, b'test'], '*')
378+
self.assertRaises(TypeError, filterfalse, [b'test', path], '*')
379+
self.assertRaises(TypeError, filterfalse, [path, 'test'], b'*')
380+
self.assertRaises(TypeError, filterfalse, ['test', path], b'*')
381+
self.assertEqual(filterfalse([path, 'other'], 'te*'), ['other'])
382+
356383
def test_case(self):
357384
self.assertEqual(filterfalse(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'),
358385
['Test.rb'] if IGNORECASE else ['Test.rb', 'Test.PL'])
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow :func:`fnmatch.filter` and :func:`fnmatch.filterfalse` to accept :term:`path-like objects <path-like object>`.

0 commit comments

Comments
 (0)