Skip to content

Commit 5135b6e

Browse files
gh-92550: Fix pathlib.Path.rglob() for empty pattern (GH-92604)
(cherry picked from commit 87f849c) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 951cfc8 commit 5135b6e

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/pathlib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ def rglob(self, pattern):
960960
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
961961
if drv or root:
962962
raise NotImplementedError("Non-relative patterns are unsupported")
963-
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
963+
if pattern and pattern[-1] in (self._flavour.sep, self._flavour.altsep):
964964
pattern_parts.append('')
965965
selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
966966
for p in selector.select_from(self):

Lib/test/test_pathlib.py

+5
Original file line numberDiff line numberDiff line change
@@ -1693,10 +1693,15 @@ def _check(glob, expected):
16931693
"dirA", "dirA/linkC", "dirB", "dirB/linkD", "dirC",
16941694
"dirC/dirD", "dirE", "linkB",
16951695
])
1696+
_check(p.rglob(""), ["", "dirA", "dirB", "dirC", "dirE", "dirC/dirD"])
16961697

16971698
p = P(BASE, "dirC")
1699+
_check(p.rglob("*"), ["dirC/fileC", "dirC/novel.txt",
1700+
"dirC/dirD", "dirC/dirD/fileD"])
16981701
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
16991702
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
1703+
_check(p.rglob("*/"), ["dirC/dirD"])
1704+
_check(p.rglob(""), ["dirC", "dirC/dirD"])
17001705
# gh-91616, a re module regression
17011706
_check(p.rglob("*.txt"), ["dirC/novel.txt"])
17021707
_check(p.rglob("*.*"), ["dirC/novel.txt"])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix :meth:`pathlib.Path.rglob` for empty pattern.

0 commit comments

Comments
 (0)