Skip to content

Commit fbfb0e1

Browse files
authored
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()`.
1 parent a04c0a9 commit fbfb0e1

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

Lib/pathlib/types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ def glob(self, pattern, *, recurse_symlinks=True):
287287
anchor, parts = _explode_path(pattern)
288288
if anchor:
289289
raise NotImplementedError("Non-relative patterns are unsupported")
290+
elif not parts:
291+
raise ValueError(f"Unacceptable pattern: {pattern!r}")
290292
elif not recurse_symlinks:
291293
raise NotImplementedError("recurse_symlinks=False is unsupported")
292294
case_sensitive = self.parser.normcase('Aa') == 'Aa'

Lib/test/test_pathlib/test_read.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ def check(pattern, expected):
127127
check("**/file*",
128128
["fileA", "dirA/linkC/fileB", "dirB/fileB", "dirC/fileC", "dirC/dirD/fileD",
129129
"linkB/fileB"])
130+
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
131+
list(p.glob(''))
130132

131133
def test_walk_top_down(self):
132134
it = self.root.walk()

0 commit comments

Comments
 (0)