3
sY]2                 @   sj   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 G dd deZG dd dejZ	e
dkrfej  dS )    N)BytecodeTestCasec               @   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#S )$TestTranformsc             C   s0   dd }| j |d | j |d | j|d d S )Nc             S   s   | dks
~ d S )N    )xr   r   $lib/python3.6/test/test_peepholer.pyunot   s    z%TestTranforms.test_unot.<locals>.unotZ	UNARY_NOTPOP_JUMP_IF_FALSEZPOP_JUMP_IF_TRUE)assertNotInBytecodeassertInBytecode)selfr   r   r   r   	test_unot   s    zTestTranforms.test_unotc             C   s0   x*dD ]"\}}t |d	d
}| j|d| qW d S )N
not a is bis not
not a in bnot innot a is not bisnot a not in bin singleZ
COMPARE_OPr   r   r   r   r   r   r   r   )r   r   r   r   )compiler   )r   lineZcmp_opcoder   r   r   test_elim_inversion_of_is_or_in   s       z-TestTranforms.test_elim_inversion_of_is_or_inc             C   s|   dd }dd }dd }x<|d f|df|dffD ]"\}}| j |d	 | j|d
| q0W dd }| j |d	 | j|d
d  d S )Nc              S   s   d } d } | S )Nr   )r   r   r   r   f    s    z0TestTranforms.test_global_as_constant.<locals>.fc              S   s   d} | S )NTr   )r   r   r   r   g$   s    z0TestTranforms.test_global_as_constant.<locals>.gc              S   s   d} | S )NFr   )r   r   r   r   h'   s    z0TestTranforms.test_global_as_constant.<locals>.hTFZLOAD_GLOBAL
LOAD_CONSTc               S   s   dS )z1Adding a docstring made this test fail in Py2.5.0Nr   r   r   r   r   r    /   s    )r
   r   )r   r    r!   r"   funcelemr   r   r   test_global_as_constant   s     z%TestTranforms.test_global_as_constantc             C   s@   dd }xdD ]}| j || qW xdD ]}| j|| q(W d S )Nc               S   s
   xqW t S )N)listr   r   r   r   r    8   s    z'TestTranforms.test_while_one.<locals>.fr#   r	   JUMP_ABSOLUTE)r#   r	   )r(   )r
   r   )r   r    r%   r   r   r   test_while_one6   s
    

zTestTranforms.test_while_onec             C   sF   x@dD ]8\}}t |dd}| j|| | j|d	 | j|d
 qW d S )Na, = a,r#   a, b = a, bROT_TWOa, b, c = a, b, c	ROT_THREEr   r   BUILD_TUPLEZUNPACK_TUPLEr*   r#   r+   r,   r-   r.   )r0   r1   r2   )r   r   r
   )r   r   r%   r   r   r   r   test_pack_unpackA   s      zTestTranforms.test_pack_unpackc             C   s   x6d!D ].\}}t |dd}| j|d| | j|d qW t tttddd}| j|d dd tj|D }| jt	|d dd }d S )"N	a = 1,2,3   r      ("a","b","c")abca,b,c = 1,2,3(None, 1, None)((1, 2), 3, 4)   r   r   r#   r/   i'  c             S   s   g | ]}|j d kr|qS )r#   )opname).0instrr   r   r   
<listcomp>\   s    zETestTranforms.test_folding_of_tuples_of_constants.<locals>.<listcomp>c            d   S   s   ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
ddddddddd	d
gd f d S )Nr   r5   r   r6   r>               	   r   r   r   r   r   craterc   s    zATestTranforms.test_folding_of_tuples_of_constants.<locals>.craterr5   r   r6   r4   rI   r8   r9   r:   r7   rK   r5   r   r6   r;   rM   Nr5   Nr<   rO   r5   r   rQ   r6   r>   r=   rR   )rJ   rL   rN   rP   rS   )
r   r   r
   reprtuplerangedisget_instructionsassertEquallen)r   r   r%   r   Zload_constsrH   r   r   r   #test_folding_of_tuples_of_constantsL   s        z1TestTranforms.test_folding_of_tuples_of_constantsc             C   s<   x6dD ].\}}t |dd}| j|d| | j|d qW d S )Na in [1,2,3]r5   r   r6   a not in ["a","b","c"]r8   r9   r:   a in [None, 1, None]a not in [(1, 2), 3, 4]r>   r   r   r#   Z
BUILD_LISTr5   r   r6   r\   r`   r8   r9   r:   r]   rb   Nr5   Nr^   rd   r5   r   rf   r6   r>   r_   rg   )ra   rc   re   rh   )r   r   r
   )r   r   r%   r   r   r   r   "test_folding_of_lists_of_constantsq   s       z0TestTranforms.test_folding_of_lists_of_constantsc          	   C   s   xdt dddhfdt dddhfd	t dd hfd
t dddhfdt dddhffD ].\}}t|dd}| j|d | j|d| qTW dd }dd }| j|d | j|d  | j|d  | j|d d S )Nza in {1,2,3}r5   r   r6   za not in {"a","b","c"}r8   r:   r9   za in {None, 1, None}za not in {(1, 2), 3, 4}r>   za in {1, 2, 3, 3, 2, 1}r   r   Z	BUILD_SETr#   c             S   s   | dkS )Nr5   r   r6   >   r5   r   r6   r   )r8   r   r   r   r       s    z:TestTranforms.test_folding_of_sets_of_constants.<locals>.fc             S   s   | dkS )Nr5   r   r6   >   r5   r   r6   r   )r8   r   r   r   r!      s    z:TestTranforms.test_folding_of_sets_of_constants.<locals>.g)r5   r   )	frozensetr   r
   r   Z
assertTrue)r   r   r%   r   r    r!   r   r   r   !test_folding_of_sets_of_constants}   s    z/TestTranforms.test_folding_of_sets_of_constantsc             C   s  xPd8D ]H\}}t |dd}| j|d| x$tj|D ]}| j|jjd q4W qW t d dd}| j|dd | j|dd! t d"dd}| j|dd# | jd$d# |j t d%dd}| j|dd& | jd'd&> |j t d(dd}| j|dd& | jdd& |j d S )9N	a = 2+3+4rG   "@"*4@@@@a="abc" + "def"abcdefa = 3**4Q   a = 3*4   	a = 13//4r6   a = 14%4r   a = 2+3rC   a = 13-4a = (12,13)[1]   a = 13 << 24   a = 13 >> 2
a = 13 & 7
a = 13 ^ 7
   
a = 13 | 7   r   r   r#   BINARY_za=2+"b"r9   za="x"*10000i'  r   z	a=1<<1000i  r5   z	a=2**1000rl   rG   rm   rn   ro   rp   rq   rr   rs   rt   ru   r6   rv   r   rw   rC   rx   rG   ry   rz   r{   r|   r}   r6   r~   rC   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )	r   r   rW   rX   assertFalser?   
startswithZassertNotIn	co_consts)r   r   r%   r   rA   r   r   r   #test_folding_of_binops_on_constants   s@                  z1TestTranforms.test_folding_of_binops_on_constantsc             C   s   t ddd}| j|dd | j|d t ddd}| j|dd | j|d t d	dd}| j|dd
 | j|d t ddd}| j|d d S )Nz"foo"[0]r   r   r#   r    ZBINARY_SUBSCRu	   "a￿"[1]u   ￿u	   "𒍅"[0]u   𒍅z	"fuu"[10])r   r   r
   )r   r   r   r   r   test_binary_subscr_on_unicode   s    z+TestTranforms.test_binary_subscr_on_unicodec             C   s   xPdD ]H\}}t |dd}| j|d| x$tj|D ]}| j|jjd q4W qW dd }x$tj|D ]}| j|jjd qfW x8d"D ]0\}}}t |dd}| j|d| | j|| qW d S )#N-0.5g      ?-0.0g        
-(1.0-1.0)-0r   ~-2r5   +1r   r   r#   UNARY_c               S   s   dS )Ng      ?g        g       r   r   r   r   r   negzero   s    zDTestTranforms.test_folding_of_unaryops_on_constants.<locals>.negzero-"abc"abcUNARY_NEGATIVE~"abc"UNARY_INVERT      ࿩r   r          r   r          r   r   r   r   r   r5   r   r5   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r   r   rW   rX   r   r?   r   )r   r   r%   r   rA   r   r?   r   r   r   %test_folding_of_unaryops_on_constants   s(          z3TestTranforms.test_folding_of_unaryops_on_constantsc             C   s>   dd }| j |dd  dd tj|D }| jt|d d S )Nc             S   s   | S )Nr   )r   r   r   r   r       s    z/TestTranforms.test_elim_extra_return.<locals>.fr#   c             S   s   g | ]}|j d kr|qS )RETURN_VALUE)r?   )r@   rA   r   r   r   rB      s    z8TestTranforms.test_elim_extra_return.<locals>.<listcomp>r5   )r
   rW   rX   rY   rZ   )r   r    returnsr   r   r   test_elim_extra_return   s    z$TestTranforms.test_elim_extra_returnc             C   sH   dd }| j |d | j |d dd tj|D }| jt|d d S )Nc             S   s   | r|S |S )Nr   )ZcondZ
true_valueZfalse_valuer   r   r   r       s    z1TestTranforms.test_elim_jump_to_return.<locals>.fJUMP_FORWARDr(   c             S   s   g | ]}|j d kr|qS )r   )r?   )r@   rA   r   r   r   rB      s    z:TestTranforms.test_elim_jump_to_return.<locals>.<listcomp>r   )r
   rW   rX   rY   rZ   )r   r    r   r   r   r   test_elim_jump_to_return   s
    z&TestTranforms.test_elim_jump_to_returnc             C   sH   dd }| j |d | j |d dd tj|D }| jt|d d S )Nc             S   s(   | rdS |rdS xdS x| r dS dS dS )Nr5   r   r6   r>   rC   rD   r   )cond1cond2r   r   r   r      s       z5TestTranforms.test_elim_jump_after_return1.<locals>.fr   r(   c             S   s   g | ]}|j d kr|qS )r   )r?   )r@   rA   r   r   r   rB     s    z>TestTranforms.test_elim_jump_after_return1.<locals>.<listcomp>rD   )r
   rW   rX   rY   rZ   )r   r    r   r   r   r   test_elim_jump_after_return1  s
    	z*TestTranforms.test_elim_jump_after_return1c             C   s`   dd }| j |d dd tj|D }| jt|d dd tj|D }| jt|d d S )	Nc             S   s   x| rdS qW d S )Nr>   r   )r   r   r   r   r   r      s     z5TestTranforms.test_elim_jump_after_return2.<locals>.fr   c             S   s   g | ]}|j d kr|qS )r(   )r?   )r@   rA   r   r   r   rB     s    z>TestTranforms.test_elim_jump_after_return2.<locals>.<listcomp>r5   c             S   s   g | ]}|j d kr|qS )r   )r?   )r@   rA   r   r   r   rB     s    r   )r
   rW   rX   rY   rZ   )r   r    r   r   r   r   test_elim_jump_after_return2  s    z*TestTranforms.test_elim_jump_after_return2c             C   s   dd }| j |d d S )Nc              S   s   dddd} | S )Nr5   )returnc               S   s   d S )Nr   r   r   r   r   r!   $  s    zBTestTranforms.test_make_function_doesnt_bail.<locals>.f.<locals>.gr   r   )r!   r   r   r   r    #  s    z7TestTranforms.test_make_function_doesnt_bail.<locals>.fZ
BINARY_ADD)r
   )r   r    r   r   r   test_make_function_doesnt_bail"  s    z,TestTranforms.test_make_function_doesnt_bailc          	   C   s~   ddddddddd	g	}xb|D ]Z}t |d
d}xHtj|D ]:}| j|jjd | j|jjd | j|jjd q8W qW d S )Nz3 * -5z-3 * 5z2 * (3 * 4)z(2 * 3) * 4z
(-1, 2, 3)z
(1, -2, 3)z
(1, 2, -3)z(1, 2, -3) * 6z:lambda x: x in {(3 * -5) + (-1 - 6), (1, -2, 3) * 2, None}r   r   r   r   ZBUILD_)r   rW   rX   r   r?   r   )r   Zexprser   rA   r   r   r   test_constant_folding)  s    
z#TestTranforms.test_constant_foldingN)__name__
__module____qualname__r   r   r&   r)   r3   r[   ri   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	   s"   	
%'	
r   c               @   s   e Zd Zdd ZdS )TestBugletsc          	   C   s(   dd }| j t |  W d Q R X d S )Nc              S   s   ddh\} }| |fS )Nr5   r   )r   yr   r   r   r    D  s    z%TestBuglets.test_bug_11510.<locals>.f)ZassertRaises
ValueError)r   r    r   r   r   test_bug_11510@  s    zTestBuglets.test_bug_11510N)r   r   r   r   r   r   r   r   r   >  s   r   __main__)rW   resystextwrapZunittestZtest.bytecode_helperr   r   ZTestCaser   r   mainr   r   r   r   <module>   s     7