Skip to content

Commit

Permalink
gh-111259: Optimize recursive wildcards in pathlib (GH-111303)
Browse files Browse the repository at this point in the history
Regular expression pattern `(?s:.)` is much faster than `[\s\S]`.
  • Loading branch information
serhiy-storchaka authored Oct 26, 2023
1 parent 67a91f7 commit 309efb3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ def _compile_pattern_lines(pattern_lines, case_sensitive):
elif part == '*':
part = r'.+'
elif part == '**\n':
# '**/' component: we use '[\s\S]' rather than '.' so that path
# '**/' component: we use '(?s:.)' rather than '.' so that path
# separators (i.e. newlines) are matched. The trailing '^' ensures
# we terminate after a path separator (i.e. on a new line).
part = r'[\s\S]*^'
part = r'(?s:.)*^'
elif part == '**':
# '**' component.
part = r'[\s\S]*'
part = r'(?s:.)*'
elif '**' in part:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize recursive wildcards in :mod:`pathlib`.

0 comments on commit 309efb3

Please sign in to comment.