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

afa at codespeak.net afa at codespeak.net
Thu Nov 18 09:05:21 CET 2010


Author: afa
Date: Thu Nov 18 09:05:19 2010
New Revision: 79227

Modified:
   pypy/branch/fast-forward/pypy/module/_io/interp_iobase.py
   pypy/branch/fast-forward/pypy/module/_io/test/test_fileio.py
Log:
add .writelines to all io classes


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	Thu Nov 18 09:05:19 2010
@@ -228,6 +228,21 @@
 
         return space.newlist(lines_w)
 
+    @unwrap_spec('self', ObjSpace, W_Root)
+    def writelines_w(self, space, w_lines):
+        self._check_closed(space)
+
+        w_iterator = space.iter(w_lines)
+
+        while True:
+            try:
+                w_line = space.next(w_iterator)
+            except OperationError, e:
+                if not e.match(space, space.w_StopIteration):
+                    raise
+                break  # done
+            space.call_method(self, "write", w_line)
+
 W_IOBase.typedef = TypeDef(
     '_IOBase',
     __new__ = generic_new_descr(W_IOBase),
@@ -251,6 +266,7 @@
 
     readline = interp2app(W_IOBase.readline_w),
     readlines = interp2app(W_IOBase.readlines_w),
+    writelines = interp2app(W_IOBase.writelines_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	Thu Nov 18 09:05:19 2010
@@ -77,6 +77,16 @@
         f.close()
         f2.close()
 
+    def test_writelines(self):
+        import _io
+        filename = self.tmpfile + '_w'
+        f = _io.FileIO(filename, 'wb')
+        f.writelines(["line1\n", "line2", "line3"])
+        f2 = _io.FileIO(filename, 'rb')
+        assert f2.read() == "line1\nline2line3"
+        f.close()
+        f2.close()
+
     def test_seek(self):
         import _io
         f = _io.FileIO(self.tmpfile, 'rb')



More information about the Pypy-commit mailing list