pathlib ABCs: remove caching of path parser case sensitivity (#130194)

Remove the caching `_is_case_sensitive()` function. 

The cache used to speed up `PurePath.[full_]match()` and `Path.[r]glob()`,
but that's no longer the case - these methods use
`self.parser is posixpath` to determine case sensitivity.

This makes the `pathlib._abc` module a little easier to backport to Python
3.8, where `functools.cache()` is unavailable.
This commit is contained in:
Barney Gale 2025-02-16 17:08:55 +00:00 committed by GitHub
parent e7f00cd14f
commit 655fc8a0fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,18 +11,12 @@ Three base classes are defined here -- JoinablePath, ReadablePath and
WritablePath. WritablePath.
""" """
import functools
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from glob import _PathGlobber, _no_recurse_symlinks from glob import _PathGlobber, _no_recurse_symlinks
from pathlib import PurePath, Path from pathlib import PurePath, Path
from pathlib._os import magic_open, CopyReader, CopyWriter from pathlib._os import magic_open, CopyReader, CopyWriter
@functools.cache
def _is_case_sensitive(parser):
return parser.normcase('Aa') == 'Aa'
def _explode_path(path): def _explode_path(path):
""" """
Split the path into a 2-tuple (anchor, parts), where *anchor* is the Split the path into a 2-tuple (anchor, parts), where *anchor* is the
@ -201,7 +195,7 @@ class JoinablePath(ABC):
if not isinstance(pattern, JoinablePath): if not isinstance(pattern, JoinablePath):
pattern = self.with_segments(pattern) pattern = self.with_segments(pattern)
if case_sensitive is None: if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser) case_sensitive = self.parser.normcase('Aa') == 'Aa'
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True) globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
match = globber.compile(str(pattern)) match = globber.compile(str(pattern))
return match(str(self)) is not None return match(str(self)) is not None
@ -297,13 +291,12 @@ class ReadablePath(JoinablePath):
anchor, parts = _explode_path(pattern) anchor, parts = _explode_path(pattern)
if anchor: if anchor:
raise NotImplementedError("Non-relative patterns are unsupported") raise NotImplementedError("Non-relative patterns are unsupported")
case_sensitive_default = self.parser.normcase('Aa') == 'Aa'
if case_sensitive is None: if case_sensitive is None:
case_sensitive = _is_case_sensitive(self.parser) case_sensitive = case_sensitive_default
case_pedantic = False
elif case_sensitive == _is_case_sensitive(self.parser):
case_pedantic = False case_pedantic = False
else: else:
case_pedantic = True case_pedantic = case_sensitive_default != case_sensitive
recursive = True if recurse_symlinks else _no_recurse_symlinks recursive = True if recurse_symlinks else _no_recurse_symlinks
globber = _PathGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive) globber = _PathGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive)
select = globber.selector(parts) select = globber.selector(parts)