Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-73435: Addpathlib.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
barneygale merged 9 commits intopython:mainfrombarneygale:globmatch
Jan 26, 2024

Conversation

barneygale
Copy link
Contributor

@barneygalebarneygale commentedJan 20, 2024
edited
Loading

In#101398 we added support for the recursive wildcard** inpathlib.PurePath.match(). This should allow arbitrary prefix and suffix matching, likep.match('foo/**') orp.match('**/foo'), but there's a problem: for relative patterns only,match() implicitly inserts a** token on the left hand side, causing all patterns to match from the right. As a result, it's impossible to match relative patterns from the left:PurePath('foo/bar').match('bar/**') is true!

This PR reverts the changes tomatch(), and instead adds a newfull_match() method that:

  • Allows empty patterns
  • Supports the recursive wildcard**
  • Matches theentire path when given a relative pattern


📚 Documentation preview 📚:https://cpython-previews--114350.org.readthedocs.build/en/114350/library/pathlib.html#pathlib.PurePath.globmatch

In49f90ba we added support for the recursive wildcard `**` in`pathlib.PurePath.match()`. This should allow arbitrary prefix and suffixmatching, like `p.match('foo/**')` or `p.match('**/foo')`, but there's aproblem: for relative patterns only, `match()` implicitly inserts a `**`token on the left hand side, causing all patterns to match from the right.As a result, it's impossible to match relative patterns from the left:`PurePath('foo/bar').match('bar/**')` is true!This commit reverts the changes to `match()`, and instead adds a new`globmatch()` method that:- Supports the recursive wildcard `**`- Matches the *entire* path when given a relative patternAs a result, `globmatch()`'s pattern language exactly matches that of`glob()`.
@zooba
Copy link
Member

Does the problem extend beyond "** is allowed to match nothing"? Or can we change the existing algorithm to require that a trailing** has to match at least one segment?

Presumably.match("foo\\*") has to find something afterfoo\, so would be a better version of"foo\\**" anyway, but I can't imagine anyone being happy that"foo\\**" matches"foo\\" - you wouldn't specify the** if you didn't wantsomething to be found there.

@barneygale
Copy link
ContributorAuthor

** matching nothing is quite normal I think - it's whatglob.glob(),pathlib.Path.glob(), zsh, and IIRC bash do. If you want at least one segment afterfoo/, you can spell that withfoo/**/*.

@barneygale
Copy link
ContributorAuthor

e.g.zsh man page:

A pathname component of the form '(foo/)#' matches a path consisting of zero or more directories matching the pattern foo.

As a shorthand, '**/' is equivalent to '(*/)#'; note that this therefore matches files in the current directory as well as subdirectories. Thus:

ls (*/)#bar

or

ls **/bar

does a recursive directory search for files named 'bar' (potentially including the file 'bar' in the current directory).

@zooba
Copy link
Member

Okay, I definitely like havingfull in the name somewhere then. It's still a match, and the parallel is fullmatch is way more helpful than to glob (which would just raise questions about "how doesmatch do its matching then?")

barneygale reacted with thumbs up emoji

@barneygalebarneygale changed the titleGH-73435: Addpathlib.PurePath.globmatch()GH-73435: Addpathlib.PurePath.full_match()Jan 22, 2024
Copy link
Member

@zoobazooba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM

@barneygale
Copy link
ContributorAuthor

Thanks for the reviews, both.

hugovk reacted with thumbs up emoji

@barneygalebarneygaleenabled auto-merge (squash)January 26, 2024 00:51
@barneygalebarneygale merged commitb69548a intopython:mainJan 26, 2024
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
In49f90ba we added support for the recursive wildcard `**` in`pathlib.PurePath.match()`. This should allow arbitrary prefix and suffixmatching, like `p.match('foo/**')` or `p.match('**/foo')`, but there's aproblem: for relative patterns only, `match()` implicitly inserts a `**`token on the left hand side, causing all patterns to match from the right.As a result, it's impossible to match relative patterns from the left:`PurePath('foo/bar').match('bar/**')` is true!This commit reverts the changes to `match()`, and instead adds a new`full_match()` method that:- Allows empty patterns- Supports the recursive wildcard `**`- Matches the *entire* path when given a relative pattern
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
In49f90ba we added support for the recursive wildcard `**` in`pathlib.PurePath.match()`. This should allow arbitrary prefix and suffixmatching, like `p.match('foo/**')` or `p.match('**/foo')`, but there's aproblem: for relative patterns only, `match()` implicitly inserts a `**`token on the left hand side, causing all patterns to match from the right.As a result, it's impossible to match relative patterns from the left:`PurePath('foo/bar').match('bar/**')` is true!This commit reverts the changes to `match()`, and instead adds a new`full_match()` method that:- Allows empty patterns- Supports the recursive wildcard `**`- Matches the *entire* path when given a relative pattern
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@hugovkhugovkhugovk left review comments

@zoobazoobazooba left review comments

@jaracojaracoAwaiting requested review from jaraco

@AlexWaygoodAlexWaygoodAwaiting requested review from AlexWaygood

Assignees
No one assigned
Labels
skip newstopic-pathlibtype-featureA feature request or enhancement
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@barneygale@zooba@hugovk

[8]ページ先頭

©2009-2025 Movatter.jp