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

Appearance settings

gh-123135: add support for path-like objects in fnmatch.filter on POSIX platforms #123122

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

Closed

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Aug 18, 2024

This fixes an inconsistency in the fnmatch module where path-like objects are allowed on Windows but not on POSIX platforms.

cc @barneygale since you were recently interested in fnmatch!

@picnixz picnixz added tests Tests in the Lib/test dir skip issue skip news labels Aug 18, 2024
Lib/test/test_fnmatch.py Outdated Show resolved Hide resolved
Lib/test/test_fnmatch.py Outdated Show resolved Hide resolved
@barneygale
Copy link
Contributor

Users might already expect that filter() accepts path-like names, because its docs say:

It is the same as [n for n in names if fnmatch(n, pat)], but implemented more efficiently.

And the fnmatch() docs say:

Both parameters are case-normalized using os.path.normcase()

And the normcase() docs say:

Accepts a path-like object.

So I think we should fix the discrepancy and explicitly support path-like names.

@picnixz
Copy link
Member Author

picnixz commented Aug 19, 2024

Let's fix it then. I think I'll make it a bugfix rather than a feature.

@picnixz picnixz changed the title Add tests for platform dependent fnmatch.filter behaviours gh-123135: fix path-like objects support in fnmatch.filter Aug 19, 2024
@picnixz picnixz added stdlib Python modules in the Lib dir and removed skip issue skip news tests Tests in the Lib/test dir labels Aug 19, 2024
picnixz added a commit to picnixz/cpython that referenced this pull request Aug 19, 2024
@sobolevn sobolevn requested a review from barneygale August 20, 2024 13:26
Copy link
Contributor

@barneygale barneygale left a comment

Choose a reason for hiding this comment

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

Looking good. It's worth a .. versionchanged:: directive in the docs to point out that path-like support was added in 3.14, and that in previous versions it only worked on Windows.

Lib/fnmatch.py Outdated Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Aug 20, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@picnixz
Copy link
Member Author

picnixz commented Aug 21, 2024

I have made the requested changes; please review again. By the way, I'm wondering whether we should explicitly call os.fspath in fnmatchcase actually so that all functions have path-like support:

 def fnmatchcase(name, pat):
     """Test whether FILENAME matches PATTERN, including case. 

     This is a version of fnmatch() which doesn't case-normalize
     its arguments.
     """
     match = _compile_pattern(pat)
-    return match(name) is not None
+    return match(os.fspath(name)) is not None

@bedevere-app
Copy link

bedevere-app bot commented Aug 21, 2024

Thanks for making the requested changes!

@barneygale: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from barneygale August 21, 2024 07:36
@picnixz picnixz changed the title gh-123135: fix path-like objects support in fnmatch.filter gh-123135: add support for path-like objects in fnmatch.filter on POSIX platforms Aug 21, 2024
@@ -0,0 +1,3 @@
Added support for supplying :term:`path-like objects <path-like object>`
to the *names* parameter of :func:`fnmatch.filter`. Previously, such
objects were only accepted on non-POSIX platforms. Patch by Bénédikt Tran.
Copy link
Contributor

Choose a reason for hiding this comment

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

The only non-POSIX platform that we support is Windows:

Suggested change
objects were only accepted on non-POSIX platforms. Patch by Bénédikt Tran.
objects were only accepted on Windows. Patch by Bénédikt Tran.

Copy link
Member Author

Choose a reason for hiding this comment

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

What about WASI?

Lib/test/test_fnmatch.py Outdated Show resolved Hide resolved
@barneygale
Copy link
Contributor

By the way, I'm wondering whether we should explicitly call os.fspath in fnmatchcase actually so that all functions have path-like support

That sounds reasonable to me!

@picnixz
Copy link
Member Author

picnixz commented Aug 22, 2024

See #123215 (comment) for the rationale of marking it as DO-NOT-MERGE for now.

@picnixz
Copy link
Member Author

picnixz commented Aug 26, 2024

See #123215 (comment) for the rationale of closing this PR.

@picnixz picnixz closed this Aug 26, 2024
@picnixz picnixz deleted the fnmatch-test-normcase-for-posix-vs-windows branch August 26, 2024 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.