[Numpy-discussion] NPY_FORCECAST

Bill Spotz wfspotz@sandia....
Fri Jan 25 13:36:33 CST 2008


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  


** 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 **

More information about the Numpy-discussion mailing list