From moritz.klammler at gmail.com Tue Jun 5 12:18:56 2012 From: moritz.klammler at gmail.com (Moritz Klammler) Date: Tue, 05 Jun 2012 12:18:56 +0200 Subject: [stdlib-sig] argparse -- binary IO via standard streams Message-ID: <878vg2jc33.fsf@gmail.com> 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: From michael at voidspace.org.uk Tue Jun 5 12:56:01 2012 From: michael at voidspace.org.uk (Michael Foord) Date: Tue, 5 Jun 2012 11:56:01 +0100 Subject: [stdlib-sig] argparse -- binary IO via standard streams In-Reply-To: <878vg2jc33.fsf@gmail.com> References: <878vg2jc33.fsf@gmail.com> Message-ID: <8CCCBE8C-DADB-414B-9939-1F82D1BE2DEA@voidspace.org.uk> On 5 Jun 2012, at 11:18, Moritz Klammler wrote: > 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. Please post patches to the bug tracker so they don't get lost. http://bugs.python.org All the best, Michael Foord > > 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 > _______________________________________________ > stdlib-sig mailing list > stdlib-sig at python.org > http://mail.python.org/mailman/listinfo/stdlib-sig -- http://www.voidspace.org.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html