Testing the python buffer protocol (bf_getbuffer / tp_as_buffer)
Hi, I've implemented the buffer protocol (http://www.python.org/dev/peps/pep-3118/) for some matrix class and when I manually call PyObject_GetBuffer on that object I see that I get the right matrix. Now I'd like to see numpy use the buffer protocol of my class. Does anyone know how to test that? What do I need to write, just x=MyMatrix([1,2,3]) y=numpy.array(x) (that doesn't call the buffer function though - so it must be sth else)? Any ideas? Soeren -- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
What happens if you use y=numpy.frombuffer(x) ? //Torgil On Sat, Dec 17, 2011 at 1:41 AM, Soeren Sonnenburg <sonne@debian.org> wrote:
Hi,
I've implemented the buffer protocol (http://www.python.org/dev/peps/pep-3118/) for some matrix class and when I manually call PyObject_GetBuffer on that object I see that I get the right matrix.
Now I'd like to see numpy use the buffer protocol of my class. Does anyone know how to test that? What do I need to write, just
x=MyMatrix([1,2,3]) y=numpy.array(x)
(that doesn't call the buffer function though - so it must be sth else)?
Any ideas? Soeren -- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Doesn't work, complaining that the object has no __buffer__ attribute. Digging into the numpy c code it seems numpy doesn't even support the buffer protocol but only the deprecated (old) one http://docs.python.org/c-api/objbuffer.html . At least there is nowhere a PyObject_CheckBuffer() call but frombuffer in the numpy C code checks for (Py_TYPE(buf)->tp_as_buffer->bf_getwritebuffer == NULL && Py_TYPE(buf)->tp_as_buffer->bf_getreadbuffer == NULL) . So it needs bf_read/writebuffer to be set instead of bf_getbuffer and the array buffer protocol :-( Soeren On Sat, 2011-12-17 at 03:20 +0100, Torgil Svensson wrote:
What happens if you use
y=numpy.frombuffer(x) ?
//Torgil
On Sat, Dec 17, 2011 at 1:41 AM, Soeren Sonnenburg <sonne@debian.org> wrote:
Hi,
I've implemented the buffer protocol (http://www.python.org/dev/peps/pep-3118/) for some matrix class and when I manually call PyObject_GetBuffer on that object I see that I get the right matrix.
Now I'd like to see numpy use the buffer protocol of my class. Does anyone know how to test that? What do I need to write, just
x=MyMatrix([1,2,3]) y=numpy.array(x)
(that doesn't call the buffer function though - so it must be sth else)?
Any ideas? Soeren -- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
What version of numpy are you using? IIRC the new buffer protocol has been supported since numpy 1.5. On 17 December 2011 08:42, Soeren Sonnenburg <sonne@debian.org> wrote:
Doesn't work, complaining that the object has no __buffer__ attribute.
Digging into the numpy c code it seems numpy doesn't even support the buffer protocol but only the deprecated (old) one http://docs.python.org/c-api/objbuffer.html .
At least there is nowhere a PyObject_CheckBuffer() call but frombuffer in the numpy C code checks for
(Py_TYPE(buf)->tp_as_buffer->bf_getwritebuffer == NULL && Py_TYPE(buf)->tp_as_buffer->bf_getreadbuffer == NULL)
.
So it needs bf_read/writebuffer to be set instead of bf_getbuffer and the array buffer protocol :-(
Soeren
On Sat, 2011-12-17 at 03:20 +0100, Torgil Svensson wrote:
What happens if you use
y=numpy.frombuffer(x) ?
//Torgil
On Sat, Dec 17, 2011 at 1:41 AM, Soeren Sonnenburg <sonne@debian.org> wrote:
Hi,
I've implemented the buffer protocol (http://www.python.org/dev/peps/pep-3118/) for some matrix class and when I manually call PyObject_GetBuffer on that object I see that I get the right matrix.
Now I'd like to see numpy use the buffer protocol of my class. Does anyone know how to test that? What do I need to write, just
x=MyMatrix([1,2,3]) y=numpy.array(x)
(that doesn't call the buffer function though - so it must be sth else)?
Any ideas? Soeren -- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
17.12.2011 09:42, Soeren Sonnenburg kirjoitti:
Doesn't work, complaining that the object has no __buffer__ attribute.
Digging into the numpy c code it seems numpy doesn't even support the buffer protocol but only the deprecated (old) one http://docs.python.org/c-api/objbuffer.html . [clip]
Since Numpy version 1.5, the new buffer protocol is supported. -- Pauli Virtanen
On Sat, 2011-12-17 at 15:29 +0100, Pauli Virtanen wrote:
17.12.2011 09:42, Soeren Sonnenburg kirjoitti:
Doesn't work, complaining that the object has no __buffer__ attribute.
Digging into the numpy c code it seems numpy doesn't even support the buffer protocol but only the deprecated (old) one http://docs.python.org/c-api/objbuffer.html . [clip]
Since Numpy version 1.5, the new buffer protocol is supported.
I've looked at the source code of numpy 1.6.1 and couldn't find the respective code... I guess I must be doing something wrong but there really was no call to PyObject_CheckBuffer() ... The problem is I don't really know what is supposed to happen if the new buffer protocol is supported by some class named say Foo. Could I then do x=Foo([1,2,3]) numpy.array([2,2,2])+x and such operations? Soeren -- For the one fact about the future of which we can be certain is that it will be utterly fantastic. -- Arthur C. Clarke, 1962
18.12.2011 00:49, Soeren Sonnenburg kirjoitti: [clip]
I've looked at the source code of numpy 1.6.1 and couldn't find the respective code... I guess I must be doing something wrong but there really was no call to PyObject_CheckBuffer() ...
Look for PyObject_GetBuffer
The problem is I don't really know what is supposed to happen if the new buffer protocol is supported by some class named say Foo. Could I then do
x=Foo([1,2,3])
numpy.array([2,2,2])+x
and such operations?
Yes. You can try it out with Python's builtin memoryview class. -- Pauli Virtanen
participants (4)
-
mark florisson
-
Pauli Virtanen
-
Soeren Sonnenburg
-
Torgil Svensson