[Numpy-discussion] numpy array casting ruled not safe

Sebastian Berg sebastian at sipsolutions.net
Sun Mar 8 06:49:20 EDT 2015

On Sa, 2015-03-07 at 18:21 -0800, Jaime Fernández del Río wrote:
>         I note that on SO Jaime made the suggestion that take use
>         unsafe casting and throw an error on out of bounds indexes.
>         That sounds reasonable, although for sufficiently large
>         integer types an index could wrap around to a good value.
>         Maybe make it work only for npy_uintp.
>         Chuck  
> It is mostly about consistency, and having take match what indexing
> already does, which is to unsafely cast all integers:
> In [11]: np.arange(10)[np.uint64(2**64-1)]
> Out[11]: 9
> I think no one has ever complained about that obviously wrong
> behavior, but people do get annoyed if they cannot use their perfectly
> valid uint64 array because we want to protect them from themselves.
> Sebastian has probably given this more thought than anyone else, it
> would be interesting to hear his thoughts on this.

Not really, there was no change in behaviour for arrays here. Apparently
though (which I did not realize), there was a change for numpy
scalars/0-d arrays. Of course I think ideally "same_type" casting would
raise an error or at least warn on out of bounds integers, but we do not
have a mechanism for that.

We could fix this, I think Jaime you had thought about that at some
point? But it would require loop specializations for every integer type.

So, I am not sure what to prefer, but for the user indexing with
unsigned integers has to keep working without explicit cast. Of course
the fact that it is dangerous, is bothering me a bit, even if a
dangerous wrap-around seems unlikely in practice.

- Sebastian

> Jaime
> -- 
> (\__/)
> ( O.o)
> ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
> planes de dominación mundial.
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150308/89416272/attachment.sig>

More information about the NumPy-Discussion mailing list