From 69ae20cc696a68546b3978da0c8d8e09c0a1a1e7 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Thu, 10 Jul 2025 21:02:55 +0800 Subject: [PATCH 1/6] Fix mimetypes CLI cannot handle multiple file parameters --- Lib/mimetypes.py | 18 +++++++++++------- ...5-07-10-21-02-43.gh-issue-136507.pnEuGS.rst | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index 33e86d51a0fe50..f71ec3699880f6 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -717,25 +717,29 @@ def _parse_args(args): def _main(args=None): - """Run the mimetypes command-line interface and return a text to print.""" + """Run the mimetypes command-line interface.""" import sys args, help_text = _parse_args(args) + if not args.type: + print(help_text) + return 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}") + print(str(guess)) + else: + print(f"error: unknown type {gtype}", file=sys.stderr) 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 + print(f"type: {guess} encoding: {encoding}") + else: + print(f"error: media type unknown for {gtype}", file=sys.stderr) if __name__ == '__main__': - print(_main()) + _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 00000000000000..58cc5461b92bfd --- /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 cannot handle multiple file parameters From 639644b041d6604cdd3e63c70a810cc6149f2898 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Thu, 10 Jul 2025 21:17:55 +0800 Subject: [PATCH 2/6] fix test --- Lib/test/test_mimetypes.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index fb57d5e5544c12..3e7c17557786d1 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -1,3 +1,4 @@ +import contextlib import io import mimetypes import os @@ -476,7 +477,11 @@ def test_invocation(self): ("-e image/jpeg", ".jpg"), ("-l foo.webp", "type: image/webp encoding: None"), ]: - self.assertEqual(mimetypes._main(shlex.split(command)), expected) + with self.subTest(command=command): + out = io.StringIO() + with contextlib.redirect_stdout(out): + mimetypes._main(shlex.split(command)) + self.assertEqual(out.getvalue().strip(), expected) def test_invocation_error(self): for command, expected in [ @@ -484,8 +489,10 @@ def test_invocation_error(self): ("foo.bar_ext", "error: media type unknown for foo.bar_ext"), ]: with self.subTest(command=command): - with self.assertRaisesRegex(SystemExit, expected): + err = io.StringIO() + with contextlib.redirect_stderr(err): mimetypes._main(shlex.split(command)) + self.assertIn(expected, err.getvalue().strip()) if __name__ == "__main__": From ebf9b253b374265a70c5723607685bcb5a448743 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 11 Jul 2025 13:32:41 +0800 Subject: [PATCH 3/6] finish --- Lib/mimetypes.py | 19 ++++++++++--------- Lib/test/test_mimetypes.py | 11 ++--------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index f71ec3699880f6..ffaf1b1006cb3b 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -717,29 +717,30 @@ def _parse_args(args): def _main(args=None): - """Run the mimetypes command-line interface.""" + """Run the mimetypes command-line interface and return a text to print.""" import sys args, help_text = _parse_args(args) - if not args.type: - print(help_text) - return + results = [] if args.extension: for gtype in args.type: guess = guess_extension(gtype, not args.lenient) if guess: - print(str(guess)) + results.append(str(guess)) else: - print(f"error: unknown type {gtype}", file=sys.stderr) + sys.exit(f"error: unknown type {gtype}") + return '\n'.join(results) else: for gtype in args.type: guess, encoding = guess_type(gtype, not args.lenient) if guess: - print(f"type: {guess} encoding: {encoding}") + results.append(f"type: {guess} encoding: {encoding}") else: - print(f"error: media type unknown for {gtype}", file=sys.stderr) + sys.exit(f"error: media type unknown for {gtype}") + return '\n'.join(results) + return help_text if __name__ == '__main__': - _main() + print(_main()) diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index 3e7c17557786d1..fb57d5e5544c12 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -1,4 +1,3 @@ -import contextlib import io import mimetypes import os @@ -477,11 +476,7 @@ def test_invocation(self): ("-e image/jpeg", ".jpg"), ("-l foo.webp", "type: image/webp encoding: None"), ]: - with self.subTest(command=command): - out = io.StringIO() - with contextlib.redirect_stdout(out): - mimetypes._main(shlex.split(command)) - self.assertEqual(out.getvalue().strip(), expected) + self.assertEqual(mimetypes._main(shlex.split(command)), expected) def test_invocation_error(self): for command, expected in [ @@ -489,10 +484,8 @@ def test_invocation_error(self): ("foo.bar_ext", "error: media type unknown for foo.bar_ext"), ]: with self.subTest(command=command): - err = io.StringIO() - with contextlib.redirect_stderr(err): + with self.assertRaisesRegex(SystemExit, expected): mimetypes._main(shlex.split(command)) - self.assertIn(expected, err.getvalue().strip()) if __name__ == "__main__": From 9500cb37398a3438671d01bc345d9d3460acdfd2 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Thu, 17 Jul 2025 12:58:17 +0800 Subject: [PATCH 4/6] test_multiple_inputs --- Lib/test/test_mimetypes.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index fb57d5e5544c12..49a935b1f27e59 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -470,6 +470,14 @@ def test_parse_args(self): self.assertFalse(args.lenient) self.assertEqual(args.type, ["foo.pic"]) + def test_multiple_inputs(self): + result = mimetypes._main(shlex.split("foo.pdf foo.png")) + self.assertEqual( + result, + "type: application/pdf encoding: None\n" + "type: image/png encoding: None" + ) + def test_invocation(self): for command, expected in [ ("-l -e image/jpg", ".jpg"), From 272c7e8bea924811577746467317f5d816ae2702 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Tue, 22 Jul 2025 22:28:25 +0800 Subject: [PATCH 5/6] Update Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- .../next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 58cc5461b92bfd..b72fd26b38a83b 100644 --- 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 @@ -1 +1 @@ -Fix mimetypes CLI cannot handle multiple file parameters +Fix mimetypes CLI to handle multiple file parameters. From d68cc179e72a4a87dd5d0af327b1d2f9db22cefa Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Wed, 23 Jul 2025 19:21:51 +0800 Subject: [PATCH 6/6] test the failure case --- Lib/mimetypes.py | 1 - Lib/test/test_mimetypes.py | 8 ++++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index ffaf1b1006cb3b..d38c6e1aba23aa 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -739,7 +739,6 @@ def _main(args=None): else: sys.exit(f"error: media type unknown for {gtype}") return '\n'.join(results) - return help_text if __name__ == '__main__': diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py index 49a935b1f27e59..98b1f8ec7e94ed 100644 --- a/Lib/test/test_mimetypes.py +++ b/Lib/test/test_mimetypes.py @@ -478,6 +478,14 @@ def test_multiple_inputs(self): "type: image/png encoding: None" ) + def test_multiple_inputs_error(self): + with self.assertRaises(SystemExit) as err: + mimetypes._main(shlex.split("foo.pdf foo.bar_ext")) + + self.assertNotEqual(err.exception.code, 0) + self.assertIn("error: media type unknown for foo.bar_ext", str(err.exception)) + + def test_invocation(self): for command, expected in [ ("-l -e image/jpg", ".jpg"),
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: