Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Oct 16, 2007, 4:59 AM

Post #1 of 8 (501 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol

Changes by Yitz Gale:


----------
components: Extension Modules
nosy: ygale
severity: normal
status: open
title: fileinput, StringIO, and cStringIO do not support the with protocol
type: behavior
versions: Python 2.5, Python 2.6, Python 3.0

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 16, 2007, 5:42 AM

Post #2 of 8 (474 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

New submission from Yitz Gale:

The standard idiom for opening a file is now with open...
So I think it should be a goal that this should work with
any built-in file-like object that needs to be closed,
without having to explicitly wrap it in closing().
It certainly should work for fileinput and StringIO - since
these really are files, in some sense.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 21, 2007, 6:16 PM

Post #3 of 8 (465 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Alexandre Vassalotti added the comment:

Do you have a use-case for this? In Py3k, I don't think adding support
for the 'with' statement to StringIO makes any sense, since the close()
method does nothing.

----------
nosy: +alexandre.vassalotti

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 22, 2007, 1:05 AM

Post #4 of 8 (458 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Yitz Gale added the comment:

These objects are supposed to be drop-in replacements
for file handles. Except in legacy code, the way you
use a file handle is:

with function_to_create_fh as fh:
<do stuff>

If these objects do not support the with protocol,
the only way to use them is to refactor this code.
That may not even be possible, e.g., if it is in
a library, or it may not be desirable to refactor.

Even if you can refactor it, I don't think you
can call these objects file-like objects if
you can't use them like a file.

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
file-like object and not an actual file,
this still works fine without any refactoring.

You wrote:
> In Py3k, I don't think adding support
> for the 'with' statement to StringIO makes
> any sense, since the close()
> method does nothing.

So do you propse removing the close() method
altogether? Of course the answer is "no",
because then you could not use the object like
a file. The same is true for the with protocol.

(I now retract the words "that needs to be closed"
from my original comment. All file-like objects
should support the with protocol, regardles of
whether their close() method actually does anything.)

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 23, 2007, 5:31 AM

Post #5 of 8 (458 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Georg Brandl added the comment:

Makes sense to me.

----------
nosy: +georg.brandl

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 24, 2007, 11:30 AM

Post #6 of 8 (453 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Guido van Rossum added the comment:

Let's not get overexcited. I agree that it makes sense for fileinput,
but I disagree about *StringIO; its close() isn't needed to free
resources (since it doesn't use up a scarce resource like a file
descriptor).

Can you whip up a patch for fileinput? Please include unit tests and
documentation.

----------
nosy: +gvanrossum
priority: -> low
versions: -Python 2.5

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 25, 2007, 5:09 AM

Post #7 of 8 (454 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Yitz Gale added the comment:

I was actually bitten badly by this issue with
StringIO. I added fileinput only as an afterthought.

In an xml.sax app, I needed seek() support for a
codec-wrapped file handle, so I over-wrapped it with
StringIO. The result was that I had to refactor code all over
the place to handle StringIO as a special case. What a
mess!

Why is this getting over-excited? It's a very
lightweight change. You can't beat the cost/benefit ratio.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Oct 25, 2007, 3:48 PM

Post #8 of 8 (452 views)
Permalink
[issue1286] fileinput, StringIO, and cStringIO do not support the with protocol [In reply to]

Guido van Rossum added the comment:

2007/10/25, Yitz Gale <report [at] bugs>:
> I was actually bitten badly by this issue with
> StringIO. I added fileinput only as an afterthought.
>
> In an xml.sax app, I needed seek() support for a
> codec-wrapped file handle, so I over-wrapped it with
> StringIO. The result was that I had to refactor code all over
> the place to handle StringIO as a special case. What a
> mess!

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
handle opening/closing of streams and let the rest of the code just
deal with streams without ever closing them. E.g.

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
this I'm still not sure what the issue is.

> Why is this getting over-excited? It's a very
> lightweight change. You can't beat the cost/benefit ratio.

Until you submit a patch it's more work for me. :-)

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue1286>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.