Skip to content

Commit 5b75aaa

Browse files
committed
Address review feedback: revert stderr routing, update docs and tests
Per sobolevn's review: revert the behavior change that routed error messages to stderr, as existing code may rely on stdout behavior. Instead update docs to accurately reflect that errors go to stdout. Also update tests to assert full output contents as suggested.
1 parent 77e5b2a commit 5b75aaa

4 files changed

Lines changed: 13 additions & 21 deletions

File tree

Doc/library/mimetypes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ it converts file extensions to MIME types.
348348

349349
For each ``type`` entry, the script writes a line into the standard output
350350
stream. If an unknown type occurs, it writes an error message into the
351-
standard error stream and exits with the return code ``1``.
351+
standard output stream and exits with the return code ``1``.
352352

353353

354354
.. mimetypes-cli-example:

Lib/mimetypes.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,5 @@ def _main(args=None):
756756
import sys
757757

758758
results = _main()
759-
has_error = False
760-
for result in results:
761-
if result.startswith("error: "):
762-
print(result, file=sys.stderr, flush=True)
763-
has_error = True
764-
else:
765-
print(result, flush=True)
766-
sys.exit(has_error)
759+
print("\n".join(results))
760+
sys.exit(any(result.startswith("error: ") for result in results))

Lib/test/test_mimetypes.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,13 @@ def test_help(self):
519519

520520
def test_type_lookup(self):
521521
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf')
522-
self.assertIn(b'application/pdf', stdout)
522+
self.assertEqual(stdout.strip(), b'type: application/pdf encoding: None')
523523
self.assertEqual(stderr, b'')
524524

525525
def test_type_lookup_unknown(self):
526526
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', 'foo.unknownext12345')
527-
self.assertEqual(stdout, b'')
528-
self.assertIn(b'error:', stderr)
527+
self.assertEqual(stdout.strip(), b'error: media type unknown for foo.unknownext12345')
528+
self.assertEqual(stderr, b'')
529529

530530
def test_extension_flag(self):
531531
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', 'image/jpeg')
@@ -534,8 +534,8 @@ def test_extension_flag(self):
534534

535535
def test_extension_flag_unknown(self):
536536
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '-e', 'image/unknowntype12345')
537-
self.assertEqual(stdout, b'')
538-
self.assertIn(b'error:', stderr)
537+
self.assertEqual(stdout.strip(), b'error: unknown type image/unknowntype12345')
538+
self.assertEqual(stderr, b'')
539539

540540
def test_lenient_flag(self):
541541
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', '-e', '--lenient', 'text/xul')
@@ -544,17 +544,17 @@ def test_lenient_flag(self):
544544

545545
def test_multiple_inputs(self):
546546
rc, stdout, stderr = assert_python_ok('-m', 'mimetypes', 'foo.pdf', 'foo.png')
547-
self.assertIn(b'application/pdf', stdout)
548-
self.assertIn(b'image/png', stdout)
547+
self.assertIn(b'type: application/pdf encoding: None', stdout)
548+
self.assertIn(b'type: image/png encoding: None', stdout)
549549
self.assertEqual(stderr, b'')
550550

551551
def test_multiple_inputs_with_error(self):
552552
rc, stdout, stderr = assert_python_failure(
553553
'-m', 'mimetypes', 'foo.pdf', 'foo.unknownext12345'
554554
)
555-
self.assertIn(b'application/pdf', stdout)
556-
self.assertNotIn(b'error:', stdout)
557-
self.assertIn(b'error:', stderr)
555+
self.assertIn(b'type: application/pdf encoding: None', stdout)
556+
self.assertIn(b'error: media type unknown for foo.unknownext12345', stdout)
557+
self.assertEqual(stderr, b'')
558558

559559
def test_unknown_flag(self):
560560
rc, stdout, stderr = assert_python_failure('-m', 'mimetypes', '--unknown-flag')

Misc/NEWS.d/next/Library/2026-05-11-23-38-47.gh-issue-149682.UE08R4.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)