[Numpy-discussion] ufunc and errors

Robert Kern robert.kern@gmail....
Wed Sep 30 10:33:46 CDT 2009

On Wed, Sep 30, 2009 at 09:34, Dag Sverre Seljebotn
<dagss@student.matnat.uio.no> wrote:
> I looked and looked in the docs, but couldn't find an answer to this:
> When writing a ufunc, is it possible somehow to raise a Python exception
> (by acquiring the GIL first to raise it, set a flag and a callback which
> will be called with the GIL, or otherwise?).

You cannot acquire the GIL inside the loop. In order to do so, you
would have to have access to the saved PyGILState_STATE which you

> Or should one always use
> NaN even if the input does not make any sense (like, herhm, passing
> anything but integers or half-integers to a Wigner 3j symbol).

You should use a NaN and ideally set the fpstatus to INVALID (creating
the NaN may or may not do this; you will have to experiment). This
will allow people to handle the issue as they wish using
numpy.seterr(). An exception for just one value out of thousands is
often undesirable.

> I know how I'd to it manually in a wrapper w/ passed in context if not,
> but wanted to see.
> Also, will the arguments always be named x1, x2, x3, ..., or can I
> somehow give them custom names?

The only place where names appear is in the docstring. Write whatever
text you like.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list