8000 GH-73435: Implement recursive wildcards in `pathlib.PurePath.match()` by barneygale · Pull Request #101398 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73435: Implement recursive wildcards in pathlib.PurePath.match() #101398

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

Merged
merged 40 commits into from
May 30, 2023
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
608e917
gh-73435: Implement recursive wildcards in pathlib.PurePath.match()
barneygale Jan 28, 2023
9a43c7f
Simplify code slightly
barneygale Jan 29, 2023
a846279
Fix support for newlines
barneygale Feb 15, 2023
bbd8cd6
Cache translation of individual components
barneygale Feb 15, 2023
b5c002e
Drop 'recursive' argument, make this the only behaviour.
barneygale Feb 15, 2023
0afcd54
Undo modifications to fnmatch.py
barneygale Feb 16, 2023
fe32717
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale Feb 17, 2023
7b6f850
Fix Windows support
barneygale Feb 17, 2023
037488a
Tidy up code.
barneygale Feb 17, 2023
0741950
Add news blurb.
barneygale Feb 17, 2023
e1c9731
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale Feb 20, 2023
db6f0ad
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale Apr 3, 2023
8dff9e2
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale Apr 9, 2023
314679f
Simplify patch; prepare for use in `glob()`
barneygale Apr 9, 2023
90eebcc
Make better use of path object caching.
barneygale Apr 9, 2023
4b5fffd
Add performance tip to docs
barneygale Apr 9, 2023
5e8bc28
Skip re-initialisation of PurePath patterns.
barneygale Apr 20, 2023
e81ab5a
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale Apr 29, 2023
afb8047
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 2, 2023
722a1ab
Use `re.IGNORECASE` rather than `os.path.normcase()`
barneygale May 2, 2023
0ccf3df
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 6, 2023
ccea5e1
Add whats new entry
barneygale May 11, 2023
dd04294
Update Doc/whatsnew/3.12.rst
barneygale May 11, 2023
b258641
Apply suggestions from code review
barneygale May 14, 2023
ced8998
Explain _FNMATCH_SLICE
barneygale May 14, 2023
a33c7b6
Accidentally a word.
barneygale May 14, 2023
4b3bddb
Cache pattern compilation
barneygale May 14, 2023
6ad30dd
Remove unneeded `from None` suffix, whoops.
barneygale May 14, 2023
052890f
Tiny performance improvement: avoid accessing path.parts
barneygale May 14, 2023
d789b6d
Typo fix
barneygale May 14, 2023
4fe77c6
Avoid hashing path object when compiling pattern.
barneygale May 14, 2023
4770c13
More performance tweaks
barneygale May 14, 2023
559787d
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 18, 2023
9c09fc4
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 23, 2023
eb35dbc
Re-target to 3.13.
barneygale May 23, 2023
8959dfd
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 27, 2023
fec7702
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 29, 2023
89bc380
Merge branch 'main' into gh-73435-pathlib-match-recursive
barneygale May 29, 2023
9211297
Add more comments!
barneygale May 30, 2023
73bb309
Update Lib/pathlib.py
barneygale May 30, 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
Cache translation of individual components
  • Loading branch information
barneygale committed Feb 15, 2023
commit bbd8cd603c71f87c948e11a1528a29536ae21827
23 changes: 13 additions & 10 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,26 @@ def _is_wildcard_pattern(pat):
_SWAP_SLASH_AND_NEWLINE = str.maketrans({'/': '\n', '\n': '/'})


@functools.lru_cache()
def _translate(pattern, recursive):
if recursive:
if pattern == '**\n':
return r'[\S\s]*^'
elif pattern == '**':
return r'[\S\s]*'
elif '**' in pattern:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
return fnmatch._translate(pattern)


@functools.lru_cache()
def _make_matcher(path_cls, pattern, recursive):
pattern = path_cls(pattern)
if not pattern._parts:
raise ValueError("empty pattern")
result = [r'\A' if pattern._drv or pattern._root else '^']
for line in pattern._lines_normcase.splitlines(keepends=True):
if recursive:
if line == '**\n':
result.append(r'[\S\s]*^')
continue
elif line == '**':
result.append(r'[\S\s]*')
continue
elif '**' in line:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
result.append(fnmatch._translate(line))
result.append(_translate(line, recursive))
result.append(r'\Z')
return re.compile(''.join(result), flags=re.MULTILINE)

Expand Down
0