]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
GH-126363: Speed up pattern parsing in `pathlib.Path.glob()` (#126364)
authorBarney Gale <barney.gale@gmail.com>
Mon, 4 Nov 2024 19:29:57 +0000 (19:29 +0000)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2024 19:29:57 +0000 (19:29 +0000)
commit9b7294c3a560f43f1e26a0f48c258829076d6464
treebc6936818bae49242cebd431545cd380e853149a
parent2e95c5ba3bf7e5004c7e2304afda4a8f8e2443a7
GH-126363: Speed up pattern parsing in `pathlib.Path.glob()` (#126364)

The implementation of `Path.glob()` does rather a hacky thing: it calls
`self.with_segments()` to convert the given pattern to a `Path` object, and
then peeks at the private `_raw_path` attribute to see if pathlib removed a
trailing slash from the pattern.

In this patch, we make `glob()` use a new `_parse_pattern()` classmethod
that splits the pattern into parts while preserving information about any
trailing slash. This skips the cost of creating a `Path` object, and avoids
some path anchor normalization, which makes `Path.glob()` slightly faster.
But mostly it's about making the code less naughty.

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
Lib/pathlib/_local.py
Misc/NEWS.d/next/Library/2024-11-03-14-43-51.gh-issue-126363.Xus7vU.rst [new file with mode: 0644]