Title: mimetypes CLI writes error messages to stdout instead of stderr · Issue #149682 · python/cpython · GitHub
Open Graph Title: mimetypes CLI writes error messages to stdout instead of stderr · Issue #149682 · python/cpython
X Title: mimetypes CLI writes error messages to stdout instead of stderr · Issue #149682 · python/cpython
Description: Description The mimetypes command-line interface documentation states: 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 an...
Open Graph Description: Description The mimetypes command-line interface documentation states: For each type entry, the script writes a line into the standard output stream. If an unknown type occurs, it writes an error m...
X Description: Description The mimetypes command-line interface documentation states: For each type entry, the script writes a line into the standard output stream. If an unknown type occurs, it writes an error m...
Opengraph URL: https://github.com/python/cpython/issues/149682
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mimetypes CLI writes error messages to stdout instead of stderr","articleBody":"## Description\n\nThe `mimetypes` command-line interface documentation states:\n\n> 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`.\n\nHowever, the actual implementation writes *all* output — including error messages — to stdout via a single `print()` call:\n\n```python\n# Lib/mimetypes.py\nif __name__ == '__main__':\n import sys\n results = _main()\n print(\"\\n\".join(results)) # errors and normal output mixed into stdout\n sys.exit(any(result.startswith(\"error: \") for result in results))\n```\n\n**Verified behavior:**\n```\n$ python -m mimetypes foo.unknownext > out.txt 2> err.txt\n$ cat out.txt\nerror: media type unknown for foo.unknownext\n$ cat err.txt\n(empty)\n```\n\nError messages go to stdout, stderr is empty — contradicting the docs.\n\n## Additional issue: stale docs example\n\nThe documentation example shows:\n\n```\n$ python -m mimetypes filename.pict\nerror: unknown extension of filename.pict\n```\n\nBut `filename.pict` is now recognized:\n\n```\n$ python -m mimetypes filename.pict\ntype: image/pict encoding: None\n```\n\nThe example error message format also doesn't match the actual output (`\"error: unknown extension of...\"` vs `\"error: media type unknown for...\"`).\n\n## Proposed fix\n\nRoute error messages to stderr in the `__main__` block:\n\n```python\nif __name__ == '__main__':\n import sys\n results = _main()\n has_error = False\n for result in results:\n if result.startswith(\"error: \"):\n print(result, file=sys.stderr)\n has_error = True\n else:\n print(result)\n sys.exit(has_error)\n```\n\nAnd update the docs example to use a genuinely unrecognized extension.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149683\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/htjworld","@type":"Person","name":"htjworld"},"datePublished":"2026-05-11T14:28:44Z","url":"https://github.com/python/cpython/issues/149682"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b6edf86a-00c8-7f5c-d7e7-c3bf1b7e0de9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF7C:330881:1780819:1F480D1:6AEE00A6 |
| html-safe-nonce | b20d449568180c58e45f20cac2913c040201cbbecfebb244463ad0df7d09601b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjdDOjMzMDg4MToxNzgwODE5OjFGNDgwRDE6NkFFRTAwQTYiLCJ2aXNpdG9yX2lkIjoiMzYwODkzMzgxNjU3NjM3Njk5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a8d2a7d146ad415b272237a1fe029492050c34ed947e11676afd5131e0d8c920 |
| hovercard-subject-tag | issue:4421637538 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/149682/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b12694db4ac3baaa05fdf7540cdd6f1b7bf72f875966ffe084e0489445f58f06/python/cpython/issues/149682 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b12694db4ac3baaa05fdf7540cdd6f1b7bf72f875966ffe084e0489445f58f06/python/cpython/issues/149682 |
| og:image:alt | Description The mimetypes command-line interface documentation states: For each type entry, the script writes a line into the standard output stream. If an unknown type occurs, it writes an error m... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | htjworld |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0dd941b5245e608c751507bdb7c7016789050735e3c97662aac0f0791b67227a |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | bf78b34e3533f50fdd2e62be099271a8641eefe7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width