Author: Armin Rigo <[email protected]>
Branch:
Changeset: r2701:61e03368485c
Date: 2016-06-03 13:08 +0200
http://bitbucket.org/cffi/cffi/changeset/61e03368485c/
Log: From PyPy 5.2, bytearray buffers can fetch a raw pointer, so there
is no reason any more to prevent from_buffer(bytearray()).
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5945,8 +5945,8 @@
if (PyBytes_Check(x) || PyUnicode_Check(x))
return 1;
- if (PyByteArray_Check(x)) /* <= this one here for PyPy compatibility */
- return 1;
+ /* From PyPy 5.2, bytearray buffers can fetch a raw pointer, so
+ there is no reason any more to prevent from_buffer(bytearray()). */
return 0;
}
@@ -5958,8 +5958,7 @@
if (invalid_input_buffer_type(x)) {
PyErr_SetString(PyExc_TypeError,
"from_buffer() cannot return the address of the "
- "raw string within a "STR_OR_BYTES" or unicode or "
- "bytearray object");
+ "raw string within a "STR_OR_BYTES" or unicode
object");
return NULL;
}
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3328,13 +3328,12 @@
cast(p, c)[1] += 500
assert list(a) == [10000, 20500, 30000]
-def test_from_buffer_not_str_unicode_bytearray():
+def test_from_buffer_not_str_unicode():
BChar = new_primitive_type("char")
BCharP = new_pointer_type(BChar)
BCharA = new_array_type(BCharP, None)
py.test.raises(TypeError, from_buffer, BCharA, b"foo")
py.test.raises(TypeError, from_buffer, BCharA, u+"foo")
- py.test.raises(TypeError, from_buffer, BCharA, bytearray(b"foo"))
try:
from __builtin__ import buffer
except ImportError:
@@ -3342,16 +3341,27 @@
else:
py.test.raises(TypeError, from_buffer, BCharA, buffer(b"foo"))
py.test.raises(TypeError, from_buffer, BCharA, buffer(u+"foo"))
- py.test.raises(TypeError, from_buffer, BCharA,
- buffer(bytearray(b"foo")))
try:
from __builtin__ import memoryview
except ImportError:
pass
else:
py.test.raises(TypeError, from_buffer, BCharA, memoryview(b"foo"))
- py.test.raises(TypeError, from_buffer, BCharA,
- memoryview(bytearray(b"foo")))
+
+def test_from_buffer_bytearray():
+ a = bytearray(b"xyz")
+ BChar = new_primitive_type("char")
+ BCharP = new_pointer_type(BChar)
+ BCharA = new_array_type(BCharP, None)
+ p = from_buffer(BCharA, a)
+ assert typeof(p) is BCharA
+ assert len(p) == 3
+ assert repr(p) == "<cdata 'char[]' buffer len 3 from 'bytearray' object>"
+ assert p[2] == b"z"
+ p[2] = b"."
+ assert a[2] == ord(".")
+ a[2] = ord("?")
+ assert p[2] == b"?"
def test_from_buffer_more_cases():
try:
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit