This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse.FileType.__call__ returns unwrapped sys.stdin and stdout
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: keviv, paul.j3, r.david.murray
Priority: normal Keywords: patch

Created on 2014-11-16 14:58 by keviv, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
argparse.patch keviv, 2014-11-16 15:28
Messages (4)
msg231249 - (view) Author: Kevin Orr (keviv) Date: 2014-11-16 14:58
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.
msg231251 - (view) Author: Kevin Orr (keviv) Date: 2014-11-16 15:28
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.
msg231254 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-11-16 17:06
Perhaps instead it could return an io object built from the file descriptor with closefd=False.
msg231874 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2014-11-30 01:20
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.
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67073
2014-11-30 01:20:34paul.j3setnosy: + paul.j3
messages: + msg231874
2014-11-16 17:06:02r.david.murraysetnosy: + r.david.murray

messages: + msg231254
versions: + Python 3.4, Python 3.5, - Python 2.7, Python 3.6
2014-11-16 15:28:17kevivsetfiles: + argparse.patch
keywords: + patch
messages: + msg231251
2014-11-16 14:58:06kevivcreate