https://github.com/python/cpython/commit/2b94b923943a1f75cdbd5a5e4e2eb5f93eb43e23
commit: 2b94b923943a1f75cdbd5a5e4e2eb5f93eb43e23
branch: main
author: htjworld <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-05-31T07:45:48Z
summary:

gh-131178: Fix mimetypes CLI docs, mention that errors go to stdout (#149683)

Co-authored-by: sobolevn <[email protected]>

files:
M Doc/library/mimetypes.rst
M Lib/test/test_mimetypes.py

diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst
index 5103eacde1dd5b..f33098faf7d8a7 100644
--- a/Doc/library/mimetypes.rst
+++ b/Doc/library/mimetypes.rst
@@ -358,7 +358,7 @@ it converts file extensions to MIME types.
 
 For each ``type`` entry, the script writes a line into the standard output
 stream. If an unknown type occurs, it writes an error message into the
-standard error stream and exits with the return code ``1``.
+standard output stream and exits with the return code ``1``.
 
 
 .. mimetypes-cli-example:
@@ -385,7 +385,7 @@ interface:
 
    $ # get a MIME type for a rare file extension
    $ python -m mimetypes filename.pict
-   error: unknown extension of filename.pict
+   error: media type unknown for filename.pict
 
    $ # now look in the extended database built into Python
    $ python -m mimetypes --lenient filename.pict
@@ -407,7 +407,8 @@ interface:
    $ python -m mimetypes filename.sh filename.nc filename.xxx filename.txt
    type: application/x-sh encoding: None
    type: application/x-netcdf encoding: None
-   error: unknown extension of filename.xxx
+   error: media type unknown for filename.xxx
+   type: text/plain encoding: None
 
    $ # try to feed an unknown MIME type
    $ python -m mimetypes --extension audio/aac audio/opus audio/future 
audio/x-wav
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index 607aff8418edcf..eccdc5bdf70795 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -6,8 +6,9 @@
 import unittest.mock
 from platform import win32_edition
 from test import support
-from test.support import cpython_only, force_not_colorized, os_helper
+from test.support import cpython_only, force_not_colorized, os_helper, 
requires_subprocess
 from test.support.import_helper import ensure_lazy_imports
+from test.support.script_helper import assert_python_ok, assert_python_failure
 
 try:
     import _winapi
@@ -511,5 +512,59 @@ def test_invocation_error(self):
                 self.assertEqual(result, expected)
 
 
+@requires_subprocess()
+class CommandLineSubprocessTest(unittest.TestCase):
+    def test_help(self):
+        rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '--help')
+        self.assertIn(b'mimetypes', stdout)
+        self.assertIn(b'--extension', stdout)
+        self.assertIn(b'--lenient', stdout)
+
+    def test_type_lookup(self):
+        rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf')
+        self.assertEqual(stdout.strip(), b'type: application/pdf encoding: 
None')
+        self.assertEqual(stderr, b'')
+
+    def test_type_lookup_unknown(self):
+        rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 
'foo.unknownext12345')
+        self.assertEqual(stdout.strip(), b'error: media type unknown for 
foo.unknownext12345')
+        self.assertEqual(stderr, b'')
+
+    def test_extension_flag(self):
+        rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 
'image/jpeg')
+        self.assertEqual(stdout.strip(), b'.jpg')
+        self.assertEqual(stderr, b'')
+
+    def test_extension_flag_unknown(self):
+        rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '-e', 
'image/unknowntype12345')
+        self.assertEqual(stdout.strip(), b'error: unknown type 
image/unknowntype12345')
+        self.assertEqual(stderr, b'')
+
+    def test_lenient_flag(self):
+        rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 
'--lenient', 'text/xul')
+        self.assertIn(b'.xul', stdout)
+        self.assertEqual(stderr, b'')
+
+    def test_multiple_inputs(self):
+        rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf', 
'foo.png')
+        self.assertIn(b'type: application/pdf encoding: None', stdout)
+        self.assertIn(b'type: image/png encoding: None', stdout)
+        self.assertEqual(stderr, b'')
+
+    def test_multiple_inputs_with_error(self):
+        rc, stdout, stderr = assert_python_failure(
+            '-m', 'mimetypes', 'foo.pdf', 'foo.unknownext12345'
+        )
+        self.assertIn(b'type: application/pdf encoding: None', stdout)
+        self.assertIn(b'error: media type unknown for foo.unknownext12345', 
stdout)
+        self.assertEqual(stderr, b'')
+
+    @force_not_colorized
+    def test_unknown_flag(self):
+        rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 
'--unknown-flag', 'foo.pdf')
+        self.assertEqual(stdout, b'')
+        self.assertIn(b'error: unrecognized arguments: --unknown-flag', stderr)
+
+
 if __name__ == "__main__":
     unittest.main()

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to