GH-127381: pathlib ABCs: remove case_sensitive argument (#131024)

Remove the *case_sensitive* argument from `_JoinablePath.full_match()` and
`_ReadablePath.glob()`. Using a non-native case sensitivity forces the use
of "case-pedantic" globbing, where we `iterdir()` even for non-wildcard
pattern segments. But it's hard to know when to enable this mode, as
case-sensitivity can vary by directory, so `_PathParser.normcase()` doesn't
always give the full picture. The `Path.glob()` implementation is forced to
make an educated guess, but we can avoid the issue in the ABCs by dropping
the *case_sensitive* argument.

(I probably shouldn't have added these arguments in `PurePath` and `Path`
in the first place!)

Also drop support for `_ReadablePath.glob(recurse_symlinks=False)`, which
makes recursive globbing much slower.
This commit is contained in:
Barney Gale 2025-03-10 17:50:48 +00:00 committed by GitHub
parent c3487c941d
commit 93fc3d34f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 30 deletions

View file

@ -433,6 +433,13 @@ class PurePathTest(test_pathlib_abc.JoinablePathTest):
with self.assertWarns(DeprecationWarning):
p.is_reserved()
def test_full_match_case_sensitive(self):
P = self.cls
self.assertFalse(P('A.py').full_match('a.PY', case_sensitive=True))
self.assertTrue(P('A.py').full_match('a.PY', case_sensitive=False))
self.assertFalse(P('c:/a/B.Py').full_match('C:/A/*.pY', case_sensitive=True))
self.assertTrue(P('/a/b/c.py').full_match('/A/*/*.Py', case_sensitive=False))
def test_match_empty(self):
P = self.cls
self.assertRaises(ValueError, P('a').match, '')
@ -2737,6 +2744,18 @@ class PathTest(test_pathlib_abc.RWPathTest, PurePathTest):
self.assertEqual(expect, set(p.glob(P(pattern))))
self.assertEqual(expect, set(p.glob(FakePath(pattern))))
def test_glob_case_sensitive(self):
P = self.cls
def _check(path, pattern, case_sensitive, expected):
actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)}
expected = {str(P(self.base, q)) for q in expected}
self.assertEqual(actual, expected)
path = P(self.base)
_check(path, "DIRB/FILE*", True, [])
_check(path, "DIRB/FILE*", False, ["dirB/fileB"])
_check(path, "dirb/file*", True, [])
_check(path, "dirb/file*", False, ["dirB/fileB"])
@needs_symlinks
def test_glob_dot(self):
P = self.cls