From: Wulian233 <1055917385@qq.com> Date: Mon, 25 Aug 2025 14:38:43 +0000 (+0800) Subject: gh-136507: Fix mimetypes CLI to handle multiple file parameters (GH-136508) X-Git-Tag: v3.15.0a1~594 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=81268a3e2a6aa936a9941dde31965c7b90a963f5;p=thirdparty%2FPython%2Fcpython.git gh-136507: Fix mimetypes CLI to handle multiple file parameters (GH-136508) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index 33e86d51a0fe..7d0f4c1fd400 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -718,24 +718,30 @@ 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 = [] if args.extension: for gtype in args.type: guess = guess_extension(gtype, not args.lenient) if guess: - return str(guess) - sys.exit(f"error: unknown type {gtype}") + results.append(str(guess)) + else: + 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: - return f"type: {guess} encoding: {encoding}" - sys.exit(f"error: media type unknown for {gtype}") - return help_text + results.append(f"type: {guess} encoding: {encoding}") + else: + 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)) diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index fb57d5e5544c..c1806b1c1337 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -470,13 +470,31 @@ class CommandLineTest(unittest.TestCase): self.assertFalse(args.lenient) self.assertEqual(args.type, ["foo.pic"]) + def test_multiple_inputs(self): + result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.png"))) + self.assertEqual( + result, + "type: application/pdf encoding: None\n" + "type: image/png encoding: None" + ) + + def test_multiple_inputs_error(self): + result = "\n".join(mimetypes._main(shlex.split("foo.pdf foo.bar_ext"))) + self.assertEqual( + result, + "type: application/pdf encoding: None\n" + "error: media type unknown for foo.bar_ext" + ) + + def test_invocation(self): for command, expected in [ ("-l -e image/jpg", ".jpg"), ("-e image/jpeg", ".jpg"), ("-l foo.webp", "type: image/webp encoding: None"), ]: - self.assertEqual(mimetypes._main(shlex.split(command)), expected) + result = "\n".join(mimetypes._main(shlex.split(command))) + self.assertEqual(result, expected) def test_invocation_error(self): for command, expected in [ @@ -484,8 +502,8 @@ class CommandLineTest(unittest.TestCase): ("foo.bar_ext", "error: media type unknown for foo.bar_ext"), ]: with self.subTest(command=command): - with self.assertRaisesRegex(SystemExit, expected): - mimetypes._main(shlex.split(command)) + result = "\n".join(mimetypes._main(shlex.split(command))) + self.assertEqual(result, expected) if __name__ == "__main__": diff --git a/Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst b/Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst new file mode 100644 index 000000000000..b72fd26b38a8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst @@ -0,0 +1 @@ +Fix mimetypes CLI to handle multiple file parameters.