classification
Title: patch to make io.StringIO consistent with open with respect to newlines
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, erickt
Priority: normal Keywords: patch

Created on 2009-03-09 00:09 by erickt, last changed 2009-03-09 01:04 by erickt. This issue is now closed.

Files
File name Uploaded Description Edit
0001-Make-StringIO-consistent-with-open-wrt-newlines.patch erickt, 2009-03-09 00:09
Messages (4)
msg83341 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-03-09 00:09
I noticed that io.StringIO is inconsistent with open on how newlines are 
handled. The default approach open uses is universal newlines:

>>> with open('foo', 'w') as f:
...   f.write('hello hi\r\nla la\r\n')
... 
17
>>> open('foo').readlines()
['hello hi\n', 'la la\n']

io.StringIO, however, defaults to just treating \n as newlines:

>>> io.StringIO('hello hi\r\nla la \r\n').readlines()
['hello hi\r\n', 'la la \r\n']

The attached patch changes this so that if the newline keyword isn't 
specified, then StringIO will act just like open with respect to 
keywords. It will then produce this:

>>> io.StringIO('hello hi\r\nla la \r\n').readlines()
['hello hi\n', 'la la \n']
msg83343 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-03-09 00:16
This is the intended behavior. It is consist with 2.x's
StringIO.StringIO behavior.
msg83344 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-03-09 00:55
Thanks Benjamin. Could the documentation for StringIO be updated then? The 
docs for io.StringIO [1] says that io.TextIOWrapper implements the 
constructor, but TextIOWrapper defaults newline to None [2], and there's 
nothing that states that io.StringIO changes the default to newline='\n'.

[1]: http://docs.python.org/3.0/library/io.html#io.StringIO
[2]: http://docs.python.org/3.0/library/io.html#io.TextIOWrapper
msg83346 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-03-09 01:04
Moving this to a documentation bug here: http://bugs.python.org/issue5452
History
Date User Action Args
2009-03-09 01:04:09ericktsetmessages: + msg83346
2009-03-09 00:55:02ericktsetmessages: + msg83344
2009-03-09 00:16:24benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg83343

resolution: rejected
2009-03-09 00:09:53ericktcreate