Skip to content

Commit 655fc8a

Browse files
authored
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.
1 parent e7f00cd commit 655fc8a

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

Lib/pathlib/_abc.py

+4-11
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@
1111
WritablePath.
1212
"""
1313

14-
import functools
1514
from abc import ABC, abstractmethod
1615
from glob import _PathGlobber, _no_recurse_symlinks
1716
from pathlib import PurePath, Path
1817
from pathlib._os import magic_open, CopyReader, CopyWriter
1918

2019

21-
@functools.cache
22-
def _is_case_sensitive(parser):
23-
return parser.normcase('Aa') == 'Aa'
24-
25-
2620
def _explode_path(path):
2721
"""
2822
Split the path into a 2-tuple (anchor, parts), where *anchor* is the
@@ -201,7 +195,7 @@ def full_match(self, pattern, *, case_sensitive=None):
201195
if not isinstance(pattern, JoinablePath):
202196
pattern = self.with_segments(pattern)
203197
if case_sensitive is None:
204-
case_sensitive = _is_case_sensitive(self.parser)
198+
case_sensitive = self.parser.normcase('Aa') == 'Aa'
205199
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
206200
match = globber.compile(str(pattern))
207201
return match(str(self)) is not None
@@ -297,13 +291,12 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
297291
anchor, parts = _explode_path(pattern)
298292
if anchor:
299293
raise NotImplementedError("Non-relative patterns are unsupported")
294+
case_sensitive_default = self.parser.normcase('Aa') == 'Aa'
300295
if case_sensitive is None:
301-
case_sensitive = _is_case_sensitive(self.parser)
302-
case_pedantic = False
303-
elif case_sensitive == _is_case_sensitive(self.parser):
296+
case_sensitive = case_sensitive_default
304297
case_pedantic = False
305298
else:
306-
case_pedantic = True
299+
case_pedantic = case_sensitive_default != case_sensitive
307300
recursive = True if recurse_symlinks else _no_recurse_symlinks
308301
globber = _PathGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive)
309302
select = globber.selector(parts)

0 commit comments

Comments
 (0)