a :jg:@sdZddlZddlZddlZddlZddlmZddlm Z m Z m Z gdZ eddd d Z edd d Zedd dZedddZedddZedddZGdddZeZedGdddZejdddZedejddZdS)z Functions for changing global ufunc configuration This provides helpers which wrap `_get_extobj_dict` and `_make_extobj`, and `_extobj_contextvar` from umath. N) set_module) _make_extobj_get_extobj_dict_extobj_contextvar)seterrgeterr setbufsize getbufsize seterrcall geterrcallerrstate_no_nep50_warningnumpycCs>t}|dd|ddt|||||d}t||S)a Set how floating-point errors are handled. Note that operations on integer scalar types (such as `int16`) are handled like floating point, and are affected by these settings. Parameters ---------- all : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional Set treatment for all types of floating-point errors at once: - ignore: Take no action when the exception occurs. - warn: Print a :exc:`RuntimeWarning` (via the Python `warnings` module). - raise: Raise a :exc:`FloatingPointError`. - call: Call a function specified using the `seterrcall` function. - print: Print a warning directly to ``stdout``. - log: Record error in a Log object specified by `seterrcall`. The default is not to change the current behavior. divide : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional Treatment for division by zero. over : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional Treatment for floating-point overflow. under : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional Treatment for floating-point underflow. invalid : {'ignore', 'warn', 'raise', 'call', 'print', 'log'}, optional Treatment for invalid floating-point operation. Returns ------- old_settings : dict Dictionary containing the old settings. See also -------- seterrcall : Set a callback function for the 'call' mode. geterr, geterrcall, errstate Notes ----- The floating-point exceptions are defined in the IEEE 754 standard [1]_: - Division by zero: infinite result obtained from finite numbers. - Overflow: result too large to be expressed. - Underflow: result so close to zero that some precision was lost. - Invalid operation: result is not an expressible number, typically indicates that a NaN was produced. .. [1] https://en.wikipedia.org/wiki/IEEE_754 Examples -------- >>> orig_settings = np.seterr(all='ignore') # seterr to known value >>> np.int16(32000) * np.int16(3) 30464 >>> np.seterr(over='raise') {'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'} >>> old_settings = np.seterr(all='warn', over='raise') >>> np.int16(32000) * np.int16(3) Traceback (most recent call last): File "", line 1, in FloatingPointError: overflow encountered in scalar multiply >>> old_settings = np.seterr(all='print') >>> np.geterr() {'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'} >>> np.int16(32000) * np.int16(3) 30464 >>> np.seterr(**orig_settings) # restore original {'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'} callNbufsizealldivideoverunderinvalid)rpoprrset)rrrrroldextobjrC/usr/local/lib/python3.9/site-packages/numpy/_core/_ufunc_config.pyrsM    rcCs"t}|dd|dd|S)a Get the current way of handling floating-point errors. Returns ------- res : dict A dictionary with keys "divide", "over", "under", and "invalid", whose values are from the strings "ignore", "print", "log", "warn", "raise", and "call". The keys represent possible floating-point exceptions, and the values define how these exceptions are handled. See Also -------- geterrcall, seterr, seterrcall Notes ----- For complete documentation of the types of floating-point exceptions and treatment options, see `seterr`. Examples -------- >>> np.geterr() {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'} >>> np.arange(3.) / np.arange(3.) # doctest: +SKIP array([nan, 1., 1.]) RuntimeWarning: invalid value encountered in divide >>> oldsettings = np.seterr(all='warn', invalid='raise') >>> np.geterr() {'divide': 'warn', 'over': 'warn', 'under': 'warn', 'invalid': 'raise'} >>> np.arange(3.) / np.arange(3.) Traceback (most recent call last): ... FloatingPointError: invalid value encountered in divide >>> oldsettings = np.seterr(**oldsettings) # restore original rNr)rr)resrrrr ms(  r cCs"td}t|d}t||S)a0 Set the size of the buffer used in ufuncs. .. versionchanged:: 2.0 The scope of setting the buffer is tied to the `numpy.errstate` context. Exiting a ``with errstate():`` will also restore the bufsize. Parameters ---------- size : int Size of buffer. r)rrrrr)sizerrrrrr s   r cCs tdS)z Return the size of the buffer used in ufuncs. Returns ------- getbufsize : int Size of ufunc buffer in bytes. rrrrrrr s r cCs"td}t|d}t||S)a 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 `seterr`. Then, set the function to call using this function. The second is to set the error-handler to 'log', using `seterr`. Floating-point errors then trigger a call to the 'write' method of the provided object. Parameters ---------- func : callable f(err, flag) or object with write method Function 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":: [0 0 0 0 divide over under invalid] 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 ------- h : callable, log instance or None The old error handler. See Also -------- seterr, geterr, geterrcall Examples -------- Callback upon error: >>> def err_handler(type, flag): ... print("Floating point error (%s), with flag %s" % (type, flag)) ... >>> orig_handler = np.seterrcall(err_handler) >>> orig_err = np.seterr(all='call') >>> np.array([1, 2, 3]) / 0.0 Floating point error (divide by zero), with flag 1 array([inf, inf, inf]) >>> np.seterrcall(orig_handler) >>> np.seterr(**orig_err) {'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'} Log error message: >>> class Log: ... def write(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.0 LOG: Warning: divide by zero encountered in divide array([inf, inf, inf]) >>> np.seterrcall(orig_handler) >>> np.seterr(**orig_err) {'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'} r)rr )funcrrrrrr sR   r cCs tdS)ap Return the current callback function used on floating-point errors. When the error handling for a floating-point error (one of "divide", "over", "under", or "invalid") is set to 'call' or 'log', the function that is called or the log instance that is written to is returned by `geterrcall`. This function or log instance has been set with `seterrcall`. Returns ------- errobj : callable, log instance or None The current error handler. If no handler was set through `seterrcall`, ``None`` is returned. See Also -------- seterrcall, seterr, geterr Notes ----- For complete documentation of the types of floating-point exceptions and treatment options, see `seterr`. Examples -------- >>> np.geterrcall() # we did not yet set a handler, returns None >>> orig_settings = np.seterr(all='call') >>> def err_handler(type, flag): ... print("Floating point error (%s), with flag %s" % (type, flag)) >>> old_handler = np.seterrcall(err_handler) >>> np.array([1, 2, 3]) / 0.0 Floating point error (divide by zero), with flag 1 array([inf, inf, inf]) >>> cur_handler = np.geterrcall() >>> cur_handler is err_handler True >>> old_settings = np.seterr(**orig_settings) # restore original >>> old_handler = np.seterrcall(None) # restore original rr"rrrrr s-r c@s eZdZdS) _unspecifiedN)__name__ __module__ __qualname__rrrrr$Gsr$c@sDeZdZdZdZeddddddddZddZd d Zd d Z dS) ra 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 `seterrcall`, and upon exiting it is reset to what it was before. .. versionchanged:: 1.17.0 `errstate` is also usable as a function decorator, saving a level of indentation if an entire function is wrapped. .. versionchanged:: 2.0 `errstate` is now fully thread and asyncio safe, but may not be entered more than once. It is not safe to decorate async functions using ``errstate``. Parameters ---------- kwargs : {divide, over, under, invalid} Keyword arguments. The valid keywords are the possible floating-point exceptions. Each keyword should have a string value that defines the treatment for the particular error. Possible values are {'ignore', 'warn', 'raise', 'call', 'print', 'log'}. See Also -------- seterr, geterr, seterrcall, geterrcall Notes ----- For complete documentation of the types of floating-point exceptions and treatment options, see `seterr`. Examples -------- >>> olderr = np.seterr(all='ignore') # Set error handling to known state. >>> np.arange(3) / 0. array([nan, inf, inf]) >>> with np.errstate(divide='ignore'): ... np.arange(3) / 0. array([nan, inf, inf]) >>> np.sqrt(-1) np.float64(nan) >>> with np.errstate(invalid='raise'): ... np.sqrt(-1) Traceback (most recent call last): File "", line 2, in FloatingPointError: invalid value encountered in sqrt Outside the context the error handling behavior has not changed: >>> np.geterr() {'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'} >>> olderr = np.seterr(**olderr) # restore original state )_call_all_divide_over_under_invalid_tokenNrrrrrrcCs.d|_||_||_||_||_||_||_dSN)r.r(r)r*r+r,r-)selfrrrrrrrrr__init__szerrstate.__init__cCsj|jdurtd|jtur:t|j|j|j|j|j d}n t|j|j|j|j|j|j d}t ||_dS)Nz!Cannot enter `np.errstate` twice.rr/) r. TypeErrorr( _Unspecifiedrr)r*r+r,r-rr)r1rrrr __enter__s    zerrstate.__enter__cGst|jdSr0)rresetr.)r1exc_inforrr__exit__szerrstate.__exit__cstfdd}|S)Nc s~jtur(tjjjjjd}n tjjjjjjd}t |}z|i|Wt |St |0dS)Nrr/) r(r4rr)r*r+r,r-rrr6)argskwargsrr.r#r1rrinners      z errstate.__call__..inner) functoolswraps)r1r#r<rr;r__call__szerrstate.__call__) r%r&r'__doc__ __slots__r4r2r5r8r?rrrrrNs= rrF)defaultc cs0td}zdVWt|n t|0dS)z Context manager to disable NEP 50 warnings. This context manager is only relevant if the NEP 50 warnings are enabled globally (which is not thread/context safe). This warning context manager itself is fully safe, however. TN)NO_NEP50_WARNINGrr6)tokenrrrrs )NNNNN)r@collections.abc collections contextlibZ contextvarsr=Z_utilsrZumathrrr__all__rr r r r r r$r4rZ ContextVarrCcontextmanagerrrrrrs6  W .   W /y