a
    /§»a0(  ã                   @   sr  d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ edƒZeed dœdd„ƒZeddœeeeee f  ddœdd„ƒZeedef eeedœd d„ƒZd,eedef  eeedef dœd!d„Zeddœeeee eee df f  eeeee f  d"d#œd$d%„ƒZeeeee eee df f  edef eeed&œd'd%„ƒZddœeeee eee df f  eeeeee f  eed"ef d(œd)d%„ZG d*d„ dej ƒZ!eG d+d"„ d"e!ƒƒZ"dS )-z/Record warnings during test function execution.é    N)ÚTracebackType)ÚAny)ÚCallable)Ú	Generator)ÚIterator)ÚList)ÚOptional)Úoverload)ÚPattern)ÚTuple)ÚType)ÚTypeVar)ÚUnion)Úfinal)Úcheck_ispytest)Úfixture)ÚfailÚT)ÚWarningsRecorderNN©Úreturnc                  c   sB   t dd�} | �  t d¡ | V  W d  ƒ n1 s40    Y  dS )zÆReturn a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.

    See http://docs.python.org/library/warnings.html for information
    on warning categories.
    T©Ú	_ispytestÚdefaultN)r   ÚwarningsÚsimplefilter)Zwrec© r   úI/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/_pytest/recwarn.pyÚrecwarn   s    

r   .©Úmatchr   )r    r   c                 C   s   d S ©Nr   r   r   r   r   Údeprecated_call(   s    r"   )ÚfuncÚargsÚkwargsr   c                 O   s   d S r!   r   )r#   r$   r%   r   r   r   r"   /   s    c                 O   s0   d}| dur| f| }t ttfg|¢R i |¤ŽS )a�  Assert that code produces a ``DeprecationWarning`` or ``PendingDeprecationWarning``.

    This function can be used as a context manager::

        >>> import warnings
        >>> def api_call_v2():
        ...     warnings.warn('use v3 of this api', DeprecationWarning)
        ...     return 200

        >>> import pytest
        >>> with pytest.deprecated_call():
        ...    assert api_call_v2() == 200

    It can also be used by passing a function and ``*args`` and ``**kwargs``,
    in which case it will ensure calling ``func(*args, **kwargs)`` produces one of
    the warnings types above. The return value is the return value of the function.

    In the context manager form you may use the keyword argument ``match`` to assert
    that the warning matches a text or regex.

    The context manager produces a list of :class:`warnings.WarningMessage` objects,
    one for each warning raised.
    TN)ÚwarnsÚDeprecationWarningÚPendingDeprecationWarning)r#   r$   r%   Ú__tracebackhide__r   r   r   r"   4   s    
ÚWarningsChecker)Úexpected_warningr    r   c                C   s   d S r!   r   )r+   r    r   r   r   r&   T   s    r&   )r+   r#   r$   r%   r   c                 O   s   d S r!   r   )r+   r#   r$   r%   r   r   r   r&   ]   s    )r+   r$   r    r%   r   c                O   sª   d}|s@|r2d}|d  t|ƒ¡7 }|d7 }t|ƒ‚t| |dd�S |d }t|ƒsdtd |t|ƒ¡ƒ‚t| dd��& ||d	d
… i |¤ŽW  d
  ƒ S 1 sœ0    Y  d
S )a  Assert that code raises a particular class of warning.

    Specifically, the parameter ``expected_warning`` can be a warning class or
    sequence of warning classes, and the inside the ``with`` block must issue a warning of that class or
    classes.

    This helper produces a list of :class:`warnings.WarningMessage` objects,
    one for each warning raised.

    This function can be used as a context manager, or any of the other ways
    :func:`pytest.raises` can be used::

        >>> import pytest
        >>> with pytest.warns(RuntimeWarning):
        ...    warnings.warn("my warning", RuntimeWarning)

    In the context manager form you may use the keyword argument ``match`` to assert
    that the warning matches a text or regex::

        >>> with pytest.warns(UserWarning, match='must be 0 or None'):
        ...     warnings.warn("value must be 0 or None", UserWarning)

        >>> with pytest.warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("value must be 42", UserWarning)

        >>> with pytest.warns(UserWarning, match=r'must be \d+$'):
        ...     warnings.warn("this is not here", UserWarning)
        Traceback (most recent call last):
          ...
        Failed: DID NOT WARN. No warnings of type ...UserWarning... was emitted...

    Tz5Unexpected keyword arguments passed to pytest.warns: z, z"
Use context-manager form instead?)Ú
match_exprr   r   z'{!r} object (type: {}) must be callabler   é   N)ÚjoinÚsortedÚ	TypeErrorr*   ÚcallableÚformatÚtype)r+   r    r$   r%   r)   Úmsgr#   r   r   r   r&   g   s    &ÿc                       sÔ   e Zd ZdZddœeddœ‡ fdd„Zeed d	œd
d„ƒZe	ddœdd„Z
ed d	œdd„Ze	d	œdd„Zefee ddœdd„Zdd	œdd„Zd d	œ‡ fdd„Zeee  ee ee ddœ‡ fdd„Z‡  ZS )r   z^A context manager to record raised warnings.

    Adapted from `warnings.catch_warnings`.
    Fr   N)r   r   c                   s&   t |ƒ tƒ jdd� d| _g | _d S )NT)ÚrecordF)r   ÚsuperÚ__init__Ú_enteredÚ_list)Úselfr   ©Ú	__class__r   r   r7   ¥   s    zWarningsRecorder.__init__zwarnings.WarningMessager   c                 C   s   | j S )zThe list of recorded warnings.©r9   ©r:   r   r   r   Úlist¬   s    zWarningsRecorder.list)Úir   c                 C   s
   | j | S )z Get a recorded warning by index.r=   )r:   r@   r   r   r   Ú__getitem__±   s    zWarningsRecorder.__getitem__c                 C   s
   t | jƒS )z&Iterate through the recorded warnings.)Úiterr9   r>   r   r   r   Ú__iter__µ   s    zWarningsRecorder.__iter__c                 C   s
   t | jƒS )z The number of recorded warnings.)Úlenr9   r>   r   r   r   Ú__len__¹   s    zWarningsRecorder.__len__)Úclsr   c                 C   sD   t | jƒD ]$\}}t|j|ƒr
| j |¡  S q
d}td| ƒ‚dS )z>Pop the first recorded warning, raise exception if not exists.Tz%r not found in warning listN)Ú	enumerater9   Ú
issubclassÚcategoryÚpopÚAssertionError)r:   rF   r@   Úwr)   r   r   r   rJ   ½   s
    zWarningsRecorder.popc                 C   s   g | j dd…< dS )z$Clear the list of recorded warnings.Nr=   r>   r   r   r   ÚclearÅ   s    zWarningsRecorder.clearc                    s@   | j rd}td|  ƒ‚tƒ  ¡ }|d us,J ‚|| _t d¡ | S )NTzCannot enter %r twiceÚalways)r8   ÚRuntimeErrorr6   Ú	__enter__r9   r   r   )r:   r)   r9   r;   r   r   rP   Ë   s    

zWarningsRecorder.__enter__©Úexc_typeÚexc_valÚexc_tbr   c                    s0   | j sd}td|  ƒ‚tƒ  |||¡ d| _ d S )NTz%Cannot exit %r without entering firstF)r8   rO   r6   Ú__exit__)r:   rR   rS   rT   r)   r;   r   r   rU   Ö   s
    zWarningsRecorder.__exit__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úboolr7   Úpropertyr   r?   ÚintrA   r   rC   rE   ÚWarningr   rJ   rM   rP   r   ÚBaseExceptionr   rU   Ú__classcell__r   r   r;   r   r   Ÿ   s   
ûc                       s„   e Zd Zdddœeeee eee df f  eeee	e f  e
ddœ‡ fdd„Zeee  ee ee ddœ‡ fd	d
„Z‡  ZS )r*   NFr   .)r+   r,   r   r   c                   sŽ   t |ƒ tƒ jdd� d}|d u r(d }nVt|tƒr\|D ]}t|tƒs6t|t|ƒ ƒ‚q6|}n"t|tƒrn|f}nt|t|ƒ ƒ‚|| _	|| _
d S )NTr   z/exceptions must be derived from Warning, not %s)r   r6   r7   Ú
isinstanceÚtuplerH   r]   r0   r3   r+   r,   )r:   r+   r,   r   r4   Zexpected_warning_tupÚexcr;   r   r   r7   é   s    	


zWarningsChecker.__init__rQ   c                    sÐ   t ƒ  |||¡ d}|d u rÌ|d u rÌ|d u rÌˆ jd urÌt‡ fdd„ˆ D ƒƒsnd}td ˆ jdd„ ˆ D ƒ¡ƒ n^ˆ jd urÌˆ D ].}t|jˆ jƒr|t	 
ˆ j¡ t|jƒ¡r| qÌq|td ˆ jˆ jdd„ ˆ D ƒ¡ƒ d S )	NTc                 3   s   | ]}t |jˆ jƒV  qd S r!   )rH   rI   r+   )Ú.0Úrr>   r   r   Ú	<genexpr>  ó    z+WarningsChecker.__exit__.<locals>.<genexpr>zVDID NOT WARN. No warnings of type {} was emitted. The list of emitted warnings is: {}.c                 S   s   g | ]
}|j ‘qS r   ©Úmessage©rc   Zeachr   r   r   Ú
<listcomp>  rf   z,WarningsChecker.__exit__.<locals>.<listcomp>zfDID NOT WARN. No warnings of type {} matching ('{}') was emitted. The list of emitted warnings is: {}.c                 S   s   g | ]
}|j ‘qS r   rg   ri   r   r   r   rj   &  rf   )r6   rU   r+   Úanyr   r2   r,   rH   rI   ÚreÚcompileÚsearchÚstrrh   )r:   rR   rS   rT   r)   rd   r;   r>   r   rU     s.    
þÿ
ûÿzWarningsChecker.__exit__)NN)rV   rW   rX   r   r   r   r]   r   ro   r
   rZ   r7   r^   r   rU   r_   r   r   r;   r   r*   ç   s"     ûùÿø
û)N)#rY   rl   r   Útypesr   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r   Z_pytest.compatr   Z_pytest.deprecatedr   Z_pytest.fixturesr   Z_pytest.outcomesr   r   r   ro   r"   r]   r&   Úcatch_warningsr   r*   r   r   r   r   Ú<module>   sx   ÿþ ÿ
þ ýü
ûý
û8H