You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse _print_message silently fails with invalid file object #153967
When an invalid file object is passed to argparse.print_help or argparse.print_usage, the current behaviour does not raise any error, which is confusing as users may expect if no error is thrown, the usage text is successfully redirected. Consider the example below:
Both print_help and print_usage do not throw any error, and the users may think, for instance, passing the file path as a str, the help/usage message is written on that file, which is not the case.
The current behaviour was introduced 42f54d1 in #101802, where it does not error out when stderr is None with no explicit file argument. However if the users explicitly pass something to file, there is an expectation that the message will be redirected to file, and if the redirection fails, an error should be raised.
No exception should be raised and this keeps the behaviour backward compatible.
Proposal
Remove the try...except and check explicitly if file is a valid file object or not. If the file is valid, redirect the message to it; otherwise raise ValueError.
Activity
gh-153967: handle invalid file object in argparse._print_message (#15…
pythongh-153967: handle invalid file object in argparse._print_message (