[Python-checkins] gh-92550: Fix pathlib.Path.rglob() for empty pattern (GH-92604)

serhiy-storchaka webhook-mailer at python.org
Wed May 11 00:43:31 EDT 2022


https://github.com/python/cpython/commit/87f849c775ca54f56ad60ebf96822b93bbd0029a
commit: 87f849c775ca54f56ad60ebf96822b93bbd0029a
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2022-05-11T07:43:04+03:00
summary:

gh-92550: Fix pathlib.Path.rglob() for empty pattern (GH-92604)

files:
A Misc/NEWS.d/next/Library/2022-05-10-07-57-27.gh-issue-92550.Rk_UzM.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 67b744604c189..c608ba0954f32 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -960,7 +960,7 @@ def rglob(self, pattern):
         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):
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 0d0c2f1eccb84..964cc85d53153 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1693,10 +1693,15 @@ def _check(glob, expected):
                 "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
index 0000000000000..1f0fde31108a7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-10-07-57-27.gh-issue-92550.Rk_UzM.rst
@@ -0,0 +1 @@
+Fix :meth:`pathlib.Path.rglob` for empty pattern.



More information about the Python-checkins mailing list