Hi, I am currently using PyArray_FromObject() to convert an input argument to a 32-bit integer array. On a 64-bit architecture, it is easy to create an integer array whose default type is 64-bit. When this is sent to PyArray_FromObject(), it raises an error, saying "array cannot be safely cast to required type", even though all of its elements are representable by 32 bits. I see from the "Guide to NumPy" that I could call PyArray_FromAny() instead, providing the flag NPY_FORCECAST to force the cast to be made. However, this code is within numpy.i, will be used by others, and could lead to unexpected behavior if I were to make the change. How hard would it be to implement a new option (NPY_ATTEMPTCAST?) that attempts to make the cast, but raises an OverflowError if any of the source data is too large for the target array? (Or, alternatively, make this the default behavior if NPY_FORCECAST is false?) Thanks ** Bill Spotz ** ** Sandia National Laboratories Voice: (505)845-0170 ** ** P.O. Box 5800 Fax: (505)284-0154 ** ** Albuquerque, NM 87185-0370 Email: wfspotz@sandia.gov **
Bill Spotz wrote:
Hi,
I am currently using PyArray_FromObject() to convert an input argument to a 32-bit integer array. On a 64-bit architecture, it is easy to create an integer array whose default type is 64-bit. When this is sent to PyArray_FromObject(), it raises an error, saying "array cannot be safely cast to required type", even though all of its elements are representable by 32 bits.
There is not a per-element check to casting. The casting is done with a C-level coercion which does not check for overflow.
How hard would it be to implement a new option (NPY_ATTEMPTCAST?) that attempts to make the cast, but raises an OverflowError if any of the source data is too large for the target array? (Or, alternatively, make this the default behavior if NPY_FORCECAST is false?)
This would be a big change because it would have to be implemented (as far as I can tell) with some kind of checked-casting loops at the C-level. So, it could be done, but it would be a fairly large effort (and would impact the size of numpy significantly, I think). -Travis
participants (2)
-
Bill Spotz
-
Travis E. Oliphant