GH-115060: Speed up pathlib.Path.glob() by removing redundant regex matching (#115061)

When expanding and filtering paths for a `**` wildcard segment, build an `re.Pattern` object from the subsequent pattern parts, rather than the entire pattern, and match against the `os.DirEntry` object prior to instantiating a path object. Also skip compiling a pattern when expanding a `*` wildcard segment.
This commit is contained in:
Barney Gale 2024-02-10 18:12:34 +00:00 committed by GitHub
parent 9d1a353230
commit 6f93b4df92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 76 additions and 28 deletions

View file

@ -1250,6 +1250,19 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertEqual(expect, set(p.glob(P(pattern))))
self.assertEqual(expect, set(p.glob(FakePath(pattern))))
@needs_symlinks
def test_glob_dot(self):
P = self.cls
with os_helper.change_cwd(P(self.base, "dirC")):
self.assertEqual(
set(P('.').glob('*')), {P("fileC"), P("novel.txt"), P("dirD")})
self.assertEqual(
set(P('.').glob('**')), {P("fileC"), P("novel.txt"), P("dirD"), P("dirD/fileD"), P(".")})
self.assertEqual(
set(P('.').glob('**/*')), {P("fileC"), P("novel.txt"), P("dirD"), P("dirD/fileD")})
self.assertEqual(
set(P('.').glob('**/*/*')), {P("dirD/fileD")})
def test_rglob_pathlike(self):
P = self.cls
p = P(self.base, "dirC")