GH-73435: Implement recursive wildcards in pathlib.PurePath.match() (#101398)

`PurePath.match()` now handles the `**` wildcard as in `Path.glob()`, i.e. it matches any number of path segments.

We now compile a `re.Pattern` object for the entire pattern. This is made more difficult by `fnmatch` not treating directory separators as special when evaluating wildcards (`*`, `?`, etc), and so we arrange the path parts onto separate *lines* in a string, and ensure we don't set `re.DOTALL`.

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Barney Gale 2023-05-30 21:18:09 +01:00 committed by GitHub
parent 4c770617c0
commit 49f90ba1ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 15 deletions

View file

@ -569,6 +569,13 @@ Pure paths provide the following methods and properties:
>>> PurePath('a/b.py').match('/*.py')
False
The *pattern* may be another path object; this speeds up matching the same
pattern against multiple files::
>>> pattern = PurePath('*.py')
>>> PurePath('a/b.py').match(pattern)
True
As with other methods, case-sensitivity follows platform defaults::
>>> PurePosixPath('b.py').match('*.PY')
@ -581,6 +588,10 @@ Pure paths provide the following methods and properties:
.. versionadded:: 3.12
The *case_sensitive* argument.
.. versionchanged:: 3.13
Support for the recursive wildcard "``**``" was added. In previous
versions, it acted like the non-recursive wildcard "``*``".
.. method:: PurePath.relative_to(other, walk_up=False)