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.

classification
Title: io.StringIO's universal newlines support is broken in 3.0.1
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, erickt
Priority: normal Keywords:

Created on 2009-03-09 03:05 by erickt, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg83352 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-03-09 03:05
Python version 3.0.1's io.StringIO has a bug when trying to use 
universal newlines on the mac. It's fixed in 3.1a1 though. Here's the 
exception:

>>> io.StringIO('hello there\r\nlela\r\n', newline=None).readlines()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3
.0/io.py", line 536, in readlines
    return list(self)
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3
.0/io.py", line 523, in __next__
    line = self.readline()
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3
.0/io.py", line 2110, in readline
    more_line = self.read(self._CHUNK_SIZE)
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3
.0/io.py", line 2007, in read
    res = self._decode_newlines(self._read(n), True)
  File 
"/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3
.0/io.py", line 1953, in _decode_newlines
    return output
UnboundLocalError: local variable 'output' referenced before assignment


The broken code is this:

            if self._readtranslate:
                if crlf:
                    output = input.replace("\r\n", "\n")
                if cr:
                    output = input.replace("\r", "\n")
            else:
                output = input

It appears to fix the problem if we do this:

            output = input
            if self._readtranslate:
                if crlf:
                    output = output.replace("\r\n", "\n")
                if cr:
                    output = output.replace("\r", "\n")
msg83405 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-03-09 21:16
Thanks, fixed in r70285.
History
Date User Action Args
2022-04-11 14:56:46adminsetgithub: 49706
2009-03-09 21:16:56benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg83405

resolution: fixed
2009-03-09 03:05:30ericktcreate