Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-136507: Fix mimetypes CLI to handle multiple file parameters#136508
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Wulian233 commented Jul 14, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Supplement: #136507 (comment) , so this only in 3.14 and 3.15. CC: @hugovk could you review this? |
mpkocher commented Jul 17, 2025
It's probably a good idea to add both success and failure test case for the multiple provided values (e.g., |
encukou commented Jul 22, 2025
Could you also test the failure case? |
Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Wulian233 commented Jul 22, 2025
Sorry, I might not have understood it well. 9500cb3 Run this test was failing before the fix, now it runs pass after the fix |
…nEuGS.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Lib/mimetypes.py Outdated
| else: | ||
| sys.exit(f"error: media type unknown for {gtype}") | ||
| return'\n'.join(results) | ||
| returnhelp_text |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is return help_text line used?
Either:
- delete
return help_text(that branch of the code is never used) - change
return help_texttoreturn "\n".join(results)and delete the otherreturncalls at the end of eachfor type in args.typeblock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not used, IDE already given a hint
In addition, test failure case have been added
mpkocher commented Jul 23, 2025
I believe there needs to be a test for the Note that this appears to be slightly changing the interface from a streaming model to a fail fast on the first error case. |
hugovk commented Aug 21, 2025
With this PR we get both results when successful: ❯ ./python.exe -m mimetypes 1.pdf 1.pngtype: application/pdf encoding: Nonetype: image/png encoding: NoneOr if it fails on the last one: ❯ ./python.exe -m mimetypes 1.pdf 1.pnxtype: application/pdf encoding: Noneerror: media type unknown for 1.pnxBut not if it fails on the first one (or both): ❯ ./python.exe -m mimetypes 1.pdx 1.pnxerror: media type unknown for 1.pdxShall we collect all results and display them all at the end? ❯ ./python.exe -m mimetypes 1.pdx 1.pngerror: media type unknown for 1.pdxtype: image/png encoding: NoneSomething like this: diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index d38c6e1aba2..7d0f4c1fd40 100644 --- a/Lib/mimetypes.py+++ b/Lib/mimetypes.py@@ -718,8 +718,6 @@ def _parse_args(args): def _main(args=None): """Run the mimetypes command-line interface and return a text to print.""" - import sys- args, help_text = _parse_args(args) results = [] @@ -729,17 +727,21 @@ def _main(args=None): if guess: results.append(str(guess)) else: - sys.exit(f"error: unknown type{gtype}")- return '\n'.join(results)+ results.append(f"error: unknown type{gtype}")+ return results else: for gtype in args.type: guess, encoding = guess_type(gtype, not args.lenient) if guess: results.append(f"type:{guess} encoding:{encoding}") else: - sys.exit(f"error: media type unknown for{gtype}")- return '\n'.join(results)+ results.append(f"error: media type unknown for{gtype}")+ return results if __name__ == '__main__': - print(_main())+ import sys++ results = _main()+ print("\n".join(results))+ sys.exit(any(result.startswith("error: ") for result in results)) |
Wulian233 commented Aug 22, 2025
Very thanks! Applyed and fixed test |
hugovk left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
81268a3 into python:mainUh oh!
There was an error while loading. Please reload this page.
Thanks @Wulian233 for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14. |
…pythonGH-136508) (cherry picked from commit 81268a3) Co-authored-by: Wulian233 <1055917385@qq.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
GH-138140 is a backport of this pull request to the 3.14 branch. |
mpkocher commented Aug 26, 2025
Why not encapsulate the functionality in Also, it would be better if errors were written to stderr, not stdout. |
Uh oh!
There was an error while loading. Please reload this page.