[pypy-commit] pypy refactor-buffer-api: fix bytearray buffer readonly flag
bdkearns
noreply at buildbot.pypy.org
Thu Apr 24 21:37:40 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch: refactor-buffer-api
Changeset: r70931:ad400df172a4
Date: 2014-04-24 15:13 -0400
http://bitbucket.org/pypy/pypy/changeset/ad400df172a4/
Log: fix bytearray buffer readonly flag
diff --git a/pypy/module/_io/test/test_bufferedio.py b/pypy/module/_io/test/test_bufferedio.py
--- a/pypy/module/_io/test/test_bufferedio.py
+++ b/pypy/module/_io/test/test_bufferedio.py
@@ -143,6 +143,8 @@
assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, f.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
+ exc = raises(TypeError, f.readinto, buffer(bytearray("hello")))
+ assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, f.readinto, memoryview(b"hello"))
assert str(exc.value) == "must be read-write buffer, not memoryview"
f.close()
diff --git a/pypy/module/_io/test/test_bytesio.py b/pypy/module/_io/test/test_bytesio.py
--- a/pypy/module/_io/test/test_bytesio.py
+++ b/pypy/module/_io/test/test_bytesio.py
@@ -103,6 +103,8 @@
assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, b.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
+ exc = raises(TypeError, b.readinto, buffer(bytearray("hello")))
+ assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, b.readinto, memoryview(b"hello"))
assert str(exc.value) == "must be read-write buffer, not memoryview"
b.close()
diff --git a/pypy/module/_io/test/test_fileio.py b/pypy/module/_io/test/test_fileio.py
--- a/pypy/module/_io/test/test_fileio.py
+++ b/pypy/module/_io/test/test_fileio.py
@@ -140,6 +140,8 @@
assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, f.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
+ exc = raises(TypeError, f.readinto, buffer(bytearray("hello")))
+ assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, f.readinto, memoryview(b"hello"))
assert str(exc.value) == "must be read-write buffer, not memoryview"
f.close()
diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -28,13 +28,13 @@
return "%s(%s)" % (w_self.__class__.__name__, ''.join(w_self.data))
def buffer_w(self, space, flags):
- return BytearrayBuffer(self.data)
+ return BytearrayBuffer(self.data, False)
def readbuf_w(self, space):
- return BytearrayBuffer(self.data)
+ return BytearrayBuffer(self.data, True)
def writebuf_w(self, space):
- return BytearrayBuffer(self.data)
+ return BytearrayBuffer(self.data, False)
def charbuf_w(self, space):
return ''.join(self.data)
@@ -1131,9 +1131,9 @@
class BytearrayBuffer(Buffer):
_immutable_ = True
- def __init__(self, data):
+ def __init__(self, data, readonly):
self.data = data
- self.readonly = False
+ self.readonly = readonly
def getlength(self):
return len(self.data)
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -426,10 +426,10 @@
b = bytearray('abcdefghi')
buf = buffer(b)
assert buf[2] == 'c'
- buf[3] = 'D'
- assert b == 'abcDefghi'
- buf[4:6] = 'EF'
- assert b == 'abcDEFghi'
+ exc = raises(TypeError, "buf[2] = 'D'")
+ assert str(exc.value) == "buffer is read-only"
+ exc = raises(TypeError, "buf[4:6] = 'EF'")
+ assert str(exc.value) == "buffer is read-only"
def test_decode(self):
b = bytearray('abcdefghi')
More information about the pypy-commit
mailing list