]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-79634: Speed up pathlib globbing by removing `joinpath()` call. (#114623)
authorBarney Gale <barney.gale@gmail.com>
Sat, 27 Jan 2024 19:59:51 +0000 (19:59 +0000)
committerGitHub <noreply@github.com>
Sat, 27 Jan 2024 19:59:51 +0000 (19:59 +0000)
Remove `self.joinpath('')` call that should have been removed in 6313cdde.

This makes `PathBase.glob('')` yield itself *without* adding a trailing slash. It's hard to say whether this is more or less correct, but at least everything else is faster, and there's no behaviour change in the public classes where empty glob patterns are disallowed.

Lib/pathlib/_abc.py
Lib/test/test_pathlib/test_pathlib.py
Lib/test/test_pathlib/test_pathlib_abc.py

index 6303a18680befc43ebf74a9c5da7f721399746e4..ad5684829ebc80069039986faf3140fe07bbf0b7 100644 (file)
@@ -771,7 +771,7 @@ class PathBase(PurePathBase):
         filter_paths = False
         deduplicate_paths = False
         sep = self.pathmod.sep
-        paths = iter([self.joinpath('')] if self.is_dir() else [])
+        paths = iter([self] if self.is_dir() else [])
         while stack:
             part = stack.pop()
             if part in specials:
index 9c2b26d41d73f8e0bcd04938220e8da577b5aebd..5ce3b605c58e6373022c222c0586c86d08512849 100644 (file)
@@ -1232,6 +1232,8 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
             list(p.glob(''))
         with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
             list(p.glob('.'))
+        with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
+            list(p.glob('./'))
 
     def test_glob_many_open_files(self):
         depth = 30
index ea70931eaa2c7e65de0cb3d34e4a1807d3b5ef04..ab989cb5503f99df1aed4be7a5a6ebe9fdb5f8d8 100644 (file)
@@ -1733,12 +1733,11 @@ class DummyPathTest(DummyPurePathTest):
         self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"})
 
     def test_glob_empty_pattern(self):
-        def _check(glob, expected):
-            self.assertEqual(set(glob), { P(self.base, q) for q in expected })
         P = self.cls
         p = P(self.base)
-        _check(p.glob(""), [""])
-        _check(p.glob("."), ["."])
+        self.assertEqual(list(p.glob("")), [p])
+        self.assertEqual(list(p.glob(".")), [p / "."])
+        self.assertEqual(list(p.glob("./")), [p / "./"])
 
     def test_glob_case_sensitive(self):
         P = self.cls