B
    u9a              (   @   s4  U d dl mZ d dlZd dlZd dlZd dlT d dlmZ d dlZd dlm	Z	 d dl
mZ d dl
Z
dddd	d
ddddddddddddddddgZdddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBg(ZG dCdD dDejZeedE< G dFdG dGZG dHdI dIejZedJkr0e  dS )K    )check_syntax_errorN)*)ChainMap)ann_module2Z0_0_0Z4_2Z1_0000_0000Z0b1001_0100Z0xffff_ffffZ0o5_7_7z	1_00_00.5z1_00_00.5e5Z1_00_00e5_1Z1e1_0z.1_4z.1_4e1Z0b_0Z0x_fZ0o_5Z1_00_00jz
1_00_00.5jZ1_00_00e5_1jz.1_4jz(1_2.5+3_3j)z(.5_6j)Z0_Z42_z1.4j_Z0x_Z0b1_Z0xf_Z0o5_z0 if 1_Else 1Z0_b0Z0_xfZ0_o5Z0_7Z09_99Z	4_______2z0.1__4z0.1__4jZ0b1001__0100Z0xffff__ffffZ0x___Z0o5__77Z1e1__0Z1e1__0jz1_.4z1_.4jz1._4z1._4jz._5z._5jz1.0e+_1z1.0e+_1jz1.4_jz1.4e5_jZ1_e1z1.4_e1z1.4_e1jZ1e_1z1.4e_1z1.4e_1jz
(1+1.5_j_)z	(1+1.5_j)c               @   sT   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S )
TokenTestsc             C   s(   d}|  |dd d}|  |dd d S )N   zbackslash for line continuationr   zbackslash ending comment)assertEqual)selfx r   "/usr/lib/python3.7/test_grammar.pytest_backslashg   s    zTokenTests.test_backslashc          	   C   sr  |  tdtd |  dd |  dd |  dd |  dd | ttd ddlm} |dkr|  dd | ddk | ddk | ddk xd	D ]4}yt|}W q tk
r   | 	d
|  Y qX qW n|dkr`|  dd | ddk | ddk | ddk xPdD ]8}yt|}W n$ tk
rV   | 	d
|  Y nX q"W n| 	d|  d S )Nr      i	   0x)maxsizei   l    )Z
2147483648Z0o40000000000Z0x100000000Z"0b10000000000000000000000000000000z(OverflowError on huge integer literal %rl    l         l    l    )Z9223372036854775808Z0o2000000000000000000000Z0x10000000000000000ZA0b100000000000000000000000000000000000000000000000000000000000000zWeird maxsize value %r)
r   typeassertRaisesSyntaxErrorevalsysr   
assertTrueOverflowErrorfail)r	   r   sr
   r   r   r   test_plain_integersq   s8    


zTokenTests.test_plain_integersc             C   s$   d}d}d}d}d}d}d}d}d S )Nr   l    l   ? l   
~l!]c l            l   r   )r	   r
   r   r   r   test_long_integers   s    zTokenTests.test_long_integersc             C   s0   d}d}d}d}d}d}d}d}d}d}d}d S )NgQ	@g     s@gjt?g  Bgt =g  WHBg     F@r   )r	   r
   r   r   r   test_floats   s    zTokenTests.test_floatsc             C   s6   |  dndd |  drdndd | ttd d S )N   r   z0 if 1Else 0)r   r   r   r   )r	   r   r   r    test_float_exponent_tokenization   s    z+TokenTests.test_float_exponent_tokenizationc          	   C   sX   x(t D ] }| t|t|dd qW xtD ]}| tt| q0W | ttd d S )N_ Z_0)VALID_UNDERSCORE_LITERALSr   r   replaceINVALID_UNDERSCORE_LITERALSr   r   	NameError)r	   Zlitr   r   r   test_underscore_literals   s
    
 
z#TokenTests.test_underscore_literalsc             C   s
  d}d}|  t|dko||k d}d}|  t|dkoL||koLt|dk d}d}|  t|dkoz||kozt|dk d}d}|  t|d	ko||k d
}d
}|  t|d	ko||k d}d}| || d}| || d}| || d}| || d S )Nr!   r   'r   '   ""   zdoesn't "shrink" does it   zdoes "shrink" doesn't itz2
The "quick"
brown fox
jumps over
the 'lazy' dog.
)r   lenordr   )r	   r
   yr   r   r   test_string_literals   s0        &  &zTokenTests.test_string_literalsc             C   s$   d}|  |tk | ttd d S )N.z.. .)r   Ellipsisr   r   r   )r	   r
   r   r   r   test_ellipsis   s    zTokenTests.test_ellipsisc          
   C   sJ   d}x@|D ]8}|  t}t|dd W d Q R X | dt|j q
W d S )N)zdef foo(z	
def foo(z	def foo(
z<test>execzunexpected EOF)r   r   compileZassertInstrZ	exception)r	   Zsamplesr   cmr   r   r   test_eof_error   s
    
zTokenTests.test_eof_errorN)__name__
__module____qualname__r   r   r   r   r   r&   r/   r1   r6   r   r   r   r   r   e   s   
$
(r   Zvar_annot_globalc               @   s$   e Zd Zdd Zdd Zdd ZdS )CNSc             C   s
   i | _ d S )N)_dct)r	   r   r   r   __init__   s    zCNS.__init__c             C   s   || j | < d S )N)r;   lower)r	   itemvaluer   r   r   __setitem__   s    zCNS.__setitem__c             C   s
   | j | S )N)r;   )r	   r>   r   r   r   __getitem__   s    zCNS.__getitem__N)r7   r8   r9   r<   r@   rA   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%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z e!"d=d>d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8dkdl Z9dmdn Z:dodp Z;dqdr Z<dsdt Z=duS )vGrammarTestsc             C   s   t d}d S )Nz	1, 0 or 1)r   )r	   r
   r   r   r   test_eval_input  s    zGrammarTests.test_eval_inputc             C   s@   d}dg}dd }t  tg d d|| d < | |dg d S )N   *   c               S   s   dS )Nr   r   r   r   r   r   one  s    z/GrammarTests.test_var_annot_basics.<locals>.oner   r   )intlistr   )r	   Zvar1Zmy_lstrF   r   r   r   test_var_annot_basics  s    
z"GrammarTests.test_var_annot_basicsc             C   s^   t | d t | d t | d t | d t | d t | d t | d t | d t | d	 d S )
Nz
def f: intzx: int: strzdef f():
    nonlocal x: int
z[x, 0]: int
z	f(): int
z	(x,): intz"def f():
    (x, y): int = (1, 2)
z!def f():
    x: int
    global x
z!def f():
    global x
    x: int
)r   )r	   r   r   r   test_var_annot_syntax_errors  s    







z)GrammarTests.test_var_annot_syntax_errorsc       	   
   C   s>  |  t dd tt< W d Q R X |  t dtt< W d Q R X dd }| |ji  dd }|  dd }|  t |  W d Q R X d	d
 }y
|  W n2 tk
r } z| 	t
|t W d d }~X Y nX G dd d}| |jttd |  t G dd d}W d Q R X |  t G dd d|}W d Q R X d S )Nr   r   c              S   s   d} dt _| S )NZHello)r   r   )ab)str   r   r   f4  s    z6GrammarTests.test_var_annot_basic_semantics.<locals>.fc               S   s   d S )Nr   r   r   r   r   f_OK9  s    z9GrammarTests.test_var_annot_basic_semantics.<locals>.f_OKc              S   s   t |  d S )N)print)r
   r   r   r   fbad<  s    z9GrammarTests.test_var_annot_basic_semantics.<locals>.fbadc               S   s   t t d S )N)rP   Zno_such_globalr   r   r   r   f2badA  s    z:GrammarTests.test_var_annot_basic_semantics.<locals>.f2badc               @   s.   e Zd ZU eed< dZeed< dZdd ZdS )z6GrammarTests.test_var_annot_basic_semantics.<locals>.C_C__fooattrr   r   c             S   s
   || _ d S )N)r
   )r	   r
   r   r   r   r<   N  s    z?GrammarTests.test_var_annot_basic_semantics.<locals>.C.__init__N)	r7   r8   r9   rG   __annotations__r   r4   zr<   r   r   r   r   CJ  s   
rW   )rS   r   c               @   s   e Zd ZU de_e dS )z9GrammarTests.test_var_annot_basic_semantics.<locals>.CBadr   N)r7   r8   r9   Zno_such_name_definedrT   rG   r   r   r   r   CBadR  s   
rX   c               @   s    e Zd ZU eed< g e_e dS )z:GrammarTests.test_var_annot_basic_semantics.<locals>.Cbad2r
   N)r7   r8   r9   rG   rU   r
   r.   rH   r   r   r   r   Cbad2U  s   
rY   )r   ZeroDivisionErrorZno_nameZdoes_not_existr%   r   rU   UnboundLocalError	ExceptionZassertIsr   rG   r4   )	r	   rN   rO   rQ   rR   erW   rX   rY   r   r   r   test_var_annot_basic_semantics+  s,    
"z+GrammarTests.test_var_annot_basic_semanticsc             C   s8   G dd dt }G dd d|d}| |jd d d S )Nc               @   s   e Zd Zedd ZdS )z>GrammarTests.test_var_annot_metaclass_semantics.<locals>.CMetac             [   s
   dt  iS )NrU   )r:   )metaclsnamebaseskwdsr   r   r   __prepare__[  s    zJGrammarTests.test_var_annot_metaclass_semantics.<locals>.CMeta.__prepare__N)r7   r8   r9   classmethodrc   r   r   r   r   CMetaZ  s   re   c               @   s   e Zd ZU ded< dS )z;GrammarTests.test_var_annot_metaclass_semantics.<locals>.CCANNOTZXXN)r7   r8   r9   rU   r   r   r   r   CC^  s   
rg   )	metaclassZxxrf   )r   r   rU   )r	   re   rg   r   r   r   "test_var_annot_metaclass_semanticsY  s    z/GrammarTests.test_var_annot_metaclass_semanticsc          	   C   sj   |  t ttj W d Q R X | tjdttt	j
ttf d | tjjdtd | tji  d S )Nr   )r   r
   r.   rN   {   )Z123o)r   AttributeErrorrP   testrU   r   
ann_modulerG   r4   typingZTupleMr   r   )r	   r   r   r   test_var_annot_module_semanticsb  s    
z,GrammarTests.test_var_annot_module_semanticsc          	   C   sn   ddl m}m}m} | t |  W d Q R X | t |  W d Q R X | t |d W d Q R X d S )Nr   )	f_bad_ann	g_bad_ann	D_bad_annrD   )Ztest.ann_module3rr   rs   rt   r   r%   )r	   rr   rs   rt   r   r   r   test_var_annot_in_modulek  s    z%GrammarTests.test_var_annot_in_modulec          	   C   sL   i }i }t d|| | |d dtd | t |d  W d Q R X d S )Nz.'docstring'
__annotations__[1] = 2
x: int = 5
rU   r   )r   r
   )r2   r   rG   r   KeyError)r	   ZgnsZlnsr   r   r   test_var_annot_simple_execv  s     z'GrammarTests.test_var_annot_simple_execc          	      s   dt  i}td| | |d d t | |d d t | t |d d  W d Q R X i  G  fddd}tdi |  |  d d t d S )	NrU   z'X: int; Z: str = "Z"; (w): complex = 1jr
   rV   wc                   s(   e Zd Zdd Z fddZdd ZdS )z5GrammarTests.test_var_annot_custom_maps.<locals>.CNS2c             S   s
   i | _ d S )N)r;   )r	   r   r   r   r<     s    z>GrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__init__c                s   || j |< | |< d S )N)r;   )r	   r>   r?   )	nonloc_nsr   r   r@     s    
zAGrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__setitem__c             S   s
   | j | S )N)r;   )r	   r>   r   r   r   rA     s    zAGrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__getitem__N)r7   r8   r9   r<   r@   rA   r   )ry   r   r   CNS2  s   rz   z
x: int = 1)r:   r2   r   rG   r4   r   rv   )r	   nsrz   r   )ry   r   test_var_annot_custom_maps  s    

	z'GrammarTests.test_var_annot_custom_mapsc                sH   t   d iG  fddd}tdi |  | d d t d S )NrU   c                   s,   e Zd Z fddZfddZdd ZdS )z1GrammarTests.test_var_annot_refleak.<locals>.CNS2c                s   d i| _ d S )NrU   )r;   )r	   )cnsr   r   r<     s    z:GrammarTests.test_var_annot_refleak.<locals>.CNS2.__init__c                s   || j |< | |< d S )N)r;   )r	   r>   r?   )ry   r   r   r@     s    
z=GrammarTests.test_var_annot_refleak.<locals>.CNS2.__setitem__c             S   s
   | j | S )N)r;   )r	   r>   r   r   r   rA     s    z=GrammarTests.test_var_annot_refleak.<locals>.CNS2.__getitem__N)r7   r8   r9   r<   r@   rA   r   )r}   ry   r   r   rz     s   rz   zX: strr
   )r:   r2   r   r4   )r	   rz   r   )r}   ry   r   test_var_annot_refleak  s
    	z#GrammarTests.test_var_annot_refleakc                s  dd }|  |d  |di  dd }dd }|  |jjd |  |jj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
 ddd}	|	  |	d |	d   |	g pdg  |	dpdi oddpi  |	f d!di |	f d!dipi  dd"d#}
|
d |
dd |
dd$di dd%d&}|dd |ddd |d'  |dd(  |dd)  |dd*di dd+d,}|  |d |dd |d-  |dd.  |dd$di |f ddd/ dd0d1}|d |dd |ddd dd2d3}|dd |ddd |dddd dd4d5}|  |d |dd |d6  |d   |f d!di dd7d8}|d |dd |ddd dd9d:}|dd |ddd |dddd |d6  |dd*di dd;d<}|  |d |dd |ddd |dd=  |f ddd/ dd>d?}|d |dd |ddd |dddd |d6  |dd@  |dd.d*di ddAdB}|dd |ddd |dddd |ddddd |d6  |dd@  |dd(dCdi ytddEdi W n tk
r   Y nX | dF dGdH }|dIdJ dIdKdLdM}|dddIdN |dddIdOdP |dddIdOdQ dIdNdRdS}|dddIdIdOdT |dddIdOdIdU | ttdV | ttdW | ttdX dYdZ }|  |dddgddd[d\ddd[f |  |dd(d d6i f | ttd] |  |f d^d_d`dd^d_d`f |  |f dad_idbd^idd^d_d`f t	| dc t	| dd t
dedfdZ}|  |jdgt
i tdhdidZ}|  |jdDti tdhdjdZ}|  |jdDti tdhdkdZ}|  |jdDti ddldmdZ}|  |jdndi dddodpdZ}|  |jdddo ddddddqdrdZ}|  |jdddddq dddsdtdddddddudvdwdxdZ}|  |jdddddddudvdw G dydz dz}G d{d| d||}|  |jjd}di |  |jjd}di d~d }|t
deddZ}|  |jdgt
i d  fddZ}d fddZ	}dd fddZ
}tde fddZ}ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }ddZ }d S )Nc               S   s   d S )Nr   r   r   r   r   f1      z%GrammarTests.test_funcdef.<locals>.f1r   c             S   s   d S )Nr   )one_argumentr   r   r   f2  r   z%GrammarTests.test_funcdef.<locals>.f2c             S   s   d S )Nr   )two	argumentsr   r   r   f3  r   z%GrammarTests.test_funcdef.<locals>.f3)r   )r   r   c             S   s   d S )Nr   )Zone_argr   r   r   a1  r   z%GrammarTests.test_funcdef.<locals>.a1c             S   s   d S )Nr   )r   argsr   r   r   a2  r   z%GrammarTests.test_funcdef.<locals>.a2c              W   s   d S )Nr   )restr   r   r   v0  r   z%GrammarTests.test_funcdef.<locals>.v0c             W   s   d S )Nr   )rK   r   r   r   r   v1  r   z%GrammarTests.test_funcdef.<locals>.v1c             W   s   d S )Nr   )rK   rL   r   r   r   r   v2  r   z%GrammarTests.test_funcdef.<locals>.v2r   r         rD            r   r   c             S   s   d S )Nr   )rK   r   r   r   d01  r   z&GrammarTests.test_funcdef.<locals>.d01)r   rK   c             S   s   d S )Nr   )rK   rL   r   r   r   d11  r   z&GrammarTests.test_funcdef.<locals>.d11rL   c             S   s   d S )Nr   )rK   rL   cr   r   r   d21  r   z&GrammarTests.test_funcdef.<locals>.d21)r   r   r   )r   r   )r   r   c             S   s   d S )Nr   )rK   rL   r   r   r   d02  r   z&GrammarTests.test_funcdef.<locals>.d02)r   r   )r   )rK   rL   c             S   s   d S )Nr   )rK   rL   r   r   r   r   d12  r   z&GrammarTests.test_funcdef.<locals>.d12c             S   s   d S )Nr   )rK   rL   r   dr   r   r   d22  r   z&GrammarTests.test_funcdef.<locals>.d22c             W   s   d S )Nr   )rK   r   r   r   r   d01v  r   z'GrammarTests.test_funcdef.<locals>.d01v)r   r   r   r   c             W   s   d S )Nr   )rK   rL   r   r   r   r   d11v  r   z'GrammarTests.test_funcdef.<locals>.d11vc             W   s   d S )Nr   )rK   rL   r   r   r   r   r   d21v  r   z'GrammarTests.test_funcdef.<locals>.d21vc             W   s   d S )Nr   )rK   rL   r   r   r   r   d02v  r   z'GrammarTests.test_funcdef.<locals>.d02v)r   r   r   c             W   s   d S )Nr   )rK   rL   r   r   r   r   r   d12v  r   z'GrammarTests.test_funcdef.<locals>.d12v)r   r   rD   c             W   s   d S )Nr   )rK   rL   r   r   r   r   r   r   d22v  r   z'GrammarTests.test_funcdef.<locals>.d22vr   r
   s   fooz/Bytes should not work as keyword argument namesc             S   s   | S )Nr   )keyr   r   r   pos0key1(  r   z+GrammarTests.test_funcdef.<locals>.pos0key1d   )r   )k2c            S   s   | |||fS )Nr   )p1p2k1r   r   r   r   pos2key2*  r   z+GrammarTests.test_funcdef.<locals>.pos2key2)r      )r   r   )r   r   c            [   s   | ||||fS )Nr   )r   r   r   r   Zkwargr   r   r   pos2key2dict.  r   z/GrammarTests.test_funcdef.<locals>.pos2key2dict)r   tokwarg1tokwarg2)r   r   r   zdef f(*): passzdef f(*,): passzdef f(*, **kwds): passc              _   s   | |fS )Nr   )r   kwargsr   r   r   rN   7  s    z$GrammarTests.test_funcdef.<locals>.f)r
   r.   )r   r   r   zf(1, x=2, *(3,4), x=5)Z	scrambledZfried)eggsspamr   r   z
f(*g(1=2))zf(**g(1=2)))returnc             S   s   d S )Nr   )r
   r   r   r   rN   G  r   r   )r
   c             S   s   d S )Nr   )r
   r   r   r   rN   I  r   c              W   s   d S )Nr   )r
   r   r   r   rN   K  r   c              [   s   d S )Nr   )r
   r   r   r   rN   M  r   )r.   c             S   s   d S )Nr   )r
   r.   r   r   r   rN   O  r   r.   )rL   r   c             S   s   d S )Nr   )rK   rL   r   r   r   r   r   rN   Q  r   )rL   r   r]   gc             W   s   d S )Nr   )rK   rL   r   r   r]   rN   r   r   r   r   rN   S  r   
   )ij      )rL   r   r]   r   hr   kr   c            _   s   d S )Nr   )rK   rL   r   r   r]   rN   r   r   r   r   r   r   r   r   rN   V  s    c               @   s   e Zd ZddddZdS )z'GrammarTests.test_funcdef.<locals>.Spamr   )	_Spam__kwc            S   s   d S )Nr   )r	   r   r   r   r   rN   ]  s    z)GrammarTests.test_funcdef.<locals>.Spam.fN)r7   r8   r9   rN   r   r   r   r   Spam\  s   r   c               @   s   e Zd ZdS )z&GrammarTests.test_funcdef.<locals>.HamN)r7   r8   r9   r   r   r   r   Ham_  s    r   r   c             S   s   | S )Nr   )r
   r   r   r   nullc  r   z'GrammarTests.test_funcdef.<locals>.nullc             S   s   d S )Nr   )r
   r   r   r   rN   d  s    c                  s    S )Nr   r   )closurer   r   rN   j  r   c                s    S )Nr   )r
   )r   r   r   rN   k  r   )r   c                s    S )Nr   )r   )r   r   r   rN   l  r   c                  s    S )Nr   r   )r   r   r   rN   m  r   c             S   s   d S )Nr   )rK   r   r   r   rN   p  r   c              W   s   d S )Nr   )r   r   r   r   rN   q  r   c              [   s   d S )Nr   )rb   r   r   r   rN   r  r   c             W   s   d S )Nr   )rK   r   r   r   r   rN   s  r   c             [   s   d S )Nr   )rK   rb   r   r   r   rN   t  r   c             W   s   d S )Nr   )rL   r   r   r   r   rN   u  r   c             S   s   d S )Nr   )rL   r   r   r   rN   v  r   c              _   s   d S )Nr   )r   rb   r   r   r   rN   w  r   c            W   s   d S )Nr   )rK   rL   r   r   r   r   rN   x  r   c            S   s   d S )Nr   )rK   rL   r   r   r   rN   y  r   c             _   s   d S )Nr   )rK   r   rb   r   r   r   rN   z  r   c             _   s   d S )Nr   )rL   r   rb   r   r   r   rN   {  r   c             [   s   d S )Nr   )rL   rb   r   r   r   rN   |  r   c            _   s   d S )Nr   )rK   rL   r   rb   r   r   r   rN   }  r   c            [   s   d S )Nr   )rK   rL   rb   r   r   r   rN   ~  r   )r   )r   )r   )r   )r   )r   r   )r   r   )r   r   )r   )r   )r   r   )r   r   )r   )r   )r   )r   r   )r   r   )r   )r   r   )r   r   )r   )r   r   )r   r   )r   )r
   )r   )r   )r   )r   rD   )r   rD   )r   )r   __code__co_varnamesr4   	TypeErrorr   r   r   r   r   rH   rU   rG   floatrN   )r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rN   r   r   r   r   )r   r   test_funcdef  sn   






 

























zGrammarTests.test_funcdefc             C   sX  dd }|  | d dd }dd }|  | dddg d&ddfddfd	d}|  | d d'dd}|  |dd
d |  |dd
dd t| d t| 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 S )(Nc               S   s   dS )Nr   r   r   r   r   r   <lambda>  r   z+GrammarTests.test_lambdef.<locals>.<lambda>r   c               S   s   t t S )N)rK   r   r   r   r   r   r     r   c               S   s   dd dD S )Nc             S   s   g | ]}d |k qS )r   r   ).0r
   r   r   r   
<listcomp>  s    z?GrammarTests.test_lambdef.<locals>.<lambda>.<locals>.<listcomp>)r   r   r   r   r   r   r   r     r   r   c             S   s   | S )Nr   )rV   r   r   r   r     r   c             S   s   |  S )Nr   )r.   r   r   r   r     r   c             S   s   |  S )Nr   )r
   r   r   r   r     r   r   c             S   s   | | | S )Nr   )r
   r.   rV   r   r   r   r     r   rD   r   r   zlambda x: x = 2zlambda (None,): None   )r   c            S   s   | | | S )Nr   )r
   r.   r   r   r   r   r     r      r      c             S   s   dS )Nr   r   )rK   r   r   r   r     r   c              W   s   dS )Nr   r   )r   r   r   r   r     r   c              [   s   dS )Nr   r   )rb   r   r   r   r     r   c             W   s   dS )Nr   r   )rK   r   r   r   r   r     r   c             [   s   dS )Nr   r   )rK   rb   r   r   r   r     r   c             W   s   dS )Nr   r   )rL   r   r   r   r   r     r   c             S   s   dS )Nr   r   )rL   r   r   r   r     r   c              _   s   dS )Nr   r   )r   rb   r   r   r   r     r   c            W   s   dS )Nr   r   )rK   rL   r   r   r   r   r     r   c            S   s   dS )Nr   r   )rK   rL   r   r   r   r     r   c             _   s   dS )Nr   r   )rK   r   rb   r   r   r   r     r   c             _   s   dS )Nr   r   )rL   r   rb   r   r   r   r     r   c             [   s   dS )Nr   r   )rL   rb   r   r   r   r     r   c            _   s   dS )Nr   r   )rK   rL   r   rb   r   r   r   r     r   c            [   s   dS )Nr   r   )rK   rL   rb   r   r   r   r     r   )r   )r   )r   r   )r	   l1l2l3l4l5l6l10Zl11Zl12Zl13Zl14Zl15Zl16Zl17Zl18Zl19Zl20Zl21Zl22Zl23Zl24r   r   r   test_lambdef  s<    


zGrammarTests.test_lambdefc             C   s   d}~dd }|  d S )Nr   c              S   s
   d} ~ d S )Nr   r   )r
   r   r   r   foo  s     z*GrammarTests.test_simple_stmt.<locals>.foor   )r	   r
   r   r   r   r   test_simple_stmt  s     zGrammarTests.test_simple_stmtc       	      C   sR   d}d}d } }}d\}}}d } \}}} \}}}}t | d t | d d S )Nr   )r   r   r   )r   r   )r   r   z	x + 1 = 1za + 1 = b + 2)r   )	r	   r
   r.   rV   abcrK   rL   r   xyzr   r   r   test_expr_stmt  s    

zGrammarTests.test_expr_stmtc             C   s   d}ddddddg}x|D ]}d |}x|D ]}| |}| j|d	& | t| t| W d Q R X W d Q R X |d
d}| j|d	& | td t| W d Q R X W d Q R X q.W qW d S )N)rP   r2   z{} fooz{} {{1:foo}}zif 1: {} foozif 1: {} {{1:foo}}zif 1:
    {} foozif 1:
    {} {{1:foo}}zcall to '{}')sourcer   z(foo.)zinvalid syntax)formatZsubTestassertRaisesRegexr   r2   r#   )r	   keywordsZcaseskeywordZ
custom_msgZcaser   r   r   r   (test_former_statements_refer_to_builtins  s$    



z5GrammarTests.test_former_statements_refer_to_builtinsc             C   s,   dddg}|\}}}|||f}~~~~~d S )Nr   r   r   r   )r	   r   r
   r.   rV   r   r   r   r   test_del_stmt  s
    


zGrammarTests.test_del_stmtc             C   s   d S )Nr   )r	   r   r   r   test_pass_stmt  s    zGrammarTests.test_pass_stmtc             C   s   xP qW d S )Nr   )r	   r   r   r   test_break_stmt  s     zGrammarTests.test_break_stmtc             C   s   d}x|rd}qqW d}x(|s@d}y
wd}W q   d}Y qX qW |dkrT|  | d}x|srd}zwZW d d}X qZW |dkr|  | d S )Nr   r   r!   okz&continue failed to continue inside tryz'continue inside try called except blockzfinally block not called)r   )r	   r   msgr   r   r   test_continue_stmt  s,      

zGrammarTests.test_continue_stmtc                s   d fdd	}|  d S )Nr   r   c                sh   d}xD|rH|d7 }y$| r*|dkr*| d8 } P |d8 }wW q    Y qX qW |dksZ|dkrd  d d S )Nr   r   z1continue then break in try/except in loop broken!)r   )Zextra_burning_oilcountZ	big_hippo)r	   r   r   
test_inner  s    z9GrammarTests.test_break_continue_loop.<locals>.test_inner)r   r   r   )r	   r   r   )r	   r   test_break_continue_loop  s    
z%GrammarTests.test_break_continue_loopc             C   s*   dd }dd }|  | }t | d d S )Nc               S   s   d S )Nr   r   r   r   r   g1"  r   z$GrammarTests.test_return.<locals>.g1c               S   s   dS )Nr   r   r   r   r   r   g2#  r   z$GrammarTests.test_return.<locals>.g2zclass foo:return 1)r   )r	   r   r   r
   r   r   r   test_return   s
    zGrammarTests.test_returnc             C   s:  d}x|dk r"|d7 }zW d P X qW |  |d d}x |dk rT|d7 }zw6W d P X q6W |  |d d}x&|dk r|d7 }zdd  W d P X qhW |  |d x"dD ]}|  |d zW d P X qW |  |d x$dD ]}|  |d zwW d P X qW |  |d x,dD ]$}|  |d zdd  W d P X qW |  |d d S )Nr   r   r   )r   r   )r   )r	   r   r   r   r   test_break_in_finally(  sN    






z"GrammarTests.test_break_in_finallyc             C   sF   dd }|  | d dd }|  | d dd }|  | d	 d S )
Nc               S   s
   zW d dS )Nr   r   r   r   r   r   r   ]  s    z/GrammarTests.test_return_in_finally.<locals>.g1r   c               S   s
   zdS dS )Nr   r   r   r   r   r   r   r   d  s    z/GrammarTests.test_return_in_finally.<locals>.g2r   c               S   s   zdd  W d dS )Nr   r   r   r   r   r   r   r   g3k  s    z/GrammarTests.test_return_in_finally.<locals>.g3r   )r   )r	   r   r   r   r   r   r   test_return_in_finally\  s    z#GrammarTests.test_return_in_finallyc             C   s   dd }dd }dd }dd }dd }dd }t | d t | d	 d
d }dd }t | d t | d dd }dd }dd }dd }t | d t | d t | d t | d t | d t | d t | d t | d t | d d S )Nc               s   s
   dV  d S )Nr   r   r   r   r   r   r   t  r   z"GrammarTests.test_yield.<locals>.gc               s   s   dE d H  d S )Nr   r   r   r   r   r   r   u  r   c              s   s
   dV } d S )Nr   r   )r
   r   r   r   r   w  r   c              s   s   dE d H } d S )Nr   r   )r
   r   r   r   r   x  r   c               s   s
   dV  d S )N)r   r   r   r   r   r   r   r   z  r   c              s   s
   dV } d S )N)r   r   r   )r
   r   r   r   r   {  r   zdef g(): yield from (), 1zdef g(): x = yield from (), 1c               s   s   ddV f d S )Nr   r   r   r   r   r   r     r   c               s   s   ddE d H f d S )Nr   r   r   r   r   r   r   r     r   zdef g(): 1, yield 1zdef g(): 1, yield from ()c               s   s   t dV  d S )Nr   )rN   r   r   r   r   r     r   c               s   s   t dV d d S )Nr   )rN   r   r   r   r   r     r   c               s   s   t dE d H  d S )Nr   )rN   r   r   r   r   r     r   c               s   s   t dE d H d d S )Nr   r   )rN   r   r   r   r   r     r   zdef g(): f(yield 1)zdef g(): f(yield 1, 1)zdef g(): f(yield from ())zdef g(): f(yield from (), 1)yieldz
yield fromzclass foo:yield 1zclass foo:yield from ()zdef g(a:(yield)): pass)r   )r	   r   r   r   r   
test_yieldr  s2    











zGrammarTests.test_yieldc                s   dd }dd } fdd}|dd |dd |d	d |d
d |dd |dd |dd |dd |dd |dd d S )Nc               s   s   dd dV gD  d S )Nc             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r     s    zHGrammarTests.test_yield_in_comprehensions.<locals>.g.<locals>.<listcomp>r   r   r   r   r   r   r     r   z4GrammarTests.test_yield_in_comprehensions.<locals>.gc               s   s   dd dE d H gD  d S )Nc             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r     s    zHGrammarTests.test_yield_in_comprehensions.<locals>.g.<locals>.<listcomp>r   r   r   r   r   r   r     r   c          
      sv     t| t| dd W d Q R X dd l}| 8 |jdtd  t| t| dd W d Q R X W d Q R X d S )Nz<test string>r2   r   error)category)ZassertWarnsRegexDeprecationWarningr3   warningscatch_warningsfilterwarningsr   r   )codeZwarntextr   )r	   r   r   check  s    
z8GrammarTests.test_yield_in_comprehensions.<locals>.checkz def g(): [(yield x) for x in ()]z!'yield' inside list comprehensionz)def g(): [x for x in () if not (yield x)]z-def g(): [y for x in () for y in [(yield x)]]z def g(): {(yield x) for x in ()}z 'yield' inside set comprehensionz#def g(): {(yield x): x for x in ()}z!'yield' inside dict comprehensionz#def g(): {x: (yield x) for x in ()}z def g(): ((yield x) for x in ())z#'yield' inside generator expressionz%def g(): [(yield from x) for x in ()]z class C: [(yield x) for x in ()]z[(yield x) for x in ()]r   )r	   r   r   r   )r	   r   test_yield_in_comprehensions  s.    	z)GrammarTests.test_yield_in_comprehensionsc             C   sD   yt dW n t k
r    Y nX ytW n tk
r>   Y nX d S )Nzjust testing)RuntimeErrorKeyboardInterrupt)r	   r   r   r   
test_raise  s        zGrammarTests.test_raisec             C   sd   dd l }dd l}dd l }ddlm} ddlm} ddl m}m} ddl m}m} ddl m}m} d S )Nr   )time)pathargv)r   r   r   r   )r	   r   r   r   r   r   r   r   test_import  s    zGrammarTests.test_importc             C   s   d S )Nr   )r	   r   r   r   test_global  s    zGrammarTests.test_globalc                s   d d fdd}d S )Nr   c                  s   d S )Nr   r   )r
   r.   r   r   rN     s    z%GrammarTests.test_nonlocal.<locals>.fr   )r	   rN   r   )r
   r.   r   test_nonlocal  s    zGrammarTests.test_nonlocalc          
   C   sh   yW n, t k
r0 } z| d W d d }~X Y nX yW n, t k
rb } z| d W d d }~X Y nX d S )Nz6'assert True' should not have raised an AssertionErrorz;'assert True, msg' should not have raised an AssertionError)AssertionErrorr   )r	   r]   r   r   r   test_assert  s    zGrammarTests.test_assertFz Won't work if __debug__ is Falsec          
   C   s   yW n4 t k
r8 } z| |jd d W d d }~X Y nX | d yW n4 t k
r| } z| t|jd W d d }~X Y nX | d d S )Nr   r   z%AssertionError not raised by assert 0z+AssertionError not raised by 'assert False')r   r   r   r   r,   )r	   r]   r   r   r   testAssert2  s    $
$zGrammarTests.testAssert2c             C   s   d S )Nr   )r	   r   r   r   test_if  s    zGrammarTests.test_ifc             C   s   d}d}|  |d d S )Nr   r   )r   )r	   r
   r   r   r   
test_while  s    zGrammarTests.test_whilec             C   s   xdD ]}qW xdD ]
\}}}qW G dd d}d}x|dD ]}|| }q>W |dkr`|  d g }xd	D ]\}|| qjW | |d
ddg d S )N)r   r   r   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )z&GrammarTests.test_for.<locals>.Squaresc             S   s   || _ g | _d S )N)maxsofar)r	   r  r   r   r   r<   $  s    z/GrammarTests.test_for.<locals>.Squares.__init__c             S   s
   t | jS )N)r,   r  )r	   r   r   r   __len__'  r   z.GrammarTests.test_for.<locals>.Squares.__len__c             S   sV   d|  kr| j k sn tt| j}x$||krJ| j||  |d }q(W | j| S )Nr   r   )r  
IndexErrorr,   r  append)r	   r   nr   r   r   rA   (  s     

z2GrammarTests.test_for.<locals>.Squares.__getitem__N)r7   r8   r9   r<   r  rA   r   r   r   r   Squares#  s   r
  r   r   i  zfor over growing sequence))r   )r   )r   r   r   r   )r   r  r   )r	   r   r   r   r
  r	  r
   resultr   r   r   test_for  s    
   
zGrammarTests.test_forc          
   C   s   ydd  W n t k
r    Y nX ydd  W n> tk
rB   Y n, tk
rb } zW d d }~X Y n   Y nX ydd  W n ttt fk
r   Y nX ydd  W n( ttt fk
r } zW d d }~X Y nX zW d X d S )Nr   r   )rZ   EOFErrorr   )r	   r   r   r   r   test_try9  s.             zGrammarTests.test_tryc             C   s   d S )Nr   )r	   r   r   r   
test_suiteO  s    zGrammarTests.test_suitec             C   s   dsdrdsdsdrd S )Nr   Fr   )r	   r   r   r   	test_test^  s
      zGrammarTests.test_testc             C   s   ddk}ddkrddkrddk r ddkr(ddkr0ddkr8ddkr@ddk	rHddkrPddkrXdd  k rd  krd  krd  krd  krd  krd  krd  krd  krdk	rn n d S )Nr   r   r   )r	   r
   r   r   r   test_comparisoni  s    h zGrammarTests.test_comparisonc             C   s   d}d}d}d S )Nr   r   r   )r	   r
   r   r   r   test_binary_mask_opsz  s    z!GrammarTests.test_binary_mask_opsc             C   s   d}d}d}d S )Nr   r   r   r   )r	   r
   r   r   r   test_shift_ops  s    zGrammarTests.test_shift_opsc             C   s   d}d}d}d}d S )Nr   r   r   r   )r	   r
   r   r   r   test_additive_ops  s    zGrammarTests.test_additive_opsc             C   s   d}d}d}d}d S )Nr   g      ?r   g        r   )r	   r
   r   r   r   test_multiplicative_ops  s    z$GrammarTests.test_multiplicative_opsc             C   s   d}d}d}d}d}d S )Nr   r   g      ?r   )r	   r
   r   r   r   test_unary_ops  s
    zGrammarTests.test_unary_opsc       	      C   s   dd l }dd l}|jd }| }|jd  }d}|d }|d }|dd }|d d }|dd  }|d d  }|dd  }|d d }|dd }i }d	|d	< d
|d< d|d< d|d< t|}|jdd d | t|d d S )Nr   r   Z01234r   rD   r   r   )r   r   )r   r   r   )r   r   r   c             S   s   t | j| fS )N)r   r7   )r
   r   r   r   r     r   z-GrammarTests.test_selectors.<locals>.<lambda>)r   z[1, (1,), (1, 2), (1, 2, 3)])r   r   r   modulesrH   sortr   r4   )	r	   r   r   r   r
   rK   r   r   Lr   r   r   test_selectors  s,    
zGrammarTests.test_selectorsc             C   s   d}dpdpd}dpdpdddf}g }dg}dp6dp6dg}dpDdpDdddg}g }i }ddi}ddi}dpjddppdi}ddd}ddd}dddddd	d
}dh}ddh}dddh}dddh}|}d}d}d S )Nr   r   r   rF   r   )rF   r   r   rD   r   )rF   r   threeZfourZfiveZsixr  r
   rj   r   )r	   r
   r   r   r   
test_atoms  s,    



zGrammarTests.test_atomsc       	      C   s|   G dd d}G dd d}G dd d|}G dd d|}G d	d
 d
|||}G dd d}dd }|G dd d}d S )Nc               @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.BN)r7   r8   r9   r   r   r   r   B  s    r!  c               @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.B2N)r7   r8   r9   r   r   r   r   B2  s    r"  c               @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.C1N)r7   r8   r9   r   r   r   r   C1  s    r#  c               @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.C2N)r7   r8   r9   r   r   r   r   C2  s    r$  c               @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.DN)r7   r8   r9   r   r   r   r   D  s    r%  c               @   s$   e Zd Zdd Zdd Zdd ZdS )z%GrammarTests.test_classdef.<locals>.Cc             S   s   d S )Nr   )r	   r   r   r   meth1  r   z+GrammarTests.test_classdef.<locals>.C.meth1c             S   s   d S )Nr   )r	   argr   r   r   meth2  r   z+GrammarTests.test_classdef.<locals>.C.meth2c             S   s   d S )Nr   )r	   r   r   r   r   r   meth3  r   z+GrammarTests.test_classdef.<locals>.C.meth3N)r7   r8   r9   r&  r(  r)  r   r   r   r   rW     s   rW   c             S   s   | S )Nr   )r
   r   r   r   class_decorator  r   z3GrammarTests.test_classdef.<locals>.class_decoratorc               @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.GN)r7   r8   r9   r   r   r   r   G  s   r+  r   )	r	   r!  r"  r#  r$  r%  rW   r*  r+  r   r   r   test_classdef  s    zGrammarTests.test_classdefc             C   s,   dddg}|  dd |D dddd d S )Nr   r   r   c             S   s   i | ]}|d  |qS )r   r   )r   r   r   r   r   
<dictcomp>  s    z/GrammarTests.test_dictcomps.<locals>.<dictcomp>r   )r   r   r   )r   )r	   numsr   r   r   test_dictcomps  s    
zGrammarTests.test_dictcompsc                s  dddddg}dddgd	d
dg} dd |D dddg  dd |D dddddg  dd |D dddg  fdd|D ddddddddddd d!d"d#d$g  fd%d|D ddddddd d!d#d$g
  d&d tdD dgddgdddgdddd'gddd(d)d*gg d+d, } ||d-d-d-g fd.d/}|  td0 td1 d2d3d4g}d5d6d7g d8d9d:d;g fd<d|D } |d=d>d?d@g d S )ANr   r   r   r   rD   AppleBananaCoconutz  Applez Banana zCoco  nut  c             S   s   g | ]}|  qS r   )strip)r   r   r   r   r   r     s    z/GrammarTests.test_listcomps.<locals>.<listcomp>z	Coco  nutc             S   s   g | ]}d | qS )r   r   )r   r
   r   r   r   r     s    r   r   r      c             S   s   g | ]}|d kr|qS )r   r   )r   r
   r   r   r   r     s    c                s   g | ]} D ]}||fqqS r   r   )r   r   r   )strsr   r   r     s    )r   r0  )r   r1  )r   r2  )r   r0  )r   r1  )r   r2  )r   r0  )r   r1  )r   r2  )r   r0  )r   r1  )r   r2  )rD   r0  )rD   r1  )rD   r2  c                s(   g | ] }d d  D D ]}||fqqS )c             S   s   g | ]}d |kr|qS )r	  r   )r   rN   r   r   r   r     s    z:GrammarTests.test_listcomps.<locals>.<listcomp>.<listcomp>r   )r   r   r   )r5  r   r   r     s    c             S   s   g | ]}d d |qS )c                s    fddt  d D S )Nc                s   g | ]} | qS r   r   )r   r   )rK   r   r   r     s    zLGrammarTests.test_listcomps.<locals>.<listcomp>.<lambda>.<locals>.<listcomp>r   )range)rK   r   )rK   r   r     r   z8GrammarTests.test_listcomps.<locals>.<listcomp>.<lambda>r   )r   r   r   r   r   r     s          @      c             S   s   dd | D S )Nc             S   s,   g | ]$}|d krd|  k o"dk n  qS )r   r   r   r   )r   r
   r   r   r   r   	  s    zEGrammarTests.test_listcomps.<locals>.test_in_func.<locals>.<listcomp>r   )lr   r   r   test_in_func  s    z1GrammarTests.test_listcomps.<locals>.test_in_funcFc                  s,     dd dD ddgddgdd	gg d S )
Nc             S   s"   g | ]}d d ||d gD qS )c             S   s   g | ]}|qS r   r   )r   r.   r   r   r   r     s    zUGrammarTests.test_listcomps.<locals>.test_nested_front.<locals>.<listcomp>.<listcomp>r   r   )r   r
   r   r   r   r     s    zJGrammarTests.test_listcomps.<locals>.test_nested_front.<locals>.<listcomp>)r   r   rD   r   r   r   r   rD   r   )r   r   )r	   r   r   test_nested_front  s    z6GrammarTests.test_listcomps.<locals>.test_nested_frontz"[i, s for i in nums for s in strs]z[x if y])r   Boeing)r   Ford)r   
Macdonalds)r   Airliner)r   Engine)   Cheeseburger)r   r   )r   r   )r   r   )r   rC  c                sD   g | ]<\}} D ].\}}D ] \}}||kr||kr||fqqqS r   r   )r   ZsnoZsnameZpnoZpnameZsp_snoZsp_pno)partssuppartr   r   r   '  s   )r>  rA  )r>  rB  )r?  rB  )r@  rD  )r   r6  r   )r	   r.  Zspcsr<  r=  Z	suppliersr
   r   )rE  r	   r5  rF  r   test_listcomps  sF    

 
.

zGrammarTests.test_listcompsc                s  dd t dD }| t|dd t dD  yt| | d W n tk
rZ   Y nX d y( fdd D }t| | d	 W n tk
r   Y nX | td
d dD dd dD  | tdd dD dd dD  dd t dD  dd dd  D D }| t|tdd t dD  | tdd t dD tdd t dD  | tdd t dD tdd t dD  | tdd dd t dD D tdd t dD  | tdd dd dd t dD D D tdd t dD  | tdd d d d!d t dD D D td"d t dD  | td#d d$d d%d t dD D D td&d t dD  | td'd d(d d)d t dD D D d* t| d+ t| d, d S )-Nc             s   s    | ]}d d t dD V  qdS )c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   3  s    z7GrammarTests.test_genexps.<locals>.<genexpr>.<listcomp>r   N)r6  )r   r
   r   r   r   	<genexpr>3  s    z,GrammarTests.test_genexps.<locals>.<genexpr>r   c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   4  s    z-GrammarTests.test_genexps.<locals>.<listcomp>r   z&should produce StopIteration exceptionc             3   s   | ]
} V  qd S )Nr   )r   r   )rK   r   r   rH  =  s    zshould produce TypeErrorc             s   s    | ]}d D ]}||fV  q
qdS )abcdNr   )r   r
   r.   r   r   r   rH  C  s    rI  c             S   s   g | ]}d D ]}||fqqS )rI  r   )r   r
   r.   r   r   r   r   C  s    c             s   s    | ]}d D ]}||fV  q
qdS )xyNr   )r   r
   r.   r   r   r   rH  D  s    Zabc             S   s   g | ]}d D ]}||fqqS )rJ  r   )r   r
   r.   r   r   r   r   D  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   F  s    c             s   s   | ]
}|V  qd S )Nr   )r   r
   r   r   r   rH  G  s    c             s   s   | ]
}|V  qd S )Nr   )r   r.   r   r   r   rH  G  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   H  s    c             s   s   | ]}|d  V  qdS )r   Nr   )r   r
   r   r   r   rH  J  s    c             S   s   g | ]}|d  qS )r   r   )r   r
   r   r   r   r   J  s    c             s   s   | ]}|d  r|| V  qdS )r   Nr   )r   r
   r   r   r   rH  K  s    c             S   s   g | ]}|d  r|| qS )r   r   )r   r
   r   r   r   r   K  s    c             s   s   | ]
}|V  qd S )Nr   )r   r
   r   r   r   rH  L  s    c             s   s   | ]
}|V  qd S )Nr   )r   r.   r   r   r   rH  L  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   L  s    c             s   s   | ]
}|V  qd S )Nr   )r   r
   r   r   r   rH  M  s    c             s   s   | ]
}|V  qd S )Nr   )r   r.   r   r   r   rH  M  s    c             s   s   | ]
}|V  qd S )Nr   )r   rV   r   r   r   rH  M  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   M  s    c             s   s   | ]
}|V  qd S )Nr   )r   r
   r   r   r   rH  N  s    c             S   s   g | ]}|qS r   r   )r   r.   r   r   r   r   N  s    c             s   s   | ]
}|V  qd S )Nr   )r   rV   r   r   r   rH  N  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   N  s    c             s   s   | ]
}|V  qdS )TNr   )r   r
   r   r   r   rH  O  s    c             s   s   | ]
}|V  qd S )Nr   )r   r.   r   r   r   rH  O  s    c             s   s   | ]
}|V  qdS )TNr   )r   rV   r   r   r   rH  O  s    c             S   s   g | ]}|qS r   r   )r   r
   r   r   r   r   O  s    c             s   s   | ]
}|V  qdS )TNr   )r   r
   r   r   r   rH  P  s    c             s   s   | ]}d r|V  qdS )FNr   )r   r.   r   r   r   rH  P  s    c             s   s   | ]
}|V  qdS )TNr   )r   rV   r   r   r   rH  P  s    r   zfoo(x for x in range(10), 100)zfoo(100, x for x in range(10)))	r6  r   nextr   StopIterationr   rH   sumr   )r	   r   rL   r   )rK   r   test_genexps1  s8    $$"00:DDD2
zGrammarTests.test_genexpsc                s   ddd t D }d| tt|d dd  fddt D }dd | dd	 t dD t| | d
d	 t dD dddg | tdd t dD dddg | dd	 dD dddg | tdd dD dddg d S )Nr   c             s   s   | ]
}|V  qd S )Nr   )r   r   r   r   r   rH  V  s    z;GrammarTests.test_comprehension_specials.<locals>.<genexpr>rD   Fc             3   s(   | ] } rt D ]}||fV  qqd S )N)r6  )r   r   r   )tr
   r   r   rH  Z  s    Tc             S   s"   g | ]}t d D ]}||fqqS )rD   )r6  )r   r   r   r   r   r   r   \  s    z<GrammarTests.test_comprehension_specials.<locals>.<listcomp>c             S   s    g | ]}|d  r|d r|qS )r   r   r   )r   r
   r   r   r   r   `  s    r   r   c             s   s"   | ]}|d  r|d r|V  qdS )r   r   Nr   )r   r
   r   r   r   rH  a  s    c             S   s   g | ]
\}|qS r   r   )r   r
   r   r   r   r   d  s    ))r   )rD   )r   r   r   c             s   s   | ]\}|V  qd S )Nr   )r   r
   r   r   r   rH  e  s    ))r   )r   )r   r   r   )r6  r   r,   rH   )r	   r   r   )rO  r
   r   test_comprehension_specialsT  s          $z(GrammarTests.test_comprehension_specialsc          
   C   s   G dd dt }|  W d Q R X | }W d Q R X | 
\}}W d Q R X |  |  W d Q R X W d Q R X | }| }W d Q R X W d Q R X | }|  W d Q R X W d Q R X d S )Nc               @   s   e Zd Zdd Zdd ZdS )z1GrammarTests.test_with_statement.<locals>.managerc             S   s   dS )N)r   r   r   )r	   r   r   r   	__enter__i  s    z;GrammarTests.test_with_statement.<locals>.manager.__enter__c             W   s   d S )Nr   )r	   r   r   r   r   __exit__k  s    z:GrammarTests.test_with_statement.<locals>.manager.__exit__N)r7   r8   r9   rQ  rR  r   r   r   r   managerh  s   rS  )object)r	   rS  r
   r.   r   r   r   test_with_statementg  s    


z GrammarTests.test_with_statementc             C   s  dd }|  dd dd dd fD dg |  d	d d
d dd fD dg |  dn|ddd |  drx|ddndd |  drdodndd |  drdodndd |  dodndd |  drdp|ddndd |  d p|ddn|ddd |  dpdn|ddd |  dndd |  dr>dndd |  dndd |  dndd |  dndd |  dndd |  drddk ndd d S )Nc             S   s   t |  |S )z@helper to check that evaluation of expressions is done correctly)rP   )r   retr   r   r   
_checkeval}  s    z2GrammarTests.test_if_else_expr.<locals>._checkevalc             S   s   g | ]}| r| qS r   r   )r   r
   r   r   r   r     s    z2GrammarTests.test_if_else_expr.<locals>.<listcomp>c               S   s   dS )NTr   r   r   r   r   r     r   z0GrammarTests.test_if_else_expr.<locals>.<lambda>c               S   s   dS )NFr   r   r   r   r   r     r   Tc             S   s   g | ]}|d r|d qS )Fr   )r   r
   r   r   r   r     s    c             S   s   | rdS dS )NFTr   )r
   r   r   r   r     r   c             S   s   | rdS dS )NTFr   )r
   r   r   r   r     r   r   rD   zcheck 1r   zcheck 2r   zcheck 3r   r   zcheck 4zcheck 5zcheck 6Fr   r   r   g      @)r   )r	   rW  r   r   r   test_if_else_expr{  s$    $$"zGrammarTests.test_if_else_exprc             C   sj   |  dd |  dd |  dd | dddkk | ddkdk | dd  ko^dkn   d S )Nr   r   Fr   )r   r   ZassertFalse)r	   r   r   r   test_paren_evaluation  s    z"GrammarTests.test_paren_evaluationc             C   s>   G dd d}| }|  || d |d }|  |jd d S )Nc               @   s   e Zd Zdd Zdd ZdS )z'GrammarTests.test_matrix_mul.<locals>.Mc             S   s   dS )Nr   r   )r	   rk   r   r   r   
__matmul__  s    z2GrammarTests.test_matrix_mul.<locals>.M.__matmul__c             S   s
   || _ | S )N)other)r	   rk   r   r   r   __imatmul__  s    z3GrammarTests.test_matrix_mul.<locals>.M.__imatmul__N)r7   r8   r9   rZ  r\  r   r   r   r   rp     s   rp   r   rE   )r   r[  )r	   rp   mr   r   r   test_matrix_mul  s
    zGrammarTests.test_matrix_mulc             C   sx   dd }|  |jd | t|jjtj@  dd }|dd }| |j |  |jd | t|jjtj@  d S )	Nc                 s   dd } t  I d H  d S )Nc               S   s   d S )Nr   r   r   r   r   rM    s    z8GrammarTests.test_async_await.<locals>.test.<locals>.sum)Zsomeobj)rM  r   r   r   rm     s    z+GrammarTests.test_async_await.<locals>.testrm   c             S   s   t | dd | S )N_markedT)setattr)funcr   r   r   	decorator  s    z0GrammarTests.test_async_await.<locals>.decoratorc                  s   dS )N   r   r   r   r   r   test2  s    z,GrammarTests.test_async_await.<locals>.test2rd  )	r   r7   r   boolr   co_flagsinspectZCO_COROUTINEr_  )r	   rm   rb  rd  r   r   r   test_async_await  s    zGrammarTests.test_async_awaitc          	      sR   G dd dt G dd d  fdd}|  | d  W d Q R X d S )Nc               @   s   e Zd ZdS )z)GrammarTests.test_async_for.<locals>.DoneN)r7   r8   r9   r   r   r   r   Done  s    ri  c               @   s   e Zd Zdd Zdd ZdS )z*GrammarTests.test_async_for.<locals>.AIterc             S   s   | S )Nr   )r	   r   r   r   	__aiter__  s    z4GrammarTests.test_async_for.<locals>.AIter.__aiter__c                s   t d S )N)StopAsyncIteration)r	   r   r   r   	__anext__  s    z4GrammarTests.test_async_for.<locals>.AIter.__anext__N)r7   r8   r9   rj  rl  r   r   r   r   AIter  s   rm  c           	      s   x,  2 y3 d H } W q t k
s"X q   Y  W x0  2 y3 d H \} }W q6 t k
sTX q6   Y  W x,  2 y3 d H } W qh t k
sX qh   Y  W d S )N)rk  )r   r   )rm  ri  r   r   r     s     $ z(GrammarTests.test_async_for.<locals>.foo)r\   r   send)r	   r   r   )rm  ri  r   test_async_for  s
    zGrammarTests.test_async_forc          	      sR   G dd dt  G dd d fdd}|   | d  W d Q R X d S )Nc               @   s   e Zd ZdS )z*GrammarTests.test_async_with.<locals>.DoneN)r7   r8   r9   r   r   r   r   ri    s    ri  c               @   s   e Zd Zdd Zdd ZdS )z-GrammarTests.test_async_with.<locals>.managerc                s   dS )N)r   r   r   )r	   r   r   r   
__aenter__  s    z8GrammarTests.test_async_with.<locals>.manager.__aenter__c                s   dS )NFr   )r	   excr   r   r   	__aexit__  s    z7GrammarTests.test_async_with.<locals>.manager.__aexit__N)r7   r8   r9   rp  rr  r   r   r   r   rS    s   rS  c                 s,   4 I d H  W d Q I d H R X  4 I d H } W d Q I d H R X  4 I d H 
\} }W d Q I d H R X  4 I d H &  4 I d H  W d Q I d H R X W d Q I d H R X  4 I d H &}  4 I d H }W d Q I d H R X W d Q I d H R X  4 I d H &}  4 I d H  W d Q I d H R X W d Q I d H R X  d S )Nr   )r
   r.   )ri  rS  r   r   r     s          z)GrammarTests.test_async_with.<locals>.foo)r\   r   rn  )r	   r   r   )ri  rS  r   test_async_with  s
    zGrammarTests.test_async_withN)>r7   r8   r9   rC   rI   rJ   r^   ri   rq   ru   rw   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   unittestZ
skipUnlessr  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r,  r/  rG  rN  rP  rU  rX  rY  r^  rh  ro  rs  r   r   r   r   rB      st   .			 \'	4$##?#rB   __main__)Ztest.supportr   rg  rt  r   Ztest.ann_modulern   ro   collectionsr   rm   r   r"   r$   ZTestCaser   rG   rU   r:   rB   r7   mainr   r   r   r   <module>   s     	          
