bpo-22276: Change pathlib.Path.glob not to ignore trailing path separator (GH-10349)

Now pathlib.Path.glob() **only** matches directories when the pattern ends in a path separator.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Eisuke Kawashima 2022-04-29 04:45:03 +09:00 committed by GitHub
parent 0ef8d921f5
commit ea2f5bcda1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 0 deletions

View file

@ -281,6 +281,8 @@ _posix_flavour = _PosixFlavour()
def _make_selector(pattern_parts, flavour):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
if not pat:
return _TerminatingSelector()
if pat == '**':
cls = _RecursiveWildcardSelector
elif '**' in pat:
@ -943,6 +945,8 @@ class Path(PurePath):
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
selector = _make_selector(tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p
@ -956,6 +960,8 @@ class Path(PurePath):
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p