a
    
3j5                     @   s  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Zd dlZd dl	Z	d dl
mZ d dlmZmZ d dlmZ d dlZejddgd	Zejddgd
ZeedZedd ZG dd deZG dd dZG dd deZG dd dee	jZG dd dee	jZ G dd deZ!G dd de!e	jZ"G dd de!e	jZ#G dd deZ$G d d! d!e$e	jZ%G d"d# d#e$e	jZ&G d$d% d%eZ'G d&d' d'e'e	jZ(G d(d) d)e'e	jZ)G d*d+ d+ee	jZ*G d,d- d-eZ+G d.d/ d/e+e	jZ,G d0d1 d1e+e	jZ-G d2d3 d3eZ.G d4d5 d5e.e	jZ/G d6d7 d7e.e	jZ0G d8d9 d9eZ1G d:d; d;e1e	jZ2G d<d= d=e1e	jZ3G d>d? d?e	jZ4G d@dA dAe	jZ5dBdC Z6e6Z7e8dDkre	9  dS )E    )contextmanagerN)StringIO)support)assert_python_okassert_python_failure
stacklevelwarnings	_warnings)Zblocked)ZfreshZgettotalrefcountc              	   c   s   t tfD ]&}z|j  W q ty,   Y q0 qzt  W n tyN   Y n0 tj}| j}z4|d d  | _| d | t_d V  W |t_|| _n|t_|| _0 d S )Nonce)	syswarning_tests__warningregistry__clearAttributeError	NameErrorr	   filterssimplefilter)moduleZto_clearoriginal_warningsZoriginal_filters r   W/www/server/python_manager/versions/3.9.10/lib/python3.9/test/test_warnings/__init__.pywarnings_state   s(    
r   c                   @   s   e Zd ZdS )TestWarningN__name__
__module____qualname__r   r   r   r   r   .   s   r   c                       s(   e Zd Z fddZ fddZ  ZS )BaseTestc                    s`   t jj| _dt v rt d= ttdr*t`ttdr8t`| j	tj
d< | j	t j_tt|   d S )Nr   r	   )unittestcaser	   old_unittest_moduleglobalshasattrr   r   r   r   modulessuperr   setUpself	__class__r   r   r&   6   s    




zBaseTest.setUpc                    s&   t tjd< | jtj_tt| 	  d S )Nr	   )
r   r   r$   r!   r   r    r	   r%   r   tearDownr'   r)   r   r   r+   I   s    

zBaseTest.tearDown)r   r   r   r&   r+   __classcell__r   r   r)   r   r   2   s   r   c                   @   s   e Zd Zdd ZdS )PublicAPITestsc                 C   s6   |  t| jd g d}| t| jjt| d S )N__all__)warnwarn_explicitshowwarningformatwarningfilterwarningsr   resetwarningscatch_warnings)
assertTruer#   r   ZassertSetEqualsetr.   )r(   Z
target_apir   r   r   test_module_all_attributeT   s
    z(PublicAPITests.test_module_all_attributeN)r   r   r   r8   r   r   r   r   r-   N   s   r-   c                   @   s   e Zd ZeZdS )CPublicAPITestsNr   r   r   
c_warningsr   r   r   r   r   r9   \   s   r9   c                   @   s   e Zd ZeZdS )PyPublicAPITestsNr   r   r   py_warningsr   r   r   r   r   r<   _   s   r<   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&FilterTestsc                 C   s^   t j| jd<}| j  | jjdtd | t| jjd W d    n1 sP0    Y  d S )Nr   errorcategoryFilterTests.test_error)r   r5   r   r4   r3   UserWarningassertRaisesr/   r(   wr   r   r   
test_errorf   s    
rD   c              	      s   t jjd}j  d  fdd}t }|  W d    n1 sP0    Y  | }d| d| jjdt	d 
t	| W d    n1 s0    Y  d S )	Nr@   %FilterTests.test_ignore_after_defaultc                      s   j  t d S Nr   r/   rE   r   messager(   r   r   fq   s    z/FilterTests.test_error_after_default.<locals>.fz2UserWarning: FilterTests.test_ignore_after_defaultz&self.module.warn(message, UserWarning)rA   rB   )r   r5   r   r4   r   captured_stderrgetvalueassertInr3   rE   rF   )r(   rH   rO   stderrr   rM   r   test_error_after_defaultm   s    

$z$FilterTests.test_error_after_defaultc                 C   s~   t jd| jdZ}| j  | jjdtd | jdt | t|d | t	t
dg W d    n1 sp0    Y  d S )NTrecordr   ignorerB   FilterTests.test_ignorer   version)r   r5   r   r4   r3   rE   r/   assertEquallenlistr   rG   r   r   r   test_ignore   s    
rX   c                    s   t jdjd^}j  d  fdd}|  jjdtd |  |  t|d W d    n1 st0    Y  d S )	NTrU   rJ   c                      s   j  t d S rK   rL   r   rM   r   r   rO      s    z0FilterTests.test_ignore_after_default.<locals>.frW   rB      )r   r5   r   r4   r3   rE   rZ   r[   r(   rH   rO   r   rM   r   test_ignore_after_default   s    
rJ   c                    s   t jdjd}j  jjdtd d  fdd}|  t|d |d	 jj	d
   |  t|d |d	 jj	d
   W d    n1 s0    Y  d S )NTrU   alwaysrB   FilterTests.test_alwaysc                      s   j  t d S rK   rL   r   rM   r   r   rO      s    z"FilterTests.test_always.<locals>.fr^   r      )
r   r5   r   r4   r3   rE   rZ   r[   rN   argsr_   r   rM   r   test_always   s    
rb   c                    s   t jdjd}j  d  fdd}|  t|d |d jjd   |  t|d jjd	t	d
 |  t|d |d jjd   |  t|d |d jjd   W d    n1 s0    Y  d S )NTrU   z$FilterTests.test_always_after_ignorec                      s   j  t d S rK   rL   r   rM   r   r   rO      s    z0FilterTests.test_always_after_default.<locals>.fr^   rc   r   ra   rB   rd      )
r   r5   r   r4   rZ   r[   rN   re   r3   rE   r_   r   rM   r   test_always_after_default   s&    
z%FilterTests.test_always_after_defaultc                 C   s   t jd| jd}| j  | jjdtd td}tdD ]Z}| j|t |dkrt| |d j	| |d d = q<|d	kr| t
|d q<td
q<W d    n1 s0    Y  d S )NTrU   defaultrB   FilterTests.test_defaultrd   r   rc   r^   zloop variant unhandled)r   r5   r   r4   r3   rE   ranger/   rZ   rN   r[   
ValueError)r(   rH   rN   xr   r   r   test_default   s    
rj   c                 C   s   t jd| jdz}| j  | jjdtd td}| j|t | |d j| |d d = | j|t | t	|d W d    n1 s0    Y  d S )NTrU   r   rB   FilterTests.test_modulerc   r   )
r   r5   r   r4   r3   rE   r/   rZ   rN   r[   r(   rH   rN   r   r   r   test_module   s    

ro   c                 C   s   t jd| jd}| j  | jjdtd td}| j|tdd | |d j| |d d = | j|tdd	 | t	|d
 | j|tdd | t	|d
 W d    n1 s0    Y  d S )NTrU   r   rB   FilterTests.test_once__init__.py*   rc      r   ztest_warnings2.py)
r   r5   r   r4   r3   rE   r0   rZ   rN   r[   rp   r   r   r   	test_once   s(    

rr   c              	   C   s   t jd| jd}| jdt | jjdtddd d | t|d | t	& | jjdtdddd W d    n1 s|0    Y  | t|d | jjdtddi d | t|d	 W d    n1 s0    Y  d S )
NTrU   ra   msgfilenamert   module_globalsr^   rd   )
r   r5   r   r   rE   r0   rZ   r[   rF   	TypeErrorrG   r   r   r   test_module_globals   s$    $zFilterTests.test_module_globalsc                 C   s`   t j| jd>}| j  | jjdtd | t| jjdt W d    n1 sR0    Y  d S )Nr@   rA   rB   FilterTests.test_inheritance)	r   r5   r   r4   r3   WarningrF   rE   r/   rG   r   r   r   test_inheritance   s    
r}   c              	   C   s   t jd| jd}| j  | jjdtd | jjdtdd |d d = z| jdt W n tyv   | d Y n0 | t	|d	 W d    n1 s0    Y  d S )
NTrU   rW   rB   rA   )rC   appendFilterTests.test_orderingz!order handling for actions failedr   )
r   r5   r   r4   r3   rE   r/   ZfailrZ   r[   rG   r   r   r   test_ordering  s    


r   c                 C   sB  t jd| jd}| jddtdd | t| jjd | j  d}| j| | 	t
|d j| | |d jt | jd	dtdd d
}| j| | t
|d j| | j  | jddtdd | t| jjd d}| j| | 	t
|d j| | |d jt W d    n1 s40    Y  d S )NTrU   rA    r   convert to errorzhandle normallyrc   rW   zfiltered outzhex*zhex/octznonmatching text)r   r5   r   r3   r~   rF   rE   r/   r4   rZ   strrN   assertIsrC   ZassertNotEqual)r(   rH   textr   r   r   test_filterwarnings  s,    

zFilterTests.test_filterwarningsc                 C   s   t jd| jd}| jdt | jddt | t| jjd | t| jjd | jd | |g  | jd | |g  W d    n1 s0    Y  d S )	NTrU   rW   rA   matchzmatch prefixzsuffix matchzsomething completely different)	r   r5   r   r   rE   r3   rF   r/   rZ   rG   r   r   r   test_message_matching*  s    z!FilterTests.test_message_matchingc                    s   G  fdddfddt dD  tjd| jdD} | j_| jtdd d	d
 | t|d j	d W d    n1 s0    Y  d S )Nc                       s   e Zd Z fddZdS )z.FilterTests.test_mutate_filter_list.<locals>.Xc                    s   g  d d < d S rK   r   )r(   aLr   r   r   8  s    z4FilterTests.test_mutate_filter_list.<locals>.X.matchN)r   r   r   r   r   r   r   r   X7  s   r   c                    s   g | ]}d   t   dfqS )ri   r   )rE   ).0i)r   r   r   
<listcomp>;      z7FilterTests.test_mutate_filter_list.<locals>.<listcomp>rd   TrU   bzf.pyrt   rc   )
rk   r   r5   r   r   r0   rE   rZ   r   rN   rG   r   )r   r   r   test_mutate_filter_list6  s    z#FilterTests.test_mutate_filter_listc                 C   s   t j| jd | j  | jjdtd | t| jjd | jjdtd | jjdtd | t| jjdd | | jjd d dd	 W d    n1 s0    Y  d S )
Nr@   rA   rB   r^   rW   rd   z(filterwarnings inserted duplicate filterr   z>filterwarnings did not promote filter to the beginning of list)	r   r5   r   r4   r3   rE   rZ   r[   r   r'   r   r   r   %test_filterwarnings_duplicate_filtersB  s    
z1FilterTests.test_filterwarnings_duplicate_filtersc                 C   s   t j| jd | j  | jjdtd | t| jjd | jjdtd | jjdtd | t| jjdd | | jjd d dd	 W d    n1 s0    Y  d S )
Nr@   rA   rB   r^   rW   rd   &simplefilter inserted duplicate filterr   z<simplefilter did not promote filter to the beginning of list)	r   r5   r   r4   r   rE   rZ   r[   r   r'   r   r   r   #test_simplefilter_duplicate_filtersS  s    
z/FilterTests.test_simplefilter_duplicate_filtersc                 C   s   t j| jdd~}| j  | jd | jjddd | jjddd | jjdtd | t| jj	dd	 | t|d
d W d    n1 s0    Y  d S )NTr   rV   rW   rA   r   test_append_duplicaterB   rd   r   r   z+appended duplicate changed order of filters)
r   r5   r   r4   r   r/   rE   rZ   r[   r   rG   r   r   r   r   c  s    
z!FilterTests.test_append_duplicateN)r   r   r   rI   rT   r]   r`   rf   rh   rn   rq   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   r?   b   s$   	r?   c                   @   s   e Zd ZeZdS )CFilterTestsNr:   r   r   r   r   r   r  s   r   c                   @   s   e Zd ZeZdS )PyFilterTestsNr=   r   r   r   r   r   u  s   r   c                   @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd ZdS )	WarnTestsc                 C   s   t jd| jdf}| jd tdD ]@}d| }| j| | t|d j| | 	|d j
t q&W d    n1 s|0    Y  d S )NTrU   r      zmulti %drc   )r   r5   r   r   rk   r/   rZ   r   rN   r   rC   rE   )r(   rH   r   r   r   r   r   test_message}  s    zWarnTests.test_messagec              	   C   sz   t d dfD ]j}tjd| jdF}| jd | j| | t|d jtt	| W d    q
1 sj0    Y  q
d S )Nrt   TrU   r   rc   )
r~   r   r5   r   r   r/   rZ   r   rN   rE   )r(   obrH   r   r   r   test_warn_nonstandard_types  s    z%WarnTests.test_warn_nonstandard_typesc              	   C   s   t | j tjd| jdX}td | tj	|d j
d td | tj	|d j
d W d    n1 sz0    Y  W d    n1 s0    Y  d S )NTrU   Zspam1rc   stacklevel.pyZspam2r   r   r   r5   r   innerrZ   ospathbasenamerx   ZouterrG   r   r   r   test_filename  s    

zWarnTests.test_filenamec              	   C   sV  t | j4 tjd| jd }tjddd | tj	|d j
d tjddd | tj	|d j
d tjd	d
d | tj	|d j
d tjdd
d | tj	|d j
d tjddd | tj	|d j
d tjddd | tj	|d j
d W d    n1 s(0    Y  W d    n1 sH0    Y  d S )NTrU   Zspam3r^   r   rc   r   Zspam4Zspam5rd   rs   Zspam6zspam6.5rg   Zspam7i'  r   r   rG   r   r   r   test_stacklevel  s:    zWarnTests.test_stacklevelc              	   C   s   t d t| jv tjd| jdF}| jd dd l}| t	|d | |d j
t W d    n1 sr0    Y  W d    n1 s0    Y  d S )Nz&test.test_warnings.data.import_warningTrU   ra   r   r^   )r   Zunloadr   r   r   r5   r   Z&test.test_warnings.data.import_warningrZ   r[   rx   __file__)r(   rH   testr   r   r   test_stacklevel_import  s    
z WarnTests.test_stacklevel_importc                 C   sj   d}t d|d}tjdd(}| jjdtd t| W d    n1 sJ0    Y  | |d j| d S )	Nz<warnings-test>z3import warnings
warnings.warn('hello', UserWarning)execT)rV   ra   rB   r   )	compiler   r5   r   r   rE   r   rZ   rx   )r(   rx   ZcodeobjrH   r   r   r   test_exec_filename  s    &zWarnTests.test_exec_filenamec              
   C   s   t jd| jd~}| j  | jjdtd dD ]N}zt| W n tyX   Y q0Y n0 | j	dt|d | 
|d j| q0W d    n1 s0    Y  d S )	NTrU   ra   rB   )u   nonasciié€u   surrogater   r^   rc   )r   r5   r   r4   r3   rE   r   fsencodeUnicodeEncodeErrorr0   rZ   rx   )r(   rH   rx   r   r   r   %test_warn_explicit_non_ascii_filename  s    

z/WarnTests.test_warn_explicit_non_ascii_filenamec              	   C   sT   |  t| jjd td d  |  t| jjd d d d | j ttf| jjd td ddd d S )Nr^   rt   registry)rF   r{   r   r0   rE   r   r~   r'   r   r   r   test_warn_explicit_type_errors  s    

z(WarnTests.test_warn_explicit_type_errorsc                 C   sL   G dd dt }| t | j|  W d    n1 s>0    Y  d S )Nc                   @   s   e Zd Zdd ZdS )z-WarnTests.test_bad_str.<locals>.BadStrWarningc                 S   s   dddi S )NzA bad formatted string %(err)errzthere is no %(err)sr   r'   r   r   r   __str__  s    z5WarnTests.test_bad_str.<locals>.BadStrWarning.__str__N)r   r   r   r   r   r   r   r   BadStrWarning  s   r   )r~   rF   rl   r   r/   )r(   r   r   r   r   test_bad_str  s    zWarnTests.test_bad_strc              	   C   s*  G dd dt }G dd d}| t}| jdd W d    n1 sL0    Y  | dt|j | t}| jd| W d    n1 s0    Y  | dt|j | t }| jd|  W d    n1 s0    Y  | dt|j tj	| jd | j
  | jd	 | |}| jd
| W d    n1 sV0    Y  | d
t|j | t}| jd
d  W d    n1 s0    Y  | d
t|j | |}| jd
| W d    n1 s0    Y  | |jt  W d    n1 s0    Y  d S )Nc                   @   s   e Zd ZdS )z6WarnTests.test_warning_classes.<locals>.MyWarningClassNr   r   r   r   r   MyWarningClass  s   r   c                   @   s   e Zd ZdS )z:WarnTests.test_warning_classes.<locals>.NonWarningSubclassNr   r   r   r   r   NonWarningSubclass  s   r   zbad warning categoryr   z)category must be a Warning subclass, not r@   ri   zgood warning category)r~   rF   r{   r   r/   rR   r   	exceptionr   r5   r4   r3   ZassertWarnsrZ   warningrE   ZassertIsInstance)r(   r   r   cmr   r   r   test_warning_classes  s:    ,,.
...zWarnTests.test_warning_classesN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   y  s   
r   c                   @   s   e Zd ZeZdd ZdS )
CWarnTestsc                 C   s&   |  t| j | t| jjd d S N__code__)assertIsNotr   r   assertFalser#   r/   r'   r   r   r   test_accelerated#  s    zCWarnTests.test_acceleratedN)r   r   r   r;   r   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZeZdd ZdS )PyWarnTestsc                 C   s&   |  t| j | t| jjd d S r   )r   r   r   r6   r#   r/   r'   r   r   r   test_pure_python,  s    zPyWarnTests.test_pure_pythonN)r   r   r   r>   r   r   r   r   r   r   r   '  s   r   c                   @   s   e Zd Zdd Zdd ZdS )WCmdLineTestsc              	   C   s  t j| jd\ | | jj| jjd | | jj| jjd | | jj| jjd | | jj | jd W d    n1 s0    Y  | | jj | jd W d    n1 s0    Y  | | jj | jd W d    n1 s0    Y  | | jjd | jd	 W d    n1 s60    Y  | jd
 | t| jjd W d    n1 st0    Y  d S )Nr@   z1:2:3:4:5:6zbogus::Warningzignore:2::4:-5zignore::123zignore::123abczignore::===u   Wärningu   ignore::Wärningzerror::Warning::0r   )	r   r5   r   rF   _OptionError
_setoptionZassertRaisesRegexrE   r/   r'   r   r   r   test_improper_input3  s(    


***,z!WCmdLineTests.test_improper_inputc              	   C   s   t j| jd | jd | | jj | jd W d    n1 sL0    Y  | | jj | jd W d    n1 s0    Y  | jd | t | jdt W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr@   zignore::Warningzignore::TestWarningz ignore::test.test_warnings.bogusz%error::test.test_warnings.TestWarningztest warning)r   r5   r   r   rF   r   r   r/   r'   r   r   r   test_import_from_moduleH  s    **z%WCmdLineTests.test_import_from_moduleN)r   r   r   r   r   r   r   r   r   r   1  s   r   c                   @   s   e Zd ZeZdS )CWCmdLineTestsNr:   r   r   r   r   r   T  s   r   c                   @   s    e Zd ZeZdd Zdd ZdS )PyWCmdLineTestsc                 C   s"   t ddd\}}}| d| d S )Nz-Wxxx-cpasss0   Invalid -W option ignored: invalid action: 'xxx')r   rR   r(   rcoutr   r   r   r   test_improper_option[  s    z$PyWCmdLineTests.test_improper_optionc                 C   s0   t ddd\}}}| |  | d| d S )Nz-Wir   z?import sys; sys.modules['warnings'].warn('foo', RuntimeWarning)s   RuntimeWarning)r   r   stripassertNotInr   r   r   r   test_warnings_bootstrapb  s
    
z'PyWCmdLineTests.test_warnings_bootstrapN)r   r   r   r>   r   r   r   r   r   r   r   r   X  s   r   c                   @   s   e Zd ZeZdd Zdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zejdd Zejdd Zejdd ZdS )_WarningsTestsc                 C   sp   t j| jdN}| jddtdd | t| jjd | j`| t| jjd W d    n1 sb0    Y  d S )Nr@   rA   r   r   r   )	r   r5   r   r3   r~   rF   rE   r/   r   rG   r   r   r   test_filterr  s    z_WarningsTests.test_filterc                 C   s2  t d}z| jj}i atjd| jd}| j  | jjdt d | j|t dd | 	|d j
| |d d = | j|t dd | 	t|d	 i | j_i a| jd | 	|d j
j|j |d d = | j`i a| j|t dd | 	t|d	 W d    n1 s0    Y  W || j_n
|| j_0 d S )
Nzonceregistry testTrU   r   rB   filert   rc   r   )rE   r   onceregistryr   r   r5   r4   r3   r0   rZ   rN   r[   r/   re   )r(   rN   Zoriginal_registryrH   r   r   r   test_onceregistry|  s2    


2z _WarningsTests.test_onceregistryc                 C   sd  t d}| jj}zBtjd| jd}| j  i }| jj|t dd|d | |d j| | t	|d | t	|d	 | 
d
| |d d = | j`i }i }| jj|t dd|d | |d j| | t	|d | t	|d	 |d d = d| j_i }i }| jj|t dd|d | t	|d W d    n1 s@0    Y  W || j_n
|| j_0 d S )Nzdefaultaction testTrU   z<test>rt   r   rc   r^   rd   rY   +   rW   ,   r   )rE   r   defaultactionr   r5   r4   r0   rZ   rN   r[   rR   )r(   rN   ZoriginalrH   r   r   r   r   r   test_default_action  sF    


2z"_WarningsTests.test_default_actionc              	   C   s   d}t j| jdd | jjdtd | j`td$}| j| |	 }W d    n1 s^0    Y  W d    n1 s|0    Y  | 
|| d S )Nzdel showwarning testr@   ra   rB   rS   )r   r5   r   r3   rE   r1   r   captured_outputr/   rQ   rR   )r(   r   streamresultr   r   r   test_showwarning_missing  s    Dz'_WarningsTests.test_showwarning_missingc              	   C   s   d}t j| jd | jjdtd | jj}zP| j`td$}| j| |	 }W d    n1 sh0    Y  W || j_n
|| j_0 W d    n1 s0    Y  | 
|| d S )Nzdel _showwarnmsg testr@   ra   rB   rS   )r   r5   r   r3   rE   _showwarnmsgr   r   r/   rQ   rR   )r(   r   showr   r   r   r   r   test_showwarnmsg_missing  s    (2z'_WarningsTests.test_showwarnmsg_missingc              	   C   s   t j| jdx | jjdtd t| j_td | j	d W d    n1 sT0    Y  d| j_| 
t| jj	d W d    n1 s0    Y  d S )Nr@   ra   rB   stdoutzWarning!   )r   r5   r   r3   rE   printr1   r   r   r/   rF   r{   r'   r   r   r   test_showwarning_not_callable  s    *z,_WarningsTests.test_showwarning_not_callablec              	   C   s8  d}t j| jdb | jjdtd | j`td"}t	| |
 }W d    n1 s\0    Y  W d    n1 sz0    Y  | |ddd|  |dd	\}}tjtjd
 d }|dd}|\}}	}
}t|	}	| || | |
dtj  | |d|  dt||	  d }| || d S )Nztest show_warningr@   ra   rB   rS   
rd   zToo many newlines in %rr^   r   .py:rg    z  )r   r5   r   r3   rE   r1   r   r   r   r   rQ   rZ   countsplitr   r   splitextr   rsplitintr   	linecachegetliner   )r(   r   r   r   Z
first_lineZsecond_lineZexpected_fileZfirst_line_partsr   lineZwarning_classrN   Zexpected_liner   r   r   test_show_warning_output  s(    
Dz'_WarningsTests.test_show_warning_outputc              	   C   s   t  }|d }zrtjd| jd}|B}| jjdtd d |d< tdt | t| W d    n1 sl0    Y  W ||d< n
||d< 0 d S )Nr   TrU   ra   rB   r   )	r"   r   r5   r   r3   rE   r/   r6   r[   )r(   Zglobals_dictZoldfileZcatchrH   r   r   r   test_filename_none  s    .z!_WarningsTests.test_filename_nonec                 C   s8   t dd\}}}| |d | d| | d| d S )Nr   zjimport sys; sys.stderr = None; import warnings; warnings.simplefilter('always'); warnings.warn('Warning!')r   s   Warning!s   Error)r   rZ   r   r(   r   r   rS   r   r   r   test_stderr_none	  s    
z_WarningsTests.test_stderr_nonec              
   C   s  dd }| j }tj|d |jdtd t .}|jdtdd|d	d
dd W d    n1 sf0    Y  | d|	  |j
}zf|`
t 0}|jdtdd|d	gd
dd W d    n1 s0    Y  | d|	  W ||_
n||_
0 W d    n1 s0    Y  d S )Nc                    s   G  fddd}| S )Nc                       s   e Zd Z fddZdS )zI_WarningsTests.test_issue31285.<locals>.get_bad_loader.<locals>.BadLoaderc                    s   G  fdddt }|dS )Nc                       s   e Zd Z fddZdS )zg_WarningsTests.test_issue31285.<locals>.get_bad_loader.<locals>.BadLoader.get_source.<locals>.BadSourcec                    s    S rK   r   r'   splitlines_ret_valr   r   
splitlines  s    zr_WarningsTests.test_issue31285.<locals>.get_bad_loader.<locals>.BadLoader.get_source.<locals>.BadSource.splitlinesN)r   r   r   r   r   r   r   r   	BadSource  s   r   Zspam)r   )r(   fullnamer   r   r   r   
get_source  s    zT_WarningsTests.test_issue31285.<locals>.get_bad_loader.<locals>.BadLoader.get_sourceN)r   r   r   r  r   r   r   r   	BadLoader  s   r  r   )r   r  r   r   r   get_bad_loader  s    z6_WarningsTests.test_issue31285.<locals>.get_bad_loaderr@   ri   rB   foobarr^   rt   Zfoobar)
__loader__r   ry   zUserWarning: fooZeggszUserWarning: eggs)r   r   r5   r3   rE   r   rP   r0   rR   rQ   r   )r(   r  wmodrS   r   r   r   r   test_issue31285  s0    	
$
$z_WarningsTests.test_issue31285c              
   C   s   | j }tj|d |d t|dd N | t$ |jdt	ddd d W d    n1 sb0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr@   r   r   r  r  r^   r   )
r   r   r5   r3   r   	swap_attrrF   r{   r0   r~   r(   r  r   r   r   test_issue314116  s    
z_WarningsTests.test_issue31411c              
   C   s   | j }tj|d d d td dfg|_| t  |dtdd W d    n1 sV0    Y  g |_t	|dd J | t  |dtdd W d    n1 s0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr@   r   r  r  r^   r   )
r   r   r5   r~   r   rF   r{   r0   r   r	  r
  r   r   r   test_issue31416A  s    .z_WarningsTests.test_issue31416c              
   C   s   t j| jd | jjdtd tt ddR tt dd " | t| jj	d W d    n1 sj0    Y  W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr@   rA   rB   r   s   foor   r  )
r   r5   r   r3   rE   r   Z	swap_itemr"   rF   r/   r'   r   r   r   test_issue31566P  s    z_WarningsTests.test_issue31566N)r   r   r   r;   r   r   r   r   r   r   r   r   r   r   r  r   Zcpython_onlyr  r  r  r   r   r   r   r   l  s"   
%		$


r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )WarningsDisplayTestsc              
   C   s   d}t }tjtjd d }d}t|| }d}||||j	||f }| 
|| j|||| |d7 }||||j	||f }| 
|| j||||| d S )Nrw   r   r   rg   z%s:%s: %s: %s
  %s
z for the win!)r~   r   r   r   r   r   r   r   r   r   rZ   r   r2   )r(   rN   rC   	file_nameline_num	file_lineformatexpectr   r   r   test_formatwarning`  s&    z'WarningsDisplayTests.test_formatwarningc                 C   s   t jtjd d }d}t|| }d}t}t	 }| j
||||}| j
||||| | | | |d7 }| j
|||||}t	 }| j
|||||| | ||  d S )Nr   r   rg   rw   zfor the win!)r   r   r   r   r   r   r   r   r~   r   r   r2   r1   rZ   rQ   )r(   r  r  Zexpected_file_linerN   rC   file_objectr  r   r   r   test_showwarningr  s,    z%WarningsDisplayTests.test_showwarningc           	   	   C   s   dd }t jtjd d }d}t|| }d}t}t	 }d| d| d	| d
| d|  }t
| jd|6 | j|||||| | | | W d    n1 s0    Y  d S )Nc              
   S   s"   d|  d| d| d| d| 
S )Nm=:c=:f=:l=:t=r   )rN   rC   rx   linenor   r   r   r   myformatwarning  s    zIWarningsDisplayTests.test_formatwarning_override.<locals>.myformatwarningr   r   rg   rw   r  r  r  r  r  r2   )r   r   r   r   r   r   r   r   r~   r   r   r	  r   r1   rZ   rQ   )	r(   r  r  r  r  rN   rC   r  expectedr   r   r   test_formatwarning_override  s    z0WarningsDisplayTests.test_formatwarning_overrideN)r   r   r   r  r  r  r   r   r   r   r  [  s   r  c                   @   s   e Zd ZeZdS )CWarningsDisplayTestsNr:   r   r   r   r   r     s   r   c                   @   s   e Zd ZeZdd ZdS )PyWarningsDisplayTestsc              	   C   s   |  tjtj ttjd }|td W d    n1 sB0    Y  dd }tj	
tj}|dtj}td| d }| || |ddd	tj}td| d
| d| d }| || d S )NrH   z
                def func():
                    f = open(__file__)
                    # Emit ResourceWarning
                    f = None

                func()
            c                  W   s<   t | ddi}|jdd}d| }tdd|}|S )NPYTHONIOENCODINGutf-8replacer   z<.*>z<...>)r   r   decodejoinr   resub)re   resrS   r   r   r   run  s
    z4PyWarningsDisplayTests.test_tracemalloc.<locals>.run-Wdz
            z:5: ResourceWarning: unclosed file <...>
              f = None
            ResourceWarning: Enable tracemalloc to get the object allocation traceback
        z-Xztracemalloc=2z:5: ResourceWarning: unclosed file <...>
              f = None
            Object allocated at (most recent call last):
              File "z7", lineno 7
                func()
              File "z7", lineno 3
                f = open(__file__)
        )Z
addCleanupr   unlinkZTESTFNopenwritetextwrapdedentr   r   abspathr   rZ   )r(   fpr*  rx   rS   r  r   r   r   test_tracemalloc  s&    .	
	z'PyWarningsDisplayTests.test_tracemallocN)r   r   r   r>   r   r3  r   r   r   r   r!    s   r!  c                   @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )CatchWarningTestsc                 C   s   | j }|j}|j}|j|dd t  |_|_W d    n1 sD0    Y  | |j| | |j| |j|dd t  |_|_W d    n1 s0    Y  | |j| | |j| d S NTr   F)r   r   r1   r5   objectr   )r(   r  orig_filtersorig_showwarningr   r   r   test_catch_warnings_restore  s    ,,z-CatchWarningTests.test_catch_warnings_restorec                 C   s.  | j }|j|dd}| |g  | t|t |d |d | t|d j	d |d | t|d j	d | t|d j	d | t|d j	d |d d = | |g  W d    n1 s0    Y  |j
}|j|d	d(}| | | |j
| W d    n1 s 0    Y  d S )
NTr   ra   r  rc   r  r   r^   F)r   r5   rZ   r   typer\   r   r/   r   rN   r1   assertIsNone)r(   r  rH   r8  r   r   r   test_catch_warnings_recording  s"    



*
z/CatchWarningTests.test_catch_warnings_recordingc                 C   s   | j }|j|dd}| t|j | | t|j W d    n1 sJ0    Y  |j|dd}| t|j | | t|j W d    n1 s0    Y  d S r5  )r   r5   rF   RuntimeError__exit__	__enter__)r(   r  rm   r   r   r   !test_catch_warnings_reentry_guard  s    ,z3CatchWarningTests.test_catch_warnings_reentry_guardc                 C   s   | j }|j}|j}|j|d6}| | | |j| | |j| W d    n1 sZ0    Y  | |j| |tjd u r| 6}| | | |j| | |j| W d    n1 s0    Y  | |j| d S )Nr@   r	   )	r   r   r1   r5   r;  r   r   r   r$   )r(   r  r7  r8  rH   r   r   r   test_catch_warnings_defaults  s    
,

,z.CatchWarningTests.test_catch_warnings_defaultsc              	      s   d}| j }g  d
 fdd	}t|d|n |j|dd2}| |j| |d || W d    n1 sr0    Y  | |j| W d    n1 s0    Y  | 	t
|d| | 	|d	 jjd	 | | 	 g  d S )NThis is a warningc                    s     |  d S rK   r   rN   rC   rx   r  r   r   Zmy_logr   r   	my_logger  s    zLCatchWarningTests.test_record_override_showwarning_before.<locals>.my_loggerr1   Tr   ra   r^   r   )NN)r   r   r	  r5   r   r1   r   r/   r   rZ   r[   rN   re   r(   r   r  rE  logr   rD  r   'test_record_override_showwarning_before  s    
(,z9CatchWarningTests.test_record_override_showwarning_beforec                    s   d}| j }g  d	 fdd	}|j|dd*}|d ||_|| W d    n1 sZ0    Y  | t d  |  d jd | | |g  d S )
NrB  c                    s     |  d S rK   r   rC  rD  r   r   rE  6  s    zLCatchWarningTests.test_record_override_showwarning_inside.<locals>.my_loggerTr   ra   r^   r   )NN)r   r5   r   r1   r/   rZ   r[   re   rF  r   rD  r   'test_record_override_showwarning_inside/  s    
(z9CatchWarningTests.test_record_override_showwarning_insidec              	   C   s  | j }|tjd ur| d tjdd}| |jg  |d |	d | t
|jd |	d | t
|jd | t
|jd jd | t
|jd	 jd |  | |jg  W d    n1 s0    Y  t  W d    n1 s0    Y  tdtf |	d W d    n1 s80    Y  | t@ td
tf W d    n1 st0    Y  W d    n1 s0    Y  | tJ tdtf |	d W d    n1 s0    Y  W d    n1 s0    Y  d S )Nr	   z,module to test is not loaded warnings moduleF)quietra   r  r  r   r^   r   )r   r   r$   ZskipTestr   Zcheck_warningsrZ   r	   r   r/   r   rN   resetrE   rF   AssertionErrorRuntimeWarning)r(   r  rH   r   r   r   test_check_warningsC  s0    



,
*@z%CatchWarningTests.test_check_warningsN)
r   r   r   r9  r<  r@  rA  rH  rI  rN  r   r   r   r   r4    s   r4  c                   @   s   e Zd ZeZdS )CCatchWarningTestsNr:   r   r   r   r   rO  b  s   rO  c                   @   s   e Zd ZeZdS )PyCatchWarningTestsNr=   r   r   r   r   rP  e  s   rP  c                   @   sP   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze	e
 dkddd ZdS )EnvironmentVariableTestsc                 C   s&   t ddddd\}}}| |d d S )Nr   2import sys; sys.stdout.write(str(sys.warnoptions))ignore::DeprecationWarningr   PYTHONWARNINGSPYTHONDEVMODEs   ['ignore::DeprecationWarning']r   rZ   r   r   r   r   test_single_warningk  s    z,EnvironmentVariableTests.test_single_warningc                 C   s&   t ddddd\}}}| |d d S )Nr   rR  z1ignore::DeprecationWarning,ignore::UnicodeWarningr   rT  8   ['ignore::DeprecationWarning', 'ignore::UnicodeWarning']rW  r   r   r   r   test_comma_separated_warningsr  s    z6EnvironmentVariableTests.test_comma_separated_warningsc                 C   s(   t dddddd\}}}| |d d S )Nz-Wignore::UnicodeWarningr   rR  rS  r   rT  rY  rW  r   r   r   r   test_envvar_and_command_linez  s    z5EnvironmentVariableTests.test_envvar_and_command_linec                 C   s<   t dddddd\}}}| |d | | g d d S )	Nz-Werror::DeprecationWarningr   zjimport sys, warnings; sys.stdout.write(str(sys.warnoptions)); warnings.warn('Message', DeprecationWarning)zdefault::DeprecationWarningr   rT  s<   ['default::DeprecationWarning', 'error::DeprecationWarning'])s"   Traceback (most recent call last):s&     File "<string>", line 1, in <module>s   DeprecationWarning: Message)r   rZ   r   r   r   r   r   (test_conflicting_envvar_and_command_line  s    
zAEnvironmentVariableTests.test_conflicting_envvar_and_command_linec           
   	   C   s   | j tu }trg }nT|r$td}nd}dd t|dfdd td dfdd td dfdd td dfdd td dfg}dd |D }|rd}nd}|d	7 }t	d
|dd\}}}dd |
 D }	d | _| |	| d S )N__main__ri   r   rW   c                 S   s   g | ]}t | qS r   )r   encode)r   rO   r   r   r   r     r   zNEnvironmentVariableTests.test_default_filter_configuration.<locals>.<listcomp>zPimport sys; sys.modules.pop('warnings', None); sys.modules['_warnings'] = None; r   z5import warnings; [print(f) for f in warnings.filters]r   T)Z
__isolatedc                 S   s   g | ]}|  qS r   )r   )r   r   r   r   r   r     r   )r   r>   Py_DEBUGr'  r   DeprecationWarningPendingDeprecationWarningImportWarningResourceWarningr   r   ZmaxDiffrZ   )
r(   Zpure_python_apiZexpected_default_filtersZmain_module_filterZexpected_outputcoder   r   rS   Zstdout_linesr   r   r   !test_default_filter_configuration  s*    
z:EnvironmentVariableTests.test_default_filter_configurationasciiz%requires non-ascii filesystemencodingc                 C   s<   dt j }tddd|dd\}}}| |t|g  d S )Nzignore:DeprecationWarningr   rR  r#  r   )r"  rU  rV  )r   ZFS_NONASCIIr   rZ   r   r^  )r(   rU  r   r   rS   r   r   r   test_nonascii  s    
z&EnvironmentVariableTests.test_nonasciiN)r   r   r   rX  rZ  r[  r\  re  r   Z
skipUnlessr   getfilesystemencodingrg  r   r   r   r   rQ  i  s   rQ  c                   @   s   e Zd ZeZdS )CEnvironmentVariableTestsNr:   r   r   r   r   ri    s   ri  c                   @   s   e Zd ZeZdS )PyEnvironmentVariableTestsNr=   r   r   r   r   rj    s   rj  c                   @   s   e Zd Zdd ZdS )BootstrapTestc              	   C   sv   t  Z}t d0 tdd|d tdddd|d W d    n1 sJ0    Y  W d    n1 sh0    Y  d S )N	encodingsr   r   )Z
PYTHONPATHz-Wra   )r   Ztemp_cwdr   )r(   cwdr   r   r   test_issue_8766  s    zBootstrapTest.test_issue_8766N)r   r   r   rn  r   r   r   r   rk    s   rk  c                   @   s   e Zd Zdd Zdd ZdS )FinalizationTestc                 C   s,   d}t d|\}}}| |  d d S )Nzk
import warnings
warn = warnings.warn

class A:
    def __del__(self):
        warn("test")

a=A()
        r   z<string>:7: UserWarning: test)r   rZ   r%  rstrip)r(   rd  r   r   r   r   r   r   test_finalization  s
    
z"FinalizationTest.test_finalizationc                 C   sh   d}dt  }tdd|\}}}| ||t| dt  }tdd|\}}}| ||t| d S )Ns&   sys:1: ResourceWarning: unclosed file zf = open(%a)r+  r   zimport warnings; f = open(%a))r   r   r6   
startswithrf  )r(   r  rd  r   r   r   r   r   r   test_late_resource_warning  s    z+FinalizationTest.test_late_resource_warningN)r   r   r   rq  rs  r   r   r   r   ro    s   ro  c                   C   s   t j  tj  d S rK   )r>   r   r   r;   r   r   r   r   setUpModule  s    
rt  r]  ):
contextlibr   r   r   ior   r'  r   r/  r   r   r   Ztest.support.script_helperr   r   Ztest.test_warnings.datar   r   r	   r   Zimport_fresh_moduler>   r;   r#   r_  r   r~   r   r   r-   ZTestCaser9   r<   r?   r   r   r   r   r   r   r   r   r   r  r   r!  r4  rO  rP  rQ  ri  rj  rk  ro  rt  ZtearDownModuler   mainr   r   r   r   <module>   sf   

   &	
# p@1 P$
