numpy.seterrcall(func)
Set the floating-point error callback function or log object.
There are two ways to capture floating-point error messages. The first is to set the error-handler to ‘call’, using
. Then, set the function to call using this function.
The second is to set the error-handler to ‘log’, using
. Floating-point errors then trigger a call to the ‘write’ method of the provided object.
Parameters:funccallable f(err, flag) or object with write methodFunction to call upon floating-point errors (‘call’-mode) or object whose ‘write’ method is used to log such message (‘log’-mode).
The call function takes two arguments. The first is a string describing the type of error (such as “divide by zero”, “overflow”, “underflow”, or “invalid value”), and the second is the status flag. The flag is a byte, whose four least-significant bits indicate the type of error, one of “divide”, “over”, “under”, “invalid”:
[0000divideoverunderinvalid]In other words, flags=divide+2*over+4*under+8*invalid.
If an object is provided, its write method should take one argument, a string.
Returns:hcallable, log instance or NoneThe old error handler.
Notes
Concurrency note: see
Examples
Callback upon error:
>>> deferr_handler(type,flag):... print("Floating point error (%s), with flag %s"%(type,flag))...>>> importnumpyasnp>>> orig_handler=np.seterrcall(err_handler)>>> orig_err=np.seterr(all='call')>>> np.array([1,2,3])/0.0Floating point error (divide by zero), with flag 1array([inf, inf, inf])>>> np.seterrcall(orig_handler)<function err_handler at 0x...>>>> np.seterr(**orig_err){'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'}Log error message:
>>> classLog:... defwrite(self,msg):... print("LOG: %s"%msg)...>>> log=Log()>>> saved_handler=np.seterrcall(log)>>> save_err=np.seterr(all='log')>>> np.array([1,2,3])/0.0LOG: Warning: divide by zero encountered in dividearray([inf, inf, inf])>>> np.seterrcall(orig_handler)<numpy.Log object at 0x...>>>> np.seterr(**orig_err){'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'}