Skip to content

Commit 1f58b94

Browse files
committed
pythonGH-104114: Fix pathlib.WindowsPath.glob() use of literal pattern segment case
We now use `_WildcardSelector` to evaluate literal pattern segments, which allows us to retrieve the real filesystem case. This change is necessary in order to implement a *case_sensitive* argument (see pythonGH-81079) and a *follow_symlinks* argument (see pythonGH-77609).
1 parent 65a49c6 commit 1f58b94

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

Lib/pathlib.py

+13-38
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ def _ignore_error(exception):
5454
getattr(exception, 'winerror', None) in _IGNORED_WINERRORS)
5555

5656

57-
def _is_wildcard_pattern(pat):
58-
# Whether this pattern needs actual matching using fnmatch, or can
59-
# be looked up directly as a file.
60-
return "*" in pat or "?" in pat or "[" in pat
61-
6257
def _is_case_sensitive(flavour):
6358
return flavour.normcase('Aa') == 'Aa'
6459

@@ -78,10 +73,8 @@ def _make_selector(pattern_parts, flavour):
7873
cls = _ParentSelector
7974
elif '**' in pat:
8075
raise ValueError("Invalid pattern: '**' can only be an entire path component")
81-
elif _is_wildcard_pattern(pat):
82-
cls = _WildcardSelector
8376
else:
84-
cls = _PreciseSelector
77+
cls = _WildcardSelector
8578
return cls(pat, child_parts, flavour)
8679

8780

@@ -102,54 +95,36 @@ def select_from(self, parent_path):
10295
"""Iterate over all child paths of `parent_path` matched by this
10396
selector. This can contain parent_path itself."""
10497
path_cls = type(parent_path)
105-
is_dir = path_cls.is_dir
106-
exists = path_cls.exists
10798
scandir = path_cls._scandir
108-
if not is_dir(parent_path):
99+
if not parent_path.is_dir():
109100
return iter([])
110-
return self._select_from(parent_path, is_dir, exists, scandir)
101+
return self._select_from(parent_path, scandir)
111102

112103

113104
class _TerminatingSelector:
114105

115-
def _select_from(self, parent_path, is_dir, exists, scandir):
106+
def _select_from(self, parent_path, scandir):
116107
yield parent_path
117108

118109

119110
class _ParentSelector(_Selector):
120111
def __init__(self, name, child_parts, flavour):
121112
_Selector.__init__(self, child_parts, flavour)
122113

123-
def _select_from(self, parent_path, is_dir, exists, scandir):
114+
def _select_from(self, parent_path, scandir):
124115
path = parent_path._make_child_relpath('..')
125-
for p in self.successor._select_from(path, is_dir, exists, scandir):
116+
for p in self.successor._select_from(path, scandir):
126117
yield p
127118

128119

129-
class _PreciseSelector(_Selector):
130-
131-
def __init__(self, name, child_parts, flavour):
132-
self.name = name
133-
_Selector.__init__(self, child_parts, flavour)
134-
135-
def _select_from(self, parent_path, is_dir, exists, scandir):
136-
try:
137-
path = parent_path._make_child_relpath(self.name)
138-
if (is_dir if self.dironly else exists)(path):
139-
for p in self.successor._select_from(path, is_dir, exists, scandir):
140-
yield p
141-
except PermissionError:
142-
return
143-
144-
145120
class _WildcardSelector(_Selector):
146121

147122
def __init__(self, pat, child_parts, flavour):
148123
flags = re.NOFLAG if _is_case_sensitive(flavour) else re.IGNORECASE
149124
self.match = re.compile(fnmatch.translate(pat), flags=flags).fullmatch
150125
_Selector.__init__(self, child_parts, flavour)
151126

152-
def _select_from(self, parent_path, is_dir, exists, scandir):
127+
def _select_from(self, parent_path, scandir):
153128
try:
154129
# We must close the scandir() object before proceeding to
155130
# avoid exhausting file descriptors when globbing deep trees.
@@ -170,7 +145,7 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
170145
name = entry.name
171146
if self.match(name):
172147
path = parent_path._make_child_relpath(name)
173-
for p in self.successor._select_from(path, is_dir, exists, scandir):
148+
for p in self.successor._select_from(path, scandir):
174149
yield p
175150
except PermissionError:
176151
return
@@ -181,7 +156,7 @@ class _RecursiveWildcardSelector(_Selector):
181156
def __init__(self, pat, child_parts, flavour):
182157
_Selector.__init__(self, child_parts, flavour)
183158

184-
def _iterate_directories(self, parent_path, is_dir, scandir):
159+
def _iterate_directories(self, parent_path, scandir):
185160
yield parent_path
186161
try:
187162
# We must close the scandir() object before proceeding to
@@ -197,18 +172,18 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
197172
raise
198173
if entry_is_dir and not entry.is_symlink():
199174
path = parent_path._make_child_relpath(entry.name)
200-
for p in self._iterate_directories(path, is_dir, scandir):
175+
for p in self._iterate_directories(path, scandir):
201176
yield p
202177
except PermissionError:
203178
return
204179

205-
def _select_from(self, parent_path, is_dir, exists, scandir):
180+
def _select_from(self, parent_path, scandir):
206181
try:
207182
yielded = set()
208183
try:
209184
successor_select = self.successor._select_from
210-
for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
211-
for p in successor_select(starting_point, is_dir, exists, scandir):
185+
for starting_point in self._iterate_directories(parent_path, scandir):
186+
for p in successor_select(starting_point, scandir):
212187
if p not in yielded:
213188
yield p
214189
yielded.add(p)

Lib/test/test_pathlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3118,15 +3118,15 @@ def test_glob(self):
31183118
self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") })
31193119
self.assertEqual(set(p.glob("*a\\")), { P(BASE, "dirA") })
31203120
self.assertEqual(set(p.glob("F*a")), { P(BASE, "fileA") })
3121-
self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\FILEa"})
3121+
self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\fileA"})
31223122
self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"})
31233123

31243124
def test_rglob(self):
31253125
P = self.cls
31263126
p = P(BASE, "dirC")
31273127
self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") })
31283128
self.assertEqual(set(p.rglob("*\\")), { P(BASE, "dirC/dirD") })
3129-
self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\FILEd"})
3129+
self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\fileD"})
31303130

31313131
def test_expanduser(self):
31323132
P = self.cls
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix issue where :meth:`pathlib.Path.glob` returns paths using the case of
2+
non-wildcard segments for corresponding path segments, rather than the real
3+
filesystem case.

0 commit comments

Comments
 (0)