Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-123135: add support for path-like objects in fnmatch.filter on POSIX platforms #123122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
4 changes: 4 additions & 0 deletions 4 Doc/library/fnmatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`,
It is the same as ``[n for n in names if fnmatch(n, pat)]``,
but implemented more efficiently.

.. versionchanged:: 3.14
Added support for :term:`path-like objects <path-like object>` for
the *names* parameter.


.. function:: translate(pat)

Expand Down
16 changes: 3 additions & 13 deletions 16 Lib/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
corresponding to PATTERN. (It does not compile it.)
"""
import os
import posixpath
import re
import functools

Expand Down Expand Up @@ -47,19 +46,10 @@ def _compile_pattern(pat):

def filter(names, pat):
"""Construct a list from those elements of the iterable NAMES that match PAT."""
result = []
pat = os.path.normcase(pat)
normcase = os.path.normcase
pat = normcase(pat)
match = _compile_pattern(pat)
if os.path is posixpath:
# normcase on posix is NOP. Optimize it away from the loop.
for name in names:
if match(name):
result.append(name)
else:
for name in names:
if match(os.path.normcase(name)):
result.append(name)
return result
return [name for name in names if match(normcase(name))]

def fnmatchcase(name, pat):
"""Test whether FILENAME matches PATTERN, including case.
Expand Down
3 changes: 3 additions & 0 deletions 3 Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ def __init__(self, path):
def __repr__(self):
return f'<FakePath {self.path!r}>'

def __eq__(self, other):
return isinstance(other, type(self)) and self.path == other.path

def __fspath__(self):
if (isinstance(self.path, BaseException) or
isinstance(self.path, type) and
Expand Down
22 changes: 18 additions & 4 deletions 22 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 Down Expand Up @@ -253,14 +255,26 @@ def test_translate(self):
class FilterTestCase(unittest.TestCase):

def test_filter(self):
self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'),
['Python', 'Perl'])
self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'),
[b'Python', b'Perl'])
for cls in (str, Path, FakePath):
names = list(map(cls, ['Python', 'Ruby', 'Perl', 'Tcl']))
filtered = list(map(cls, ['Python', 'Perl']))
with self.subTest('string pattern', cls=cls, names=names):
self.assertListEqual(filter(names, 'P*'), filtered)
# We want to test the case when os.path.normcase() returns bytes but
# we cannot use pathlib.Path since they only accept string objects.
for cls in (bytes, FakePath):
names = list(map(cls, [b'Python', b'Ruby', b'Perl', b'Tcl']))
filtered = list(map(cls, [b'Python', b'Perl']))
with self.subTest('bytes pattern', cls=cls, names=names):
self.assertListEqual(filter(names, b'P*'), filtered)

def test_mix_bytes_str(self):
self.assertRaises(TypeError, filter, ['test'], b'*')
self.assertRaises(TypeError, filter, [b'test'], '*')
# We want to test the case when os.path.normcase() returns bytes but
# we cannot use pathlib.Path since they only accept string objects.
self.assertRaises(TypeError, filter, [FakePath('test')], b'*')
self.assertRaises(TypeError, filter, [FakePath(b'test')], '*')

def test_case(self):
ignorecase = os.path.normcase('P') == os.path.normcase('p')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added support for supplying :term:`path-like objects <path-like object>`
to the *names* parameter of :func:`fnmatch.filter`. Previously, such
objects were only accepted on non-POSIX platforms. Patch by Bénédikt Tran.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only non-POSIX platform that we support is Windows:

Suggested change
objects were only accepted on non-POSIX platforms. Patch by Bénédikt Tran.
objects were only accepted on Windows. Patch by Bénédikt Tran.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about WASI?

Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.