8000 GH-73435: Add `pathlib.PurePath.full_match()` by barneygale · Pull Request #114350 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73435: Add pathlib.PurePath.full_match() #114350

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 9 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
globmatch --> full_match
  • Loading branch information
barneygale committed Jan 22, 2024
commit aaa1ddb3b87bb97f4c52c8a2d638f860c66712f5
5 changes: 3 additions & 2 deletions Doc/library/glob.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ The :mod:`glob` module defines the following functions:

.. seealso::

:meth:`pathlib.PurePath.globmatch` and :meth:`pathlib.Path.glob` methods,
which call this function to implement pattern matching and globbing.
:meth:`pathlib.PurePath.full_match` and :meth:`pathlib.Path.glob`
methods, which call this function to implement pattern matching and
globbing.

.. versionadded:: 3.13

Expand Down
16 changes: 8 additions & 8 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -552,25 +552,25 @@ Pure paths provide the following methods and properties:
PureWindowsPath('c:/Program Files')


.. method:: PurePath.globmatch(pattern, *, case_sensitive=None)
.. method:: PurePath.full_match(pattern, *, case_sensitive=None)

Match this path against the provided glob-style pattern. Return ``True``
if matching is successful, ``False`` otherwise. For example::

>>> PurePath('a/b.py').globmatch('a/*.py')
>>> PurePath('a/b.py').full_match('a/*.py')
True
>>> PurePath('a/b.py').globmatch('*.py')
>>> PurePath('a/b.py').full_match('*.py')
False
>>> PurePath('/a/b/c.py').globmatch('/a/**')
>>> PurePath('/a/b/c.py').full_match('/a/**')
True
>>> PurePath('/a/b/c.py').globmatch('**/*.py')
>>> PurePath('/a/b/c.py').full_match('**/*.py')
True

As with other methods, case-sensitivity follows platform defaults::

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

Set *case_sensitive* to ``True`` or ``False`` to override this behaviour.
Expand All @@ -583,7 +583,7 @@ Pure paths provide the following methods and properties:
Match this path against the provided non-recursive glob-style pattern.
Return ``True`` if matching is successful, ``False`` otherwise.

This method is similar to :meth:`~PurePath.globmatch`, but the recursive
This method is similar to :meth:`~PurePath.full_match`, but the recursive
wildcard "``**``" is not supported (it acts like non-recursive "``*``"),
and if a relative pattern is given, then matching is done from the right::

Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ pathlib
object from a 'file' URI (``file:/``).
(Contributed by Barney Gale in :gh:`107465`.)

* Add :meth:`pathlib.PurePath.globmatch` for matching paths with
* Add :meth:`pathlib.PurePath.full_match` for matching paths with
shell-style wildcards, including the recursive wildcard "``**``".
(Contributed by Barney Gale in :gh:`73435`.)

Expand Down
2 changes: 1 addition & 1 deletion Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def match(self, path_pattern, *, case_sensitive=None):
return False
return True

def globmatch(self, pattern, *, case_sensitive=None):
def full_match(self, pattern, *, case_sensitive=None):
"""
Return True if this path matches the given glob-style pattern.
"""
Expand Down
112 changes: 56 additions & 56 deletions Lib/test/test_pathlib/test_pathlib_abc.py
4F4A
Original file line number Diff line number Diff line change
Expand Up @@ -261,71 +261,71 @@ def test_match_common(self):
self.assertFalse(P('').match('**'))
self.assertFalse(P('').match('**/*'))

def test_globmatch_common(self):
def test_full_match_common(self):
P = self.cls
# Simple relative pattern.
self.assertTrue(P('b.py').globmatch('b.py'))
self.assertFalse(P('a/b.py').globmatch('b.py'))
self.assertFalse(P('/a/b.py').globmatch('b.py'))
self.assertFalse(P('a.py').globmatch('b.py'))
self.assertFalse(P('b/py').globmatch('b.py'))
self.assertFalse(P('/a.py').globmatch('b.py'))
self.assertFalse(P('b.py/c').globmatch('b.py'))
self.assertTrue(P('b.py').full_match('b.py'))
self.assertFalse(P('a/b.py').full_match('b.py'))
self.assertFalse(P('/a/b.py').full_match('b.py'))
self.assertFalse(P('a.py').full_match('b.py'))
self.assertFalse(P('b/py').full_match('b.py'))
self.assertFalse(P('/a.py').full_match('b.py'))
self.assertFalse(P('b.py/c').full_match('b.py'))
# Wildcard relative pattern.
self.assertTrue(P('b.py').globmatch('*.py'))
self.assertFalse(P('a/b.py').globmatch('*.py'))
self.assertFalse(P('/a/b.py').globmatch('*.py'))
self.assertFalse(P('b.pyc').globmatch('*.py'))
self.assertFalse(P('b./py').globmatch('*.py'))
self.assertFalse(P('b.py/c').globmatch('*.py'))
self.assertTrue(P('b.py').full_match('*.py'))
self.assertFalse(P('a/b.py').full_match('*.py'))
self.assertFalse(P('/a/b.py').full_match('*.py'))
self.assertFalse(P('b.pyc').full_match('*.py'))
self.assertFalse(P('b./py').full_match('*.py'))
self.assertFalse(P('b.py/c').full_match('*.py'))
# Multi-part relative pattern.
self.assertTrue(P('ab/c.py').globmatch('a*/*.py'))
self.assertFalse(P('/d/ab/c.py').globmatch('a*/*.py'))
self.assertFalse(P('a.py').globmatch('a*/*.py'))
self.assertFalse(P('/dab/c.py').globmatch('a*/*.py'))
self.assertFalse(P('ab/c.py/d').globmatch('a*/*.py'))
self.assertTrue(P('ab/c.py').full_match('a*/*.py'))
self.assertFalse(P('/d/ab/c.py').full_match('a*/*.py'))
self.assertFalse(P('a.py').full_match('a*/*.py'))
self.assertFalse(P('/dab/c.py').full_match('a*/*.py'))
self.assertFalse(P('ab/c.py/d').full_match('a*/*.py'))
# Absolute pattern.
self.assertTrue(P('/b.py').globmatch('/*.py'))
self.assertFalse(P('b.py').globmatch('/*.py'))
self.assertFalse(P('a/b.py').globmatch('/*.py'))
self.assertFalse(P('/a/b.py').globmatch('/*.py'))
self.assertTrue(P('/b.py').full_match('/*.py'))
self.assertFalse(P('b.py').full_match('/*.py'))
self.assertFalse(P('a/b.py').full_match('/*.py'))
self.assertFalse(P('/a/b.py').full_match('/*.py'))
# Multi-part absolute pattern.
self.assertTrue(P('/a/b.py').globmatch('/a/*.py'))
self.assertFalse(P('/ab.py').globmatch('/a/*.py'))
self.assertFalse(P('/a/b/c.py').globmatch('/a/*.py'))
self.assertTrue(P('/a/b.py').full_match('/a/*.py'))
self.assertFalse(P('/ab.py').full_match('/a/*.py'))
self.assertFalse(P('/a/b/c.py').full_match('/a/*.py'))
# Multi-part glob-style pattern.
self.assertTrue(P('a').globmatch('**'))
self.assertTrue(P('c.py').globmatch('**'))
self.assertTrue(P('a/b/c.py').globmatch('**'))
self.assertTrue(P('/a/b/c.py').globmatch('**'))
self.assertTrue(P('/a/b/c.py').globmatch('/**'))
self.assertTrue(P('/a/b/c.py').globmatch('/a/**'))
self.assertTrue(P('/a/b/c.py').globmatch('**/*.py'))
self.assertTrue(P('/a/b/c.py').globmatch('/**/*.py'))
self.assertTrue(P('/a/b/c.py').globmatch('/a/**/*.py'))
self.assertTrue(P('/a/b/c.py').globmatch('/a/b/**/*.py'))
self.assertTrue(P('/a/b/c.py').globmatch('/**/**/**/**/*.py'))
self.assertFalse(P('c.py').globmatch('**/a.py'))
self.assertFalse(P('c.py').globmatch('c/**'))
self.assertFalse(P('a/b/c.py').globmatch('**/a'))
self.assertFalse(P('a/b/c.py').globmatch('**/a/b'))
self.assertFalse(P('a/b/c.py').globmatch('**/a/b/c'))
self.assertFalse(P('a/b/c.py').globmatch('**/a/b/c.'))
self.assertFalse(P('a/b/c.py').globmatch('**/a/b/c./**'))
self.assertFalse(P('a/b/c.py').globmatch('**/a/b/c./**'))
self.assertFalse(P('a/b/c.py').globmatch('/a/b/c.py/**'))
self.assertFalse(P('a/b/c.py').globmatch('/**/a/b/c.py'))
self.assertRaises(ValueError, P('a').globmatch, '**a/b/c')
self.assertRaises(ValueError, P('a').globmatch, 'a/b/c**')
self.assertTrue(P('a').full_match('**'))
self.assertTrue(P('c.py').full_match('**'))
self.assertTrue(P('a/b/c.py').full_match('**'))
self.assertTrue(P('/a/b/c.py').full_match('**'))
self.assertTrue(P('/a/b/c.py').full_match('/**'))
self.assertTrue(P('/a/b/c.py').full_match('/a/**'))
self.assertTrue(P('/a/b/c.py').full_match('**/*.py'))
self.assertTrue(P('/a/b/c.py').full_match('/**/*.py'))
self.assertTrue(P('/a/b/c.py').full_match('/a/**/*.py'))
self.assertTrue(P('/a/b/c.py').full_match('/a/b/**/*.py'))
self.assertTrue(P('/a/b/c.py').full_match('/**/**/**/**/*.py'))
self.assertFalse(P('c.py').full_match('**/a.py'))
self.assertFalse(P('c.py').full_match('c/**'))
self.assertFalse(P('a/b/c.py').full_match('**/a'))
self.assertFalse(P('a/b/c.py').full_match('**/a/b'))
self.assertFalse(P('a/b/c.py').full_match('**/a/b/c'))
self.assertFalse(P('a/b/c.py').full_match('**/a/b/c.'))
self.assertFalse(P('a/b/c.py').full_match('**/a/b/c./**'))
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'))
self.assertRaises(ValueError, P('a').full_match, '**a/b/c')
self.assertRaises(ValueError, P('a').full_match, 'a/b/c**')
# Case-sensitive flag
self.assertFalse(P('A.py').globmatch('a.PY', case_sensitive=True))
self.assertTrue(P('A.py').globmatch('a.PY', case_sensitive=False))
self.assertFalse(P('c:/a/B.Py').globmatch('C:/A/*.pY', case_sensitive=True))
self.assertTrue(P('/a/b/c.py').globmatch('/A/*/*.Py', case_sensitive=False))
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('').globmatch('*'))
self.assertTrue(P('').globmatch('**'))
self.assertFalse(P('').globmatch('**/*'))
self.assertFalse(P('').full_match('*'))
self.assertTrue(P('').full_match('**'))
self.assertFalse(P('').full_match('**/*'))

def test_parts_common(self):
# `parts` returns a tuple.
Expand Down
0