B
    `0(                 @   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   s.   t dd} |  td | V  W dQ R X dS )zReturn 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   W/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/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 )Nr   )r   r    r!   r   r   r   r   /   s    c             O   s*   d}| dk	r| f| }t ttff||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 )Nr   )r'   r   r   r   r   r"   T   s    r"   )r'   r   r    r!   r   c             O   s   d S )Nr   )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
 |S Q R X 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 callable)r      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`.
    F)r   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   r3      s    zWarningsRecorder.__init__zwarnings.WarningMessage)r   c             C   s   | j S )zThe list of recorded warnings.)r5   )r6   r   r   r   list   s    zWarningsRecorder.list)ir   c             C   s
   | j | S )z Get a recorded warning by index.)r5   )r6   r9   r   r   r   __getitem__   s    zWarningsRecorder.__getitem__c             C   s
   t | jS )z&Iterate through the recorded warnings.)iterr5   )r6   r   r   r   __iter__   s    zWarningsRecorder.__iter__c             C   s
   t | jS )z The number of recorded warnings.)lenr5   )r6   r   r   r   __len__   s    zWarningsRecorder.__len__)clsr   c             C   sD   x.t | jD ] \}}t|j|r| j|S qW d}td| dS )z>Pop the first recorded warning, raise exception if not exists.Tz%r not found in warning listN)	enumerater5   
issubclasscategorypopAssertionError)r6   r?   r9   wr%   r   r   r   rC      s
    zWarningsRecorder.popc             C   s   g | j dd< dS )z$Clear the list of recorded warnings.N)r5   )r6   r   r   r   clear   s    zWarningsRecorder.clearc                s@   | j rd}td|  t  }|d k	s,t|| _td | S )NTzCannot enter %r twicealways)r4   RuntimeErrorr2   	__enter__rD   r5   r   r   )r6   r%   r5   )r7   r   r   rI      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)r4   rH   r2   __exit__)r6   rJ   rK   rL   r%   )r7   r   r   rM      s
    zWarningsRecorder.__exit__)__name__
__module____qualname____doc__boolr3   propertyr   r8   intr:   r   r<   r>   Warningr   rC   rF   rI   r   BaseExceptionr   rM   __classcell__r   r   )r7   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&   NF)r   .)r'   r(   r   r   c               s   t | t jdd d}|d kr(d }nZt|tr`x&|D ]}t|ts8t|t| q8W |}n"t|trr|f}nt|t| || _	|| _
d S )NT)r   z/exceptions must be derived from Warning, not %s)r   r2   r3   
isinstancetuplerA   rU   r,   r/   r'   r(   )r6   r'   r(   r   r0   Zexpected_warning_tupexc)r7   r   r   r3      s    	



zWarningsChecker.__init__)rJ   rK   rL   r   c                s   t  ||| d}|d kr|d kr|d krΈ jd k	rt fdd D snd}td jdd  D  n` jd k	rxT D ],}t|j jr~t	
 jt|jr~P q~W td j jdd  D  d S )	NTc             3   s   | ]}t |j jV  qd S )N)rA   rB   r'   ).0r)r6   r   r   	<genexpr>  s    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)r[   eachr   r   r   
<listcomp>  s    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   )r^   )r[   r_   r   r   r   r`   &  s    )r2   rM   r'   anyr   r.   r(   rA   rB   recompilesearchstrr^   )r6   rJ   rK   rL   r%   r\   )r7   )r6   r   rM     s&    


zWarningsChecker.__exit__)NN)rN   rO   rP   r   r   r   rU   r   re   r
   rR   r3   rV   r   rM   rW   r   r   )r7   r   r&      s    8
)N)#rQ   rb   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   re   r   rU   r"   catch_warningsr   r&   r   r   r   r   <module>   sX   6
83H