errstate(**kwargs) Context manager for floating-point 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 `
| 366 | |
| 367 | @set_module('numpy') |
| 368 | class errstate(contextlib.ContextDecorator): |
| 369 | """ |
| 370 | errstate(**kwargs) |
| 371 | |
| 372 | Context manager for floating-point error handling. |
| 373 | |
| 374 | Using an instance of `errstate` as a context manager allows statements in |
| 375 | that context to execute with a known error handling behavior. Upon entering |
| 376 | the context the error handling is set with `seterr` and `seterrcall`, and |
| 377 | upon exiting it is reset to what it was before. |
| 378 | |
| 379 | .. versionchanged:: 1.17.0 |
| 380 | `errstate` is also usable as a function decorator, saving |
| 381 | a level of indentation if an entire function is wrapped. |
| 382 | See :py:class:`contextlib.ContextDecorator` for more information. |
| 383 | |
| 384 | Parameters |
| 385 | ---------- |
| 386 | kwargs : {divide, over, under, invalid} |
| 387 | Keyword arguments. The valid keywords are the possible floating-point |
| 388 | exceptions. Each keyword should have a string value that defines the |
| 389 | treatment for the particular error. Possible values are |
| 390 | {'ignore', 'warn', 'raise', 'call', 'print', 'log'}. |
| 391 | |
| 392 | See Also |
| 393 | -------- |
| 394 | seterr, geterr, seterrcall, geterrcall |
| 395 | |
| 396 | Notes |
| 397 | ----- |
| 398 | For complete documentation of the types of floating-point exceptions and |
| 399 | treatment options, see `seterr`. |
| 400 | |
| 401 | Examples |
| 402 | -------- |
| 403 | >>> olderr = np.seterr(all='ignore') # Set error handling to known state. |
| 404 | |
| 405 | >>> np.arange(3) / 0. |
| 406 | array([nan, inf, inf]) |
| 407 | >>> with np.errstate(divide='warn'): |
| 408 | ... np.arange(3) / 0. |
| 409 | array([nan, inf, inf]) |
| 410 | |
| 411 | >>> np.sqrt(-1) |
| 412 | nan |
| 413 | >>> with np.errstate(invalid='raise'): |
| 414 | ... np.sqrt(-1) |
| 415 | Traceback (most recent call last): |
| 416 | File "<stdin>", line 2, in <module> |
| 417 | FloatingPointError: invalid value encountered in sqrt |
| 418 | |
| 419 | Outside the context the error handling behavior has not changed: |
| 420 | |
| 421 | >>> np.geterr() |
| 422 | {'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'} |
| 423 | |
| 424 | """ |
| 425 |
no outgoing calls