diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-05-11 04:14:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-11 04:14:25 (GMT) |
commit | b1c4368824e5c2e4d4d5875f9c5f7a9c5a0d2ce2 (patch) | |
tree | 9666205baa9e05d5dceceab74f829daf74fb1aee | |
parent | dfdebda0524ce4cc945621d69eef77a64f260095 (diff) | |
download | cpython-b1c4368824e5c2e4d4d5875f9c5f7a9c5a0d2ce2.zip cpython-b1c4368824e5c2e4d4d5875f9c5f7a9c5a0d2ce2.tar.gz cpython-b1c4368824e5c2e4d4d5875f9c5f7a9c5a0d2ce2.tar.bz2 |
Revert "gh-92550 - Fix regression in `pathlib.Path.rglob()` (GH-92583)" (GH-92598)
This reverts commit dcdf250d2de1428f7d8b4e9ecf51d2fd8200e21a.
-rw-r--r-- | Doc/library/pathlib.rst | 6 | ||||
-rw-r--r-- | Doc/whatsnew/3.11.rst | 9 | ||||
-rw-r--r-- | Lib/pathlib.py | 6 | ||||
-rw-r--r-- | Lib/test/test_pathlib.py | 17 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-05-09-23-36-19.gh-issue-92550.qZ4AhU.rst | 2 |
5 files changed, 38 insertions, 2 deletions
diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 535b5ab..d45e7aa 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -815,6 +815,9 @@ call fails (for example because the path doesn't exist). .. audit-event:: pathlib.Path.glob self,pattern pathlib.Path.glob + .. versionchanged:: 3.11 + Return only directories if *pattern* ends with a pathname components + separator (:data:`~os.sep` or :data:`~os.altsep`). .. method:: Path.group() @@ -1104,6 +1107,9 @@ call fails (for example because the path doesn't exist). .. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob + .. versionchanged:: 3.11 + Return only directories if *pattern* ends with a pathname components + separator (:data:`~os.sep` or :data:`~os.altsep`). .. method:: Path.rmdir() diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index db5ede6..40e68e6 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -559,6 +559,15 @@ os instead of ``CryptGenRandom()`` which is deprecated. (Contributed by Dong-hee Na in :issue:`44611`.) + +pathlib +------- + +* :meth:`~pathlib.Path.glob` and :meth:`~pathlib.Path.rglob` return only + directories if *pattern* ends with a pathname components separator: + :data:`~os.sep` or :data:`~os.altsep`. + (Contributed by Eisuke Kawasima in :issue:`22276` and :issue:`33392`.) + re -- diff --git a/Lib/pathlib.py b/Lib/pathlib.py index be3fd01..67b7446 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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 diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 3412d84..0d0c2f1 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1662,6 +1662,11 @@ class _BasePathTest(object): else: _check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB']) + if not os_helper.can_symlink(): + _check(p.glob("*/"), ["dirA", "dirB", "dirC", "dirE"]) + else: + _check(p.glob("*/"), ["dirA", "dirB", "dirC", "dirE", "linkB"]) + def test_rglob_common(self): def _check(glob, expected): self.assertEqual(set(glob), { P(BASE, q) for q in expected }) @@ -1679,6 +1684,16 @@ class _BasePathTest(object): "linkB/fileB", "dirA/linkC/fileB"]) _check(p.rglob("file*"), ["fileA", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD"]) + if not os_helper.can_symlink(): + _check(p.rglob("*/"), [ + "dirA", "dirB", "dirC", "dirC/dirD", "dirE", + ]) + else: + _check(p.rglob("*/"), [ + "dirA", "dirA/linkC", "dirB", "dirB/linkD", "dirC", + "dirC/dirD", "dirE", "linkB", + ]) + p = P(BASE, "dirC") _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"]) @@ -2682,6 +2697,7 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase): P = self.cls p = P(BASE) self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") }) + self.assertEqual(set(p.glob("*a\\")), { P(BASE, "dirA") }) self.assertEqual(set(p.glob("F*a")), { P(BASE, "fileA") }) self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\FILEa"}) self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"}) @@ -2690,6 +2706,7 @@ class WindowsPathTest(_BasePathTest, unittest.TestCase): P = self.cls p = P(BASE, "dirC") self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") }) + self.assertEqual(set(p.rglob("*\\")), { P(BASE, "dirC/dirD") }) self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\FILEd"}) def test_expanduser(self): diff --git a/Misc/NEWS.d/next/Library/2022-05-09-23-36-19.gh-issue-92550.qZ4AhU.rst b/Misc/NEWS.d/next/Library/2022-05-09-23-36-19.gh-issue-92550.qZ4AhU.rst deleted file mode 100644 index 1931b32..0000000 --- a/Misc/NEWS.d/next/Library/2022-05-09-23-36-19.gh-issue-92550.qZ4AhU.rst +++ /dev/null @@ -1,2 +0,0 @@ -:meth:`pathlib.Path.rglob` raised :exc:`IndexError` when called with an -empty string. This regression was introduced in 3.11b1. |