This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: StringIO and with statement
Type: behavior Stage: resolved
Components: None Versions: Python 2.6
process
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 2022-04-11 14:56 by admin. 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()
            
abc
def
>>> 
>>> import StringIO
>>> fo = StringIO.StringIO('abc\ndef\n')
>>> fo.seek(0)
>>> 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')
>>> fo.seek(0)
0
>>> with fo as f:
	for line in f:
	    print(line.rstrip())

	    
abc
def
>>>
msg95970 - (view) Author: Brian Curtin (brian.curtin) * (Python committer) Date: 2009-12-04 18:43
#1286 looks related
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51675
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