summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorMojoVampire <shadowranger+github@gmail.com>2022-03-06 11:49:42 (GMT)
committerGitHub <noreply@github.com>2022-03-06 11:49:42 (GMT)
commiteafec26ae5327bb23b6dace2650b074c3327dfa0 (patch)
tree4b29c9a699bd802142c29a07f5e7f99afdc0b162 /Lib/argparse.py
parent602024e6e12c69d836aa191d63db75862aec2493 (diff)
downloadcpython-eafec26ae5327bb23b6dace2650b074c3327dfa0.zip
cpython-eafec26ae5327bb23b6dace2650b074c3327dfa0.tar.gz
cpython-eafec26ae5327bb23b6dace2650b074c3327dfa0.tar.bz2
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').
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 3c6aa3c..429a72a 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -728,7 +728,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):
@@ -1259,9 +1259,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)