[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Changes by Martin v. Löwis: -- keywords: +patch __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Guido van Rossum added the comment: I'ev fixed this slightly differently, by simply changing the *default* of the newline argument to StringIO to be \n. This is a good default; but I see no reason to prevent users from messing with it if they have a need. -- nosy: +gvanrossum resolution: - fixed status: open - closed __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Amaury Forgeot d'Arc added the comment: Here is a new version of the class, which removes the 'newline' argument from the constructor. I also removed the 'encoding' argument, since this is really an implementation detail of the underlying buffer. Index: Lib/io.py === --- Lib/io.py (revision 57564) +++ Lib/io.py (working copy) @@ -1390,10 +1390,10 @@ # XXX This is really slow, but fully functional -def __init__(self, initial_value=, encoding=utf-8, newline=None): +def __init__(self, initial_value=): super(StringIO, self).__init__(BytesIO(), - encoding=encoding, - newline=newline) + encoding=utf-8, + newline='\n') if initial_value: if not isinstance(initial_value, basestring): initial_value = str(initial_value) __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Alexandre Vassalotti added the comment: That's why the current behaviour is not correct: When I write('\n'), getvalue() currently returns '\r\n'. Oh, I missed your example in your initial message. So yes, I agree that StringIO shouldn't translate newlines like that. I attached a patch that should fix the bug. However, I would favor removing the newline keyword argument, instead. __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __Index: Lib/io.py === --- Lib/io.py (revision 57506) +++ Lib/io.py (working copy) @@ -1367,6 +1367,7 @@ initial_value = str(initial_value) self.write(initial_value) self.seek(0) +self._writetranslate = False def getvalue(self): self.flush() ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Alexandre Vassalotti added the comment: As far as I know, StringIO should not do any string transformations. From PEP-3116 New I/O, last paragraph of the section Text I/O: Another implementation, StringIO, creates a file-like TextIO implementation without an underlying Buffered I/O object. [...] It does not support encodings or newline translations; you always read back exactly the characters you wrote. -- nosy: +alexandre.vassalotti __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1029] py3k: io.StringIO.getvalue() returns \r\n
Amaury Forgeot d'Arc added the comment: As far as I know, StringIO should not do any string transformations. (Not sure if you agree with the patch or not) That's why the current behaviour is not correct: When I write('\n'), getvalue() currently returns '\r\n'. __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1029 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com