Skip to content

Commit

Permalink
pythonGH-79634: Speed up globbing by removing joinpath() call.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
barneygale committed Jan 26, 2024
1 parent b69548a commit 81dcf58
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None):
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:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,8 @@ def test_glob_empty_pattern(self):
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
Expand Down
7 changes: 3 additions & 4 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,12 +1097,11 @@ def _check(glob, expected):
_check(p.glob("*/"), ["dirA/", "dirB/", "dirC/", "dirE/", "linkB/"])

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
Expand Down

0 comments on commit 81dcf58

Please sign in to comment.