From 7551c1108ae1e5d9498ee70f8e02a2a6d0b26226 Mon Sep 17 00:00:00 2001 From: barneygale Date: Wed, 27 Nov 2024 17:25:54 +0000 Subject: [PATCH] pathlib ABCs: reject empty pattern in `glob()` For compatibility with `Path.glob()`, raise `ValueError` if an empty pattern is given to `PathBase.glob()`. --- Lib/pathlib/_abc.py | 2 ++ Lib/test/test_pathlib/test_pathlib_abc.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 2c243d470d4eda..263c1f1ff22df9 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -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) diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index aaa30a17f2af14..7ef925e71b0356 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -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 / "./"])