[issue22884] argparse.FileType.__call__ returns unwrapped sys.stdin and stdout
paul j3 added the comment: Related issues are: http://bugs.python.org/issue13824 - argparse.FileType opens a file and never closes it http://bugs.python.org/issue14156 - argparse.FileType for '-' doesn't work for a mode of 'rb' As discussed earlier FileType was meant as a convenience for small scripting applications, ones that don't try to close the file before exiting. But now we are encouraged to open files in a context that guarantees closure. In 13824 I proposed a 'FileContext', and included a dummy context like this handle stdin/out. But I think the closefd=False approach raised in 14156 and David is probably the better way to go. I think the main thing that is lacking is a testing mechanism. -- nosy: +paul.j3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22884 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22884] argparse.FileType.__call__ returns unwrapped sys.stdin and stdout
New submission from Kevin Orr: When one uses a file object returned by `FileType.__call__` as a context manager, `sys.stdin`'s or `sys.stdout`'s `__exit__` will be triggered upon exit of the context, in turn calling their `close` method. Perhaps the issue is that `sys.stdin` and `sys.stdout` have poor `__exit__` methods, but my proposal (and it's not a particularly clean one) is to override the file object's `__exit__` if it happens to be either `sys.stdin` or `sys.stdout` to simply return True when called. -- components: Library (Lib) messages: 231249 nosy: keviv priority: normal severity: normal status: open title: argparse.FileType.__call__ returns unwrapped sys.stdin and stdout type: behavior versions: Python 2.7, Python 3.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22884 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22884] argparse.FileType.__call__ returns unwrapped sys.stdin and stdout
Kevin Orr added the comment: Hmm, not sure why I thought I needed `inline code formatting`. It's all monospace anyway! Anyway, I'm thinking that adding this somewhere in argparse.py: class _WrappedIO(object): def __init__(self, fileobj): self._file = fileobj def __exit__(tp, val, tb): return True def __getattr__(self, name): return self._file.__gettattr__(name) and then applying the attached patch *may* fix this. -- keywords: +patch Added file: http://bugs.python.org/file37209/argparse.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22884 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22884] argparse.FileType.__call__ returns unwrapped sys.stdin and stdout
R. David Murray added the comment: Perhaps instead it could return an io object built from the file descriptor with closefd=False. -- nosy: +r.david.murray versions: +Python 3.4, Python 3.5 -Python 2.7, Python 3.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22884 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com