Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bbfd404
GH-72904: Add optional *seps* argument to `fnmatch.translate()`
barneygale Jun 18, 2023
da9948d
Simplify `_make_child_relpath()` further
barneygale Jul 13, 2023
a07118b
Fix default value in docs
barneygale Jul 13, 2023
2728dcd
Match style of surrounding `fnmatch` code a little better.
barneygale Jul 13, 2023
fbcf4e3
Merge branch 'main' into gh-72904-fnmatch-seps
barneygale Jul 19, 2023
a0ce9c4
Docs + naming improvements
barneygale Jul 19, 2023
5b620fb
Replace *seps* with *sep*
barneygale Jul 26, 2023
51f2698
Update Doc/library/fnmatch.rst
barneygale Aug 4, 2023
9c8c3f3
Move to `glob.translate()`
barneygale Aug 11, 2023
8518ea2
Whoops
barneygale Aug 12, 2023
75129c8
Deduplicate code to handle character sets
barneygale Aug 13, 2023
2505590
Add support for `include_hidden=False`
barneygale Sep 23, 2023
1754d42
Fix doctest
barneygale Sep 23, 2023
dd2d401
Merge branch 'main' into gh-72904-fnmatch-seps
barneygale Sep 23, 2023
7b1ad63
Improve implementation; minimise fnmatch and pathlib diffs.
barneygale Sep 25, 2023
1485ff3
Fix tests
barneygale Sep 26, 2023
4c6d6f0
Tiny performance tweak
barneygale Sep 26, 2023
5aae7a2
Merge branch 'main' into gh-72904-fnmatch-seps
barneygale Sep 26, 2023
afb2d43
Fix `_make_child_relpath()`
barneygale Sep 26, 2023
d73df1b
Minor code improvements
barneygale Sep 26, 2023
c70afe3
Add another test for `include_hidden=False`
barneygale Sep 26, 2023
9cb2952
Merge branch 'main' into gh-72904-fnmatch-seps
barneygale Sep 30, 2023
f178b14
Add whatsnew entry
barneygale Sep 30, 2023
4a726aa
Collapse adjacent `**` segments.
barneygale Sep 30, 2023
78292eb
Apply suggestions from code review
barneygale Sep 30, 2023
5d4062c
Add comment explaining regex that consumes "empty" paths.
barneygale Sep 30, 2023
1ad624d
Merge branch 'main' into gh-72904-fnmatch-seps
barneygale Oct 28, 2023
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
Prev Previous commit
Next Next commit
Replace *seps* with *sep*
  • Loading branch information
barneygale committed Jul 26, 2023
commit 5b620fb51eb5382d2ee6afbc1b5ec27883eb66fb
11 changes: 5 additions & 6 deletions Doc/library/fnmatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`,
``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.


.. function:: translate(pattern, seps=None)
.. function:: translate(pattern, sep=None)

Return the shell-style *pattern* converted to a regular expression for
using with :func:`re.match`.
Expand All @@ -98,9 +98,8 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`,
>>> reobj.match('foobar.txt')
<re.Match object; span=(0, 10), match='foobar.txt'>

A sequence of path separator characters may be supplied to the *seps*
argument. If given, the separators are used to split the pattern into
segments, where:
A path separator character may be supplied to the *sep* argument. If given,
the separator is sed to split the pattern into segments, where:
Comment thread
barneygale marked this conversation as resolved.
Outdated

- A ``*`` pattern segment matches precisely one path segment.
- A ``**`` pattern segment matches any number of path segments.
Expand All @@ -109,11 +108,11 @@ cache the compiled regex patterns in the following functions: :func:`fnmatch`,
- ``*`` and ``?`` wildcards in other positions don't match path separators.

These rules approximate shell recursive globbing. The :mod:`pathlib` module
calls this function and supplies *seps* to implement
calls this function and supplies *sep* to implement
:meth:`~pathlib.PurePath.match` and :meth:`~pathlib.Path.glob`.

.. versionchanged:: 3.13
The *seps* parameter was added.
The *sep* parameter was added.


.. seealso::
Expand Down
25 changes: 12 additions & 13 deletions Lib/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,23 @@ def fnmatchcase(name, pat):
return match(name) is not None


def translate(pat, seps=None):
def translate(pat, sep=None):
Comment thread
barneygale marked this conversation as resolved.
Outdated
"""Translate a shell PATTERN to a regular expression.

A sequence of path separator characters may be supplied to the *seps*
argument. If given, '*' and '?' wildcards will not match separators.
'*' wildcards in standalone pattern segments match precisely one path
segment; '**' wildcards in standalone segments match any number of path
segments.
A path separator character may be supplied to the *sep* argument. If
Comment thread
barneygale marked this conversation as resolved.
Outdated
given, '*' and '?' wildcards will not match separators; '*' wildcards in
standalone pattern segments match precisely one path segment; and '**'
wildcards in standalone segments match any number of path segments.

There is no way to quote meta-characters.
"""

STAR = object()
if seps:
SEPS = re.escape(''.join(seps))
DOT = f'[^{SEPS}]'
if sep:
SEP = re.escape(sep)
DOT = f'[^{SEP}]'
else:
SEPS = None
SEP = None
DOT = '.'
res = []
add = res.append
Expand All @@ -101,9 +100,9 @@ def translate(pat, seps=None):
h = i-1
while i < n and pat[i] == '*':
i = i+1
if seps:
if sep:
star_count = i-h
is_segment = (h == 0 or pat[h-1] in seps) and (i == n or pat[i] in seps)
is_segment = (h == 0 or pat[h-1] == sep) and (i == n or pat[i] == sep)
if star_count == 1:
if is_segment:
add(f'{DOT}+')
Comment thread
barneygale marked this conversation as resolved.
Outdated
Expand All @@ -113,7 +112,7 @@ def translate(pat, seps=None):
if i == n:
add('.*')
else:
add(f'(.*[{SEPS}])?')
add(f'(.*{SEP})?')
i = i+1
else:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
Expand Down
20 changes: 10 additions & 10 deletions Lib/test/test_fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,16 @@ def test_translate(self):
self.assertTrue(re.match(fatre, 'cbabcaxc'))
self.assertFalse(re.match(fatre, 'dabccbad'))

def test_translate_seps(self):
self.assertEqual(translate('*', seps='/'), r'(?s:[^/]+)\Z')
self.assertEqual(translate('?', seps='/'), r'(?s:[^/])\Z')
self.assertEqual(translate('a?b*', seps='/'), r'(?s:a[^/]b[^/]*)\Z')
self.assertEqual(translate('/**/*/*.*/**', seps='/'),
r'(?s:/(.*[/])?[^/]+/[^/]*\.[^/]*/.*)\Z')
self.assertEqual(translate('foo/bar\\baz', seps=('/', '\\')),
r'(?s:foo/bar\\baz)\Z')
self.assertRaises(ValueError, translate, 'a**', seps='/')
self.assertRaises(ValueError, translate, '**b', seps='/')
def test_translate_sep(self):
self.assertEqual(translate('*', sep='/'), r'(?s:[^/]+)\Z')
Comment thread
barneygale marked this conversation as resolved.
Outdated
self.assertEqual(translate('?', sep='/'), r'(?s:[^/])\Z')
self.assertEqual(translate('a?b*', sep='/'), r'(?s:a[^/]b[^/]*)\Z')
self.assertEqual(translate('/**/*/*.*/**', sep='/'),
r'(?s:/(.*/)?[^/]+/[^/]*\.[^/]*/.*)\Z')
self.assertEqual(translate(r'\**\*\*.*\**', sep='\\'),
r'(?s:\\(.*\\)?[^\\]+\\[^\\]*\.[^\\]*\\.*)\Z')
self.assertRaises(ValueError, translate, 'a**', sep='/')
self.assertRaises(ValueError, translate, '**b', sep='/')
Comment thread
barneygale marked this conversation as resolved.
Outdated


class FilterTestCase(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Add optional *seps* argument to :func:`fnmatch.translate`. If a sequence of
path separators is given, the resulting pattern matches paths similarly to
:func:`glob.glob`.
Add optional *sep* argument to :func:`fnmatch.translate`. If a path separator
character is given, the resulting pattern matches paths like
:meth:`pathlib.PurePath.match` and :meth:`pathlib.Path.glob`. For example, the
``*`` wildcard will not match path separators.