GH-70303: Make pathlib.Path.glob('**') return both files and directories (#114684)

Return files and directories from `pathlib.Path.glob()` if the pattern ends
with `**`. This is more compatible with `PurePath.full_match()` and with
other glob implementations such as bash and `glob.glob()`. Users can add a
trailing slash to match only directories.

In my previous patch I added a `FutureWarning` with the intention of fixing
this in Python 3.15. Upon further reflection I think this was an
unnecessarily cautious remedy to a clear bug.
This commit is contained in:
Barney Gale 2024-01-30 19:52:53 +00:00 committed by GitHub
parent 6de8aa31f3
commit fda7445ca5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 35 additions and 23 deletions

View file

@ -465,14 +465,6 @@ class PurePath(_abc.PurePathBase):
elif pattern[-1] in (self.pathmod.sep, self.pathmod.altsep):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
parts.append('')
elif parts[-1] == '**':
# GH-70303: '**' only matches directories. Add trailing slash.
warnings.warn(
"Pattern ending '**' will match files and directories in a "
"future Python release. Add a trailing slash to match only "
"directories and remove this warning.",
FutureWarning, 4)
parts.append('')
parts.reverse()
return parts