B
    ܇\f                 @   s   d dl mZ d dl mZ d dl mZ d dlZd dlZd dlmZ d dlZd dl	m
Z
 dZdd	 Zd
d Zdd Zedd Zdd Zejddddd Zejddddd Zejdddd Zdd ZdS )    )absolute_import)division)print_functionN)contextmanager)compatz(-Walways::pytest.RemovedInPytest4Warningc          	   C   s   | d}t|dkr&| d|f xt|dk r@|d q(W dd |D \}}}}}| |}| |}|ryt|}|dk rtW q ttfk
r   | d|f Y qX nd}| 	||||| d	S )
zP
    Copy of the warning._setoption function but does not escape arguments.
    :   ztoo many fields (max 5): %r c             S   s   g | ]}|  qS  )strip).0sr
   r
   /lib/python3.7/site-packages/_pytest/warnings.py
<listcomp>   s    z_setoption.<locals>.<listcomp>r   zinvalid lineno %rN)
splitlen_OptionErrorappend
_getaction_getcategoryint
ValueErrorOverflowErrorfilterwarnings)Zwmodargpartsactionmessagecategorymodulelinenor
   r
   r   
_setoption   s"    


r!   c             C   s0   |  d}|jddddd | jddd	d
 d S )Nzpytest-warningsz-Wz--pythonwarningsr   z=set which warnings to report, see -W option of python itself.)r   helpr   ZlinelistzcEach line specifies a pattern for warnings.filterwarnings. Processed after -W and --pythonwarnings.)typer"   )ZgetgroupZ	addoptionZaddini)parsergroupr
   r
   r   pytest_addoption'   s    
r&   c             C   s   |  dd d S )NZmarkerszfilterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/latest/warnings.html#pytest-mark-filterwarnings )Zaddinivalue_line)configr
   r
   r   pytest_configure8   s    r(   c       
   	   c   s   |  dpg }| d}tjdd}tjsHtjdtd tjdtd tjdt	j
d x|D ]}tt| q^W x|D ]}t| qvW |dk	rx.|jdd	D ]}x|jD ]}tt| qW qW dV  x$|D ]}	|jjt|	||d
d qW W dQ R X dS )z
    Context manager that catches warnings generated in the contained execution block.

    ``item`` can be None if we are not in the context of an item execution.

    Each warning captured triggers the ``pytest_warning_captured`` hook.
    Zpythonwarningsr   T)recordalways)r   errorN)name)warning_messagewhenitem)kwargs)Z	getoptionZgetiniwarningscatch_warningssyswarnoptionsr   DeprecationWarningPendingDeprecationWarningpytestZRemovedInPytest4Warningr!   Ziter_markersargspytest_warning_capturedcall_historicdict)
r'   ihookr.   r/   Zcmdline_filtersZ
inifilterslogr   Zmarkr-   r
   r
   r   catch_warnings_for_item@   s&    	



r>   c             C   s   | j }d}tjrltdd |jD rlg }x.|jD ]$}|t|tjrNt|n| q0W t	|j|k}||_t
|| j| j| j| j}|rt
dt|t |S )zConvert a warnings.WarningMessage to a string, taking in account a lot of unicode shenaningans in Python 2.

    When Python 2 support is dropped this function can be greatly simplified.
    Fc             s   s   | ]}t |tjV  qd S )N)
isinstancer   UNICODE_TYPES)r   mr
   r
   r   	<genexpr>p   s    z(warning_record_to_str.<locals>.<genexpr>z^Warning is using unicode non convertible to ascii, converting to a safe representation:
  {!r})r   r   Z_PY2anyr8   r   r?   r@   Zascii_escapedlistr1   formatwarningr   filenamer    linewarnformatZsafe_strUnicodeWarning)r-   Zwarn_msgZunicode_warningZnew_argsrA   msgr
   r
   r   warning_record_to_stri   s*     
rL   T)hookwrapperZtryfirstc          	   c   s*   t | j| jd| d d V  W d Q R X d S )NZruntest)r'   r<   r.   r/   )r>   r'   r<   )r/   r
   r
   r   pytest_runtest_protocol   s    rN   c          	   c   s.   | j }t||jdd d d V  W d Q R X d S )NZcollect)r'   r<   r.   r/   )r'   r>   hook)Zsessionr'   r
   r
   r   pytest_collection   s    rP   )rM   c          	   c   s.   | j }t||jdd d d V  W d Q R X d S )Nr'   )r'   r<   r.   r/   )r'   r>   rO   )Zterminalreporterr'   r
   r
   r   pytest_terminal_summary   s    rQ   c          	   C   sV   t jdd$}t dt|  t j| |d W dQ R X |jjt|d dddd	 dS )
a  
    This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage:
    at this point the actual options might not have been set, so we manually trigger the pytest_warning_captured
    hook so we can display this warnings in the terminal. This is a hack until we can sort out #2891.

    :param warning: the warning instance.
    :param hook: the hook caller
    :param stacklevel: stacklevel forwarded to warnings.warn
    T)r)   r*   )
stacklevelNr   r'   )r-   r.   r/   )r0   )r1   r2   simplefilterr#   rH   r9   r:   r;   )ZwarningrO   rR   Zrecordsr
   r
   r   _issue_warning_captured   s
    
rT   )Z
__future__r   r   r   r3   r1   
contextlibr   r7   Z_pytestr   ZSHOW_PYTEST_WARNINGS_ARGr!   r&   r(   r>   rL   ZhookimplrN   rP   rQ   rT   r
   r
   r
   r   <module>   s"   ) 		