a
    zet                     @   s   U d dl mZmZmZ d dl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g dZg dZG dd	 d	ejZeed
< G dd dZG dd dejZedkre  dS )    )check_syntax_errorcheck_syntax_warninguse_old_parserN)*)ChainMap)ann_module2)Z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_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                   @   sh   e Zd 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dd Zdd Zdd ZdS )
TokenTestsr   r   c                 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.9/test/test_grammar.pytest_backslashj   s    zTokenTests.test_backslashc              	   C   sf  |  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 d	D ]2}zt|}W q ty   | 	d
|  Y q0 qn|dkrT|  dd | ddk | ddk | ddk dD ]6}zt|}W n" tyL   | 	d
|  Y n0 qn| 	d|  d S )Nr      i	   0x)maxsizei   l    )Z
2147483648Z0o40000000000Z0x100000000Z"0b10000000000000000000000000000000z(OverflowError on huge integer literal %rl    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_integerst   s8    
zTokenTests.test_plain_integersc                 C   s$   d}d}d}d}d}d}d}d}d S )Nr   r   l   ? l   
~l!]c l            l   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   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   sP   t D ] }| t|t|dd qtD ]}| tt| q*| 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   | 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 S )NZ0b12z#invalid digit '2' in binary literalZ0b1_2Z0b2r   zinvalid binary literalZ0bZ0o18z"invalid digit '8' in octal literalZ0o1_8Z0o8Z0o1_zinvalid octal literalZ0oZ0x1_zinvalid hexadecimal literalr   Z1_zinvalid decimal literalZ012z`leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integersz1.2_Z1e2_z1e+r
   r   checkr   r   r   test_bad_numerical_literals   s(    














z&TokenTests.test_bad_numerical_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   s$    "..zTokenTests.test_string_literalsc                 C   s$   d}|  |tu  | ttd d S )N.z.. .)r   Ellipsisr   r   r   r   r   r   r   test_ellipsis   s    zTokenTests.test_ellipsisc              	   C   sZ   d}|D ]L}|  t}t|dd W d    n1 s80    Y  | dt|j qd S )N)zdef foo(z	
def foo(z	def foo(
z<test>execzunexpected EOF)r   r   compileZassertInstr	exception)r   Zsamplesr!   cmr   r   r   test_eof_error  s
    *zTokenTests.test_eof_errorN)__name__
__module____qualname__test.supportr   r   r"   r#   r$   r'   r.   r1   r;   r=   rC   r   r   r   r   r	   f   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_dctr&   r   r   r   __init__  s    zCNS.__init__c                 C   s   || j | < d S rI   )rK   lowerr   itemvaluer   r   r   __setitem__  s    zCNS.__setitem__c                 C   s
   | j | S rI   rJ   r   rO   r   r   r   __getitem__  s    zCNS.__getitem__NrD   rE   rF   rL   rQ   rS   r   r   r   r   rH     s   rH   c                   @   s   e Zd ZddlmZm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"d=d> Z#d?d@ Z$dAdB Z%dCdD Z&dEdF Z'e()dGdHdIdJ Z*dKdL Z+dMdN Z,dOdP Z-dQdR Z.dSdT Z/dUdV Z0dWdX Z1dYdZ Z2d[d\ Z3d]d^ Z4d_d` Z5dadb Z6dcdd Z7dedf Z8dgdh Z9didj Z:dkdl Z;dmdn Z<dodp Z=dqdr Z>dsdt Z?dudv Z@dwdx ZAdydz ZBd{d| ZCd}d~ ZDdd ZEdd ZFdS )GrammarTestsr   )r   r   c                 C   s   t d}d S )Nz	1, 0 or 1)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_lstrZ   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_errors2  s    







z)GrammarTests.test_var_annot_syntax_errorsc           	   
   C   s  |  t dd tt< W d    n1 s,0    Y  |  t dtt< W d    n1 s^0    Y  dd }| |ji  dd }|  dd }|  t |  W d    n1 s0    Y  d	d
 }z
|  W n6 ty } z| 	t
|t W Y d }~n
d }~0 0 G dd d}| |jttd |  t G dd d}W d    n1 s`0    Y  |  t  G dd d|}W d    n1 s0    Y  d S )Nr%   r   c                  S   s   d} dt _| S )NZHellor%   r   ab)str   r   r   fO  s    z6GrammarTests.test_var_annot_basic_semantics.<locals>.fc                   S   s   d S rI   r   r   r   r   r   f_OKT  s    z9GrammarTests.test_var_annot_basic_semantics.<locals>.f_OKc                  S   s   t |  d S rI   printr   r   r   r   fbadW  s    z9GrammarTests.test_var_annot_basic_semantics.<locals>.fbadc                   S   s   t t d S rI   )rg   Zno_such_globalr   r   r   r   f2bad\  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 rI   rh   r   r   r   r   rL   i  s    z?GrammarTests.test_var_annot_basic_semantics.<locals>.C.__init__N)	rD   rE   rF   r[   __annotations__r!   r@   zrL   r   r   r   r   Ce  s   
ro   )rk   r!   c                   @   s   e Zd ZU de_e dS )z9GrammarTests.test_var_annot_basic_semantics.<locals>.CBadr   N)rD   rE   rF   Zno_such_name_definedrl   r[   r   r   r   r   CBadm  s   
rp   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)rD   rE   rF   r[   rm   r   r:   r\   r   r   r   r   Cbad2p  s   
rq   )r   ZeroDivisionErrorZno_nameZdoes_not_existr-   r   rm   UnboundLocalError	ExceptionZassertIsr   r[   r@   )	r   rd   re   ri   rj   ero   rp   rq   r   r   r   test_var_annot_basic_semanticsF  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 Nrm   )rH   )metaclsnamebaseskwdsr   r   r   __prepare__v  s    zJGrammarTests.test_var_annot_metaclass_semantics.<locals>.CMeta.__prepare__N)rD   rE   rF   classmethodr|   r   r   r   r   CMetau  s   r~   c                   @   s   e Zd ZU ded< dS )z;GrammarTests.test_var_annot_metaclass_semantics.<locals>.CCANNOTZXXN)rD   rE   rF   rm   r   r   r   r   CCy  s   
r   )	metaclassxxr   )r   r   rm   )r   r~   r   r   r   r   "test_var_annot_metaclass_semanticst  s    z/GrammarTests.test_var_annot_metaclass_semanticsc              	   C   s~   |  t ttj W d    n1 s*0    Y  | 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:   rd   {   )Z123o)r   AttributeErrorrg   testrm   r   
ann_moduler[   r@   typingTupleMr   r   r&   r   r   r   test_var_annot_module_semantics}  s    (
z,GrammarTests.test_var_annot_module_semanticsc                 C   s   ddl m}m}m} | t |  W d    n1 s:0    Y  | t |  W d    n1 sj0    Y  | t |d W d    n1 s0    Y  d S )Nr   )	f_bad_ann	g_bad_ann	D_bad_annrW   )Ztest.ann_module3r   r   r   r   r-   )r   r   r   r   r   r   r   test_var_annot_in_module  s    $$z%GrammarTests.test_var_annot_in_modulec                 C   s`   i }i }t d|| | |d dtd | t |d  W d    n1 sR0    Y  d S )Nz.'docstring'
__annotations__[1] = 2
x: int = 5
rm   r   )r%   r   )r>   r   r[   r   KeyError)r   ZgnsZlnsr   r   r   test_var_annot_simple_exec  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    n1 sh0    Y  i  G  fddd}tdi |  |  d d t d S )	Nrm   z'X: int; Z: str = "Z"; (w): complex = 1jr   rn   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 rI   rJ   r&   r   r   r   rL     s    z>GrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__init__c                    s   || j |< | |< d S rI   rJ   rN   	nonloc_nsr   r   rQ     s    
zAGrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__setitem__c                 S   s
   | j | S rI   rJ   rR   r   r   r   rS     s    zAGrammarTests.test_var_annot_custom_maps.<locals>.CNS2.__getitem__NrT   r   r   r   r   CNS2  s   r   z
x: int = 1)rH   r>   r   r[   r@   r   r   )r   nsr   r   r   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 )Nrm   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 rw   rJ   r&   )cnsr   r   rL     s    z:GrammarTests.test_var_annot_refleak.<locals>.CNS2.__init__c                    s   || j |< | |< d S rI   rJ   rN   r   r   r   rQ     s    
z=GrammarTests.test_var_annot_refleak.<locals>.CNS2.__setitem__c                 S   s
   | j | S rI   rJ   rR   r   r   r   rS     s    z=GrammarTests.test_var_annot_refleak.<locals>.CNS2.__getitem__NrT   r   r   r   r   r   r     s   r   zX: strr   )rH   r>   r   r@   )r   r   r   r   r   test_var_annot_refleak  s
    	z#GrammarTests.test_var_annot_refleakc                 C   sr   i }t d| | |d d d}t || | t|d  d g dddtjd	}t d
| | |d d d S )Nzx: tuple = 1, 2r   r_   zdef f():
    x: int = yieldrd   r%   r         rW   )ra   rb   cr   zx: Tuple[int, ...] = a,*b,cr%   r   r   r   rW   )r>   r   r\   r   r   )r   r   Zstmtr   r   r   test_var_annot_rhs  s    


zGrammarTests.test_var_annot_rhsc                    sX
  dd }|  |d  |di 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  |	g dpdi odR i dpi  |	f i d!di |	f i d!dipi  dd"d#}
|
d |
dd |
di d$di dd%d&}|dd |ddd |d'  |dgd(R   |ddgd)R   |di d*di dd+d,}|  |d |dd |d-  |dgd.R   |di d$di |f i 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 i d!di dd7d8}|d |dd |ddd dd9d:}|dd |ddd |dddd |d6  |di d*di dd;d<}|  |d |dd |ddd |dgd=R   |f i ddd/ dd>d?}|d |dd |ddd |dddd |d6  |ddgd@R   |dgd.R i d*di ddAdB}|dd |ddd |dddd |ddddd |d6  |ddgd@R   |dgd(R i dCdi ztdi dEdi W n ty.   Y n0 | 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 }|  |dgddgR ddd[d\ddd[f |  |dgd(dR  d6i f | ttd] |  |f i 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 dtdhdldZ}|  |jdDti tdhdmdZ}|  |jdDti tdhdndZ}|  |jdDti ddodpdZ}|  |jdqdi ddodrdZ}|  |jdqdi dddsdtdZ}|  |jddds dddudvdZ}|  |jddds ddddddwdxdZ}|  |jdddddw dddydzddddddd{d|d}d~dZ}|  |jddddddd{d|d} dddydzdddtdddd{d|d	ddZ}|  |jdddtdddd{d|d	 G dd d}G dd d|}|  |jjddi |  |jjddi dd t
deddZ}|  |jdgt
i d	p,ddZ } }ddZ }fddddZ }ddgd ddZ }ddZ }gd jjddZ }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 rI   r   r   r   r   r   f1      z%GrammarTests.test_funcdef.<locals>.f1r   c                 S   s   d S rI   r   Zone_argumentr   r   r   f2  r   z%GrammarTests.test_funcdef.<locals>.f2c                 S   s   d S rI   r   twoZ	argumentsr   r   r   f3  r   z%GrammarTests.test_funcdef.<locals>.f3r   r   c                 S   s   d S rI   r   )Zone_argr   r   r   a1  r   z%GrammarTests.test_funcdef.<locals>.a1c                 S   s   d S rI   r   )r   argsr   r   r   a2  r   z%GrammarTests.test_funcdef.<locals>.a2c                  W   s   d S rI   r   restr   r   r   v0  r   z%GrammarTests.test_funcdef.<locals>.v0c                 W   s   d S rI   r   ra   r   r   r   r   v1  r   z%GrammarTests.test_funcdef.<locals>.v1c                 W   s   d S rI   r   ra   rb   r   r   r   r   v2  r   z%GrammarTests.test_funcdef.<locals>.v2r%   r   r   r   rW            r   r   c                 S   s   d S rI   r   ra   r   r   r   d01  r   z&GrammarTests.test_funcdef.<locals>.d01r%   ra   c                 S   s   d S rI   r   r`   r   r   r   d11  r   z&GrammarTests.test_funcdef.<locals>.d11rb   c                 S   s   d S rI   r   ra   rb   r   r   r   r   d21  r   z&GrammarTests.test_funcdef.<locals>.d21r%   r   r   r   r   r   r   c                 S   s   d S rI   r   r`   r   r   r   d02  r   z&GrammarTests.test_funcdef.<locals>.d02r_   r   r`   c                 S   s   d S rI   r   r   r   r   r   d12  r   z&GrammarTests.test_funcdef.<locals>.d12c                 S   s   d S rI   r   ra   rb   r   dr   r   r   d22  r   z&GrammarTests.test_funcdef.<locals>.d22c                 W   s   d S rI   r   r   r   r   r   d01v  r   z'GrammarTests.test_funcdef.<locals>.d01v)r%   r   r   r   c                 W   s   d S rI   r   r   r   r   r   d11v&  r   z'GrammarTests.test_funcdef.<locals>.d11vc                 W   s   d S rI   r   ra   rb   r   r   r   r   r   d21v*  r   z'GrammarTests.test_funcdef.<locals>.d21vc                 W   s   d S rI   r   r   r   r   r   d02v0  r   z'GrammarTests.test_funcdef.<locals>.d02vr   c                 W   s   d S rI   r   r   r   r   r   d12v7  r   z'GrammarTests.test_funcdef.<locals>.d12vr   r   rW   c                 W   s   d S rI   r   )ra   rb   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 rI   r   keyr   r   r   pos0key1P  r   z+GrammarTests.test_funcdef.<locals>.pos0key1d   r   )k2c                S   s   | |||fS rI   r   )p1p2k1r   r   r   r   pos2key2R  r   z+GrammarTests.test_funcdef.<locals>.pos2key2)r      )r   r   )r   r   c                [   s   | ||||fS rI   r   )r   r   r   r   kwargr   r   r   pos2key2dictV  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 rI   r   )r   kwargsr   r   r   rd   _  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 rI   r   rh   r   r   r   rd   o  r   r   rh   c                 S   s   d S rI   r   rh   r   r   r   rd   q  r   c                S   s   d S rI   r   rh   r   r   r   rd   s  r   r5   c                S   s   d S rI   r   rh   r   r   r   rd   u  r   c                  W   s   d S rI   r   rh   r   r   r   rd   w  r   c                  [   s   d S rI   r   rh   r   r   r   rd   y  r   r:   c                 S   s   d S rI   r   r   r   r   r   rd   {  r   r:   c                S   s   d S rI   r   r   r   r   r   rd   }  r   )rb   r   c                 S   s   d S rI   r   r   r   r   r   rd     r   )r   rb   c                S   s   d S rI   r   r   r   r   r   rd     r   )rb   r   ru   gc                 W   s   d S rI   r   )ra   rb   r   r   ru   rd   r   r   r   r   rd     r   
   ij      )rb   r   ru   r   hr   kr   c                _   s   d S rI   r   ra   rb   r   r   ru   rd   r   r   r   r   r   r   r   r   rd     s    )	rb   r   ru   rd   r   r   r   r   r   c               _   s   d S rI   r   r   r   r   r   rd     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 rI   r   )r   r   r   r   r   rd     s    z)GrammarTests.test_funcdef.<locals>.Spam.fN)rD   rE   rF   rd   r   r   r   r   Spam  s   r   c                   @   s   e Zd ZdS )z&GrammarTests.test_funcdef.<locals>.HamNrD   rE   rF   r   r   r   r   Ham  r   r   r   c                 S   s   | S rI   r   rh   r   r   r   null  r   z'GrammarTests.test_funcdef.<locals>.nullc                 S   s   d S rI   r   rh   r   r   r   rd     s    Fc                 S   s   d S rI   r   rh   r   r   r   rd     s    c                 S   s   d S rI   r   rh   r   r   r   rd     s    c                    s    | S rI   r   rd   )r   r   r   <lambda>  r   z+GrammarTests.test_funcdef.<locals>.<lambda>c                 S   s   d S rI   r   rh   r   r   r   rd     s    .c                 S   s   d S rI   r   rh   r   r   r   rd     s    c                 S   s   d S rI   r   rh   r   r   r   rd     s    c                 S   s   d S rI   r   rh   r   r   r   rd     s    c                      s    S rI   r   r   closurer   r   rd     r   c                    s    S rI   r   rh   r   r   r   rd     r   r   c                    s    S rI   r   r   r   r   r   rd     r   c                      s    S rI   r   r   r   r   r   rd     r   c                 S   s   d S rI   r   r   r   r   r   rd     r   c                  W   s   d S rI   r   r   r   r   r   rd     r   c                  [   s   d S rI   r   r{   r   r   r   rd     r   c                 W   s   d S rI   r   ra   r   r   r   r   rd     r   c                 [   s   d S rI   r   ra   r{   r   r   r   rd     r   c                 W   s   d S rI   r   rb   r   r   r   r   rd     r   c                 S   s   d S rI   r   rb   r   r   r   rd     r   c                  _   s   d S rI   r   r   r{   r   r   r   rd     r   c                W   s   d S rI   r   ra   rb   r   r   r   r   rd     r   c                S   s   d S rI   r   r`   r   r   r   rd     r   c                 _   s   d S rI   r   ra   r   r{   r   r   r   rd     r   c                 _   s   d S rI   r   rb   r   r{   r   r   r   rd     r   c                 [   s   d S rI   r   rb   r{   r   r   r   rd     r   c                _   s   d S rI   r   ra   rb   r   r{   r   r   r   rd     r   c                [   s   d S rI   r   ra   rb   r{   r   r   r   rd     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   )r5   )r   rW   )r   rW   )r   rW   )r%   )r   __code__co_varnamesr@   	TypeErrorr    r   r   r   r   r\   rm   r[   floatrd   __call__)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   r   r   r   )r   r   r   test_funcdef  s   





*























  









zGrammarTests.test_funcdefc                 C   sV  dd }|  | d dd }dd }|  | g d 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   r     r   z+GrammarTests.test_lambdef.<locals>.<lambda>r   c                   S   s   t t S rI   )ra   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>  r   z?GrammarTests.test_lambdef.<locals>.<lambda>.<locals>.<listcomp>)r   r   r   r   r   r   r   r     r   )r   r%   r   r%   c                 S   s   | S rI   r   rn   r   r   r   r     r   c                 S   s   |  S rI   r   r   r   r   r   r     r   c                 S   s   |  S rI   r   rh   r   r   r   r     r   r   c                 S   s   | | | S rI   r   )r   r:   rn   r   r   r   r     r   rW   r   r   zlambda x: x = 2zlambda (None,): None   r   c                S   s   | | | S rI   r   )r   r:   r   r   r   r   r     r      r      c                 S   s   dS r  r   r   r   r   r   r     r   c                  W   s   dS r  r   r   r   r   r   r     r   c                  [   s   dS r  r   r   r   r   r   r     r   c                 W   s   dS r  r   r   r   r   r   r     r   c                 [   s   dS r  r   r   r   r   r   r     r   c                 W   s   dS r  r   r   r   r   r   r     r   c                 S   s   dS r  r   r   r   r   r   r     r   c                  _   s   dS r  r   r   r   r   r   r     r   c                W   s   dS r  r   r   r   r   r   r     r   c                S   s   dS r  r   r`   r   r   r   r     r   c                 _   s   dS r  r   r   r   r   r   r     r   c                 _   s   dS r  r   r   r   r   r   r     r   c                 [   s   dS r  r   r   r   r   r   r     r   c                _   s   dS r  r   r   r   r   r   r     r   c                [   s   dS r  r   r  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 rY   r   rh   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   z	x + 1 = 1za + 1 = b + 2r
   )	r   r   r:   rn   abcra   rb   r   xyzr   r   r   test_expr_stmt  s    

zGrammarTests.test_expr_stmtc                 C   s  d}g d}|D ]}d |}|D ]}| |}| j|dD | t| t| W d    n1 sh0    Y  W d    n1 s0    Y  |dd}| j|dD | td t| W d    n1 s0    Y  W d    q"1 s0    Y  q"qd S )N)rg   r>   )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)formatsubTestZassertRaisesRegexr   r>   r+   )r   keywordsZcaseskeywordZ
custom_msgcaser  r   r   r   (test_former_statements_refer_to_builtins  s    

Dz5GrammarTests.test_former_statements_refer_to_builtinsc                 C   s   g d}|\}}}|||f}~~~~~d\}}}~~~d\}}}}	}
}}~~~~	~
~d\}}}}	}
}}~~~~	~
~t d}|dd= tddd	 d S )
Nr   r  Zabcdefgabcdr%   r   z-del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()
<testcase>r>   )r\   r?   )r   r  r   r:   rn   r  ra   rb   r   r   ru   rd   r   r&  r   r   r   test_del_stmt  s     



zGrammarTests.test_del_stmtc                 C   s   d S rI   r   r&   r   r   r   test_pass_stmt5  s    zGrammarTests.test_pass_stmtc                 C   s   qqd S rI   r   r&   r   r   r   test_break_stmt<  s    zGrammarTests.test_break_stmtc                 C   s   d}|rd}qqd}|s<d}zW qd}W q   d}Y q0 q|dkrN|  | d}|std}zW d}qRW d}qRd}0 qR|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}|rJ|d7 }z(| r*|dkr*| d8 } W qJ|d8 }W qW q    Y q0 q|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_hippor&   r   r   
test_innerd  s    
z9GrammarTests.test_break_continue_loop.<locals>.test_inner)r%   r   r   )r   r0  r   r&   r   test_break_continue_loopZ  s    
z%GrammarTests.test_break_continue_loopc                 C   sF   dd }dd }dd }|  | }| }|  |dd t| d	 d S )
Nc                   S   s   d S rI   r   r   r   r   r   g1v  r   z$GrammarTests.test_return.<locals>.g1c                   S   s   dS rY   r   r   r   r   r   g2w  r   z$GrammarTests.test_return.<locals>.g2c                  S   s   ddg} dg| R S )Nr   r   r%   r   r  r   r   r   g3x  s    z$GrammarTests.test_return.<locals>.g3r   z unparenthesized star expr returnzclass foo:return 1r  )r   r2  r3  r4  r   r:   r   r   r   test_returnt  s    zGrammarTests.test_returnc                 C   s~  d}|dk r*|d7 }zW q*q   Y q*0 q|  |d d}|dk rf|d7 }zW qfq:W qfq:   Y qf0 q:|  |d d}|dk r|d7 }zdd  W qqv   Y q0 qv|  |d dD ](}|  |d zW  qq   Y  q0 q|  |d dD ]6}|  |d zW  q&qW  q&q   Y  q&0 q|  |d dD ]6}|  |d zdd  W  qnn   Y  qn0 q6|  |d d S Nr   r   r%   r   r%   r   r   r/  r   r   r   test_break_in_finally  sV    

z"GrammarTests.test_break_in_finallyc                 C   sX  d}|dk r,|d7 }zW qq,   Y q0 q,q|  |d d}|dk rh|d7 }zW q<qhW q<q<   Y q<0 q<|  |d d}|dk r|d7 }zdd  W qxq   Y qx0 qqx|  |d dD ]}zW qn   Y q0  qq|  |d dD ]"}zW q q
W qq   Y q0 q|  |d dD ],}zdd  W qn   Y q0  qHq|  |d d S r6  r   r8  r   r   r   test_continue_in_finally  sX    


z%GrammarTests.test_continue_in_finallyc                 C   sF   dd }|  | d dd }|  | d dd }|  | d	 d S )
Nc                   S   s   zW dS    Y dS 0 d S rY   r   r   r   r   r   r2    s    z/GrammarTests.test_return_in_finally.<locals>.g1r%   c                   S   s   zW dS    Y dS 0 d S )Nr   r   r   r   r   r   r   r3    s
     z/GrammarTests.test_return_in_finally.<locals>.g2r   c                   S   s"   zdd  W dS    Y dS 0 d S )Nr%   r   r   r   r   r   r   r   r4    s    
z/GrammarTests.test_return_in_finally.<locals>.g3r   r   )r   r2  r3  r4  r   r   r   test_return_in_finally  s    z#GrammarTests.test_return_in_finallyc                 C   sT   dd }|  |dd |  |dd dd }|  |dd |  |dd d S )	Nc                 S   sT   dD ]D}d}|dk r|d7 }z|| W | r. q  S q| rD   Y q0 qqd||fS )Nr7  r   r  r   endr   r   r/  Zcount2r   r   r   r2    s     z;GrammarTests.test_break_in_finally_after_return.<locals>.g1Fr   T)r<  r%   r   c              	   S   sP   dD ]@}dD ]6}z|| W | r$  q    S q| r@   Y  q0 qqd||fS )Nr7  )r   r  r<  r   r=  r   r   r   r3    s    
 z;GrammarTests.test_break_in_finally_after_return.<locals>.g2r   r   r2  r3  r   r   r   "test_break_in_finally_after_return  s    	z/GrammarTests.test_break_in_finally_after_returnc                 C   sT   dd }|  |dd |  |dd dd }|  |dd	 |  |dd
 d S )Nc                 S   s@   d}|dk r8|d7 }z|W | r" qS q| r4   Y q0 qd|fS )Nr   r   r%   r<  r   r   r/  r   r   r   r2    s     z>GrammarTests.test_continue_in_finally_after_return.<locals>.g1Fr%   T)r<  r   c                 S   s8   dD ]*}z|W | r q  S q| r,   Y q0 qd|fS )Nr7  r<  r   r@  r   r   r   r3  ,  s     z>GrammarTests.test_continue_in_finally_after_return.<locals>.g2r   )r<  r%   r   r>  r   r   r   %test_continue_in_finally_after_return  s    
z2GrammarTests.test_continue_in_finally_after_returnc                 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 }dd }| t| dg 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 rY   r   r   r   r   r   r   9  r   z"GrammarTests.test_yield.<locals>.gc                   s   s   dE d H  d S Nr   r   r   r   r   r   r   :  r   c                  s   s
   dV } d S rY   r   rh   r   r   r   r   <  r   c                  s   s   dE d H } d S rB  r   rh   r   r   r   r   =  r   c                   s   s
   dV  d S N)r%   r%   r   r   r   r   r   r   ?  r   c                  s   s
   dV } d S rC  r   rh   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 rY   r   r   r   r   r   r   E  r   c                   s   s   ddE d H f d S Nr%   r   r   r   r   r   r   r   F  r   zdef g(): 1, yield 1zdef g(): 1, yield from ()c                   s   s   t dV  d S rY   r   r   r   r   r   r   J  r   c                   s   s   t dV d d S rY   r   r   r   r   r   r   K  r   c                   s   s   t dE d H  d S rB  r   r   r   r   r   r   L  r   c                   s   s   t dE d H d d S )Nr   r%   r   r   r   r   r   r   M  r   c                  s   s   d} dddg| R V  d S )Nr   rW   r   r%   r   r   r   r   r   r   r   r   O  r   )r%   r   r   r   rW   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   r   r   
test_yield7  s6    











zGrammarTests.test_yieldc                 C   s~   dd }dd }| j }|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 fD  d S )Nc                 S   s   g | ]}|qS r   r   r	  r   r   r   r  `  r   HGrammarTests.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 fD  d S )Nc                 S   s   g | ]}|qS r   r   r	  r   r   r   r  a  r   rI  r   r   r   r   r   r   r   a  r   z 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   r0   r   r   r   test_yield_in_comprehensions^  sB    z)GrammarTests.test_yield_in_comprehensionsc                 C   s@   zt dW n t y   Y n0 ztW n ty:   Y n0 d S )Nzjust testing)RuntimeErrorKeyboardInterruptr&   r   r   r   
test_raisey  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   rN  rO  rP  )r   r   rN  rO  rP  r   r   r   test_import  s    zGrammarTests.test_importc                 C   s   d S rI   r   r&   r   r   r   test_global  s    zGrammarTests.test_globalc                    s   d d fdd}d S )Nr   c                      s   d S rI   r   r   r   r   r   rd     s    z%GrammarTests.test_nonlocal.<locals>.fr   )r   rd   r   r   r   test_nonlocal  s    zGrammarTests.test_nonlocalc              
   C   sl   zW n. t y2 } z| d W Y d }~n
d }~0 0 zW n. t yf } z| d W Y d }~n
d }~0 0 d S )Nz6'assert True' should not have raised an AssertionErrorz;'assert True, msg' should not have raised an AssertionError)AssertionErrorr    r   ru   r   r   r   test_assert  s     zGrammarTests.test_assertFz Won't work if __debug__ is Falsec              
   C   s   zW n6 t y: } z| |jd d W Y d }~nd }~0 0 | d zW n6 t y } z| t|jd W Y d }~nd }~0 0 | d | dd t ( tdt	 t
dd	d
 W d    n1 s0    Y  d S )Nr   r-  z%AssertionError not raised by assert 0z+AssertionError not raised by 'assert False'zassert(x, "msg")zassertion is always trueerrorzassert x, "msg"r'  r>   )rT  r   r   r    r7   r   warningscatch_warningssimplefilterSyntaxWarningr?   rU  r   r   r   testAssert2  s     (
(

zGrammarTests.testAssert2c                 C   s   d S rI   r   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   
test_while  s    zGrammarTests.test_whilec                 C   s   dD ]}qdD ]
\}}}qG dd d}d}|dD ]}|| }q4|dkrT|  d g }d	D ]\}|| q\| |g d d S )
Nr   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 rI   )maxsofar)r   r_  r   r   r   rL     s    z/GrammarTests.test_for.<locals>.Squares.__init__c                 S   s
   t | jS rI   )r7   r`  r&   r   r   r   __len__  r   z.GrammarTests.test_for.<locals>.Squares.__len__c                 S   sR   d|  kr| j k sn tt| j}||krH| j||  |d }q&| j| S )Nr   r%   )r_  
IndexErrorr7   r`  append)r   r   nr   r   r   rS     s    

z2GrammarTests.test_for.<locals>.Squares.__getitem__N)rD   rE   rF   rL   ra  rS   r   r   r   r   Squares  s   re  r   r   i  zfor over growing sequence)r   r   r   )r    rc  r   )r   r   r   r   re  rd  r   resultr   r   r   test_for  s    


zGrammarTests.test_forc              
   C   s  zdd  W n t y   Y n0 zdd  W n> ty>   Y n. ty` } zW Y d }~nd }~0    Y n0 zdd  W n ttt fy   Y n0 zdd  W n* ttt fy } zW Y d }~n
d }~0 0 zW n0 | t( tddd tddd W d    n1 s0    Y  d S )Nr%   r   z/try:
    pass
except Exception as a.b:
    pass?r>   z0try:
    pass
except Exception as a[b]:
    pass)rr   EOFErrorr  r   r   r?   )r   r-  r   r   r   test_try  s"    "*zGrammarTests.test_tryc                 C   s   d S rI   r   r&   r   r   r   
test_suite  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r8||u r@||urHddv rPddvrXdd  k rd  krd  krd  krd  krd  krd  v r|  vr|  u r|urn n d S rD  r   r   r   r   r   test_comparison2  s    zGrammarTests.test_comparisonc                    s   d fdd	}|d |d |d |d |dd	 t  L t d
t tddd tddd tddd tddd W d    n1 s0    Y  d S )N"is" with a literalc                    s     | | d S rI   r   )r   r-  r&   r   r   r0   D  s    z6GrammarTests.test_comparison_is_literal.<locals>.checkzx is 1zx is "thing"z1 is xzx is y is 1z
x is not 1z"is not" with a literalrW  z	x is Noner'  r>   z
x is Falsez	x is Truezx is ...)rn  rX  rY  rZ  r[  r?   r/   r   r&   r   test_comparison_is_literalC  s    

z'GrammarTests.test_comparison_is_literalc                    sl   f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/ d0 |d1 |d2 d3 |d4 |d5 d6 |d7 d8 |d9 d: |d; |d< |d= d> |d? |d@ |dA |dB |dC t  d t dDt tdEdFdG tdHdFdG tdIdFdG tdJdFdG tdKdFdG tdLdFdG W d    n1 s^0    Y  d S )MNc                    s    |   d S rI   ro  )r   r-  r   r   r   r0   U  s    z2GrammarTests.test_warn_missed_comma.<locals>.checkz-is not callable; perhaps you missed a comma\?z[(1, 2) (3, 4)]z[(x, y) (3, 4)]z[[1, 2] (3, 4)]z[{1, 2} (3, 4)]z[{1: 2} (3, 4)]z[[i for i in range(5)] (3, 4)]z[{i for i in range(5)} (3, 4)]z[(i for i in range(5)) (3, 4)]z![{i: i for i in range(5)} (3, 4)]z[f"{x}" (3, 4)]z[f"x={x}" (3, 4)]z["abc" (3, 4)]z[b"abc" (3, 4)]z[123 (3, 4)]z[12.3 (3, 4)]z[12.3j (3, 4)]z[None (3, 4)]z[True (3, 4)]z[... (3, 4)]z2is not subscriptable; perhaps you missed a comma\?z[{1, 2} [i, j]]z[{i for i in range(5)} [i, j]]z[(i for i in range(5)) [i, j]]z[(lambda x, y: x) [i, j]]z[123 [i, j]]z[12.3 [i, j]]z[12.3j [i, j]]z[None [i, j]]z[True [i, j]]z[... [i, j]]zKindices must be integers or slices, not tuple; perhaps you missed a comma\?z[(1, 2) [i, j]]z[(x, y) [i, j]]z[[1, 2] [i, j]]z[[i for i in range(5)] [i, j]]z[f"{x}" [i, j]]z[f"x={x}" [i, j]]z["abc" [i, j]]z[b"abc" [i, j]]z.indices must be integers or slices, not tuple;z[[1, 2] [3, 4]]z-indices must be integers or slices, not list;z[[1, 2] [[3, 4]]]z [[1, 2] [[i for i in range(5)]]]z,indices must be integers or slices, not set;z[[1, 2] [{3, 4}]]z [[1, 2] [{i for i in range(5)}]]z-indices must be integers or slices, not dict;z[[1, 2] [{3: 4}]]z#[[1, 2] [{i: i for i in range(5)}]]z2indices must be integers or slices, not generator;z [[1, 2] [(i for i in range(5))]]z1indices must be integers or slices, not function;z[[1, 2] [(lambda x, y: x)]]z,indices must be integers or slices, not str;z[[1, 2] [f"{x}"]]z[[1, 2] [f"x={x}"]]z[[1, 2] ["abc"]]z'indices must be integers or slices, notz[[1, 2] [b"abc"]]z[[1, 2] [12.3]]z[[1, 2] [12.3j]]z[[1, 2] [None]]z[[1, 2] [...]]rW  z[(lambda x, y: x) (3, 4)]r'  r>   z[[1, 2] [i]]z[[1, 2] [0]]z[[1, 2] [True]]z[[1, 2] [1:2]]z[{(1, 2): 3} [i, j]]rp  r/   r   rr  r   test_warn_missed_commaT  s    
z#GrammarTests.test_warn_missed_commac                 C   s   d}d}d}d S )Nr%   r   r   r   r   r   r   test_binary_mask_ops  s    z!GrammarTests.test_binary_mask_opsc                 C   s   d}d}d}d S )Nr   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   test_additive_ops  s    zGrammarTests.test_additive_opsc                 C   s   d}d}d}d}d S )Nr%         ?r   g        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  rw  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   rN  Z01234r  rW   r%   r   r   r   r_   r   r   c                 S   s   t | j| fS rI   )r   rD   rh   r   r   r   r     r   z-GrammarTests.test_selectors.<locals>.<lambda>r   z[1, (1,), (1, 2), (1, 2, 3)])r   rN  rO  modulesr\   sortr   r@   )	r   r   rN  r   r   ra   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}h d}h d}|}d}d}d S )Nr%   r   r   rZ   r   )rZ   r   r   rW   r   )rZ   r   threeZfourZfiveZsix>   rZ   r   r  >   r   r   r   r   r   r   r   r   r   r   
test_atoms  s,    

zGrammarTests.test_atomsc                    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p~ G dd d}  }	G dd d}
 fddG dd d}d dgd G dd d}   G dd d} gd  j j G d!d" d"}d S )#Nc                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.BNr   r   r   r   r   B  r   r  c                   @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.B2Nr   r   r   r   r   B2  r   r  c                   @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.C1Nr   r   r   r   r   C1  r   r  c                   @   s   e Zd ZdS )z&GrammarTests.test_classdef.<locals>.C2Nr   r   r   r   r   C2	  r   r  c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.DNr   r   r   r   r   D
  r   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 rI   r   r&   r   r   r   meth1  r   z+GrammarTests.test_classdef.<locals>.C.meth1c                 S   s   d S rI   r   )r   argr   r   r   meth2  r   z+GrammarTests.test_classdef.<locals>.C.meth2c                 S   s   d S rI   r   )r   r   r   r   r   r   meth3  r   z+GrammarTests.test_classdef.<locals>.C.meth3N)rD   rE   rF   r  r  r  r   r   r   r   ro     s   ro   c                 S   s   | S rI   r   rh   r   r   r   class_decorator  r   z3GrammarTests.test_classdef.<locals>.class_decoratorc                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.GNr   r   r   r   r   G  s   r  Fc                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.HNr   r   r   r   r   H  s   r  c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.INr   r   r   r   r   I  s   r  c                    s    | S rI   r   )r   r  r   r   r     r   z,GrammarTests.test_classdef.<locals>.<lambda>c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.JNr   r   r   r   r   J  s   r  .r%   c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.KNr   r   r   r   r   K  s   r  c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.LNr   r   r   r   r   r     s   r  r   c                   @   s   e Zd ZdS )z%GrammarTests.test_classdef.<locals>.MNr   r   r   r   r   r   "  s   r   )r  )r   r  r  r  r  r  ro   r  r  r   r  r  r  r  r   r   r  r   test_classdef  s*    

zGrammarTests.test_classdefc                 C   s*   g d}|  dd |D dddd d S )Nr   c                 S   s   i | ]}||d  qS r   r   r
  r   r   r   r   
<dictcomp>*  r   z/GrammarTests.test_dictcomps.<locals>.<dictcomp>r   r   r   r   )r   numsr   r   r   test_dictcomps%  s    zGrammarTests.test_dictcompsc              	      sZ  g d}g dg d} dd |D g d  dd |D g d  d	d |D g d
  fdd|D g d  fdd|D g d  dd tdD dgddgg dg dg dg dd } ||g d fdd}|  td td g d}g d g d fdd|D } |g d  d S )!Nr   )AppleBananaCoconut)z  Applez Banana zCoco  nut  c                 S   s   g | ]}|  qS r   )strip)r
  r!   r   r   r   r  2  r   z/GrammarTests.test_listcomps.<locals>.<listcomp>)r  r  z	Coco  nutc                 S   s   g | ]}d | qS r   r   r	  r   r   r   r  3  r   )r   r   r   r      c                 S   s   g | ]}|d kr|qS r   r   r	  r   r   r   r  4  r   r   c                    s   g | ]} D ]}||fqqS r   r   r
  r   r!   strsr   r   r  5  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  )rW   r  rW   r  rW   r  c                    s(   g | ] }d d  D D ]}||fqqS )c                 S   s   g | ]}d |v r|qS )rd  r   )r
  rd   r   r   r   r  ;  r   z:GrammarTests.test_listcomps.<locals>.<listcomp>.<listcomp>r   r  r  r   r   r  ;  r   )
r  r  r  r  r  r  r  r  r  r  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   r   r   r  ?  r   zLGrammarTests.test_listcomps.<locals>.<listcomp>.<lambda>.<locals>.<listcomp>r%   ranger   r   r   r   r   ?  r   z8GrammarTests.test_listcomps.<locals>.<listcomp>.<lambda>r   )r
  r   r   r   r   r  ?  r   rW   r%   )r%   r   r   )r%   r   r      )r%   r      @      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  C  r   zEGrammarTests.test_listcomps.<locals>.test_in_func.<locals>.<listcomp>r   )lr   r   r   test_in_funcB  s    z1GrammarTests.test_listcomps.<locals>.test_in_func)FFFc                      s,     dd dD ddgddgdd	gg d S )
Nc                 S   s"   g | ]}d d ||d fD qS )c                 S   s   g | ]}|qS r   r   r
  r:   r   r   r   r  H  r   zUGrammarTests.test_listcomps.<locals>.test_nested_front.<locals>.<listcomp>.<listcomp>r%   r   r	  r   r   r   r  H  r   zJGrammarTests.test_listcomps.<locals>.test_nested_front.<locals>.<listcomp>)r%   r   rW   r%   r   r   r   rW   r   r   r   r&   r   r   test_nested_frontG  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   r  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  r  )r  r  )r  r  )r  r  )r   r  r   )r   r  Zspcsr  r  Z	suppliersr   r   )r  r   r  r  r   test_listcomps,  s8    

zGrammarTests.test_listcompsc                    s  dd t dD }| t|dd t dD  zt| | d W n tyX   Y n0 d z( fdd D }t| | d	 W n ty   Y n0 | 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  m  r   z7GrammarTests.test_genexps.<locals>.<genexpr>.<listcomp>r   Nr  r	  r   r   r   	<genexpr>m  r   z,GrammarTests.test_genexps.<locals>.<genexpr>r%   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r  n  r   z-GrammarTests.test_genexps.<locals>.<listcomp>r   z&should produce StopIteration exceptionc                 3   s   | ]
} V  qd S rI   r   )r
  r   r   r   r   r  w  r   zshould produce TypeErrorc                 s   s    | ]}d D ]}||fV  q
qdS )r&  Nr   r
  r   r:   r   r   r   r  }  r   r&  c                 S   s   g | ]}d D ]}||fqqS )r&  r   r  r   r   r   r  }  r   c                 s   s    | ]}d D ]}||fV  q
qdS )xyNr   r  r   r   r   r  ~  r   abc                 S   s   g | ]}d D ]}||fqqS )r  r   r  r   r   r   r  ~  r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]}|d  V  qdS r   Nr   r	  r   r   r   r    r   c                 S   s   g | ]}|d  qS r   r   r	  r   r   r   r    r   c                 s   s   | ]}|d  r|| V  qdS r  r   r	  r   r   r   r    r   c                 S   s   g | ]}|d  r|| qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r
  rn   r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r	  r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qdS TNr   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   c                 s   s   | ]
}|V  qdS r  r   r  r   r   r   r    r   c                 S   s   g | ]}|qS r   r   r	  r   r   r   r    r   c                 s   s   | ]
}|V  qdS r  r   r	  r   r   r   r    r   c                 s   s   | ]}d r|V  qdS )FNr   r  r   r   r   r    r   c                 s   s   | ]
}|V  qdS r  r   r  r   r   r   r    r   r   zfoo(x for x in range(10), 100)zfoo(100, x for x in range(10)))	r  r   nextr    StopIterationr  r\   sumr   )r   r   rb   r   r   r   test_genexpsk  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 g d | tdd t dD g d | dd	 dD g d | tdd dD g d d S )Nr   c                 s   s   | ]
}|V  qd S rI   r   r  r   r   r   r    r   z;GrammarTests.test_comprehension_specials.<locals>.<genexpr>rW   Fc                 3   s(   | ] } rt D ]}||fV  qqd S rI   r  r
  r   r   tr   r   r   r    r   Tc                 S   s"   g | ]}t d D ]}||fqqS rW   r  r  r   r   r   r    r   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%   rW   r   c                 s   s"   | ]}|d  r|d r|V  qdS )r   r   Nr   r	  r   r   r   r    r   c                 S   s   g | ]
\}|qS r   r   r	  r   r   r   r    r   ))r   r  )r   rE  c                 s   s   | ]\}|V  qd S rI   r   r	  r   r   r   r    r   ))r   )r   )r   )r   r   r   )r  r   r7   r\   rG  r   r  r   test_comprehension_specials  s     "z(GrammarTests.test_comprehension_specialsc              	   C   s  G dd dt }|  W d    n1 s,0    Y  | }W d    n1 sR0    Y  | \}}W d    n1 s|0    Y  | 6 |  W d    n1 s0    Y  W d    n1 s0    Y  | 6}| }W d    n1 s0    Y  W d    n1 s0    Y  | 8}|  W d    n1 sF0    Y  W d    n1 sf0    Y  t sg d}|D ]@}| j|d t|dd W d    n1 s0    Y  qd 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 Nr_   r   r&   r   r   r   	__enter__  s    z;GrammarTests.test_with_statement.<locals>.manager.__enter__c                 W   s   d S rI   r   )r   r   r   r   r   __exit__  s    z:GrammarTests.test_with_statement.<locals>.manager.__exit__N)rD   rE   rF   r  r  r   r   r   r   manager  s   r  )zif 1:
                    with (
                        manager()
                    ):
                        pass
                zif 1:
                    with (
                        manager() as x
                    ):
                        pass
                zif 1:
                    with (
                        manager() as (x, y),
                        manager() as z,
                    ):
                        pass
                zif 1:
                    with (
                        manager(),
                        manager()
                    ):
                        pass
                zif 1:
                    with (
                        manager() as x,
                        manager() as y
                    ):
                        pass
                zif 1:
                    with (
                        manager() as x,
                        manager()
                    ):
                        pass
                zif 1:
                    with (
                        manager() as x,
                        manager() as y,
                        manager() as z,
                    ):
                        pass
                zif 1:
                    with (
                        manager() as x,
                        manager() as y,
                        manager(),
                    ):
                        pass
                )r$  z<string>r>   )objectr   r!  r?   )r   r  r   r:   Z
test_casesr$  r   r   r   test_with_statement  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 correctlyrf   )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   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   c                 S   s   | rdS dS )NFTr   rh   r   r   r   r     r   c                 S   s   | rdS dS )NTFr   rh   r   r   r   r     r   r%   rW   zcheck 1r   zcheck 2r   zcheck 3r   r   zcheck 4zcheck 5zcheck 6Fr   r   r   g      @r   )r   r  r   r   r   test_if_else_expr  s$    $$"zGrammarTests.test_if_else_exprc                 C   sr   |  dd |  dd |  dd d}d}| d||u u  | d|u |u  | d|  u of|u n   d S )Nr   r   r   F)r   r   ZassertFalser9   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   r   r   r   r   
__matmul__  s    z2GrammarTests.test_matrix_mul.<locals>.M.__matmul__c                 S   s
   || _ | S rI   )otherr  r   r   r   __imatmul__   s    z3GrammarTests.test_matrix_mul.<locals>.M.__imatmul__N)rD   rE   rF   r  r  r   r   r   r   r     s   r   r   rX   )r   r  )r   r   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 rI   r   r   r   r   r   r  *  s    z8GrammarTests.test_async_await.<locals>.test.<locals>.sum)Zsomeobj)r  r   r   r   r   )  s    z+GrammarTests.test_async_await.<locals>.testr   c                 S   s   t | dd | S )N_markedT)setattr)funcr   r   r   	decorator2  s    z0GrammarTests.test_async_await.<locals>.decoratorc                      s   dS )N   r   r   r   r   r   test26  s    z,GrammarTests.test_async_await.<locals>.test2r  )	r   rD   r   boolr  co_flagsinspectZCO_COROUTINEr  )r   r   r  r  r   r   r   test_async_await(  s    
zGrammarTests.test_async_awaitc                    sf   G dd dt G dd d  fdd}|  | d  W d    n1 sX0    Y  d S )Nc                   @   s   e Zd ZdS )z)GrammarTests.test_async_for.<locals>.DoneNr   r   r   r   r   Done>  r   r  c                   @   s   e Zd Zdd Zdd ZdS )z*GrammarTests.test_async_for.<locals>.AIterc                 S   s   | S rI   r   r&   r   r   r   	__aiter__A  s    z4GrammarTests.test_async_for.<locals>.AIter.__aiter__c                    s   t d S rI   )StopAsyncIterationr&   r   r   r   	__anext__C  s    z4GrammarTests.test_async_for.<locals>.AIter.__anext__N)rD   rE   rF   r  r  r   r   r   r   AIter@  s   r  c                     sN     2 z3 d H W } q6   2 z3 d H W \} }q6   2 z3 d H W } q66 d S rI   r   r   r  r  r   r   r  F  s    z(GrammarTests.test_async_for.<locals>.foort   r   sendr   r  r   r  r   test_async_for=  s
    zGrammarTests.test_async_forc                    sf   G dd dt  G dd d fdd}|   | d  W d    n1 sX0    Y  d S )Nc                   @   s   e Zd ZdS )z*GrammarTests.test_async_with.<locals>.DoneNr   r   r   r   r   r  U  r   r  c                   @   s   e Zd Zdd Zdd ZdS )z-GrammarTests.test_async_with.<locals>.managerc                    s   dS r  r   r&   r   r   r   
__aenter__X  s    z8GrammarTests.test_async_with.<locals>.manager.__aenter__c                    s   dS r  r   )r   excr   r   r   	__aexit__Z  s    z7GrammarTests.test_async_with.<locals>.manager.__aexit__N)rD   rE   rF   r  r  r   r   r   r   r  W  s   r  c               
      s*   4 I d H  W d   I d H  q:1 I d H s00    Y   4 I d H } W d   I d H  qt1 I d H sj0    Y   4 I d H \} }W d   I d H  q1 I d H s0    Y   4 I d H R  4 I d H  W d   I d H  q1 I d H s0    Y  W d   I d H  q*1 I d H s 0    Y   4 I d H V}  4 I d H }W d   I d H  qx1 I d H sn0    Y  W d   I d H  q1 I d H s0    Y   4 I d H V}  4 I d H  W d   I d H  q1 I d H s0    Y  W d   I d H  q"1 I d H s0    Y   d S rI   r   r   r  r  r   r   r  ]  s    *** X \ \z)GrammarTests.test_async_with.<locals>.foor  r  r   r  r   test_async_withT  s
    zGrammarTests.test_async_withN)GrD   rE   rF   rG   r   r   rV   r]   r^   rv   r   r   r   r   r   r   r   r  r  r  r  r%  r(  r)  r*  r.  r1  r5  r9  r:  r;  r?  rA  rH  rJ  rM  rQ  rR  rS  rV  unittestZ
skipUnlessr\  r]  r^  rg  rj  rk  rl  rm  rq  rs  rt  ru  rv  rx  rz  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rU     s   .			 w'45'

R#!?#T
rU   __main__)rG   r   r   r   r  r  r   rX  Ztest.ann_moduler   r   collectionsr   r   r   r*   r,   ZTestCaser	   r[   rm   rH   rU   rD   mainr   r   r   r   <module>   s>   7 #	            g