Author: Armin Rigo <[email protected]>
Branch:
Changeset: r2731:a54b242f428f
Date: 2016-08-02 18:13 +0200
http://bitbucket.org/cffi/cffi/changeset/a54b242f428f/
Log: The null_byte_after_str branch of PyPy makes ffi.from_buffer(str)
possible.
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5974,6 +5974,11 @@
static int invalid_input_buffer_type(PyObject *x)
{
+ /* From PyPy 5.4, from_buffer() accepts strings (but still not buffers
+ or memoryviews on strings). */
+ if (PyBytes_Check(x))
+ return 0;
+
#if PY_MAJOR_VERSION < 3
if (PyBuffer_Check(x)) {
/* XXX fish fish fish in an inofficial way */
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3341,13 +3341,18 @@
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")
+ p1 = from_buffer(BCharA, b"foo")
+ assert p1 == from_buffer(BCharA, b"foo")
+ import gc; gc.collect()
+ assert p1 == from_buffer(BCharA, b"foo")
py.test.raises(TypeError, from_buffer, BCharA, u+"foo")
try:
from __builtin__ import buffer
except ImportError:
pass
else:
+ # from_buffer(buffer(b"foo")) does not work, because it's not
+ # implemented on pypy; only from_buffer(b"foo") works.
py.test.raises(TypeError, from_buffer, BCharA, buffer(b"foo"))
py.test.raises(TypeError, from_buffer, BCharA, buffer(u+"foo"))
try:
diff --git a/doc/source/ref.rst b/doc/source/ref.rst
--- a/doc/source/ref.rst
+++ b/doc/source/ref.rst
@@ -171,7 +171,7 @@
buffer interface. This is the opposite of ``ffi.buffer()``. It gives
a reference to the existing data, not a copy; for this
reason, and for PyPy compatibility, it does not work with the built-in
-types str or unicode (or buffers/memoryviews on them).
+type unicode; nor buffers/memoryviews to byte or unicode strings.
It is meant to be used on objects
containing large quantities of raw data, like bytearrays
or ``array.array`` or numpy
@@ -193,6 +193,9 @@
method is called), then the ``<cdata>`` object will point to freed
memory and must not be used any more.
+*New in version 1.8:* the python_buffer can be a byte string (but still
+not a buffer/memoryview on a string).
+
ffi.memmove()
+++++++++++++
diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst
--- a/doc/source/whatsnew.rst
+++ b/doc/source/whatsnew.rst
@@ -3,6 +3,13 @@
======================
+v1.8
+====
+
+* Removed the restriction that ``ffi.from_buffer()`` cannot be used on
+ byte strings (PyPy was improved and can now support that case).
+
+
v1.7
====
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit