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

Guido van Rossum report at bugs.python.org
Fri Oct 26 00:48:59 CEST 2007


Guido van Rossum added the comment:

2007/10/25, Yitz Gale <report at bugs.python.org>:
> 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.python.org>
<http://bugs.python.org/issue1286>
__________________________________


More information about the Python-bugs-list mailing list