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.
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:10 | admin | set | github: 67073 |
| 2014年11月30日 01:20:34 | paul.j3 | set | nosy:
+ paul.j3 messages: + msg231874 |
| 2014年11月16日 17:06:02 | r.david.murray | set | nosy:
+ r.david.murray messages: + msg231254 versions: + Python 3.4, Python 3.5, - Python 2.7, Python 3.6 |
| 2014年11月16日 15:28:17 | keviv | set | files:
+ argparse.patch keywords: + patch messages: + msg231251 |
| 2014年11月16日 14:58:06 | keviv | create | |