Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
pathlib ABCs: reject empty pattern in glob()
For compatibility with `Path.glob()`, raise `ValueError` if an empty
pattern is given to `PathBase.glob()`.
  • Loading branch information
barneygale committed Nov 27, 2024
commit 7551c1108ae1e5d9498ee70f8e02a2a6d0b26226
2 changes: 2 additions & 0 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
anchor, parts = pattern._stack
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
elif not parts:
raise ValueError(f"Unacceptable pattern: {pattern!r}")
select = self._glob_selector(parts, case_sensitive, recurse_symlinks)
return select(self)

Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,8 @@ def test_glob_windows(self):
def test_glob_empty_pattern(self):
P = self.cls
p = P(self.base)
self.assertEqual(list(p.glob("")), [p])
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
list(p.glob(""))
self.assertEqual(list(p.glob(".")), [p / "."])
self.assertEqual(list(p.glob("./")), [p / "./"])

Expand Down