[Numpy-discussion] Removing undocumented __buffer__ attribute lookup

Matti Picus matti.picus at gmail.com
Wed Feb 27 03:20:41 EST 2019


In digging around the code, I found a gem in PyArray_FromBuffer (exposed 
to python as numpy.frombuffer). If a PyObject* does not have a 
tp_as_buffer->bf_getbuffer function, we check if the python object has a 
__buffer__ attribute. If so we use that as buf in 
PyObject_GetBuffer(buf, ...).


This seems to stem back to the original numerics code, where getBuffer 
would look up the attribute and call it as a method. PyArray_FromBuffer 
does not call the attribute as a method, it simply passes it on to 
PyObject_GetBuffer, which will then raise an error saying it cannot 
convert a method. You can try this out by creating a class with a 
__buffer__ method and calling numpy.frombuffer on it.


I submitted a pull request to remove the code. Since it is undocumented 
and (as far as I can tell) broken, I do not think we need a deprecation 
cycle.


More details, including links to the original numeric code from 2005, in 
the PR https://github.com/numpy/numpy/pull/13049


Any thoughts or objections?


Matti



More information about the NumPy-Discussion mailing list