Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions Lib/pathlib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


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

def full_match(self, pattern, *, case_sensitive=None):
def full_match(self, pattern):
"""
Return True if this path matches the given glob-style pattern. The
pattern is matched against the entire path.
"""
if not hasattr(pattern, 'with_segments'):
pattern = self.with_segments(pattern)
if case_sensitive is None:
case_sensitive = self.parser.normcase('Aa') == 'Aa'
case_sensitive = self.parser.normcase('Aa') == 'Aa'
globber = _PathGlobber(pattern.parser.sep, case_sensitive, recursive=True)
match = globber.compile(str(pattern), altsep=pattern.parser.altsep)
return match(str(self)) is not None
Expand Down Expand Up @@ -279,7 +278,7 @@ def iterdir(self):
"""
raise NotImplementedError

def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
def glob(self, pattern, *, recurse_symlinks=True):
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
"""
Expand All @@ -288,14 +287,10 @@ def glob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
anchor, parts = _explode_path(pattern)
if anchor:
raise NotImplementedError("Non-relative patterns are unsupported")
case_sensitive_default = self.parser.normcase('Aa') == 'Aa'
if case_sensitive is None:
case_sensitive = case_sensitive_default
case_pedantic = False
else:
case_pedantic = case_sensitive_default != case_sensitive
recursive = True if recurse_symlinks else _no_recurse_symlinks
globber = _PathGlobber(self.parser.sep, case_sensitive, case_pedantic, recursive)
elif not recurse_symlinks:
raise NotImplementedError("recurse_symlinks=False is unsupported")
case_sensitive = self.parser.normcase('Aa') == 'Aa'
globber = _PathGlobber(self.parser.sep, case_sensitive, recursive=True)
select = globber.selector(parts)
return select(self.joinpath(''))

Expand Down
5 changes: 0 additions & 5 deletions Lib/test/test_pathlib/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ def test_full_match(self):
self.assertFalse(P('a/b/c.py').full_match('**/a/b/c./**'))
self.assertFalse(P('a/b/c.py').full_match('/a/b/c.py/**'))
self.assertFalse(P('a/b/c.py').full_match('/**/a/b/c.py'))
# Case-sensitive flag
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))
# Matching against empty path
self.assertFalse(P('').full_match('*'))
self.assertTrue(P('').full_match('**'))
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ def test_is_reserved_deprecated(self):
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, '')
Expand Down Expand Up @@ -2737,6 +2744,18 @@ def test_glob_pathlike(self):
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
Expand Down
12 changes: 0 additions & 12 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,18 +709,6 @@ def test_glob_empty_pattern(self):
p = P(self.base)
self.assertEqual(list(p.glob("")), [p.joinpath("")])

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"])

def test_info_exists(self):
p = self.cls(self.base)
self.assertTrue(p.info.exists())
Expand Down
Loading