a
    ¤Â=bJ  ã                   @   sh   d dl mZ d dlZd dlZd dlZd dlZd dlZg d¢Zejej	ddZ	edd„ ƒZ
edd	„ ƒZdS )
é    )ÚcontextmanagerN)Úall_warningsÚexpected_warningsÚwarné   )Ú
stacklevelc               	   c   s®   ddl } |  ¡ }|r2|  |¡D ]}i |d jd< q~ttj ¡ ƒD ]*\}}z|j 	¡  W qB t
yj   Y qB0 qBtjdd }t d¡ |V  W d  ƒ n1 s 0    Y  dS )a&  
    Context for use in testing to ensure that all warnings are raised.

    Examples
    --------
    >>> import warnings
    >>> def foo():
    ...     warnings.warn(RuntimeWarning("bar"), stacklevel=2)

    We raise the warning once, while the warning filter is set to "once".
    Hereafter, the warning is invisible, even with custom filters:

    >>> with warnings.catch_warnings():
    ...     warnings.simplefilter('once')
    ...     foo()                         # doctest: +SKIP

    We can now run ``foo()`` without a warning being raised:

    >>> from numpy.testing import assert_warns
    >>> foo()                             # doctest: +SKIP

    To catch the warning, we call in the help of ``all_warnings``:

    >>> with all_warnings():
    ...     assert_warns(RuntimeWarning, foo)
    r   NÚ__warningregistry__T)ÚrecordÚalways)ÚinspectZcurrentframeZgetouterframesÚf_localsÚlistÚsysÚmodulesÚitemsr   ÚclearÚAttributeErrorÚwarningsÚcatch_warningsÚsimplefilter)r   ÚframeÚfÚmod_nameÚmodÚw© r   ú8lib/python3.9/site-packages/skimage/_shared/_warnings.pyr      s    

r   c                 c   s>  t | tƒrtdƒ‚| du r$dV  dS tj dd¡}| ¡ dkrDd}n| ¡ dkrVd}ntt|ƒƒ}t	ƒ À}|V  d| v r„|  
d¡ qpd	d
„ | D ƒ}|D ]X}d}| D ]0}t |t|jƒ¡dur¢d}||v r¢| 
|¡ q¢|r–|s–tdt|jƒ ƒ‚q–|rt|ƒdkrdd |¡ }t|ƒ‚W d  ƒ n1 s00    Y  dS )a‹  Context for use in testing to catch known warnings matching regexes

    Parameters
    ----------
    matching : None or a list of strings or compiled regexes
        Regexes for the desired warning to catch
        If matching is None, this behaves as a no-op.

    Examples
    --------
    >>> import numpy as np
    >>> rng = np.random.default_rng()
    >>> image = rng.integers(0, 2**16, size=(100, 100), dtype=np.uint16)
    >>> # rank filters are slow when bit-depth exceeds 10 bits
    >>> from skimage import filters
    >>> with expected_warnings(['Bad rank filter performance']):
    ...     median_filtered = filters.rank.median(image)

    Notes
    -----
    Uses `all_warnings` to ensure all warnings are raised.
    Upon exiting, it checks the recorded warnings for the desired matching
    pattern(s).
    Raises a ValueError if any match was not found or an unexpected
    warning was raised.
    Allows for three types of behaviors: `and`, `or`, and `optional` matches.
    This is done to accommodate different build environments or loop conditions
    that may produce different warnings.  The behaviors can be combined.
    If you pass multiple patterns, you get an orderless `and`, where all of the
    warnings must be raised.
    If you use the `|` operator in a pattern, you can catch one of several
    warnings.
    Finally, you can use `|\A\Z` in a pattern to signify it as optional.

    zA``matching`` should be a list of strings and not a string itself.NZSKIMAGE_TEST_STRICT_WARNINGSÚ1ÚtrueTZfalseFc                 S   s   g | ]}d |  d¡vr|‘qS )z\A\Zú|)Úsplit)Ú.0Úmr   r   r   Ú
<listcomp>†   ó    z%expected_warnings.<locals>.<listcomp>zUnexpected warning: %sr   zNo warning raised matching:
%sÚ
)Ú
isinstanceÚstrÚ
ValueErrorÚosÚenvironÚgetÚlowerÚboolÚintr   ÚremoveÚreÚsearchÚmessageÚlenÚjoin)ZmatchingZstrict_warningsr   Z	remainingr   ÚfoundÚmatchÚmsgr   r   r   r   I   s8    %
r   )Ú
contextlibr   r   r   r0   Ú	functoolsr)   Ú__all__Úpartialr   r   r   r   r   r   r   Ú<module>   s   
8