summaryrefslogtreecommitdiff
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-03-06 04:12:06 -0800
committerGitHub <noreply@github.com>2022-03-06 04:12:06 -0800
commitee18df425209cfa4f394b57220177c168fc3a1da (patch)
tree025b4e50a6063a616436aa59ba77d102df180838 /Lib/argparse.py
parent4716f70c8543d12d18c64677af650d479b99edac (diff)
downloadcpython-git-ee18df425209cfa4f394b57220177c168fc3a1da.tar.gz
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 eafec26ae5327bb23b6dace2650b074c3327dfa0) Co-authored-by: MojoVampire <shadowranger+github@gmail.com>
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index b71a6703c3..2c0dd853a5 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -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)