-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
fileinput, StringIO, and cStringIO do not support the with protocol #45627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The standard idiom for opening a file is now with open... |
Do you have a use-case for this? In Py3k, I don't think adding support |
These objects are supposed to be drop-in replacements with function_to_create_fh as fh:
<do stuff> If these objects do not support the with protocol, Even if you can refactor it, I don't think you Note that in legacy code, you used to write: fh = function_to_get_fh
try:
<do stuff>
finally:
fh.close() If function_to_get_fh happens to return some other You wrote:
So do you propse removing the close() method (I now retract the words "that needs to be closed" |
Makes sense to me. |
Let's not get overexcited. I agree that it makes sense for fileinput, Can you whip up a patch for fileinput? Please include unit tests and |
I was actually bitten badly by this issue with In an xml.sax app, I needed seek() support for a Why is this getting over-excited? It's a very |
2007/10/25, Yitz Gale <report@bugs.python.org>:
I don't understand. What did your code look like after the refactoring? I find that typically a useful idiom is to have one piece of code f = open(filename)
try:
process(f)
finally:
f.close() or, if you want: with open(filename) as f:
process(f) As I don't understand how you are working the StringIO() call into
Until you submit a patch it's more work for me. :-) |
FYI, StringIO and BytesIO, in Python 3K, already support the context |
Attached patch implements context management protocol for StringIO. |
Attached is a patch against trunk r76325 which implements context |
I reviewed the patches attached.
This discussion did not conclude on the need for Context Manager for |
FWIW, for the sake of consistency I'm +1 on supporting the context |
For 3.x, the builtin io.StringIO and io.BytesIO already have context manager capability. Added fileinput in r83359. |
Any chance this patch could be applied to version 2.7? It's still an issue in 2.7.3, even though a suitable patch was supplied 3 years ago. I understand that it's fixed in python3, but for us poor maintainers of ancient code, it would be convenient to be able to do things like with StringIO() as test:
test.write("hi!")
return test.getvalue() |
It would be a new feature, and as such forbidden to add in maintenance releases. |
Keep in mind that it's pretty easy to roll your own CM wrapper: @contextlib.contextmanager
def closes(file):
yield file
file.close() Then you can do this: with closes(StringIO()) as test:
test.write("hi!")
return test.getvalue() This works for 2.5 and up. |
borrowed the time machine did we? ;) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: