[stdlib-sig] argparse -- binary IO via standard streams

Moritz Klammler moritz.klammler at gmail.com
Tue Jun 5 12:18:56 CEST 2012


Hello,

in Python 3, this is a TypeError:

>>> sys.stdout.write(b"hello, world\n")

At the moment, argparse.FileType.__call__("-") unconditionally returns
sys.stdout even if mode is binary so writing binary data will fail if
the user selects to write it to stdout.

I'm not sure if this is the best possible solution but at least the
following patch to argparse.py would fix the aforementioned problem.

Moritz


1154c1154,1157
<                 return _sys.stdin
---
>                 if 'b' in self._mode:
>                     return _sys.stdin.buffer
>                 else:
>                     return _sys.stdin
1156c1159,1162
<                 return _sys.stdout
---
>                 if 'b' in self._mode:
>                     return _sys.stdout.buffer
>                 else:
>                     return _sys.stdout

-- 
Fingerprint:  80C1 EC79 B554 3D84 0A35 A728 7057 B288 CE61 2235
Public key:   http://openpgp.klammler.eu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/stdlib-sig/attachments/20120605/030d92ed/attachment.pgp>


More information about the stdlib-sig mailing list