New submission from Martin Panter: I noticed that the newline translation in the io.StringIO class does not behave as I would expect:
>>> text = "NL\n" "CRLF\r\n" "CR\r" "EOF" >>> s = StringIO(text, newline="\r\n") >>> s.getvalue() 'NL\r\nCRLF\r\r\nCR\rEOF' # Why is this not just equal to “text”? >>> tuple(s) ('NL\r\n', 'CRLF\r\r\n', 'CR\rEOF') # Too many lines, butchered EOL sequence >>> tuple(TextIOWrapper(BytesIO(text.encode("ascii")), "ascii", newline="\r\n")) ('NL\nCRLF\r\n', 'CR\rEOF') # This seems more reasonable Although I have never had a use for newline="\r", it also seems broken: >>> tuple(StringIO(text, newline="\r")) ('NL\r', 'CRLF\r', '\r', 'CR\r', 'EOF') # Way too many lines >>> tuple(TextIOWrapper(BytesIO(text.encode("ascii")), "ascii", newline="\r")) ('NL\nCRLF\r', '\nCR\r', 'EOF') The other newline options ("\n", "", and None) seem to behave correctly though. There seem to be quite a few bug reports to do with newline translation in StringIO, but I couldn’t see anything specifically about this one. However the issue was mentioned at <https://bugs.python.org/issue20423#msg209581>. I noticed there are test cases which appear to bless the current behaviour, as seen in the patch for Issue 20498. IMO these tests are wrong. ---------- components: IO messages: 226895 nosy: vadmium priority: normal severity: normal status: open title: Bizarre StringIO(newline="\r\n") translation type: behavior versions: Python 3.4 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue22413> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com