[pypy-svn] r78130 - in pypy/branch/fast-forward/pypy/module/_io: . test

afa at codespeak.net afa at codespeak.net
Wed Oct 20 14:45:07 CEST 2010


Author: afa
Date: Wed Oct 20 14:45:05 2010
New Revision: 78130

Modified:
   pypy/branch/fast-forward/pypy/module/_io/interp_iobase.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py
Log:
FileIO.readlines()


Modified: pypy/branch/fast-forward/pypy/module/_io/interp_iobase.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/interp_iobase.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/interp_iobase.py	Wed Oct 20 14:45:05 2010
@@ -163,6 +163,29 @@
 
         return space.wrap(builder.build())
 
+    @unwrap_spec('self', ObjSpace, W_Root)
+    def readlines_w(self, space, w_hint=None):
+        hint = convert_size(space, w_hint)
+
+        if hint <= 0:
+            return space.newlist(space.unpackiterable(self))
+
+        lines_w = []
+        length = 0
+        while True:
+            w_line = space.call_method(self, "readline")
+            line_length = space.int_w(space.len(w_line))
+            if line_length == 0: # done
+                break
+
+            lines_w.append(w_line)
+
+            length += line_length
+            if length > hint:
+                break
+
+        return space.newlist(lines_w)
+
 W_IOBase.typedef = TypeDef(
     '_IOBase',
     __new__ = generic_new_descr(W_IOBase),
@@ -180,6 +203,7 @@
     closed = GetSetProperty(W_IOBase.closed_get_w),
 
     readline = interp2app(W_IOBase.readline_w),
+    readlines = interp2app(W_IOBase.readlines_w),
     )
 
 class W_RawIOBase(W_IOBase):

Modified: pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py	Wed Oct 20 14:45:05 2010
@@ -44,6 +44,14 @@
         assert f.readline() == ''
         f.close()
 
+    def test_readlines(self):
+        import _io
+        f = _io.FileIO(self.tmpfile, 'rb')
+        assert f.readlines() == ["a\n", "b\n", "c"]
+        f.seek(0)
+        assert f.readlines(3) == ["a\n", "b\n"]
+        f.close()
+
     def test_readall(self):
         import _io
         f = _io.FileIO(self.tmpfile, 'rb')



More information about the Pypy-commit mailing list