Title: StringIO and with statement
Type: behavior Stage: resolved
Components: None Versions: Python 2.6
Status: closed Resolution: duplicate
Dependencies: Superseder: fileinput, StringIO, and cStringIO do not support the with protocol
View: 1286
Assigned To: Nosy List: alexandre.vassalotti, brian.curtin, jmfauth
Priority: normal Keywords:

Created on 2009-12-03 10:12 by jmfauth, last changed 2009-12-04 19:45 by alexandre.vassalotti. This issue is now closed.

Messages (2)
msg95924 - (view) Author: jmf (jmfauth) Date: 2009-12-03 10:12
When toying with the "with" statement, I fell on this:

Python 2.6.4

>>> with open('abc.txt', 'r') as f:
        for line in f:
            print line.rstrip()
>>> import StringIO
>>> fo = StringIO.StringIO('abc\ndef\n')
>>> with fo as f2:
        for line in f2:
            print line.rstrip()
Traceback (most recent call last):
  File "<psi last command>", line 2, in <module>
AttributeError: StringIO instance has no attribute '__exit__'

Same result with cStringIO


Python 3.1.1

>>> fo = io.StringIO('abc\ndef\n')
>>> with fo as f:
	for line in f:

msg95970 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2009-12-04 18:43
#1286 looks related
Date User Action Args
2009-12-04 19:45:14alexandre.vassalottisetstatus: open -> closed
resolution: duplicate
superseder: fileinput, StringIO, and cStringIO do not support the with protocol
stage: resolved
2009-12-04 18:43:54brian.curtinsetnosy: + brian.curtin
messages: + msg95970
2009-12-03 10:57:47pitrousetnosy: + alexandre.vassalotti
2009-12-03 10:12:51jmfauthcreate