Skip to content

Commit 823a38a

Browse files
authored
GH-79634: Speed up pathlib globbing by removing joinpath() call. (#114623)
Remove `self.joinpath('')` call that should have been removed in 6313cdd. 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.
1 parent 7a47054 commit 823a38a

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

Lib/pathlib/_abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
771771
filter_paths = False
772772
deduplicate_paths = False
773773
sep = self.pathmod.sep
774-
paths = iter([self.joinpath('')] if self.is_dir() else [])
774+
paths = iter([self] if self.is_dir() else [])
775775
while stack:
776776
part = stack.pop()
777777
if part in specials:

Lib/test/test_pathlib/test_pathlib.py

+2
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@ def test_glob_empty_pattern(self):
12321232
list(p.glob(''))
12331233
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
12341234
list(p.glob('.'))
1235+
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
1236+
list(p.glob('./'))
12351237

12361238
def test_glob_many_open_files(self):
12371239
depth = 30

Lib/test/test_pathlib/test_pathlib_abc.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1733,12 +1733,11 @@ def test_glob_windows(self):
17331733
self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"})
17341734

17351735
def test_glob_empty_pattern(self):
1736-
def _check(glob, expected):
1737-
self.assertEqual(set(glob), { P(self.base, q) for q in expected })
17381736
P = self.cls
17391737
p = P(self.base)
1740-
_check(p.glob(""), [""])
1741-
_check(p.glob("."), ["."])
1738+
self.assertEqual(list(p.glob("")), [p])
1739+
self.assertEqual(list(p.glob(".")), [p / "."])
1740+
self.assertEqual(list(p.glob("./")), [p / "./"])
17421741

17431742
def test_glob_case_sensitive(self):
17441743
P = self.cls

0 commit comments

Comments
 (0)