From 05661371a77978514a6301ee491e6ed4a61426f1 Mon Sep 17 00:00:00 2001 From: barneygale Date: Tue, 20 Jun 2023 23:36:46 +0100 Subject: [PATCH] Revert "Fix handling of patterns ending `**`" This reverts commit d1a17165ed6e227a39b409354cb5e6752e1d1b2b. --- Doc/library/pathlib.rst | 10 +++++----- Lib/pathlib.py | 8 ++++++++ Lib/test/test_pathlib.py | 18 ++++++++++++++---- ...23-06-07-00-13-00.gh-issue-70303.frwUKH.rst | 7 ++++--- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 84869c20fdb549..04a0999d4c50aa 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -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')) @@ -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() diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 510e5e4d72f5c4..de92a91843c6ae 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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(). diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 7eef4183c25aad..6fa16ee79bf636 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -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"]) @@ -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) diff --git a/Misc/NEWS.d/next/Library/2023-06-07-00-13-00.gh-issue-70303.frwUKH.rst b/Misc/NEWS.d/next/Library/2023-06-07-00-13-00.gh-issue-70303.frwUKH.rst index fb472ef4d682d6..16cc6a7bb18b43 100644 --- a/Misc/NEWS.d/next/Library/2023-06-07-00-13-00.gh-issue-70303.frwUKH.rst +++ b/Misc/NEWS.d/next/Library/2023-06-07-00-13-00.gh-issue-70303.frwUKH.rst @@ -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.