aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/pathlib/_abc.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-04-12 22:19:21 +0100
committerGitHub <noreply@github.com>2024-04-12 22:19:21 +0100
commit0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e (patch)
tree2cccc9ca7a407f88e57b1bf1b7406a1c73005ce2 /Lib/pathlib/_abc.py
parent069de14cb948f56b37e507f367b99c5563d3685e (diff)
downloadcpython-0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e.tar.gz
cpython-0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e.zip
GH-115060: Speed up `pathlib.Path.glob()` by not scanning literal parts (#117732)
Don't bother calling `os.scandir()` to scan for literal pattern segments, like `foo` in `foo/*.py`. Instead, append the segment(s) as-is and call through to the next selector with `exists=False`, which signals that the path might not exist. Subsequent selectors will call `os.scandir()` or `os.lstat()` to filter out missing paths as needed.
Diffstat (limited to 'Lib/pathlib/_abc.py')
-rw-r--r--Lib/pathlib/_abc.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index b6cab0d285a..b51ad6f46d2 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -686,8 +686,14 @@ class PathBase(PurePathBase):
def _glob_selector(self, parts, case_sensitive, recurse_symlinks):
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser)
+ case_pedantic = False
+ else:
+ # The user has expressed a case sensitivity choice, but we don't
+ # know the case sensitivity of the underlying filesystem, so we
+ # must use scandir() for everything, including non-wildcard parts.
+ case_pedantic = True
recursive = True if recurse_symlinks else glob._no_recurse_symlinks
- globber = self._globber(self.parser.sep, case_sensitive, recursive)
+ globber = self._globber(self.parser.sep, case_sensitive, case_pedantic, recursive)
return globber.selector(parts)
def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):