Christopher Barker wrote: > Greg Ewing wrote: >> Variables declared as referring to extension types also >> accept the value None, because you need some way of >> representing a null value. > > I see - that makes sense. > >> 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. > > That works, thanks. > >> (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.) > > I agree -- I think that makes more sense as the default -- if a function > accepts None, there will almost always have to be special case case to > handle it anyway. > > Dag Sverre Seljebotn wrote: >> 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. > > Not for me: this seems to have no effect. Nor does using it as a decorator: > > @cython.nonecheck(True) > > Cython 0.11.2
Your example code is wrong; you need to actually try to use the object; THEN you will get an exception (where you would otherwise get a crash). Returning a None value is perfectly OK. From the docs I pointed you to (I'm sure it could be written clearer): nonecheck: If set to False, Cython is free to assume that native field accesses on variables typed as an extension type, or buffer accesses on a buffer variable, never occurs when the variable is set to None. Otherwise a check is inserted and the appropriate exception is raised. This is off by default for performance reasons. -- Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
