a
    lc^                     @   sd   d dgZ ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 edd  Z
edd ZdS )all_warningsexpected_warnings    N)contextmanager)mockc               	   c   s   t  } | r*t | D ]}i |d jd< q~ ttj D ]*\}}z|j	  W q: t
yb   Y q:0 q:tjddT}tjtjddi  td |V  W d   n1 s0    Y  W d   n1 s0    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"))

    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()

    We can now run ``foo()`` without a warning being raised:
    >>> from numpy.testing import assert_warns  # doctest: +SKIP
    >>> foo()  # doctest: +SKIP

    To catch the warning, we call in the help of ``all_warnings``:
    >>> with all_warnings():  # doctest: +SKIP
    ...     assert_warns(RuntimeWarning, foo)
    r   __warningregistry__T)recordZTRAITLETS_ALL_DEPRECATIONS1alwaysN)inspectcurrentframegetouterframesf_localslistsysmodulesitemsr   clearAttributeErrorwarningscatch_warningsr   patchdictosenvironsimplefilter)framef_modw r    U/var/www/html/django/DPS/env/lib/python3.9/site-packages/traitlets/tests/_warnings.pyr      s    "

c                 c   s   t  }|V  dd | D }|D ]T}d}| D ]0}t|t|jdur,d}||v r,|| q,|s tdt|j q t|dkrdd	| }t|W d   n1 s0    Y  dS )
a  Context for use in testing to catch known warnings matching regexes

    Parameters
    ----------
    matching : list of strings or compiled regexes
        Regexes for the desired warning to catch

    Examples
    --------
    >>> from skimage import data, img_as_ubyte, img_as_float  # doctest: +SKIP
    >>> with expected_warnings(["precision loss"]):           # doctest: +SKIP
    ...     d = img_as_ubyte(img_as_float(data.coins()))      # doctest: +SKIP

    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 accomodate different build enviroments 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.
    c                 S   s   g | ]}d | dvr|qS )z\A\Z|)split).0mr    r    r!   
<listcomp>f       z%expected_warnings.<locals>.<listcomp>FNTzUnexpected warning: %sr   zNo warning raised matching:
%s
)	r   researchstrmessageremove
ValueErrorlenjoin)Zmatchingr   	remainingwarnfoundmatchmsgr    r    r!   r   D   s    )__all__r
   r   r)   r   r   
contextlibr   Zunittestr   r   r   r    r    r    r!   <module>   s   
4