mirror of
https://github.com/python/cpython.git
synced 2025-08-01 23:53:15 +00:00
bpo-14156: Make argparse.FileType work correctly for binary file modes when argument is '-' (GH-13165)
Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-'
(so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb').
(cherry picked from commit eafec26ae5
)
Co-authored-by: MojoVampire <shadowranger+github@gmail.com>
This commit is contained in:
parent
4716f70c85
commit
ee18df4252
3 changed files with 110 additions and 17 deletions
|
@ -726,7 +726,7 @@ def _get_action_name(argument):
|
|||
if argument is None:
|
||||
return None
|
||||
elif argument.option_strings:
|
||||
return '/'.join(argument.option_strings)
|
||||
return '/'.join(argument.option_strings)
|
||||
elif argument.metavar not in (None, SUPPRESS):
|
||||
return argument.metavar
|
||||
elif argument.dest not in (None, SUPPRESS):
|
||||
|
@ -1256,9 +1256,9 @@ class FileType(object):
|
|||
# the special argument "-" means sys.std{in,out}
|
||||
if string == '-':
|
||||
if 'r' in self._mode:
|
||||
return _sys.stdin
|
||||
elif 'w' in self._mode:
|
||||
return _sys.stdout
|
||||
return _sys.stdin.buffer if 'b' in self._mode else _sys.stdin
|
||||
elif any(c in self._mode for c in 'wax'):
|
||||
return _sys.stdout.buffer if 'b' in self._mode else _sys.stdout
|
||||
else:
|
||||
msg = _('argument "-" with mode %r') % self._mode
|
||||
raise ValueError(msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue