Using the 'with' statement with cStringIO objects

peppergrower spamcomefindmeplease at gmail.com
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:
	pass
###

I get the following error message:

Traceback (most recent call last):
  File "testfile.py", 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?

Thanks,
peppergrower



More information about the Python-list mailing list