Skip to content

Commit

Permalink
Revert "Fix handling of patterns ending **"
Browse files Browse the repository at this point in the history
This reverts commit d1a1716.
  • Loading branch information
barneygale committed Jun 20, 2023
1 parent 91bbce1 commit 0566137
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
10 changes: 5 additions & 5 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -906,8 +906,8 @@ call fails (for example because the path doesn't exist).
>>> sorted(Path('.').glob('*/*.py'))
[PosixPath('docs/conf.py')]

Pattern segments are the same as for :mod:`fnmatch`, with the addition of
"``**``" which matches all files, directories and subdirectories. In other
Patterns are the same as for :mod:`fnmatch`, with the addition of "``**``"
which means "this directory and all subdirectories, recursively". In other
words, it enables recursive globbing::

>>> sorted(Path('.').glob('**/*.py'))
Expand Down Expand Up @@ -948,9 +948,9 @@ call fails (for example because the path doesn't exist).
The *follow_symlinks* argument.

.. versionchanged:: 3.13
Patterns ending with "``**``" match files and directories. In previous
versions, patterns with this ending match only directories.

Emits :exc:`FutureWarning` if the pattern ends with "``**``". In a
future Python release, patterns with this ending will match both files
and directories. Add a trailing slash to match only directories.

.. method:: Path.group()

Expand Down
8 changes: 8 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,14 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
pattern_parts.append('')
if pattern_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, 3)
pattern_parts.append('')

if case_sensitive is None:
# TODO: evaluate case-sensitivity of each directory in _select_children().
Expand Down
18 changes: 14 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,13 +1965,10 @@ def _check(glob, expected):
"dirC/dirD", "dirC/dirD/fileD"])
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("**/file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("dir*/**"), ["dirC/dirD", "dirC/dirD/fileD"])
_check(p.rglob("dir*/**/"), ["dirC/dirD"])
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
_check(p.rglob("*/"), ["dirC/dirD"])
_check(p.rglob(""), ["dirC", "dirC/dirD"])
_check(p.rglob("**"), ["dirC", "dirC/fileC", "dirC/novel.txt",
"dirC/dirD", "dirC/dirD/fileD"])
_check(p.rglob("**/"), ["dirC", "dirC/dirD"])
# gh-91616, a re module regression
_check(p.rglob("*.txt"), ["dirC/novel.txt"])
Expand Down Expand Up @@ -2118,7 +2115,20 @@ def test_glob_above_recursion_limit(self):
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.glob('**'))
list(base.glob('**/'))

def test_glob_recursive_no_trailing_slash(self):
P = self.cls
p = P(BASE)
with self.assertWarns(FutureWarning):
p.glob('**')
with self.assertWarns(FutureWarning):
p.glob('*/**')
with self.assertWarns(FutureWarning):
p.rglob('**')
with self.assertWarns(FutureWarning):
p.rglob('*/**')


def _check_resolve(self, p, expected, strict=True):
q = p.resolve(strict)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
:meth:`pathlib.Path.glob` and :meth:`~pathlib.Path.rglob` now yield files and
directories when expanding a pattern that ends with "``**``". In a earlier
Python releases, patterns with this ending match only directories.
Emit :exc:`FutureWarning` from :meth:`pathlib.Path.glob` and
:meth:`~pathlib.Path.rglob` if the given pattern ends with "``**``". In a
future Python release, patterns with this ending will match both files and
directories. Add a trailing slash to match only directories.

0 comments on commit 0566137

Please sign in to comment.