B
    Wc                 @   s  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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rd dlmZ eddddZeedee ed dddZejedddZejdddeed dddZejdddeed dddZejdd eed d!d"d#Zejdd eed dd$d%Zejdd d&ed d'd(d)ZdS )*    N)contextmanager)	Generator)Optional)TYPE_CHECKING)apply_warning_filters)Config)parse_warning_filter)Session)Item)TerminalReporter)Literal)configreturnc             C   s   |  dd d S )NZmarkerszfilterwarnings(warning): add a warning filter to the given test. see https://docs.pytest.org/en/stable/how-to/capture-warnings.html#pytest-mark-filterwarnings )Zaddinivalue_line)r    r   /lib/python3.7/site-packages/_pytest/warnings.pypytest_configure   s    r   z'Literal['config', 'collect', 'runtest'])NNN)r   whenitemr   c          	   c   s   |  d}| jjpg }tjdd}|dk	s0ttjsRtjdt	d tjdt
d t|| |dkrhdn|j}|dk	rx6|jddD ]&}x |jD ]}	tjt|	d	d
  qW qW dV  x&|D ]}
|jjt|
||ddd qW W dQ R X dS )zContext 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_recorded`` hook.
    filterwarningsT)recordNalways)category )nameF)escape)warning_messagenodeidr   location)kwargs)ZgetiniZknown_args_namespaceZpythonwarningswarningscatch_warningsAssertionErrorsyswarnoptionsr   DeprecationWarningPendingDeprecationWarningr   r   Ziter_markersargsr   Zpytest_warning_recordedZcall_historicdict)r   ihookr   r   Zconfig_filtersZcmdline_filterslogr   Zmarkargr   r   r   r   catch_warnings_for_item   s*    


r+   )r   r   c             C   s   | j }tt|| j| j| j| j}| jdk	ryddl	}W n t
k
rN   Y nPX || j}|dk	rd| }|d| 7 }nd}|d7 }|d| d7 }|S )	z.Convert a warnings.WarningMessage to a string.Nr   
z
Object allocated at:
zPhttps://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warningszDEnable tracemalloc to get traceback where the object was allocated.
zSee z for more info.)messager   formatwarningstrr   filenamelinenolinesourcetracemallocImportErrorget_object_tracebackjoinformat)r   Zwarn_msgmsgr4   tbZformatted_tbZurlr   r   r   warning_record_to_strJ   s(    
r;   T)hookwrapperZtryfirst)r   r   c          	   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_protocolh   s    r=   )sessionr   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)r>   r   r   r   r   pytest_collectionp   s    r@   )r<   )terminalreporterr   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+   r?   )rA   r   r   r   r   pytest_terminal_summaryy   s    rB   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+   r?   )r>   r   r   r   r   pytest_sessionfinish   s    rC   r   )early_configr   c          	   c   s(   t | | jdd d d V  W d Q R X d S )Nr   )r   r(   r   r   )r+   r?   )rD   r   r   r   pytest_load_initial_conftests   s    rE   ) r"   r   
contextlibr   typingr   r   r   ZpytestZ_pytest.configr   r   r   Z_pytest.mainr	   Z_pytest.nodesr
   Z_pytest.terminalr   Ztyping_extensionsr   r   r+   WarningMessager/   r;   Zhookimplr=   r@   rB   rC   rE   r   r   r   r   <module>   sB   (


