Skip to content

GH-110109: Drop use of new regex features in pathlib._abc. #113292

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
wants to merge 2 commits into from
Closed
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
19 changes: 12 additions & 7 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ def _compile_pattern(pat, sep, case_sensitive):
if re is None:
import re, glob

flags = re.NOFLAG if case_sensitive else re.IGNORECASE
flags = 0 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
return re.compile(regex, flags=flags).match


Expand Down Expand Up @@ -277,6 +274,14 @@ def __str__(self):
self._tail) or '.'
return self._str

@property
def _pattern_str(self):
"""A string representation of the path, suitable for pattern matching.
This differs from __str__() by representing empty paths as the empty
string '', rather than a dot '.' character."""
path_str = str(self)
return '' if path_str == '.' else path_str

def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
Expand Down Expand Up @@ -528,7 +533,7 @@ def match(self, path_pattern, *, case_sensitive=None):
else:
raise ValueError("empty pattern")
match = _compile_pattern(pattern_str, sep, case_sensitive)
return match(str(self)) is not None
return match(self._pattern_str) is not None



Expand Down Expand Up @@ -882,9 +887,9 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
paths = _select_recursive(paths, dir_only, follow_symlinks)

# Filter out paths that don't match pattern.
prefix_len = len(str(self._make_child_relpath('_'))) - 1
prefix_len = len(self._make_child_relpath('_')._pattern_str) - 1
match = _compile_pattern(str(path_pattern), sep, case_sensitive)
paths = (path for path in paths if match(str(path), prefix_len))
paths = (path for path in paths if match(path._pattern_str, prefix_len))
return paths

dir_only = part_idx < len(pattern_parts)
Expand Down