B
    18\              =   @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ dd Z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/d0d1d2d3g/Z	d4gZ
dd5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKgZG dLdM dMejZG dNdO dOejZG dPdQ dQejZG dRdS dSejZdTdU ZdVdWgfdVdXgfdVdYdZd[d\g dg g dg fd]gg dfgfdVdYdZd[d\g dg g dg fd^gg dfgfdVdYdZd[d\d_gdg g dg fd`gg dfgfdVdYdZd[d\d_gdg g ddagfdbgg dfgfdVdYdZd[d\g dcg g dg fddgg dfgfdVdYdZd[d\g dg g deg fdfgg dfgfdVdYdZd[d\d_dgdhdidjgdkdlgdmgdndodpdqdrg dsfdtdug g fgfdvgg dfgfdVdwdZdxg g dygg fgfdVdwdZdxg g dzgg fgfdVdwdZdxd{gg dfgg fgfdVdYdZd[d\g dg g dg fd|gg dfgfdVd}dZd~gfgfdVddZdgdfgfdVdgfdVddZdddgg fgfdVddZddygg fgfdVddZddgg fgfdVddZdgdgfgfdVddZddgdgfgfdVddZddddgg fdfgfdVddZdgdddddgfgg g fgfdVddZdgg g dgfgfdVdgfdVddZdgfgfdVddZddgd fgfdVddZdEgfgfdVdgfdVdgfdVddZdddgg fgfdVddZdddgg fgfdVddZddddgdfdddgg fgfdVddZddddddgdsfdddddgdfdg d fgffgfdVddZddddddgdsfdddddgdfdg d fgffgfdVddZddddddgdsfdddddgdfdg d fgffgfdVddZddddddgdsfdddddgdfdg d fgffgfdVddZddZdddddg d fddddgd fgffgfdVddZddZdddddddgdfdg d fgffgfdVddZddZdddddgd fgffgfdVddZddZddddddgdfdg d fgffgfdVddZd[d\g dg g dg fddddddddg g fffgg dfgfdVddZd[d\g dg g dg fdddddgdgfgg dfgfdVddZd[d\g dg g dg fdddgdgfgg dfgfdVddZdtdZddgdtddgdgfdgffgfdVddZddZddddddgfdsfdgffgfdVddZd[d\g dg g dg fddddddddg dfgffgg dfgfg/ZddgfgZddddZdddgffdddddZd\g dg g dg fdffddtdZd gdgffddtdZg g ffdddZdgffddtdZdgdgffddddddddgd fgffddddddddgd fgffdddZd	d
d
gddgffdddZdd ddgddgffdddddddqdZdddgdsffddqdZg dsffdddZd	ddgdsffddddddgdsffdddZg dsffdddZddgg ffgZe  dS (      N)supportc             C   s   | d kst | tttfr| S t | tr4dd | D S | jjg}t| drdt| drd|| j	| j
f | jd krvt|S x"| jD ]}|tt| | q~W t|S )Nc             S   s   g | ]}t |qS  )to_tuple).0er   r   Q/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_ast.py
<listcomp>   s    zto_tuple.<locals>.<listcomp>lineno
col_offset)
isinstancestrintcomplexlist	__class____name__hasattrappendr	   r
   _fieldstupler   getattr)tresultfr   r   r   r   
   s    


r   Nonez'module docstring'zdef f(): passzdef f(): 'function docstring'zdef f(a): passzdef f(a=0): passzdef f(*args): passzdef f(**kwargs): passzGdef f(a, b=1, c=None, d=[], e={}, *args, f=42, **kwargs): 'doc for f()'zclass C:passz class C: 'docstring for class C'zclass C(object): passzdef f():return 1zdel vzv = 1zv += 1zfor v in v:passzwhile v:passz	if v:passzwith x as y: passzwith x as y, z as q: passzraise Exception('string')z$try:
  pass
except Exception:
  passztry:
  pass
finally:
  passzassert vz
import syszfrom sys import vzglobal v1passzfor v in v:breakzfor v in v:continuezfor a,b in c: passz[(a,b) for a,b in c]z((a,b) for a,b in c)z((a,b) for (a,b) in c)zL(
    (
    Aa
    ,
       Bb
    )
    for
    Aa
    ,
    Bb in Cc
    )z"{a : b for w in x for m in p if g}z{a : b for v,w in x}z{r for l in x if g}z{r for l,m in x}z3async def f():
 'async function'
 await something()z,async def f():
 async for e in i: 1
 else: 2z$async def f():
 async with a as b: 1z{**{1:2}, 2:3}z{*{1, 2}, 3}z$async def f():
 [i async for b in c]z1+2za and bza + bznot vzlambda:Nonez{ 1:2 }z{}z{None,}z&{
      1
        :
          2
     }z[a for b in c if d]z(a for b in c if d)z	1 < 2 < 3zf(1,2,c=3,*d,**e)10z'string'za.bza[b:c]vz[1,2,3]z[]z1,2,3z(1,2,3)z()za.b.c.d(a.b[1:2])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ejd'd( Zd)S )*	AST_Testsc             C   s   t |tjr|jd krd S t |tjtjtjfrV|j|jf}| 	||k |j|jf}xR|jD ]H}t
||}t |trx.|D ]}| || q|W q^|d k	r^| || q^W d S )N)r   astASTr   exprstmtZexcepthandlerr	   r
   
assertTruer   r   _assertTrueorder)selfZast_nodeZ
parent_posZnode_posnamevalueZchildr   r   r   r%      s    


zAST_Tests._assertTrueorderc          	   C   s|   t  }| |jd d|_| |jd | |jd d | t |j W d Q R X | t	 t d W d Q R X d S )Nr   *   foobar   )
r    r!   assertEqualr   r*   __dict__assertRaisesAttributeErrorvararg	TypeError)r&   xr   r   r   test_AST_objects   s    zAST_Tests.test_AST_objectsc             C   sL   G dd d}t  }| |_||j_t|j}~t  | |  d S )Nc               @   s   e Zd ZdS )z0AST_Tests.test_AST_garbage_collection.<locals>.XN)r   
__module____qualname__r   r   r   r   X   s   r6   )	r    r!   r2   aweakrefrefr   
gc_collectassertIsNone)r&   r6   r7   r9   r   r   r   test_AST_garbage_collection   s    z%AST_Tests.test_AST_garbage_collectionc             C   s   xt tdfttdfttdffD ]\}}}xt||D ]v\}}| jd|d2 t|d|t	j
}| t|| | |d W d Q R X | jd||d	 t|d| W d Q R X q4W qW d S )
NexecsingleevalZparsing)actioninput?)r   r   Z	compiling)r@   rA   kind)
exec_testsexec_resultssingle_testssingle_results
eval_testseval_resultszipsubTestcompiler    PyCF_ONLY_ASTr,   r   r%   )r&   rA   outputrC   ioZast_treer   r   r   test_snippets   s    
zAST_Tests.test_snippetsc             C   s<   t djd jj}| |j | |j | |j d S )Nzx[::]r   )	r    parsebodyr(   slicer;   upperlowerstep)r&   Zslcr   r   r   
test_slice   s    zAST_Tests.test_slicec             C   s    t djd }| |j d S )Nzfrom . import yr   )r    rR   rS   r;   module)r&   Zimr   r   r   test_from_import  s    zAST_Tests.test_from_importc             C   s>   t d}| |jd t j d |jd _t|dd d S )Nzfrom __future__ import divisionr   z __future__ z<test>r=   )r    rR   assertIsInstancerS   
ImportFromstriprY   rL   )r&   modr   r   r   !test_non_interned_future_from_ast  s    
z+AST_Tests.test_non_interned_future_from_astc             C   s|   |  ttjtj |  ttjtj |  ttjtj |  ttjtj |  ttjtj |  ttj	tj d S )N)
r$   
issubclassr    Forr#   Namer"   r!   comprehensionZGt)r&   r   r   r   test_base_classes  s    zAST_Tests.test_base_classesc             C   s^   xXt j D ]J\}}t|tr|dkr|d  r| }t|t jr| t|jt	 qW d S )Nr!   r   )
r    r-   itemsr   typeisupperr!   r,   r   r   )r&   r'   itemr2   r   r   r   test_field_attr_existence  s
    z#AST_Tests.test_field_attr_existencec          	   C   sT   t  }| |jd | t |j W d Q R X t jtdd }| |jd d S )N)argsr0   
kwonlyargskw_defaultskwargdefaults      r+   )r    	argumentsr,   r   r.   r/   r0   range)r&   r2   r   r   r   test_arguments  s    zAST_Tests.test_argumentsc             C   s    t  }d|_| |jd d S )Ni  )r    Numr   r,   )r&   r2   r   r   r   test_field_attr_writable'  s    z"AST_Tests.test_field_attr_writablec          	   C   s   t  }| |jd | t |j W d Q R X t d}| |jd | t |j W d Q R X | t |j W d Q R X t jdd}| |jd t jddd}| |jd | |jd | |jd | t	t jdd | jt	t jdddd d S )N)nr)   r+   )r	   r   ro   )
r    rt   r,   r   r.   r/   rv   r	   r*   r1   )r&   r2   r   r   r   test_classattrs-  s$    
zAST_Tests.test_classattrsc             C   s(   t dg}t |}| |j| d S )Nr)   )r    rt   Moduler,   rS   )r&   rS   r2   r   r   r   test_moduleH  s    
zAST_Tests.test_modulec          	   C   s  t  }| |jd d|_| |jd t d}t d}t  }t |||}| |j| | |j| | |j	| t ddd}| |jd | |jd | |j	d t jddddd}| |jd | |jd | |j	d | |j
d | tt jdddd | jtt jdddddd t jddddd	}| |jd | |jd | |j	d | |j
d t jdddd
d}| |jd
 d S )N)leftopright   ro      r+   r   )r	      )rz   r{   r|   r	   r)   )	foobarbaz)r    BinOpr,   r   r   rt   Addrz   r{   r|   r	   r.   r1   )r&   r2   Zn1Zn3Zaddopr   r   r   test_nodeclassesM  s:    

zAST_Tests.test_nodeclassesc             C   s   t  }| |jd d S )Nr   )r    ZSubr,   r   )r&   r2   r   r   r   test_no_fieldsy  s    zAST_Tests.test_no_fieldsc       	   	   C   s   dd l }|g}ydd l}|| W n tk
r8   Y nX dddg}xX|D ]P}xJ|D ]B}x<dd tD D ]*}||||}| t|t| qhW qTW qJW d S )Nr   ro   r+   c             s   s   | ]}t |d ddV  qdS )rB   r=   i   N)rL   )r   rO   r   r   r   	<genexpr>  s    z*AST_Tests.test_pickling.<locals>.<genexpr>)	picklecPickler   ImportErrorrD   loadsdumpsr,   r   )	r&   r   Zmodsr   Z	protocolsr^   protocolr    Zast2r   r   r   test_pickling~  s    


zAST_Tests.test_picklingc          	   C   sb   t ddd}ttjtjf |f|g}| t}t|dd W d Q R X | dt	|j
 d S )Nr+   r~   )r	   r
   z<test>r=   zbut got <_ast.expr)dictr    rx   Exprr"   r.   r1   rL   assertInr   	exception)r&   posmcmr   r   r   test_invalid_sum  s
    zAST_Tests.test_invalid_sumc          	   C   s`   t t t dt  g}t | | t}t|dd W d Q R X | 	dt
|j d S )Nr)   z<test>r=   zidentifier must be of type str)r    rx   r   rb   Loadfix_missing_locationsr.   r1   rL   r   r   r   )r&   r   r   r   r   r   test_invalid_identitifer  s
    
z"AST_Tests.test_invalid_identitiferc          	   C   sV   t d}d |jd jd j_| t}t|dd W d Q R X | dt|j	 d S )Nzdef f():
 yield from g()r   z<test>r=   zfield value is required)
r    rR   rS   r(   r.   
ValueErrorrL   r   r   r   )r&   Zempty_yield_fromr   r   r   r   test_empty_yield_from  s
    
zAST_Tests.test_empty_yield_fromc          	   C   s>   dd l }dd }t|d| | ttjd W d Q R X d S )Nr   c              W   s   d S )Nr   )rj   r   r   r   bad_normalize  s    z0AST_Tests.test_issue31592.<locals>.bad_normalize	normalizeu   ϕ)unicodedatar   	swap_attrr.   r1   r    rR   )r&   r   r   r   r   r   test_issue31592  s    zAST_Tests.test_issue31592N)r   r4   r5   r%   r3   r<   rQ   rX   rZ   r_   rd   ri   rs   ru   rw   ry   r   r   r   r   r   r   r   cpython_onlyr   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S )ASTHelpers_Testc             C   s6   t d}tdddt j}| t |t | d S )Nz
foo(1 + 1)z	<unknown>r=   )r    rR   rL   rM   r,   dump)r&   r7   br   r   r   
test_parse  s    
zASTHelpers_Test.test_parsec             C   sT   ydd  W nB t k
rN   | t}td W d Q R X | |jj Y nX d S )Nro   r   z'\U')	Exceptionr.   SyntaxErrorr    literal_evalassertIsNotNoner   __context__)r&   r   r   r   r   test_parse_in_error  s    z#ASTHelpers_Test.test_parse_in_errorc             C   sL   t d}| t |d | t j|ddd | t j|ddd d S )	Nzspam(eggs, "and cheese")zModule(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], keywords=[]))])F)Zannotate_fieldszYModule([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), Str('and cheese')], []))])T)include_attributesa   Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), lineno=1, col_offset=5), Str(s='and cheese', lineno=1, col_offset=11)], keywords=[], lineno=1, col_offset=0), lineno=1, col_offset=0)]))r    rR   r,   r   )r&   noder   r   r   	test_dump  s    
zASTHelpers_Test.test_dumpc             C   sB   t jddd}t t d|jj|j_| t j|ddd d S )Nz1 + 1r?   )moder+   T)r   zExpression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, col_offset=0)))r    rR   copy_locationrt   rS   r|   r,   r   )r&   srcr   r   r   test_copy_location  s    z"ASTHelpers_Test.test_copy_locationc             C   sf   t d}|jt t t dt  t dgg  | 	|t 
| | 	t j|ddd d S )Nzwrite("spam")spamZeggsT)r   a|  Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, col_offset=6)], keywords=[], lineno=1, col_offset=0), lineno=1, col_offset=0), Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], keywords=[], lineno=1, col_offset=0), lineno=1, col_offset=0)]))r    rR   rS   r   r   Callrb   r   Strr,   r   r   )r&   r   r   r   r   test_fix_missing_locations  s    
z*ASTHelpers_Test.test_fix_missing_locationsc             C   s|   t jddd}| t j|dd| | t j|ddd t jddd}| t j|jdd|j | t j|ddd d S )	Nz1 + 1r?   )r   r~   )rv   T)r   zExpression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, col_offset=0)))r    rR   r,   Zincrement_linenor   rS   )r&   r   r   r   r   test_increment_lineno  s    z%ASTHelpers_Test.test_increment_linenoc             C   sH   t jddd}tt |j}| |djd | |g g d d S )Nzfoo()r?   )r   funcfoo)keywordsrj   )r    rR   r   Ziter_fieldsrS   r,   popid)r&   r   dr   r   r   test_iter_fields  s    z ASTHelpers_Test.test_iter_fieldsc             C   s   t jddd}| ttt |jd t |j}| t|jd | t|j	d | t|j	d | t 
t|d d S )	Nzspam(23, 42, eggs='leek')r?   )r   r   r      r)   z(keyword(arg='eggs', value=Str(s='leek')))r    rR   r,   lenr   Ziter_child_nodesrS   nextr   rv   r   )r&   r   iteratorr   r   r   test_iter_child_nodes   s    z%ASTHelpers_Test.test_iter_child_nodesc             C   s   t d}| t |d t d}| t |jd d t d}| t |jd d t d}| t |jd d d S )Nz"""line one
  line two"""zline one
line twoz&class foo:
  """line one
  line two"""r   z&def foo():
  """line one
  line two"""z#async def foo():
  """spam
  ham"""zspam
ham)r    rR   r,   get_docstringrS   )r&   r   r   r   r   test_get_docstring  s    



z"ASTHelpers_Test.test_get_docstringc             C   s.  |  ttd td}|  t| td}|  t| td}|  t|jd  td}|  t|jd  td}|  t|jd  td}|  t|jd  td}|  t|jd  td	}|  t|jd  td
}|  t|jd  d S )N zx = "not docstring"zdef foo():
  passzclass foo:
  passr   z class foo:
  x = "not docstring"z class foo:
  def bar(self): passz def foo():
  x = "not docstring"zasync def foo():
  passz&async def foo():
  x = "not docstring")r;   r    r   rR   rS   )r&   r   r   r   r   test_get_docstring_none  s&    








z'ASTHelpers_Test.test_get_docstring_nonec             C   s0  |  tddddg |  tdddi |  tdd	 |  td
dddh |  tdd | ttjd |  tdd |  tdd |  tdd |  tdd |  tdd |  tdd |  ttdd | ttjd | ttjd | ttjd d S )Nz	[1, 2, 3]ro   r+   r~   z{"foo": 42}r   r)   z(True, False, None))TFNz	{1, 2, 3}zb"hi"s   hizfoo()6   z+6z-6iz3.25g      
@z+3.25z-3.25g      
z-0.0z++6z+Truez2+3)r,   r    r   r.   r   repr)r&   r   r   r   test_literal_eval3  s     z!ASTHelpers_Test.test_literal_evalc             C   s>  |  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 |  tdd |  tdd
 | ttjd | ttjd | ttjd | ttjd | ttjd d S )NZ6jy              @z-6jy             z6.75jy              @z-6.75jy             z3+6jy      @      @z-3+6jy            @z3-6jy      @      z-3-6jy            z
3.25+6.75jy      
@      @z-3.25+6.75jy      
      @z
3.25-6.75jy      
@      z-3.25-6.75jy      
      z(3+6j)z-6j+3z-6j+3jz3+-6jz3+(0+6j)z-(3+6j))r,   r    r   r.   r   )r&   r   r   r   test_literal_eval_complexE  s$    z)ASTHelpers_Test.test_literal_eval_complexc          	   C   sb   t jdt jddgd d d dg}t |}| t}t|dd W d Q R X | dt|j	 d S )Ntimesleep)r'   )rY   nameslevelr	   r
   testr=   zinvalid integer value: None)
r    r\   aliasrx   r.   r   rL   r   r   r   )r&   rS   r^   r   r   r   r   test_bad_integerZ  s    
z ASTHelpers_Test.test_bad_integerc             C   sT   t jdt jddgd dddg}t |}t|dd}i }t|| | d| d S )Nr   r   )r'   r   )rY   r   r   r	   r
   r   r=   )r    r\   r   rx   rL   r=   r   )r&   rS   r^   codensr   r   r   test_level_as_nonee  s    

z"ASTHelpers_Test.test_level_as_noneN)r   r4   r5   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	r   c               @   s  e Zd Zd`edddZdaedddZdbd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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0d^d_ Z1dS )cASTValidatorTestsNr=   )excc         	   C   sV   d |_ |_t| | |}t|d| W d Q R X |d k	rR| |t|j d S )Nr   z<test>)	r	   r
   r    r   r.   rL   r   r   r   )r&   r^   msgr   r   r   r   r   r   r^   s  s    
zASTValidatorTests.modc            C   s&   t t |g}| j|||d d S )N)r   )r    rx   r   r^   )r&   r   r   r   r^   r   r   r   r"   {  s    zASTValidatorTests.exprc             C   s   t |g}| || d S )N)r    rx   r^   )r&   r#   r   r^   r   r   r   r#     s    zASTValidatorTests.stmtc          	   C   sT   t t t dt  g}| |dd t t dt  }| |dd d S )Nr2   zmust have Load contextr>   r?   )r    Interactiver   rb   Storer^   
Expression)r&   r   r   r   r   ry     s    zASTValidatorTests.test_modulec          	      s  d fdd	}t dt dt  g}|||dd |||dd ||t dgdd	 ||t d
gdd t dt dt  g}|||t dt  gdd t dt dt  t dt dt  g}|||d t dt  gdd d S )Nc                sL   | d krg } |d krg }|d kr$g }|d kr0g }t | |||||}  | S )N)r    rq   )rj   r0   rk   rm   rn   rl   )facr   r   rq     s    z5ASTValidatorTests._check_arguments.<locals>.argumentsr2   )rj   zmust have Load context)rk   r~   )rn   z"more positional defaults than argsr   )rl   z3length of kwonlyargs is not the same as kw_defaults)rj   rn   r7   r   y)rk   rl   )NNNNNN)r    argrb   r   rt   r   )r&   r   checkrq   rj   r   )r   r   _check_arguments  s$      z"ASTValidatorTests._check_argumentsc             C   s   t g d g g d g }t d|g g d }| |d t d|t  gt dt  gd }| |d t d|t  gg t dt  }| |d dd }| || j d S )Nr2   zempty body on FunctionDefzmust have Load contextc             S   s   t d| t  gg d S )Nr2   )r    FunctionDefPass)rj   r   r   r   r     s    z+ASTValidatorTests.test_funcdef.<locals>.fac)r    rq   r   r#   r   rb   r   r   )r&   r7   r   r   r   r   r   test_funcdef  s     zASTValidatorTests.test_funcdefc             C   s   ddd}|  |tdt gdd |  |tdtdt gdd |  |g dd |  |d gdd	 |  |tdt gd
d d S )Nc             S   sH   | d krg } |d krg }|d kr*t  g}|d kr6g }t d| |||S )NZmyclass)r    r   ClassDef)basesr   rS   decorator_listr   r   r   cls  s    

z,ASTValidatorTests.test_classdef.<locals>.clsr2   )r   zmust have Load context)r   )rS   zempty body on ClassDefzNone disallowed)r   )NNNN)r#   r    rb   r   keyword)r&   r   r   r   r   test_classdef  s    
"zASTValidatorTests.test_classdefc          	   C   sJ   |  tg d |  td gd |  ttdt gd d S )Nzempty targets on DeletezNone disallowedr2   zmust have Del context)r#   r    Deleterb   r   )r&   r   r   r   test_delete  s    zASTValidatorTests.test_deletec          
   C   s   |  tg tdd |  td gtdd |  ttdt gtdd |  ttdt gtdt d d S )Nr~   zempty targets on AssignzNone disallowedr2   zmust have Store contextr   zmust have Load context)r#   r    Assignrt   rb   r   r   )r&   r   r   r   test_assign  s    "zASTValidatorTests.test_assignc          	   C   sp   t t dt  t  t dt  }| |d t t dt  t  t dt  }| |d d S )Nr2   r   zmust have Store contextzmust have Load context)r    	AugAssignrb   r   r   r#   r   )r&   Zaugr   r   r   test_augassign  s    z ASTValidatorTests.test_augassignc          
   C   s   t dt  }t dt  }t  }| t ||g g d | t t dt  ||gg d | t |t dt  |gg d t t dt  }| t |||gg d | t |||g|gd d S )Nr2   r   zempty body on Forzmust have Store contextzmust have Load context)r    rb   r   r   r   r#   ra   r   )r&   r2   r   pr   r   r   r   test_for  s      zASTValidatorTests.test_forc             C   s~   |  ttdg g d |  ttdt t gg d |  ttdt gttdt gd d S )Nr~   zempty body on Whiler2   zmust have Load context)r#   r    Whilert   rb   r   r   r   )r&   r   r   r   
test_while  s    "zASTValidatorTests.test_whilec             C   s   |  ttdg g d ttdt t gg }|  |d ttdttdt gg }|  |d ttdt gttdt g}|  |d d S )Nr~   zempty body on Ifr2   zmust have Load context)r#   r    Ifrt   rb   r   r   r   )r&   rO   r   r   r   test_if  s     (zASTValidatorTests.test_ifc             C   s   t  }| t g |gd t t dd }| t |gg d t t dt  d }| t |g|gd t t dt dt  }| t |g|gd d S )Nzempty items on Withr~   zempty body on Withr2   zmust have Load contextzmust have Store context)	r    r   r#   Withwithitemrt   rb   r   r   )r&   r   rO   r   r   r   	test_with  s    zASTValidatorTests.test_withc             C   sp   t d t d}| |d t t dt  d }| |d t t dt dt  }| |d d S )Nr~   z!Raise with cause but no exceptionr2   zmust have Load contextr   )r    Raisert   r#   rb   r   )r&   rr   r   r   
test_raise  s    zASTValidatorTests.test_raisec             C   sx  t  }t g g g |g}| |d t t t dt  gg g |g}| |d t |gg g g }| |d t |gg |g|g}| |d t |gt d dg gg g }| |d t t dt  d|gg}| t |g|g g d t d d|gg}t |g|t t dt  g|g}| |d t |g||gt t dt  g}| |d d S )Nzempty body on Tryr2   zmust have Load contextz-Try has neither except handlers nor finalbodyz%Try has orelse but no except handlerszempty body on ExceptHandlerr   )r    r   Tryr#   r   rb   r   ExceptHandler)r&   r   r   r   r   r   r   test_try  s$    &((zASTValidatorTests.test_tryc          	   C   sT   |  ttdt d d ttdt tdt }|  |d d S )Nr2   zmust have Load contextr   )r#   r    Assertrb   r   r   )r&   Zassrtr   r   r   test_assert'  s
    zASTValidatorTests.test_assertc             C   s   |  tg d d S )Nzempty names on Import)r#   r    Import)r&   r   r   r   test_import.  s    zASTValidatorTests.test_importc             C   s>   t d t dd gd}| |d | t d g dd d S )Nr2   izNegative ImportFrom levelr   zempty names on ImportFrom)r    r\   r   r#   )r&   impr   r   r   test_importfrom1  s    z!ASTValidatorTests.test_importfromc             C   s   |  tg d d S )Nzempty names on Global)r#   r    Global)r&   r   r   r   test_global6  s    zASTValidatorTests.test_globalc             C   s   |  tg d d S )Nzempty names on Nonlocal)r#   r    ZNonlocal)r&   r   r   r   test_nonlocal9  s    zASTValidatorTests.test_nonlocalc             C   s&   t t dt  }| |d d S )Nr2   zmust have Load context)r    r   rb   r   r#   )r&   r   r   r   r   	test_expr<  s    zASTValidatorTests.test_exprc          	   C   s   t t  g }| |d t t  t dg}| |d t t  t dd g}| |d t t  t dt dt  g}| |d d S )Nzless than 2 valuesr~   r   zNone disallowedr2   zmust have Load context)r    BoolOpAndr"   rt   rb   r   )r&   r   r   r   r   test_boolop@  s    &zASTValidatorTests.test_boolopc             C   s,   t t  t dt  }| |d d S )Nr2   zmust have Load context)r    UnaryOpNotrb   r   r"   )r&   ur   r   r   test_unaryopJ  s    zASTValidatorTests.test_unaryopc          
   C   sN   t g d g g d g }| t |t dt  d dd }| || j d S )Nr2   zmust have Load contextc             S   s   t | t dt  S )Nr2   )r    Lambdarb   r   )rj   r   r   r   r   R  s    z*ASTValidatorTests.test_lambda.<locals>.fac)r    rq   r"   r  rb   r   r   )r&   r7   r   r   r   r   test_lambdaN  s
    zASTValidatorTests.test_lambdac             C   s\   t dt  }t dt  }x6|||f|||f|||ffD ]}| t j| d q>W d S )Nr2   r   zmust have Load context)r    rb   r   r   r"   ZIfExp)r&   lsrj   r   r   r   
test_ifexpV  s    "zASTValidatorTests.test_ifexpc             C   sR   t g t dt  g}| |d t t dt  gd g}| |d d S )Nr2   zsame number of keys as valueszNone disallowed)r    Dictrb   r   r"   )r&   r   r   r   r   	test_dict\  s    zASTValidatorTests.test_dictc             C   s<   |  td gd ttdt g}|  |d d S )NzNone disallowedr2   zmust have Load context)r"   r    Setrb   r   )r&   r  r   r   r   test_setb  s    zASTValidatorTests.test_setc          	   C   s   |  |g d ttdt tdt g d}|  ||gd ttdt tdt g d}|  ||gd tdt }tdt }t||d gd}|  ||gd t||tdt gd}|  ||gd d S )Nz comprehension with no generatorsr2   r   zmust have Store contextzmust have Load contextr   zNone disallowed)r"   r    rc   rb   r   r   )r&   r   gr2   r   r   r   r   _check_comprehensiong  s    z&ASTValidatorTests._check_comprehensionc                sb   t t dt  t dt  g d}|  t dt  |gd  fdd}| | d S )Nr2   r   zmust have Load contextc                s    t dt  | S )Nr2   )r    rb   r   )Zgens)r   r   r   wrap{  s    z,ASTValidatorTests._simple_comp.<locals>.wrap)r    rc   rb   r   r   r"   r  )r&   r   r  r  r   )r   r   _simple_compv  s    zASTValidatorTests._simple_compc             C   s   |  tj d S )N)r  r    ListComp)r&   r   r   r   test_listcomp  s    zASTValidatorTests.test_listcompc             C   s   |  tj d S )N)r  r    SetComp)r&   r   r   r   test_setcomp  s    zASTValidatorTests.test_setcompc             C   s   |  tj d S )N)r  r    GeneratorExp)r&   r   r   r   test_generatorexp  s    z#ASTValidatorTests.test_generatorexpc             C   s   t t dt  t dt  g d}t t dt  t dt  |g}| |d t t dt  t dt  |g}| |d dd }| | d S )Nr   r   r   r2   zmust have Load contextc             S   s.   t dt  }t dt  }t ||| S )Nr2   r   )r    rb   r   DictComp)compskr   r   r   r   factory  s    z0ASTValidatorTests.test_dictcomp.<locals>.factory)r    rc   rb   r   r   r"  r"   r  )r&   r  cr%  r   r   r   test_dictcomp  s    zASTValidatorTests.test_dictcompc          	   C   s@   |  ttdt d |  ttdt d d S )Nr2   zmust have Load)r"   r    ZYieldrb   r   Z	YieldFrom)r&   r   r   r   
test_yield  s    zASTValidatorTests.test_yieldc             C   s   t dt  }t |t  gg }| |d t |t  gt dt dg}| |d t t dt  g|g}| j|dtd t |t  gt dg}| j|dtd d S )	Nr2   zno comparatorsr   r}   z,different number of comparators and operandsZblahznon-numeric)r   )r    rb   r   CompareZInr"   rt   r1   )r&   rz   compr   r   r   test_compare  s    $zASTValidatorTests.test_comparec             C   s   t dt  }t dt  g}t dt dt  g}t t dt  ||}| |d t |d g|}| |d t dt dt  g}t |||}| |d d S )Nr2   r   wzzmust have Load contextzNone disallowed)r    rb   r   r   r   r   r"   )r&   r   rj   r   callZbad_keywordsr   r   r   	test_call  s    zASTValidatorTests.test_callc             C   sh   G dd dt }G dd dt}G dd dt}x2dd| | | fD ]}| jt|d	td
 qFW d S )Nc               @   s   e Zd ZdS )z*ASTValidatorTests.test_num.<locals>.subintN)r   r4   r5   r   r   r   r   subint  s   r0  c               @   s   e Zd ZdS )z,ASTValidatorTests.test_num.<locals>.subfloatN)r   r4   r5   r   r   r   r   subfloat  s   r1  c               @   s   e Zd ZdS )z.ASTValidatorTests.test_num.<locals>.subcomplexN)r   r4   r5   r   r   r   r   
subcomplex  s   r2  0Zhelloznon-numeric)r   )r   floatr   r"   r    rt   r1   )r&   r0  r1  r2  objr   r   r   test_num  s
    zASTValidatorTests.test_numc             C   s.   t t dt  dt  }| |d d S )Nr2   r   zmust have Load context)r    	Attributerb   r   r   r"   )r&   attrr   r   r   test_attribute  s    z ASTValidatorTests.test_attributec          
   C   s$  t t dt  t t dt  }| |d t dt  }t |t t dt  t  }| |d t dt  }xH|d d fd |d fd d |ffD ](}t j| }| t ||t  d qW t 	g }| t ||t  d t 	t |g}| t ||t  d d S )Nr2   r~   zmust have Load contextr   zempty dims on ExtSlice)
r    	Subscriptrb   r   ZIndexrt   r   r"   SliceZExtSlice)r&   subr2   r  rj   slr   r   r   test_subscript  s      

"


z ASTValidatorTests.test_subscriptc          	   C   sN   t t t dt  t  gt  }t |gt d}| |d d S )Nr2   r   zmust have Store context)	r    ListStarredrb   r   r   r   rt   r#   )r&   rz   assignr   r   r   test_starred  s     
zASTValidatorTests.test_starredc             C   s@   |  |d gt d |  |tdt gt d d S )NzNone disallowedr2   zmust have Load context)r"   r    r   rb   r   )r&   r   r   r   r   	_sequence  s    zASTValidatorTests._sequencec             C   s   |  tj d S )N)rC  r    r?  )r&   r   r   r   	test_list  s    zASTValidatorTests.test_listc             C   s   |  tj d S )N)rC  r    Tuple)r&   r   r   r   
test_tuple  s    zASTValidatorTests.test_tuplec             C   s   |  tdd d S )Nr   z&singleton must be True, False, or None)r"   r    NameConstant)r&   r   r   r   test_nameconstant  s    z#ASTValidatorTests.test_nameconstantc          
   C   s   t jtj}dd t |D }|ddg xT|D ]L}t j||}t|ddd}|	 }W d Q R X t
||}t||d q6W d S )	Nc             S   s   g | ]}| d r|qS )z.py)endswith)r   fnr   r   r   r     s    z;ASTValidatorTests.test_stdlib_validates.<locals>.<listcomp>ztest/test_grammar.pyztest/test_unpack_ex.pyr   zutf-8)encodingr=   )ospathdirnamer    __file__listdirextendjoinopenreadrR   rL   )r&   stdlibtestsrY   rJ  fpsourcer^   r   r   r   test_stdlib_validates  s    
z'ASTValidatorTests.test_stdlib_validates)Nr=   )N)N)2r   r4   r5   r   r^   r"   r#   ry   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/  r6  r9  r>  rB  rC  rD  rF  rH  rY  r   r   r   r   r   q  s\   
	


	
r   c               @   s^   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
ejdd Zdd ZdS )ConstantTestsz$Tests on the ast.Constant node type.c             C   s\   t d}|jd j}t j|d}t || ||jd _t|dd}i }t|| |d S )Nzx = 123r   )r(   z<string>r=   r2   )r    rR   rS   r(   Constantr   rL   r=   )r&   r(   treer   new_noder   r   r   r   r   compile_constant  s    

zConstantTests.compile_constantc          	   C   s<   |  t}| dddg W d Q R X | t|jd d S )Nro   r+   r~   z%got an invalid type in Constant: list)r.   r1   r^  r,   r   r   )r&   r   r   r   r   test_validation  s    zConstantTests.test_validationc          
   C   sN   xHd ddt dt fD ]2}| j|d | |}| || W d Q R X qW d S )NFT    )const)Ellipsis	frozensetrK   r^  assertIs)r&   ra  r(   r   r   r   test_singletons	  s    
zConstantTests.test_singletonsc          
   C   s   d}t dh}x$tdD ]}|df}t |dh}qW ddddd	td
t d||f	}x:|D ]2}| j|d | |}| || W d Q R X qXW d S )N)ro   ro   r~   r+   {   g     ^@y             ^@unicodes   bytesr   rc  )r(   )rc  rr   r   rK   r^  r,   )r&   Znested_tupleZnested_frozensetr   valuesr(   r   r   r   r   test_values  s    


zConstantTests.test_valuesc          	   C   sz   t d}|jd jd }t jdd}t || ||jd jd< | t}t|dd W d Q R X | 	t
|jd d S )Nzx = 1r   ro   )r(   stringr=   z6expression which can't be assigned to in Store context)r    rR   rS   Ztargetsr[  r   r.   r   rL   r,   r   r   )r&   r\  target
new_targetr   r   r   r   test_assign_to_constant  s    
z%ConstantTests.test_assign_to_constantc             C   s    t d}| t |d d S )Nz'docstring'
x = 1Z	docstring)r    rR   r,   r   )r&   r\  r   r   r   r   ,  s    
z ConstantTests.test_get_docstringc             C   s>   t |dd}g }x(t|D ]}|jdkr||j qW |S )Nz<string>r=   
LOAD_CONST)rL   disget_instructionsopnamer   argval)r&   r\  coconstsinstrr   r   r   get_load_const0  s    
zConstantTests.get_load_constc          	   C   s   d ddddddddg	}d	 d
d |D }|d7 }|td f t|}| | || xRt|j|D ]B\}}t	|tj
stt|tj|d}t||j ||_qjW | | || d S )NTF|   g       @y              @rg  s   bytes)ro   r+   r~   
c             S   s   g | ]}d  |qS )zx={!r})format)r   ra  r   r   r   r   E  s    z1ConstantTests.test_load_const.<locals>.<listcomp>z
x = ...)r(   )rR  rQ  rb  r    rR   r,   rv  rJ   rS   r   r   AssertionErrorr   r[  r   r(   )r&   rt  r   r\  rA  ra  r]  r   r   r   test_load_const:  s*    

zConstantTests.test_load_constc             C   sl   t d}|jd j}t jdd}t ||j ||_t jdd}t ||j ||_| t 	|d d S )Nz1 + 2r   
   )r(   y              4@y      $@      4@)
r    rR   rS   r(   r[  r   rz   r|   r,   r   )r&   r\  ZbinopZnew_leftZ	new_rightr   r   r   r   W  s    
zConstantTests.test_literal_evalN)r   r4   r5   __doc__r^  r_  re  ri  rm  r   rv  r   r   r{  r   r   r   r   r   rZ    s   
rZ  c              C   s   t dkrd S tjdd  dgkrxdtdftdftdffD ]J\} }t|d  x,| D ]$}t|d|}td	t	|f  qRW td
 q8W td t
t  d S )N__main__ro   z-gr=   r>   r?   z_results = [rB   z%r,]zmain())r   sysargvrD   rF   rH   printr    rR   r   
SystemExitunittestmain)Z
statementsrC   	statementr\  r   r   r   r  f  s    
r  rx   )r   )ro   r   )rG  )ro   r   N)r   )ro   r   )r   )ro   r   zmodule docstringr   )ro   r   r   rq   )r   )ro   	   )r   )ro   r  )r   )ro   r  zfunction docstring)r   )ro   r   r7   N)r   )ro   r|  )rt   )ro      r   )r   )ro      )r   )ro   rp   rj   N)r   )ro      )r   )ro   r  kwargsN)r   )ro      )r   )ro   r  r   N)r   )ro   r  r&  N)r   )ro      r   N)r   )ro      r   N)r   )ro   #   rj   N)r   )ro   )   r   N)rt   )ro   +   r)   )r   )ro   1   r  N)rt   )ro      ro   )rG  )ro      Nr?  )ro      )r   r  )ro      )r   )ro   :   )r   )ro   r  zdoc for f()r   C)r   )ro   r  )r   )ro   r  )r   )ro   r  zdocstring for class C)rb   )ro   r  object)r   )ZReturn)ro   r  )rt   )ro      ro   r   )rb   )ro   r   r   )ZDelr   )rb   )ro   r   r   )r   )rt   )ro   r   ro   )r   )ro   r   )rb   )ro   r   r   )r   )r   )rt   )ro   r}   ro   ra   )rb   )ro   r   r   )r   )rb   )ro   r  r   )r   )r   )ro   r  r   )rb   )ro   r   r   )r   r   )rb   )ro   r~   r   )r   )r   )ro   r}   r   )r   )rb   )ro   r}   r2   )r   )rb   )ro   r|  r   )r   )r   )ro      )r   )rb   )ro   r  r-  )r   )rb   )ro      q)r   )r   )ro      r   r   )ro   r   )rb   )ro   r   r   )r   )r   )ro   r  rj  r   )r   )r+   r+   r   )r~   r   )rb   )r~   rp   r   )r   )r   )r   r+   )r   )ro   r   )rb   )ro   rp   r   )r   Nr   )r   r  Nr\   r  )r   r   Nr  )r   )ro   r   )rt   )ro   r   ro   )r   )ro   r   )ZBreak)ro   r  )ZContinue)ro   r  rE  )ro   r   )rb   )ro   r   r7   )r   )rb   )ro   r   r   )r   )r   )rb   )ro   r  r&  )r   r   r  )ro   ro   )ro   r+   )rb   )ro   r+   r7   )r   )rb   )ro   r   r   )r   rc   )ro   r  )rb   )ro   r  r7   )r   )rb   )ro   r  r   )r   )rb   )ro   r  r&  )r   r   )ro   r  )rb   )ro   r  r7   )r   )rb   )ro   r  r   )r   )rb   )ro      r&  )r   )r+   r   )r~   r   )rb   )r~   r   Aa)r   )rb   )r}   rp   Bb)r   )r  r   )rb   )r  r   r  )r   )rb   )r|  r   r  )r   )rb   )r|  r|  ZCc)r   r"  )rb   )ro   ro   r7   )r   )rb   )ro   r}   r   )r   )rb   )ro   r  r,  )r   )rb   )ro   r  r2   )r   )rb   )ro   r  r   )r   )rb   )ro      r   )r   )rb   )ro       r  )r   )rb   )ro   r  r   )r   )rb   )ro   r  r,  )r   )rb   )ro   r  r2   )r   r  )rb   )ro   ro   r   )r   )rb   )ro   rp   r  )r   )rb   )ro   r  r2   )r   )rb   )ro   r  r  )r   )ro   rp   )rb   )ro   r  r   )r   )rb   )ro   r  r2   )r   ZAsyncFunctionDef)r   )r+   ro   )r   )r+   ro   zasync function)r~   ro   ZAwait)r~   rp   )rb   )r~   rp   Z	something)r   ZAsyncFor)r+   ro   )rb   )r+   r  r   )r   )rb   )r+   r  rO   )r   )r   )r+      )rt   )r+   r  ro   )r   )r~   rp   )rt   )r~   rp   r+   Z	AsyncWith)r   )rb   )r+   r  r7   )r   )rb   )r+   r  r   )r   )r   )r+   r  )rt   )r+   r  ro   )rt   )ro   r|  r+   )ro   r~   )rt   )ro   r   r+   )rt   )ro   r  r~   r  r@  )rt   )ro   r~   ro   )rt   )ro   r|  r~   )r+   r+   )rb   )r+   r+   rO   )r   )rb   )r+   r  r   )r   )rb   )r+   r  r&  )r   ro   r   )r   )ro   r   )r   )ro   r   )rt   )ro   r   ro   )r   )rt   )ro   r+   r+   )r   )rG  )ro   r   Nr   r  )r	  )rb   )ro   r   r7   )r   )rb   )ro   r   r   )r   )r   )r   )ro   r   )rb   )ro   r   r7   )r   )r   )rb   )ro   r   r   )r   )r   )r  )ro   r   )r  )rb   )ro   r   r   )r   r  )rG  )ro   rp   N)rt   )ro   r+   ro   )rt   )ro   r   r+   )rG  )ro   ro   N)rt   )r+   r   ro   )rt   )r   r|  r+   )rb   )ro   rp   r   )r   )rb   )ro   r  r&  )r   )rb   )ro   r  r   )r   r)  )rt   )ro   r   ro   )ZLt)rt   )ro   r  r~   )rb   )ro   r   r   )r   )r@  )ro   r|  )rb   )ro   r  r   )r   )r   )r   r&  )rt   )ro   r  r~   )r   N)rb   )ro   r  r   )r   )r   )rt   )ro   r   r|  )r   )r   )ro   r   rj  )r   )r7  )ro   r   )rb   )ro   r   r7   )r   r   )r   )r   )r:  )ro   r   )rb   )ro   r   r7   )r   )r;  )rb   )ro   r+   r   )r   )rb   )ro   r   r&  )r   N)r   )r   )rb   )ro   r   r   )r   )rt   )ro   ro   ro   )rt   )ro   r~   r+   )rt   )ro   r}   r~   )rt   )ro   r+   r+   )rt   )ro   r   r~   )r7  )ro   r   )r7  )ro   r   )r7  )ro   r   )rb   )ro   r   r7   )r   r   )r   r&  )r   r   )r   )r:  )ro   r  )r7  )ro   r  )rb   )ro   r  r7   )r   r   )r   )r;  )rt   )ro   r  ro   )rt   )ro   r  r+   N)r   )r    ro  rL  r  r  r8   r   r   r   rD   rF   rH   TestCaser   r   r   rZ  r  rE   rG   rI   r   r   r   r   <module>   sD   h D   t&&(*&&L&$"::::20$.>62**D""$&