B
    u9a?  ã               @   s¶   d dl 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 dd„ Z	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ ZG dd„ dejƒZejG dd„ dejƒƒZedkr²e ¡  dS )é    N)Úsupport)Úscript_helperc               C   s   t dƒ d S )NÚh1)Úprint© r   r   ú!/usr/lib/python3.7/test_atexit.pyr   
   s    r   c               C   s   t dƒ d S )NÚh2)r   r   r   r   r   r      s    r   c               C   s   t dƒ d S )NÚh3)r   r   r   r   r   r	      s    r	   c              O   s   t d| |ƒ d S )NÚh4)r   )ÚargsÚkwargsr   r   r   r
      s    r
   c               C   s   t ‚d S )N)Ú	TypeErrorr   r   r   r   Úraise1   s    r   c               C   s   t ‚d S )N)ÚSystemErrorr   r   r   r   Úraise2   s    r   c               C   s   t ‚d S )N)Ú
SystemExitr   r   r   r   Úexit   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S )ÚGeneralTestc             C   s4   t j| _t j| _t ¡ | _| j t _t _t 	¡  d S )N)
ÚsysÚstdoutÚsave_stdoutÚstderrÚsave_stderrÚioÚStringIOÚstreamÚatexitÚ_clear)Úselfr   r   r   ÚsetUp"   s
    
zGeneralTest.setUpc             C   s   | j t_| jt_t ¡  d S )N)r   r   r   r   r   r   r   )r   r   r   r   ÚtearDown)   s    zGeneralTest.tearDownc             C   sB   t  t¡ t  t¡ t jtddd t  ¡  |  | j ¡ d¡ d S )Né   Úabc)Úkwz"h4 (4,) {'kw': 'abc'}
h4 () {}
h1
)r   Úregisterr   r
   Ú_run_exitfuncsÚassertEqualr   Úgetvalue)r   r   r   r   Ú	test_args.   s    

zGeneralTest.test_argsc             C   s4   t  dd„ dddd„ dD ƒdd¡ |  tt j¡ d S )Nc               S   s   dS )Né   r   r   r   r   r   Ú<lambda>9   ó    z*GeneralTest.test_badargs.<locals>.<lambda>r   c             s   s   | ]
}|V  qd S )Nr   )Ú.0Úxr   r   r   ú	<genexpr>9   s    z+GeneralTest.test_badargs.<locals>.<genexpr>)r)   é   )r   r$   ÚassertRaisesr   r%   )r   r   r   r   Útest_badargs8   s    "zGeneralTest.test_badargsc             C   s<   t  t¡ t  t¡ t  t¡ t  ¡  |  | j ¡ d¡ d S )Nz	h3
h2
h1
)	r   r$   r   r   r	   r%   r&   r   r'   )r   r   r   r   Ú
test_order<   s
    


zGeneralTest.test_orderc             C   s&   t  t¡ t  t¡ |  tt j¡ d S )N)r   r$   r   r   r0   r   r%   )r   r   r   r   Ú
test_raiseE   s    

zGeneralTest.test_raisec             C   s2   t  dd„ ¡ |  tt j¡ |  d| j ¡ ¡ d S )Nc               S   s   dd S )Nr)   r   r   r   r   r   r   r*   O   r+   z5GeneralTest.test_raise_unnormalized.<locals>.<lambda>ÚZeroDivisionError)r   r$   r0   r4   r%   ÚassertInr   r'   )r   r   r   r   Útest_raise_unnormalizedL   s    z#GeneralTest.test_raise_unnormalizedc             C   s.   t  t¡ |  tt j¡ |  | j ¡ d¡ d S )NÚ )	r   r$   r   r0   r   r%   r&   r   r'   )r   r   r   r   Ú	test_exitT   s    
zGeneralTest.test_exitc             C   sˆ   dd„ }dd„ }dd„ }t  |¡ t  |¡ t  |¡ |  tt j¡ | j ¡ }|  | d¡d¡ |  	d	|¡ |  	d
|¡ |  	d|¡ d S )Nc               S   s   dd  d S )Nr)   r   r   r   r   r   r   Úf]   s    z,GeneralTest.test_print_tracebacks.<locals>.fc               S   s   dd  d S )Nr)   r   r   r   r   r   r   Úg_   s    z,GeneralTest.test_print_tracebacks.<locals>.gc               S   s   dd  d S )Nr)   r   r   r   r   r   r   Úha   s    z,GeneralTest.test_print_tracebacks.<locals>.hr4   é   z# onez# twoz# three)
r   r$   r0   r4   r%   r   r'   r&   Úcountr5   )r   r9   r:   r;   r   r   r   r   Útest_print_tracebacks[   s    



z!GeneralTest.test_print_tracebacksc                sJ   dg‰ ‡ fdd„}xt dƒD ]}t |¡ qW t ¡  |  ˆ d d¡ d S )Nr   c                  s   ˆ d  d7  < d S )Nr   r)   r   r   )Úar   r   Úincp   s    z$GeneralTest.test_stress.<locals>.incé€   )Úranger   r$   r%   r&   )r   r@   Úir   )r?   r   Útest_stressn   s    zGeneralTest.test_stressc                s@   dg‰ ‡ fdd„}t  |¡ t  ¡  t  ¡  |  ˆ d d¡ d S )Nr   c                  s   ˆ d  d7  < d S )Nr   r)   r   r   )r?   r   r   r@   {   s    z#GeneralTest.test_clear.<locals>.inc)r   r$   r   r%   r&   )r   r@   r   )r?   r   Ú
test_cleary   s    
zGeneralTest.test_clearc                sj   dg‰ ‡ fdd„}‡ fdd„}xt dƒD ]}t |¡ q(W t |¡ t |¡ t ¡  |  ˆ d d¡ d S )Nr   c                  s   ˆ d  d7  < d S )Nr   r)   r   r   )r?   r   r   r@   †   s    z(GeneralTest.test_unregister.<locals>.incc                  s   ˆ d  d8  < d S )Nr   r)   r   r   )r?   r   r   Údecˆ   s    z(GeneralTest.test_unregister.<locals>.decr!   éÿÿÿÿ)rB   r   r$   Ú
unregisterr%   r&   )r   r@   rF   rC   r   )r?   r   Útest_unregister„   s    

zGeneralTest.test_unregisterc             C   sN   g }t  |jd¡ t  ¡  |  |dg¡ t  |j¡ t  ¡  |  |dg¡ d S )Né   )r   r$   Úappendr%   r&   rH   )r   Úlr   r   r   Útest_bound_methods“   s    zGeneralTest.test_bound_methodsc             C   s:   d}t  d|¡}|  |j ¡  ¡ ddg¡ |  |j¡ d S )Nz­if 1:
            import atexit

            def f(msg):
                print(msg)

            atexit.register(f, "one")
            atexit.register(f, "two")
            z-cZtwoZone)r   Zassert_python_okr&   ÚoutÚdecodeÚ
splitlinesZassertFalseÚerr)r   ÚcodeZresr   r   r   Útest_shutdown   s    
zGeneralTest.test_shutdownN)Ú__name__Ú
__module__Ú__qualname__r   r    r(   r1   r2   r3   r6   r8   r>   rD   rE   rI   rM   rS   r   r   r   r   r       s   
	
r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSubinterpreterTestc             C   s6   t  ¡ }d}t |¡}|  |d¡ |  t  ¡ |¡ d S )NzŒif 1:
            import atexit
            def f():
                pass
            atexit.register(f)
            del atexit
            r   )r   Ú_ncallbacksr   Úrun_in_subinterpr&   )r   ÚnrR   Úretr   r   r   Útest_callbacks_leak°   s
    
z&SubinterpreterTest.test_callbacks_leakc             C   s6   t  ¡ }d}t |¡}|  |d¡ |  t  ¡ |¡ d S )Nzšif 1:
            import atexit
            def f():
                pass
            atexit.register(f)
            atexit.__atexit = atexit
            r   )r   rX   r   rY   r&   )r   rZ   rR   r[   r   r   r   Útest_callbacks_leak_refcycleÀ   s
    
z/SubinterpreterTest.test_callbacks_leak_refcyclec             C   sT   d}t  ¡ \}}d |¡}t |¡}t  |¡ |  t  |t|ƒ¡|¡ t  |¡ d S )Ns   The test has passed!z¸if 1:
            import os
            import atexit
            def callback():
                os.write({:d}, b"The test has passed!")
            atexit.register(callback)
        )	ÚosÚpipeÚformatr   rY   Úcloser&   ÚreadÚlen)r   ZexpectedÚrÚwrR   r[   r   r   r   Ú(test_callback_on_subinterpreter_teardownÏ   s    


z;SubinterpreterTest.test_callback_on_subinterpreter_teardownN)rT   rU   rV   r\   r]   rf   r   r   r   r   rW   ­   s   rW   Ú__main__)r   Zunittestr   r   r^   Ztestr   Ztest.supportr   r   r   r	   r
   r   r   r   ZTestCaser   Zcpython_onlyrW   rT   Úmainr   r   r   r   Ú<module>   s&    
4