[pypy-commit] pypy utf8-unicode2: Handle newlines as ascii strings in W_TextIOWrapper

waedt noreply at buildbot.pypy.org
Mon Aug 11 20:06:45 CEST 2014


Author: Tyler Wade <wayedt at gmail.com>
Branch: utf8-unicode2
Changeset: r72755:8f5d79d24198
Date: 2014-08-11 12:58 -0500
http://bitbucket.org/pypy/pypy/changeset/8f5d79d24198/

Log:	Handle newlines as ascii strings in W_TextIOWrapper

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
@@ -372,6 +372,7 @@
             newline = None
         else:
             newline = space.unicode_w(w_newline)
+
         if (newline is not None and len(newline) > 0 and
             not (utf8.EQ(newline, Utf8Str('\n')) or
                  utf8.EQ(newline, Utf8Str('\r\n')) or
@@ -379,20 +380,23 @@
             r = space.str_w(space.repr(w_newline))
             raise OperationError(space.w_ValueError, space.wrap(
                 "illegal newline value: %s" % (r,)))
+        elif newline is not None:
+            # newline is guaranteed to be either empty or ascii
+            newline = newline.bytes
 
         self.line_buffering = line_buffering
 
-        self.readuniversal = newline is None or len(newline) == 0
+        self.readuniversal = not newline
         self.readtranslate = newline is None
         self.readnl = newline
 
-        self.writetranslate = newline is None or len(newline) == 0
+        self.writetranslate = (newline is not None and newline != '')
         if not self.readuniversal:
             self.writenl = self.readnl
-            if utf8.EQ(self.writenl, Utf8Str('\n')):
+            if self.writenl == '\n':
                 self.writenl = None
         elif _WINDOWS:
-            self.writenl = Utf8Str("\r\n")
+            self.writenl = "\r\n"
         else:
             self.writenl = None
 
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
@@ -192,6 +192,26 @@
                         assert got_line == exp_line
                     assert len(got_lines) == len(exp_lines)
 
+    def test_newlines_output(self):
+        import _io
+        import os
+        testdict = {
+            "": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
+            "\n": b"AAA\nBBB\nCCC\nX\rY\r\nZ",
+            "\r": b"AAA\rBBB\rCCC\rX\rY\r\rZ",
+            "\r\n": b"AAA\r\nBBB\r\nCCC\r\nX\rY\r\r\nZ",
+            }
+        tests = [(None, testdict[os.linesep])] + sorted(testdict.items())
+        for newline, expected in tests:
+            buf = _io.BytesIO()
+            txt = _io.TextIOWrapper(buf, encoding="ascii", newline=newline)
+            txt.write(u"AAA\nB")
+            txt.write(u"BB\nCCC\n")
+            txt.write(u"X\rY\r\nZ")
+            txt.flush()
+            assert buf.closed == False
+            assert buf.getvalue() == expected
+
     def test_readline(self):
         import _io
 


More information about the pypy-commit mailing list