]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-92550: Fix pathlib.Path.rglob() for empty pattern (GH-92604)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 11 May 2022 05:13:11 +0000 (22:13 -0700)
committerGitHub <noreply@github.com>
Wed, 11 May 2022 05:13:11 +0000 (22:13 -0700)
(cherry picked from commit 87f849c775ca54f56ad60ebf96822b93bbd0029a)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2022-05-10-07-57-27.gh-issue-92550.Rk_UzM.rst [new file with mode: 0644]

index 1f098fe6bd5f37b5440f8a764ba3f645e2889784..8c1fe30d74b65e9eb6c90ec718b4c5e250ae1b4c 100644 (file)
@@ -960,7 +960,7 @@ 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):
+        if pattern and 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):
index 6737068c0ff6d6c56fe7a7ec63b01e4a978a07fe..a64bf68e8593d77fd69c5c8ee71a6791633027eb 100644 (file)
@@ -1693,10 +1693,15 @@ class _BasePathTest(object):
                 "dirA", "dirA/linkC", "dirB", "dirB/linkD", "dirC",
                 "dirC/dirD", "dirE", "linkB",
             ])
+        _check(p.rglob(""), ["", "dirA", "dirB", "dirC", "dirE", "dirC/dirD"])
 
         p = P(BASE, "dirC")
+        _check(p.rglob("*"), ["dirC/fileC", "dirC/novel.txt",
+                              "dirC/dirD", "dirC/dirD/fileD"])
         _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
         _check(p.rglob("*/*"), ["dirC/dirD/fileD"])
+        _check(p.rglob("*/"), ["dirC/dirD"])
+        _check(p.rglob(""), ["dirC", "dirC/dirD"])
         # gh-91616, a re module regression
         _check(p.rglob("*.txt"), ["dirC/novel.txt"])
         _check(p.rglob("*.*"), ["dirC/novel.txt"])
diff --git a/Misc/NEWS.d/next/Library/2022-05-10-07-57-27.gh-issue-92550.Rk_UzM.rst b/Misc/NEWS.d/next/Library/2022-05-10-07-57-27.gh-issue-92550.Rk_UzM.rst
new file mode 100644 (file)
index 0000000..1f0fde3
--- /dev/null
@@ -0,0 +1 @@
+Fix :meth:`pathlib.Path.rglob` for empty pattern.