o
    hk                     @   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 } |  }|r| |D ]	}i |d jd< q~ttj D ]\}}z|j	  W q" t
y6   Y q"w tjdd}td |V  W d   dS 1 sQw   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)inspectcurrentframegetouterframesf_localslistsysmodulesitemsr   clearAttributeErrorwarningscatch_warningssimplefilter)r   framefmod_namemodw r   T/var/www/html/scripts/venv/lib/python3.10/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r#d}n| dkr,d}ntt|}t	 p}|V  d| v rF| 
d d| v s=d	d
 | D }|D ]-}d}| D ]}t|t|jdurmd}||v rm|
| qU|r||s|tdt|j qO|rt|dkrd}d| || }t|W d   dS W d   dS 1 sw   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.NSKIMAGE_TEST_STRICT_WARNINGS1trueTfalseFc                 S   s   g | ]}d | dv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)	matchingstrict_warningsr   	remainingr   foundmatchnewlinemsgr   r   r   r   I   sJ   
%

"r   )
contextlibr   r   r   r3   	functoolsr,   __all__partialr   r   r   r   r   r   r   <module>   s    
8