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

2012-11-27 Thread Eric Snow

Eric Snow added the comment:

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.

--
nosy: +eric.snow

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2012-11-27 Thread Ezio Melotti

Ezio Melotti added the comment:

http://docs.python.org/2/library/contextlib.html#contextlib.closing

--
nosy: +ezio.melotti

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2012-11-27 Thread Eric Snow

Eric Snow added the comment:

borrowed the time machine did we? ;)

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2012-11-25 Thread Georg Brandl

Georg Brandl added the comment:

It would be a new feature, and as such forbidden to add in maintenance releases.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2012-11-24 Thread Wade Tattersall

Wade Tattersall added the comment:

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()

--
nosy: +Wade.Tattersall

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2010-07-31 Thread Georg Brandl

Georg Brandl ge...@python.org added the comment:

For 3.x, the builtin io.StringIO and io.BytesIO already have context manager 
capability.  Added fileinput in r83359.

--
resolution:  - accepted
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2009-12-28 Thread Georg Brandl

Georg Brandl ge...@python.org added the comment:

FWIW, for the sake of consistency I'm +1 on supporting the context
manager protocol on all file-like objects in the stdlib.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2009-12-20 Thread Senthil Kumaran

Senthil Kumaran orsent...@gmail.com added the comment:

I reviewed the patches attached.
- The patch to add Context Manager support for fileinput.py seems good.
It has docs too.

This discussion did not conclude on the need for Context Manager for
StringIO. With py3k having it, it should be good for py2.7 to provide
the support too. The attached patch seems good enough, Docs can be added
further.

--
nosy: +orsenthil

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2009-11-17 Thread Brian Curtin

Brian Curtin cur...@acm.org added the comment:

Attached is a patch against trunk r76325 which implements context
manager support for fileinput/FileInput, with tests and doc change.

--
keywords: +patch
nosy: +brian.curtin
Added file: http://bugs.python.org/file15355/issue1286.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2009-11-17 Thread Guido van Rossum

Changes by Guido van Rossum gu...@python.org:


--
nosy:  -gvanrossum

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2009-05-14 Thread Daniel Diniz

Changes by Daniel Diniz aja...@gmail.com:


--
stage:  - patch review

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-10-04 Thread Benjamin Peterson

Changes by Benjamin Peterson [EMAIL PROTECTED]:


--
versions: +Python 2.7 -Python 2.6

___
Python tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-02-20 Thread Alexander Belopolsky

Alexander Belopolsky added the comment:

Attached patch implements context management protocol for StringIO.

--
components: +Library (Lib)
nosy: +belopolsky
versions:  -Python 3.0
Added file: http://bugs.python.org/file9470/with_StringIO.diff

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-01-11 Thread A.M. Kuchling

Changes by A.M. Kuchling:


--
keywords: +easy

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2008-01-11 Thread Alexandre Vassalotti

Alexandre Vassalotti added the comment:

FYI, StringIO and BytesIO, in Python 3K, already support the context
management protocol.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-25 Thread Yitz Gale

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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-25 Thread Guido van Rossum

Guido van Rossum added the comment:

2007/10/25, Yitz Gale [EMAIL PROTECTED]:
 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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-24 Thread Guido van Rossum

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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-23 Thread Georg Brandl

Georg Brandl added the comment:

Makes sense to me.

--
nosy: +georg.brandl

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-22 Thread Yitz Gale

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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-21 Thread Alexandre Vassalotti

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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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

2007-10-16 Thread Yitz Gale

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 [EMAIL PROTECTED]
http://bugs.python.org/issue1286
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com