Skip to content

Commit 93fc3d3

Browse files
authored
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.
1 parent c3487c9 commit 93fc3d3

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

Lib/pathlib/types.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
from abc import ABC, abstractmethod
14-
from glob import _PathGlobber, _no_recurse_symlinks
14+
from glob import _PathGlobber
1515
from pathlib import PurePath, Path
1616
from pathlib._os import magic_open, ensure_distinct_paths, copy_file
1717
from typing import Optional, Protocol, runtime_checkable
@@ -216,15 +216,14 @@ def parents(self):
216216
parent = split(path)[0]
217217
return tuple(parents)
218218

219-
def full_match(self, pattern, *, case_sensitive=None):
219+
def full_match(self, pattern):
220220
"""
221221
Return True if this path matches the given glob-style pattern. The
222222
pattern is matched against the entire path.
223223
"""
224224
if not hasattr(pattern, 'with_segments'):
225225
pattern = self.with_segments(pattern)
226-
if case_sensitive is None:
227-
case_sensitive = self.parser.normcase('Aa') == 'Aa'
226+
case_sensitive = self.parser.normcase('Aa') == 'Aa'
228227
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
229228
match = globber.compile(str(pattern), altsep=pattern.parser.altsep)
230229
return match(str(self)) is not None
@@ -279,7 +278,7 @@ def iterdir(self):
279278
"""
280279
raise NotImplementedError
281280

282-
def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
281+
def glob(self, pattern, *, recurse_symlinks=True):
283282
"""Iterate over this subtree and yield all existing files (of any
284283
kind, including directories) matching the given relative pattern.
285284
"""
@@ -288,14 +287,10 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
288287
anchor, parts = _explode_path(pattern)
289288
if anchor:
290289
raise NotImplementedError("Non-relative patterns are unsupported")
291-
case_sensitive_default = self.parser.normcase('Aa') == 'Aa'
292-
if case_sensitive is None:
293-
case_sensitive = case_sensitive_default
294-
case_pedantic = False
295-
else:
296-
case_pedantic = case_sensitive_default != case_sensitive
297-
recursive = True if recurse_symlinks else _no_recurse_symlinks
298-
globber = _PathGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive)
290+
elif not recurse_symlinks:
291+
raise NotImplementedError("recurse_symlinks=False is unsupported")
292+
case_sensitive = self.parser.normcase('Aa') == 'Aa'
293+
globber = _PathGlobber(self.parser.sep, case_sensitive, recursive=True)
299294
select = globber.selector(parts)
300295
return select(self.joinpath(''))
301296

Lib/test/test_pathlib/test_join.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ def test_full_match(self):
130130
self.assertFalse(P('a/b/c.py').full_match('**/a/b/c./**'))
131131
self.assertFalse(P('a/b/c.py').full_match('/a/b/c.py/**'))
132132
self.assertFalse(P('a/b/c.py').full_match('/**/a/b/c.py'))
133-
# Case-sensitive flag
134-
self.assertFalse(P('A.py').full_match('a.PY', case_sensitive=True))
135-
self.assertTrue(P('A.py').full_match('a.PY', case_sensitive=False))
136-
self.assertFalse(P('c:/a/B.Py').full_match('C:/A/*.pY', case_sensitive=True))
137-
self.assertTrue(P('/a/b/c.py').full_match('/A/*/*.Py', case_sensitive=False))
138133
# Matching against empty path
139134
self.assertFalse(P('').full_match('*'))
140135
self.assertTrue(P('').full_match('**'))

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ def test_is_reserved_deprecated(self):
433433
with self.assertWarns(DeprecationWarning):
434434
p.is_reserved()
435435

436+
def test_full_match_case_sensitive(self):
437+
P = self.cls
438+
self.assertFalse(P('A.py').full_match('a.PY', case_sensitive=True))
439+
self.assertTrue(P('A.py').full_match('a.PY', case_sensitive=False))
440+
self.assertFalse(P('c:/a/B.Py').full_match('C:/A/*.pY', case_sensitive=True))
441+
self.assertTrue(P('/a/b/c.py').full_match('/A/*/*.Py', case_sensitive=False))
442+
436443
def test_match_empty(self):
437444
P = self.cls
438445
self.assertRaises(ValueError, P('a').match, '')
@@ -2737,6 +2744,18 @@ def test_glob_pathlike(self):
27372744
self.assertEqual(expect, set(p.glob(P(pattern))))
27382745
self.assertEqual(expect, set(p.glob(FakePath(pattern))))
27392746

2747+
def test_glob_case_sensitive(self):
2748+
P = self.cls
2749+
def _check(path, pattern, case_sensitive, expected):
2750+
actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)}
2751+
expected = {str(P(self.base, q)) for q in expected}
2752+
self.assertEqual(actual, expected)
2753+
path = P(self.base)
2754+
_check(path, "DIRB/FILE*", True, [])
2755+
_check(path, "DIRB/FILE*", False, ["dirB/fileB"])
2756+
_check(path, "dirb/file*", True, [])
2757+
_check(path, "dirb/file*", False, ["dirB/fileB"])
2758+
27402759
@needs_symlinks
27412760
def test_glob_dot(self):
27422761
P = self.cls

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,18 +709,6 @@ def test_glob_empty_pattern(self):
709709
p = P(self.base)
710710
self.assertEqual(list(p.glob("")), [p.joinpath("")])
711711

712-
def test_glob_case_sensitive(self):
713-
P = self.cls
714-
def _check(path, pattern, case_sensitive, expected):
715-
actual = {str(q) for q in path.glob(pattern, case_sensitive=case_sensitive)}
716-
expected = {str(P(self.base, q)) for q in expected}
717-
self.assertEqual(actual, expected)
718-
path = P(self.base)
719-
_check(path, "DIRB/FILE*", True, [])
720-
_check(path, "DIRB/FILE*", False, ["dirB/fileB"])
721-
_check(path, "dirb/file*", True, [])
722-
_check(path, "dirb/file*", False, ["dirB/fileB"])
723-
724712
def test_info_exists(self):
725713
p = self.cls(self.base)
726714
self.assertTrue(p.info.exists())

0 commit comments

Comments
 (0)