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 )z8Use a specific warnings implementation in warning_tests.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dZ fddZ fddZ  ZS )BaseTestz'Basic bookkeeping required for testing.c                    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   __doc__r&   r+   __classcell__r   r   r)   r   r   2   s   r   c                   @   s   e Zd ZdZdd ZdS )PublicAPITestszGEnsures that the correct values are exposed in the
    public API.
    c                 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   r,   r9   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   r:   \   s   r:   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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 )'FilterTestsz$Testing the filtering functionality.c                 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   r6   r   r5   r4   UserWarningassertRaisesr0   r(   wr   r   r   
test_errorf   s    
rE   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 )	NrA   %FilterTests.test_ignore_after_defaultc                      s   j  t d S Nr   r0   rF   r   messager(   r   r   fq   s    z/FilterTests.test_error_after_default.<locals>.fz2UserWarning: FilterTests.test_ignore_after_defaultz&self.module.warn(message, UserWarning)rB   rC   )r   r6   r   r5   r   captured_stderrgetvalueassertInr4   rF   rG   )r(   rI   rP   stderrr   rN   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   ignorerC   FilterTests.test_ignorer   version)r   r6   r   r5   r4   rF   r0   assertEquallenlistr   rH   r   r   r   test_ignore   s    
rY   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 )	NTrV   rK   c                      s   j  t d S rL   rM   r   rN   r   r   rP      s    z0FilterTests.test_ignore_after_default.<locals>.frX   rC      )r   r6   r   r5   r4   rF   r[   r\   r(   rI   rP   r   rN   r   test_ignore_after_default   s    
rK   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 )NTrV   alwaysrC   FilterTests.test_alwaysc                      s   j  t d S rL   rM   r   rN   r   r   rP      s    z"FilterTests.test_always.<locals>.fr_   r      )
r   r6   r   r5   r4   rF   r[   r\   rO   argsr`   r   rN   r   test_always   s    
rc   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 )NTrV   z$FilterTests.test_always_after_ignorec                      s   j  t d S rL   rM   r   rN   r   r   rP      s    z0FilterTests.test_always_after_default.<locals>.fr_   rd   r   rb   rC   re      )
r   r6   r   r5   r[   r\   rO   rf   r4   rF   r`   r   rN   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 )NTrV   defaultrC   FilterTests.test_defaultre   r   rd   r_   zloop variant unhandled)r   r6   r   r5   r4   rF   ranger0   r[   rO   r\   
ValueError)r(   rI   rO   xr   r   r   test_default   s    
rk   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 )NTrV   r   rC   FilterTests.test_modulerd   r   )
r   r6   r   r5   r4   rF   r0   r[   rO   r\   r(   rI   rO   r   r   r   test_module   s    

rp   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 )NTrV   r   rC   FilterTests.test_once__init__.py*   rd      r   ztest_warnings2.py)
r   r6   r   r5   r4   rF   r1   r[   rO   r\   rq   r   r   r   	test_once   s(    

rs   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 )
NTrV   rb   msgfilenameru   module_globalsr_   re   )
r   r6   r   r   rF   r1   r[   r\   rG   	TypeErrorrH   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 )NrA   rB   rC   FilterTests.test_inheritance)	r   r6   r   r5   r4   WarningrG   rF   r0   rH   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 )
NTrV   rX   rC   rB   )rD   appendFilterTests.test_orderingz!order handling for actions failedr   )
r   r6   r   r5   r4   rF   r0   Zfailr[   r\   rH   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 )NTrV   rB    r   convert to errorzhandle normallyrd   rX   zfiltered outzhex*zhex/octznonmatching text)r   r6   r   r4   r   rG   rF   r0   r5   r[   strrO   assertIsrD   ZassertNotEqual)r(   rI   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 )	NTrV   rX   rB   matchzmatch prefixzsuffix matchzsomething completely different)	r   r6   r   r   rF   r4   rG   r0   r[   rH   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 rL   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 )rj   r   )rF   ).0i)r   r   r   
<listcomp>;      z7FilterTests.test_mutate_filter_list.<locals>.<listcomp>re   TrV   bzf.pyru   rd   )
rl   r   r6   r   r   r1   rF   r[   r   rO   rH   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 )
NrA   rB   rC   r_   rX   re   z(filterwarnings inserted duplicate filterr   z>filterwarnings did not promote filter to the beginning of list)	r   r6   r   r5   r4   rF   r[   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 )
NrA   rB   rC   r_   rX   re   &simplefilter inserted duplicate filterr   z<simplefilter did not promote filter to the beginning of list)	r   r6   r   r5   r   rF   r[   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   rW   rX   rB   r   test_append_duplicaterC   re   r   r   z+appended duplicate changed order of filters)
r   r6   r   r5   r   r0   rF   r[   r\   r   rH   r   r   r   r   c  s    
z!FilterTests.test_append_duplicateN)r   r   r   r,   rJ   rU   r^   ra   rg   ri   ro   rr   rw   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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 )	WarnTestsz2Test warnings.warn() and warnings.warn_explicit().c                 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 )NTrV   r      zmulti %drd   )r   r6   r   r   rl   r0   r[   r   rO   r   rD   rF   )r(   rI   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 )Nru   TrV   r   rd   )
r   r   r6   r   r   r0   r[   r   rO   rF   )r(   obrI   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 )NTrV   Zspam1rd   stacklevel.pyZspam2r   r   r   r6   r   innerr[   ospathbasenamery   ZouterrH   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 )NTrV   Zspam3r_   r   rd   r   Zspam4Zspam5re   rt   Zspam6zspam6.5rh   Zspam7i'  r   r   rH   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_warningTrV   rb   r   r_   )r   Zunloadr   r   r   r6   r   Z&test.test_warnings.data.import_warningr[   r\   ry   __file__)r(   rI   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)rW   rb   rC   r   )	compiler   r6   r   r   rF   r   r[   ry   )r(   ry   ZcodeobjrI   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 )	NTrV   rb   rC   )u   nonasciié€u   surrogater   r_   rd   )r   r6   r   r5   r4   rF   r   fsencodeUnicodeEncodeErrorr1   r[   ry   )r(   rI   ry   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_   ru   registry)rG   r|   r   r1   rF   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Zdd ZdS )z-WarnTests.test_bad_str.<locals>.BadStrWarningz-Warning with a bad format string for __str__.c                 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   r   BadStrWarning  s   r   )r   rG   rm   r   r0   )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 rA   rj   zgood warning category)r   rG   r|   r   r0   rS   r   	exceptionr   r6   r5   r4   ZassertWarnsr[   warningrF   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   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#   r0   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   r7   r#   r0   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 )NrA   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   r6   r   rG   _OptionError
_setoptionZassertRaisesRegexrF   r0   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 )NrA   zignore::Warningzignore::TestWarningz ignore::test.test_warnings.bogusz%error::test.test_warnings.TestWarningztest warning)r   r6   r   r   rG   r   r   r0   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   rS   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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 )_WarningsTestsz'Tests specific to the _warnings module.c                 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 )NrA   rB   r   r   r   )	r   r6   r   r4   r   rG   rF   r0   r   rH   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 testTrV   r   rC   fileru   rd   r   )rF   r   onceregistryr   r   r6   r5   r4   r1   r[   rO   r\   r0   rf   )r(   rO   Zoriginal_registryrI   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 testTrV   z<test>ru   r   rd   r_   re   rZ   +   rX   ,   r   )rF   r   defaultactionr   r6   r5   r1   r[   rO   r\   rS   )r(   rO   ZoriginalrI   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 testrA   rb   rC   rT   )r   r6   r   r4   rF   r2   r   captured_outputr0   rR   rS   )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 testrA   rb   rC   rT   )r   r6   r   r4   rF   _showwarnmsgr   r   r0   rR   rS   )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 )NrA   rb   rC   stdoutzWarning!   )r   r6   r   r4   rF   printr2   r   r   r0   rG   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_warningrA   rb   rC   rT   
re   zToo many newlines in %rr_   r   .py:rh    z  )r   r6   r   r4   rF   r2   r   r   r   r   rR   r[   countsplitr   r   splitextr   rsplitintr   	linecachegetliner   )r(   r   r   r   Z
first_lineZsecond_lineZexpected_fileZfirst_line_partsr   lineZwarning_classrO   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   TrV   rb   rC   r   )	r"   r   r6   r   r4   rF   r0   r7   r\   )r(   Zglobals_dictZoldfileZcatchrI   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   r[   r   r(   r   r   rT   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 rL   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_loaderrA   rj   rC   foobarr_   ru   Zfoobar)
__loader__r   rz   zUserWarning: fooZeggszUserWarning: eggs)r   r   r6   r4   rF   r   rQ   r1   rS   rR   r   )r(   r  wmodrT   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 )NrA   r   r   r  r  r_   r   )
r   r   r6   r4   r   	swap_attrrG   r|   r1   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 )NrA   r   r  r  r_   r   )
r   r   r6   r   r   rG   r|   r1   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 )NrA   rB   rC   r   s   foor   r  )
r   r6   r   r4   rF   r   Z	swap_itemr"   rG   r0   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	  r   Zcpython_onlyr  r  r  r   r   r   r   r   l  s$   
%		$


r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	WarningsDisplayTestsziTest the displaying of warnings and the ability to overload functions
    related to displaying warnings.c              
   C   s   d}t }tjtjd d }d}t|| }d}||||j	||f }| 
|| j|||| |d7 }||||j	||f }| 
|| j||||| d S )Nrx   r   r   rh   z%s:%s: %s: %s
  %s
z for the win!)r   r   r   r   r   r   r   r   r   r   r[   r   r3   )r(   rO   rD   	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   rh   rx   zfor the win!)r   r   r   r   r   r   r   r   r   r   r   r3   r2   r[   rR   )r(   r  r  Zexpected_file_linerO   rD   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   )rO   rD   ry   linenor   r   r   r   myformatwarning  s    zIWarningsDisplayTests.test_formatwarning_override.<locals>.myformatwarningr   r   rh   rx   r  r  r  r  r  r3   )r   r   r   r   r   r   r   r   r   r   r   r
  r   r2   r[   rR   )	r(   r  r  r  r  rO   rD   r  expectedr   r   r   test_formatwarning_override  s    z0WarningsDisplayTests.test_formatwarning_overrideN)r   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 )NrI   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)rf   resrT   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   r[   )r(   fpr+  ry   rT   r  r   r   r   test_tracemalloc  s&    .	
	z'PyWarningsDisplayTests.test_tracemallocN)r   r   r   r?   r   r4  r   r   r   r   r"    s   r"  c                   @   sH   e Zd 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 )CatchWarningTestszTest catch_warnings().c                 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   r2   r6   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   rb   r  rd   r  r   r_   F)r   r6   r[   r   typer]   r   r0   r   rO   r2   assertIsNone)r(   r  rI   r9  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 r6  )r   r6   rG   RuntimeError__exit__	__enter__)r(   r  rn   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 )NrA   r	   )	r   r   r2   r6   r<  r   r   r   r$   )r(   r  r8  r9  rI   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 rL   r   rO   rD   ry   r  r   r   Zmy_logr   r   	my_logger  s    zLCatchWarningTests.test_record_override_showwarning_before.<locals>.my_loggerr2   Tr   rb   r_   r   )NN)r   r   r
  r6   r   r2   r   r0   r   r[   r\   rO   rf   r(   r   r  rF  logr   rE  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 )
NrC  c                    s     |  d S rL   r   rD  rE  r   r   rF  6  s    zLCatchWarningTests.test_record_override_showwarning_inside.<locals>.my_loggerTr   rb   r_   r   )NN)r   r6   r   r2   r0   r[   r\   rf   rG  r   rE  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)quietrb   r  r  r   r_   r   )r   r   r$   ZskipTestr   Zcheck_warningsr[   r	   r   r0   r   rO   resetrF   rG   AssertionErrorRuntimeWarning)r(   r  rI   r   r   r   test_check_warningsC  s0    



,
*@z%CatchWarningTests.test_check_warningsN)r   r   r   r,   r:  r=  rA  rB  rI  rJ  rO  r   r   r   r   r5    s   r5  c                   @   s   e Zd ZeZdS )CCatchWarningTestsNr;   r   r   r   r   rP  b  s   rP  c                   @   s   e Zd ZeZdS )PyCatchWarningTestsNr>   r   r   r   r   rQ  e  s   rQ  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   r[   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   rS  z1ignore::DeprecationWarning,ignore::UnicodeWarningr   rU  8   ['ignore::DeprecationWarning', 'ignore::UnicodeWarning']rX  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   rS  rT  r   rU  rZ  rX  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   rU  s<   ['default::DeprecationWarning', 'error::DeprecationWarning'])s"   Traceback (most recent call last):s&     File "<string>", line 1, in <module>s   DeprecationWarning: Message)r   r[   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__rj   r   rX   c                 S   s   g | ]}t | qS r   )r   encode)r   rP   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maxDiffr[   )
r(   Zpure_python_apiZexpected_default_filtersZmain_module_filterZexpected_outputcoder   r   rT   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   rS  r$  r   )r#  rV  rW  )r   ZFS_NONASCIIr   r[   r   r_  )r(   rV  r   r   rT   r   r   r   test_nonascii  s    
z&EnvironmentVariableTests.test_nonasciiN)r   r   r   rY  r[  r\  r]  rf  r   Z
skipUnlessr   getfilesystemencodingrh  r   r   r   r   rR  i  s   rR  c                   @   s   e Zd ZeZdS )CEnvironmentVariableTestsNr;   r   r   r   r   rj    s   rj  c                   @   s   e Zd ZeZdS )PyEnvironmentVariableTestsNr>   r   r   r   r   rk    s   rk  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-Wrb   )r   Ztemp_cwdr   )r(   cwdr   r   r   test_issue_8766  s    zBootstrapTest.test_issue_8766N)r   r   r   ro  r   r   r   r   rl    s   rl  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   r[   r&  rstrip)r(   re  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   r7   
startswithrg  )r(   r  re  r   r   r   r   r   r   test_late_resource_warning  s    z+FinalizationTest.test_late_resource_warningN)r   r   r   rr  rt  r   r   r   r   rp    s   rp  c                   C   s   t j  tj  d S rL   )r?   r   r   r<   r   r   r   r   setUpModule  s    
ru  r^  ):
contextlibr   r   r   ior   r(  r   r0  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TestCaser:   r=   r@   r   r   r   r   r   r   r   r   r   r  r!  r"  r5  rP  rQ  rR  rj  rk  rl  rp  ru  ZtearDownModuler   mainr   r   r   r   <module>   sf   

   &	
# p@1 P$
