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.

Title: patch to make io.StringIO consistent with open with respect to newlines
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1
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 2022-04-11 14:56 by admin. This issue is now closed.

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')
>>> 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'.

msg83346 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-03-09 01:04
Moving this to a documentation bug here:
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49701
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