Repository navigation
gh-160900: support path-like objects in fnmatch filters - #160980
MonikaDvorackova wants to merge 6 commits into
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
|
Thank you for the review. I've addressed the requested changes:
The list comprehension was consistently the fastest, reducing execution time by approximately 18–21% compared with the previous implementation. I've updated All 26 The benchmark results are preliminary measurements from a CPython 3.16 debug build using |
Documentation build overview
5 files changed ·
|
Please don't. Use a release build or one with a |
|
|
||
| The filenames may be strings or path-like objects. Each filename is | ||
| normalized using :func:`os.path.normcase` for matching; the original | ||
| objects are preserved in the returned list. |
There was a problem hiding this comment.
Please add a .. versionchanged:: next to indicate that path-like objects are supported. Look at the various os.path.* functions to see how we formulate this.
There was a problem hiding this comment.
This part of the documentation may need to be changed.
| It is the same as ``[n for n in names if fnmatch(n, pat)]``, | ||
| but implemented more efficiently. | ||
|
|
||
| The filenames may be strings or path-like objects. Each filename is |
There was a problem hiding this comment.
I think we should keep "strings" only here. The term "string" was not just for str, but for well-encoded ones. So instead, update the definition of "filename strings" above. If needed you can use an anonymous label at the place where we define them and add a ref in the free text so that people are not confused by what "strings" here mean.
| but implemented more efficiently. | ||
|
|
||
| The filenames may be strings or path-like objects. Each filename is | ||
| normalized using :func:`os.path.normcase` for matching; the original |
There was a problem hiding this comment.
This is redundant as we say it's equivalent to call fnmatch(n, pat)
| self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'), | ||
| [b'Python', b'Perl']) | ||
|
|
||
| def test_filter_pathlike(self): |
There was a problem hiding this comment.
We also need a test where you mix path objects with bytes and those with strings (add test_mix_bytes_str_pathlike)
|
Thanks for the review. I've pushed the documentation updates and added I also reran the performance comparison using a CPython 3.16 release build rather than a debug build. The benchmark compares three implementations:
The input consists of filename strings matched against Selected results:
The list comprehension was faster than the lambda-based implementation in 11 of the 12 tested scenarios. However, some measurements showed substantial variability, so these results should be treated as preliminary rather than definitive. I haven't yet reproduced the benchmark scripts from the original |
|
Please don't use an LLM to post your comments. I don't want to communicate with an agent only. YOur comments are not edited at since the markdown contains the HTML itself. |
Fixes #160900.
Ensure fnmatch.filter() and fnmatch.filterfalse() normalize every name with os.path.normcase(), including on POSIX. This lets path-like objects such as pathlib.Path work consistently across platforms. Add regression tests for both functions.
fnmatch.filterandfilterfalsework with pathlikes only on non-Posix platforms #160900