]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-128520: pathlib ABCs: reject empty pattern in `ReadablePath.glob()` (#127343)
authorBarney Gale <barney.gale@gmail.com>
Mon, 24 Mar 2025 15:12:29 +0000 (15:12 +0000)
committerGitHub <noreply@github.com>
Mon, 24 Mar 2025 15:12:29 +0000 (15:12 +0000)
For compatibility with `Path.glob()`, raise `ValueError` if an empty
pattern is given to `ReadablePath.glob()`.

Lib/pathlib/types.py
Lib/test/test_pathlib/test_read.py

index 85dd9e5b2d6b9a9ee10dcd942258716141072a40..cd8b2a983379d004be073240219a30cf92351ae7 100644 (file)
@@ -287,6 +287,8 @@ class _ReadablePath(_JoinablePath):
         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'
index 938d3a1e987128fd3cc0a51147105642dddb3d83..1a14649fafee801edb03daf6168b13fa8d5cd27a 100644 (file)
@@ -127,6 +127,8 @@ class ReadTestBase:
         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()