Skip to content

[3.7] bpo-31202: Preserve case of literal parts in Path.glob() on Windows. (GH-16860) #16874

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 1 commit into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
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
38 changes: 20 additions & 18 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def casefold(self, s):
def casefold_parts(self, parts):
return [p.lower() for p in parts]

def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern), re.IGNORECASE).fullmatch

def resolve(self, path, strict=False):
s = str(path)
if not s:
Expand Down Expand Up @@ -309,6 +312,9 @@ def casefold(self, s):
def casefold_parts(self, parts):
return parts

def compile_pattern(self, pattern):
return re.compile(fnmatch.translate(pattern)).fullmatch

def resolve(self, path, strict=False):
sep = self.sep
accessor = path._accessor
Expand Down Expand Up @@ -444,7 +450,7 @@ def readlink(self, path):
# Globbing helpers
#

def _make_selector(pattern_parts):
def _make_selector(pattern_parts, flavour):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
if pat == '**':
Expand All @@ -455,7 +461,7 @@ def _make_selector(pattern_parts):
cls = _WildcardSelector
else:
cls = _PreciseSelector
return cls(pat, child_parts)
return cls(pat, child_parts, flavour)

if hasattr(functools, "lru_cache"):
_make_selector = functools.lru_cache()(_make_selector)
Expand All @@ -465,10 +471,10 @@ class _Selector:
"""A selector matches a specific glob pattern part against the children
of a given path."""

def __init__(self, child_parts):
def __init__(self, child_parts, flavour):
self.child_parts = child_parts
if child_parts:
self.successor = _make_selector(child_parts)
self.successor = _make_selector(child_parts, flavour)
self.dironly = True
else:
self.successor = _TerminatingSelector()
Expand All @@ -494,9 +500,9 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _PreciseSelector(_Selector):

def __init__(self, name, child_parts):
def __init__(self, name, child_parts, flavour):
self.name = name
_Selector.__init__(self, child_parts)
_Selector.__init__(self, child_parts, flavour)

def _select_from(self, parent_path, is_dir, exists, scandir):
try:
Expand All @@ -510,13 +516,12 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _WildcardSelector(_Selector):

def __init__(self, pat, child_parts):
self.pat = re.compile(fnmatch.translate(pat))
_Selector.__init__(self, child_parts)
def __init__(self, pat, child_parts, flavour):
self.match = flavour.compile_pattern(pat)
_Selector.__init__(self, child_parts, flavour)

def _select_from(self, parent_path, is_dir, exists, scandir):
try:
cf = parent_path._flavour.casefold
entries = list(scandir(parent_path))
for entry in entries:
entry_is_dir = False
Expand All @@ -527,8 +532,7 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
raise
if not self.dironly or entry_is_dir:
name = entry.name
casefolded = cf(name)
if self.pat.match(casefolded):
if self.match(name):
path = parent_path._make_child_relpath(name)
for p in self.successor._select_from(path, is_dir, exists, scandir):
yield p
Expand All @@ -539,8 +543,8 @@ def _select_from(self, parent_path, is_dir, exists, scandir):

class _RecursiveWildcardSelector(_Selector):

def __init__(self, pat, child_parts):
_Selector.__init__(self, child_parts)
def __init__(self, pat, child_parts, flavour):
_Selector.__init__(self, child_parts, flavour)

def _iterate_directories(self, parent_path, is_dir, scandir):
yield parent_path
Expand Down Expand Up @@ -1101,11 +1105,10 @@ def glob(self, pattern):
"""
if not pattern:
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
pattern = self._flavour.casefold(pattern)
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
selector = _make_selector(tuple(pattern_parts))
selector = _make_selector(tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p

Expand All @@ -1114,11 +1117,10 @@ def rglob(self, pattern):
directories) matching the given relative pattern, anywhere in
this subtree.
"""
pattern = self._flavour.casefold(pattern)
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
selector = _make_selector(("**",) + tuple(pattern_parts))
selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p

Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2216,11 +2216,15 @@ def test_glob(self):
P = self.cls
p = P(BASE)
self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") })
self.assertEqual(set(p.glob("F*a")), { P(BASE, "fileA") })
self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\FILEa"})
self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"})

def test_rglob(self):
P = self.cls
p = P(BASE, "dirC")
self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") })
self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\FILEd"})

def test_expanduser(self):
P = self.cls
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The case the result of :func:`pathlib.WindowsPath.glob` matches now the case
of the pattern for literal parts.