Discussion:
[Numpy-discussion] deprecating assignment to ndarray.data
Nathaniel Smith
2016-01-22 00:21:48 UTC
Permalink
So it turns out that ndarray.data supports assignment at the Python
level, and what it does is just assign to the ->data field of the
ndarray object:
https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/getset.c#L325

This kind of assignment been deprecated at the C level since 1.7, and
is totally unsafe -- if there are any views pointing to the array when
this happens, then they'll be left pointing off into unallocated
memory.

E.g.:

a = np.arange(10)
b = np.linspace(0, 1, 10)
c = a.view()
a.data = b.data
# Now c points into free'd memory

Can we deprecate or just remove this?

(Also filed issue: https://github.com/numpy/numpy/issues/7093)

-n
--
Nathaniel J. Smith -- https://vorpus.org
Juan Nunez-Iglesias
2016-01-22 02:16:50 UTC
Permalink
Does this apply in any way to the .data attribute in scipy.sparse matrices?
I fiddle with that quite often!
Post by Nathaniel Smith
So it turns out that ndarray.data supports assignment at the Python
level, and what it does is just assign to the ->data field of the
https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/getset.c#L325
This kind of assignment been deprecated at the C level since 1.7, and
is totally unsafe -- if there are any views pointing to the array when
this happens, then they'll be left pointing off into unallocated
memory.
a = np.arange(10)
b = np.linspace(0, 1, 10)
c = a.view()
a.data = b.data
# Now c points into free'd memory
Can we deprecate or just remove this?
(Also filed issue: https://github.com/numpy/numpy/issues/7093)
-n
--
Nathaniel J. Smith -- https://vorpus.org
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Nathaniel Smith
2016-01-22 02:32:55 UTC
Permalink
Post by Juan Nunez-Iglesias
Does this apply in any way to the .data attribute in scipy.sparse matrices?
Nope!

-n
Jonathan J. Helmus
2016-01-22 06:24:12 UTC
Permalink
Post by Juan Nunez-Iglesias
Post by Juan Nunez-Iglesias
Does this apply in any way to the .data attribute in scipy.sparse
matrices?
Nope!
-n
How about the .data attribute of masked arrays? I'm guessing there may
be a decent amount of code that uses array.data to try to duck-type
ndarrays and MaskedArrays even though there are better ways to do
this, for example np.ma.getdata.

Cheers,

- Jonathan Helmus
Nathaniel Smith
2016-01-22 08:17:53 UTC
Permalink
Post by Juan Nunez-Iglesias
Does this apply in any way to the .data attribute in scipy.sparse matrices?
Nope!
-n
How about the .data attribute of masked arrays? I'm guessing there may be a
decent amount of code that uses array.data to try to duck-type ndarrays and
MaskedArrays even though there are better ways to do this, for example
np.ma.getdata.
It turns out the .data attribute on MaskedArrays has nothing
whatsoever to do with the .data attribute on ndarrays, so yeah, this
is also unaffected.

-n
--
Nathaniel J. Smith -- https://vorpus.org
Loading...