GH-128520: pathlib ABCs: reject empty pattern in ReadablePath.glob() (#127343)

For compatibility with `Path.glob()`, raise `ValueError` if an empty
pattern is given to `ReadablePath.glob()`.
This commit is contained in:
Barney Gale 2025-03-24 15:12:29 +00:00 committed by GitHub
parent a04c0a9658
commit fbfb0e1f6e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 0 deletions

View file

@ -287,6 +287,8 @@ def glob(self, pattern, *, recurse_symlinks=True):
anchor, parts = _explode_path(pattern)
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
elif not parts:
raise ValueError(f"Unacceptable pattern: {pattern!r}")
elif not recurse_symlinks:
raise NotImplementedError("recurse_symlinks=False is unsupported")
case_sensitive = self.parser.normcase('Aa') == 'Aa'

View file

@ -127,6 +127,8 @@ def check(pattern, expected):
check("**/file*",
["fileA", "dirA/linkC/fileB", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD",
"linkB/fileB"])
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
list(p.glob(''))
def test_walk_top_down(self):
it = self.root.walk()