Issue 20423: io.StringIO newline param has wrong default (original) (raw)

Created on 2014-01-28 19:24 by couplewavylines, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)

msg209577 - (view)

Author: (couplewavylines)

Date: 2014-01-28 19:24

In io.StringIO, the newline argument's default is currently documented as "newline=None" in the section header. However, it's described this way: "The default is to do no newline translation." The behavior of io.StringIO is consistent with this description (NO newline translation). The header should actually read "newline=''". "newline=None" would mean there IS newline translation by default, which is not the case. Code sample attached as no_translation.py.

msg209579 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2014-01-28 19:37

No, actual default is '\n'.

io.StringIO('abc\r\ndef\nghi\rklm').readlines() ['abc\r\n', 'def\n', 'ghi\rklm'] io.StringIO('abc\r\ndef\nghi\rklm', newline=None).readlines() ['abc\n', 'def\n', 'ghi\n', 'klm'] io.StringIO('abc\r\ndef\nghi\rklm', newline='').readlines() ['abc\r\n', 'def\n', 'ghi\r', 'klm'] io.StringIO('abc\r\ndef\nghi\rklm', newline='\n').readlines() ['abc\r\n', 'def\n', 'ghi\rklm'] io.StringIO('abc\r\ndef\nghi\rklm', newline='\r').readlines() ['abc\r', '\r', 'def\r', 'ghi\r', 'klm'] io.StringIO('abc\r\ndef\nghi\rklm', newline='\r\n').readlines() ['abc\r\r\n', 'def\r\n', 'ghi\rklm']

msg209580 - (view)

Author: (couplewavylines)

Date: 2014-01-28 19:44

Serhiy, you're right, I now see the default behavior is "newline='\n'".

So, the header is still wrong, and the text is "The default is to do no newline translation" may be incorrect too? Or just unclear (to me anyway)?

msg209581 - (view)

Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer)

Date: 2014-01-28 19:51

On other hand, may be the behavior of io.StringIO is wrong. Because it is different from io.TextIOWrapper.

io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm')).readlines() ['abc\n', 'def\n', 'ghi\n', 'klm'] io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm'), newline=None).readlines() ['abc\n', 'def\n', 'ghi\n', 'klm'] io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm'), newline='').readlines() ['abc\r\n', 'def\n', 'ghi\r', 'klm'] io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm'), newline='\n').readlines() ['abc\r\n', 'def\n', 'ghi\rklm'] io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm'), newline='\r').readlines() ['abc\r', '\ndef\nghi\r', 'klm'] io.TextIOWrapper(io.BytesIO(b'abc\r\ndef\nghi\rklm'), newline='\r\n').readlines() ['abc\r\n', 'def\nghi\rklm']

msg209590 - (view)

Author: Antoine Pitrou (pitrou) * (Python committer)

Date: 2014-01-28 21:42

Using '\n' as default is normal: StringIOs are not stored on disk so there's no point in converting newlines by default (it's only slower while not improving interoperability). '\n' is the default line separator in Python.

So we should just fix the docs here.

The bug when using '\r' or '\r\n' should probably be fixed though (that would be a separate issue).

msg210021 - (view)

Author: Roundup Robot (python-dev) (Python triager)

Date: 2014-02-02 21:51

New changeset 82cfab2ad98d by Antoine Pitrou in branch '3.3': Issue #20423: fix documentation of io.StringIO's newline parameter http://hg.python.org/cpython/rev/82cfab2ad98d

New changeset 69a2cc048c80 by Antoine Pitrou in branch '2.7': Issue #20423: fix documentation of io.StringIO's newline parameter http://hg.python.org/cpython/rev/69a2cc048c80

New changeset df2efd48227e by Antoine Pitrou in branch 'default': Issue #20423: fix documentation of io.StringIO's newline parameter http://hg.python.org/cpython/rev/df2efd48227e

msg210022 - (view)

Author: Antoine Pitrou (pitrou) * (Python committer)

Date: 2014-02-02 21:52

The docs are now fixed, thank you!

History

Date

User

Action

Args

2022-04-11 14:57:57

admin

set

github: 64622

2014-02-02 21:52:34

pitrou

set

status: open -> closed
resolution: fixed
messages: +

stage: needs patch -> resolved

2014-02-02 21:51:52

python-dev

set

nosy: + python-dev
messages: +

2014-01-28 21:42:10

pitrou

set

messages: +

2014-01-28 19:51:04

serhiy.storchaka

set

nosy: + pitrou, benjamin.peterson, stutzbach, hynek
messages: +

2014-01-28 19:44:20

couplewavylines

set

messages: +

2014-01-28 19:37:23

serhiy.storchaka

set

versions: + Python 2.7
nosy: + serhiy.storchaka

messages: +

keywords: + easy
stage: needs patch

2014-01-28 19:26:20

couplewavylines

set

files: + no_translation_output.txt

2014-01-28 19:24:38

couplewavylines

create