[pypy-commit] pypy default: fix for issue #3012, if no \r no need to translate

mattip pypy.commits at gmail.com
Mon May 6 22:19:38 EDT 2019


Author: Matti Picus <matti.picus at gmail.com>
Branch: 
Changeset: r96572:436eebe7adb1
Date: 2019-05-06 21:52 -0400
http://bitbucket.org/pypy/pypy/changeset/436eebe7adb1/

Log:	fix for issue #3012, if no \r no need to translate

diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -105,16 +105,10 @@
         # desired, all in one pass.
         seennl = self.seennl
 
-        # If, up to now, newlines are consistently \n, do a quick check
-        # for the \r
-        only_lf = False
-        if seennl == SEEN_LF or seennl == 0:
-            only_lf = (output.find('\r') < 0)
-
-        if only_lf:
-            # If not already seen, quick scan for a possible "\n" character.
+        if output.find('\r') < 0:
+            # If no \r, quick scan for a possible "\n" character.
             # (there's nothing else to be done, even when in translation mode)
-            if seennl == 0 and output.find('\n') >= 0:
+            if output.find('\n') >= 0:
                 seennl |= SEEN_LF
                 # Finished: we have scanned for newlines, and none of them
                 # need translating.
diff --git a/pypy/module/_io/test/test_textio.py b/pypy/module/_io/test/test_textio.py
--- a/pypy/module/_io/test/test_textio.py
+++ b/pypy/module/_io/test/test_textio.py
@@ -377,3 +377,13 @@
         _check(dec)
         dec = _io.IncrementalNewlineDecoder(None, translate=True)
         _check(dec)
+
+    def test_newlines2(self):
+        import _io, codecs
+        inner_decoder = codecs.getincrementaldecoder("utf-8")()
+        decoder = _io.IncrementalNewlineDecoder(inner_decoder, translate=True)
+        msg = b"abc\r\n\n\r\r\n\n"
+        decoded = ''
+        for ch in msg:
+            decoded += decoder.decode(ch)
+        assert set(decoder.newlines) == {"\r", "\n", "\r\n"}


More information about the pypy-commit mailing list