Note also that in Cython there is a "nonecheck" compiler directive. If you write

#cython: nonecheck=True

at the top of your file, an exception will be raised instead. The will slow 
down all accesses to cdef classes somewhat though.

wiki.cython.org/enhancements/compilerdirectives

Dag Sverre Seljebotn
-----Original Message-----
From: Greg Ewing <[email protected]>
Date: Saturday, Aug 8, 2009 3:25 am
Subject: Re: [Cython] Checking for numpy arrays and None
To: [email protected]: [email protected]

Christopher Barker wrote:
>
>> I just accidentally passed None in to a cython function that is 
> expecting a numpy array, and got a hard crash.
> 
> def none_test(np.ndarray[np.uint8_t, ndim=1, mode="c"] a):
>      return a
>
>Variables declared as referring to extension types also
>accept the value None, because you need some way of
>representing a null value.
>
>The type checking code generated for function arguments
>also accepts None by default, but you can override this
>by declaring the argument as "not None", e.g.
>
>   def none_test(np.ndarray[np.uint8_t, ndim=1, mode="c"] a not None):
>
>You will then get an exception if you try to pass None
>to this function.
>
>(In Pyrex I'm thinking about changing this so that the
>default is *not* to accept None for an argument value,
>because this seems to trip up a lot of people.)
>
>-- 
>Greg
>_______________________________________________
>Cython-dev mailing list
>[email protected]
>http://codespeak.net/mailman/listinfo/cython-dev
>

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to