Using the 'with' statement with cStringIO objects

peppergrower spamcomefindmeplease at
Sat Sep 27 11:21:06 CEST 2008

I've been experimenting with the 'with' statement (in __future__), and
so far I like it.  However, I can't get it to work with a cStringIO
object.  Here's a minimum working example:

from __future__ import with_statement
import cStringIO

teststring='this is a test'

with cStringIO.StringIO(teststring) as testfile:

I get the following error message:

Traceback (most recent call last):
  File "", line 6, in <module>
    with cStringIO.StringIO(teststring) as testfile:
AttributeError: 'cStringIO.StringI' object has no attribute '__exit__'

So, I'm guessing you can't use the 'with' statement with cStringIO
objects?  Is this a bug, or do I need to use the 'with' statement
differently to get this to work?


More information about the Python-list mailing list