[pypy-commit] pypy stdlib-2.7.8: fix file.writelines handling of buffers
bdkearns
noreply at buildbot.pypy.org
Sat Aug 23 19:02:16 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.8
Changeset: r73009:41f8b9449f35
Date: 2014-08-23 13:01 -0400
http://bitbucket.org/pypy/pypy/changeset/41f8b9449f35/
Log: fix file.writelines handling of buffers
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
@@ -466,7 +466,10 @@
for i, w_line in enumerate(lines):
if not space.isinstance_w(w_line, space.w_str):
try:
- line = w_line.charbuf_w(space)
+ if self.binary:
+ line = w_line.readbuf_w(space).as_str()
+ else:
+ line = w_line.charbuf_w(space)
except TypeError:
raise OperationError(space.w_TypeError, space.wrap(
"writelines() argument must be a sequence of strings"))
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
@@ -402,11 +402,12 @@
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"
+ f.writelines([array.array('c', 'ghi')])
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']
+ out = open(fn, 'rb').readlines()[0]
+ assert out[0:5] == 'abcd\x00'
+ assert out[-3:] == 'ghi'
with file(fn, 'wb') as f:
exc = raises(TypeError, f.writelines, ['abc', memoryview('def')])
More information about the pypy-commit
mailing list