[Numpy-discussion] NPY_FORCECAST

Travis E. Oliphant oliphant at enthought.com
Fri Jan 25 22:13:10 EST 2008


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





More information about the NumPy-Discussion mailing list