[Numpy-discussion] Ensuring one can operate on array-like argument in place
oleksandr.pavlyk at intel.com
Sat Nov 12 12:00:07 EST 2016
In my Cython code a function processes it argument x as follows:
x_arr = PyArray_CheckFromAny(
x, NULL, 0, 0,
cnp.NPY_ELEMENTSTRIDES | cnp.NPY_ENSUREARRAY | cnp.NPY_NOTSWAPPED, NULL)
if x_arr is not x:
in_place = 1 # a copy was made, so we can work in place.
The logic is of the last line turns out to be incorrect, because the input x can be a class with an array interface:
def __init__(self, data):
self._data = data
self.__array_interface__ = data.__array_interface__
Feeding my function FakeArray(xx), x_arr will point into the content of xx, resulting in unwarranted content
overwrite of xx.
I am trying to replace that condition with
if x_arr is not x and cnp.PyArray_Check(x):
# a copy was made, so we can work in place.
in_place = 1 if cnp.PyArray_CHKFLAGS(x_arr, cnp.NPY_WRITEABLE) else 0
I am wondering if I perhaps overlooked some case.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion