Skip to content

Commit

Permalink
Speed up _compile_pattern()
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
barneygale and AlexWaygood committed Nov 25, 2023
1 parent 6bfcf66 commit 97f2836
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,23 @@ def _is_case_sensitive(pathmod):
# Globbing helpers
#

re = glob = None


@functools.lru_cache(maxsize=256)
def _compile_pattern(pat, sep, case_sensitive):
"""Compile given glob pattern to a re.Pattern object (observing case
sensitivity)."""
from glob import translate
regex = translate(pat, recursive=True, include_hidden=True, seps=sep)
global re, glob
if re is None:
import re, glob

flags = re.NOFLAG if case_sensitive else re.IGNORECASE
regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
# The string representation of an empty path is a single dot ('.'). Empty
# paths shouldn't match wildcards, so we consume it with an atomic group.
regex = r'(\.\Z)?+' + regex
from re import compile, NOFLAG, IGNORECASE
return compile(regex, flags=NOFLAG if case_sensitive else IGNORECASE).match
return re.compile(regex, flags=flags).match


def _select_children(parent_paths, dir_only, follow_symlinks, match):
Expand Down

0 comments on commit 97f2836

Please sign in to comment.