Skip to content

gh-136507: Fix mimetypes CLI cannot handle multiple file parameters #136508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an error; could you print to stderr and exit wit an error code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I'll solve it tomorrow :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is validating for a non-empty string(?), then perhaps a specific error should be communicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an error; could you print to stderr and exit wit an error code?

I switched to a different fix implementation and now use list


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()
11 changes: 9 additions & 2 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import io
import mimetypes
import os
Expand Down Expand Up @@ -476,16 +477,22 @@ 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 [
("-e image/jpg", "error: unknown type image/jpg"),
("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__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix mimetypes CLI cannot handle multiple file parameters
Loading