Skip to content
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

gh-104484: Add parameter @case_sensitive to pathlib.PurePath.match() function #104565

Merged
merged 11 commits into from
May 18, 2023
Prev Previous commit
Next Next commit
Update document
  • Loading branch information
thirumurugan-git authored and thirumurugan.ka committed May 18, 2023
commit 466abc2e819c73cf1c8994917d0bb38f69082d1f
9 changes: 0 additions & 9 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,6 @@ Pure paths provide the following methods and properties:
>>> PureWindowsPath('b.py').match('*.PY')
True

By default, or when the *case_sensitive* keyword-only argument is set to
``None``, this method matches paths using platform-specific casing rules:
typically, case-sensitive on POSIX, and case-insensitive on Windows::

>>> PurePosixPath('b.py').match('*.PY')
False
>>> PureWindowsPath('b.py').match('*.PY')
True

Set *case_sensitive* to ``True`` or ``False`` to override this behaviour.

.. versionadded:: 3.12
Expand Down
9 changes: 3 additions & 6 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ def match(self, path_pattern, *, case_sensitive=None):
"""
Return True if this path matches the given pattern.
"""
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self._flavour)
pat = self.with_segments(path_pattern)
if not pat.parts:
raise ValueError("empty pattern")
Expand All @@ -700,13 +702,8 @@ def match(self, path_pattern, *, case_sensitive=None):
return False
elif len(pat_parts) > len(parts):
return False
# Generate regex flag based on |case_sensitive| parameter.
if case_sensitive is None:
case_sensitive = _is_case_sensitive(self._flavour)
flags = re.NOFLAG if case_sensitive else re.IGNORECASE

for part, pat in zip(reversed(parts), reversed(pat_parts)):
match = _compile_pattern(pat, flags)
match = _compile_pattern(pat, case_sensitive)
if not match(part):
return False
return True
Expand Down