[pypy-commit] pypy default: test/fix file.writelines(buffer) behavior to match CPython

bdkearns noreply at buildbot.pypy.org
Fri Apr 25 18:55:08 CEST 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r70975:f8870a4ed20a
Date: 2014-04-25 12:54 -0400
http://bitbucket.org/pypy/pypy/changeset/f8870a4ed20a/

Log:	test/fix file.writelines(buffer) behavior to match CPython

diff --git a/pypy/module/_file/interp_file.py b/pypy/module/_file/interp_file.py
--- a/pypy/module/_file/interp_file.py
+++ b/pypy/module/_file/interp_file.py
@@ -468,7 +468,12 @@
                 if not e.match(space, space.w_StopIteration):
                     raise
                 break  # done
-            self.file_write(w_line)
+            try:
+                line = w_line.charbuf_w(space)
+            except TypeError:
+                raise OperationError(space.w_TypeError, space.wrap(
+                    "writelines() argument must be a sequence of strings"))
+            self.file_write(space.wrap(line))
 
     def file_readinto(self, w_rwbuffer):
         """readinto() -> Undocumented.  Don't use this; it may go away."""
diff --git a/pypy/module/_file/test/test_file_extra.py b/pypy/module/_file/test/test_file_extra.py
--- a/pypy/module/_file/test/test_file_extra.py
+++ b/pypy/module/_file/test/test_file_extra.py
@@ -386,6 +386,27 @@
         assert len(somelines) > 200
         assert somelines == lines[:len(somelines)]
 
+    def test_writelines(self):
+        import array
+        fn = self.temptestfile
+        with file(fn, 'w') as f:
+            f.writelines(['abc'])
+            f.writelines([u'def'])
+            exc = raises(TypeError, f.writelines, [array.array('c', 'ghi')])
+            assert str(exc.value) == "writelines() argument must be a sequence of strings"
+            exc = raises(TypeError, f.writelines, [memoryview('jkl')])
+            assert str(exc.value) == "writelines() argument must be a sequence of strings"
+        assert open(fn, 'r').readlines() == ['abcdef']
+
+        with file(fn, 'wb') as f:
+            f.writelines(['abc'])
+            f.writelines([u'def'])
+            exc = raises(TypeError, f.writelines, [array.array('c', 'ghi')])
+            assert str(exc.value) == "writelines() argument must be a sequence of strings"
+            exc = raises(TypeError, f.writelines, [memoryview('jkl')])
+            assert str(exc.value) == "writelines() argument must be a sequence of strings"
+        assert open(fn, 'rb').readlines() == ['abcdef']
+
     def test_nasty_writelines(self):
         # The stream lock should be released between writes
         fn = self.temptestfile


More information about the pypy-commit mailing list