U
    ?hk  ã                   @   sj   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dddg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
k
rl   Y qBX qBtjdd}t d¡ |V  W 5 Q R X 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)ÚinspectÚcurrentframeÚgetouterframesÚf_localsÚlistÚsysÚmodulesÚitemsr   ÚclearÚAttributeErrorÚwarningsÚcatch_warningsÚsimplefilter)r   ÚframeÚfÚmod_nameÚmodÚw© r   úK/var/www/html/venv/lib/python3.8/site-packages/skimage/_shared/_warnings.pyr      s    

c           	   	   c   s4  t | tƒrtdƒ‚| dkr$dV  dS tj dd¡}| ¡ dkrDd}n| ¡ dkrVd}ntt|ƒƒ}t	ƒ Â}|V  d| kr„|  
d¡ qpd	d
„ | D ƒ}|D ]Z}d}| D ]0}t |t|jƒ¡dk	r¢d}||kr¢| 
|¡ q¢|r–|s–tdt|jƒ› ƒ‚q–|r&t|ƒdkr&d}d|› | |¡› }t|ƒ‚W 5 Q R X 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ÚtrueTÚfalseFc                 S   s   g | ]}d |  d¡kr|‘qS )z\A\Zú|)Úsplit)Ú.0Úmr   r   r   Ú
<listcomp>†   s      z%expected_warnings.<locals>.<listcomp>zUnexpected warning: r   Ú
zNo warning raised matching:)Ú
isinstanceÚstrÚ
ValueErrorÚosÚenvironÚgetÚlowerÚboolÚintr   ÚremoveÚreÚsearchÚmessageÚlenÚjoin)	ZmatchingZstrict_warningsr   Ú	remainingr   ÚfoundÚmatchÚnewlineÚmsgr   r   r   r   I   s:    %
)Ú
contextlibr   r   r   r2   Ú	functoolsr+   Ú__all__Úpartialr   r   r   r   r   r   r   Ú<module>   s   

8