SciPy

scipy.special.errstate

class scipy.special.errstate

Context manager for special-function error handling.

Using an instance of errstate as a context manager allows statements in that context to execute with a known error handling behavior. Upon entering the context the error handling is set with seterr, and upon exiting it is restored to what it was before.

Parameters:

kwargs : {all, singular, underflow, overflow, slow, loss, no_result, domain, arg, other}

Keyword arguments. The valid keywords are possible special-function errors. Each keyword should have a string value that defines the treatement for the particular type of error. Values must be ‘ignore’, ‘warn’, or ‘other’. See seterr for details.

See also

geterr
get the current way of handling special-function errors
seterr
set how special-function errors are handled
numpy.errstate
similar numpy function for floating-point errors

Examples

>>> import scipy.special as sc
>>> from numpy.testing import assert_raises
>>> sc.gammaln(0)
inf
>>> with sc.errstate(singular='raise'):
...     with assert_raises(sc.SpecialFunctionError):
...         sc.gammaln(0)
...
>>> sc.gammaln(0)
inf

We can also raise on every category except one.

>>> with sc.errstate(all='raise', singular='ignore'):
...     sc.gammaln(0)
...     with assert_raises(sc.SpecialFunctionError):
...         sc.spence(-1)
...
inf