classification
Title: argparse.FileType for '-' doesn't work for a mode of 'rb'
Type: crash Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: anacrolix, bethard, moritz, pitrou
Priority: normal Keywords: patch

Created on 2012-02-29 11:01 by anacrolix, last changed 2012-07-22 21:06 by bethard.

Files
File name Uploaded Description Edit
argparse-filetype-stdio-modes.patch anacrolix, 2012-03-15 18:11
14156.patch bethard, 2012-07-22 21:06 review
Messages (6)
msg154612 - (view) Author: Matt Joiner (anacrolix) Date: 2012-02-29 11:01
If an argument of '-' is handled by argparse.FileType, it defaults to sys.stdin. However a mode of 'rb' is ignored, the returned file object does not work with raw bytes.
msg154641 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-02-29 15:27
Yes, the problem is in FileType.__call__ - the handling of '-' is pretty simplistic. Patches welcome.
msg155761 - (view) Author: Matt Joiner (anacrolix) Date: 2012-03-14 16:17
Roger that. I'll start on a patch for this in a month or two if all goes well.
msg155923 - (view) Author: Matt Joiner (anacrolix) Date: 2012-03-15 18:11
Steven, patch attached. I lost steam in the unittests with all the meta, suffice it that the names match the file descriptors of the stream sources. i.e. FileType('rb') would give a file with name=0, and so forth. My chosen method also allows other mode flags as well as custom bufsizes.
msg162342 - (view) Author: Moritz Klammler (moritz) Date: 2012-06-05 13:51
I don't know how if this is the perfect solution but it keeps the program from crashing.

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
msg166170 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2012-07-22 21:06
The fix looks right, but we definitely need a test. I tried to write one, but I'm not sure how to do this properly given how test_argparse redirects standard input and output (so that fileno() doesn't work anymore). I've attached my current (failing) attempt to test this.
History
Date User Action Args
2012-07-22 21:06:30bethardsetfiles: + 14156.patch

messages: + msg166170
2012-06-05 13:51:40moritzsetversions: + Python 3.2, - Python 3.3
nosy: + moritz

messages: + msg162342

type: behavior -> crash
2012-03-15 18:11:30anacrolixsetfiles: + argparse-filetype-stdio-modes.patch
keywords: + patch
messages: + msg155923
2012-03-14 16:17:47anacrolixsetmessages: + msg155761
2012-02-29 15:27:10bethardsetmessages: + msg154641
2012-02-29 11:25:07eric.araujosetnosy: + pitrou, bethard
2012-02-29 11:01:10anacrolixcreate