[pypy-svn] pypy default: Expose TextIOWrapper.newlines

alex_gaynor commits-noreply at bitbucket.org
Mon Jan 31 06:08:12 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r41487:6c3d4e820679
Date: 2011-01-31 00:08 -0500
http://bitbucket.org/pypy/pypy/changeset/6c3d4e820679/

Log:	Expose TextIOWrapper.newlines

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
@@ -416,6 +416,12 @@
         self._check_init(space)
         return space.getattr(self.w_buffer, space.wrap("closed"))
 
+    def newlines_get_w(space, self):
+        self._check_init(space)
+        if self.w_decoder is None:
+            return space.w_None
+        return space.findattr(self.w_decoder, space.wrap("newlines"))
+
     def name_get_w(space, self):
         self._check_init(space)
         return space.getattr(self.w_buffer, space.wrap("name"))
@@ -993,6 +999,7 @@
     buffer = interp_attrproperty_w("w_buffer", cls=W_TextIOWrapper),
     closed = GetSetProperty(W_TextIOWrapper.closed_get_w),
     errors = interp_attrproperty_w("w_errors", cls=W_TextIOWrapper),
+    newlines = GetSetProperty(W_TextIOWrapper.newlines_get_w),
     _CHUNK_SIZE = GetSetProperty(
         W_TextIOWrapper.chunk_size_get_w, W_TextIOWrapper.chunk_size_set_w
     ),

diff --git a/pypy/module/_io/test/test_io.py b/pypy/module/_io/test/test_io.py
--- a/pypy/module/_io/test/test_io.py
+++ b/pypy/module/_io/test/test_io.py
@@ -298,6 +298,22 @@
                 res = f.read()
                 assert res == "aaaxxx".encode(charset)
 
+    def test_newlines_attr(self):
+        import _io
+
+        with _io.open(self.tmpfile, "r") as f:
+            assert f.newlines is None
+
+        with _io.open(self.tmpfile, "wb") as f:
+            f.write("hello\nworld\n")
+
+        with _io.open(self.tmpfile, "r") as f:
+            res = f.readline()
+            assert res == "hello\n"
+            res = f.readline()
+            assert res == "world\n"
+            assert f.newlines == "\n"
+
     def test_custom_decoder(self):
         import codecs
         import _io


More information about the Pypy-commit mailing list