]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-136507: Fix mimetypes CLI to handle multiple file parameters (GH-136508...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 7 Oct 2025 17:59:52 +0000 (19:59 +0200)
committerGitHub <noreply@github.com>
Tue, 7 Oct 2025 17:59:52 +0000 (19:59 +0200)
(cherry picked from commit 81268a3e2a6aa936a9941dde31965c7b90a963f5)

Co-authored-by: Wulian233 <1055917385@qq.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Lib/mimetypes.py
Lib/test/test_mimetypes.py
Misc/NEWS.d/next/Library/2025-07-10-21-02-43.gh-issue-136507.pnEuGS.rst [new file with mode: 0644]

index 33e86d51a0fe508dab237f425f0ea64557e902c8..7d0f4c1fd400d538a6a80b0af4cdf2da0f6910a9 100644 (file)
@@ -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))
index fb57d5e5544c125cde4aad1a4316da2c5bfb9005..c1806b1c1337786fdbbbefada073033c57d45caa 100644 (file)
@@ -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 (file)
index 0000000..b72fd26
--- /dev/null
@@ -0,0 +1 @@
+Fix mimetypes CLI to handle multiple file parameters.