ó
è¾bc           @   s=  d  Z  d d l m Z d d l m Z m Z m Z m Z m Z m	 Z	 d d l
 m Z d d l Z d d l Z d Z d d d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „  ƒ  YZ d „  Z e d k r9d d l Z d d l m Z m Z d d l Z d „  Z xäe j d D]ÒZ e GHe e ƒ Z e j  ƒ  Z! e j" ƒ  e j e! e d ƒ Z# e e# ƒ Z$ e e ƒ Z% e ƒ  Z& e e% e& ƒ e& j' e% j ƒ  Z( e e$ e( ƒ s"Hd Ge GHe) e$ ƒ GHe) e( ƒ GHe j* d ƒ n  i  Z+ e+ j, e& j' ƒ e+ e% =e+ j- ƒ  Z' [+ xá e# j. ƒ  D]Ó Z& e& j/ ƒ  r[g  e' D]! Z0 e0 j1 e& j2 ƒ  k rte0 ^ qtZ3 e4 e3 ƒ d k r¿d Ge& j2 ƒ  GHq.e e e& j5 ƒ  ƒ e3 d j ƒ  ƒ s.e& j2 ƒ  GHe) e e& j5 ƒ  ƒ ƒ GHe) e3 d j ƒ  ƒ GHe j* d ƒ q.q[q[Wq`Wn  d S(    s   Module symbol-table generatoriÿÿÿÿ(   t   ast(   t   SC_LOCALt   SC_GLOBAL_IMPLICITt   SC_GLOBAL_EXPLICITt   SC_FREEt   SC_CELLt
   SC_UNKNOWN(   t   mangleNi   t   Scopec           B   s¤   e  Z d d  „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s½   | |  _  | |  _ i  |  _ i  |  _ i  |  _ i  |  _ i  |  _ i  |  _ g  |  _ d  |  _
 d  |  _ d  |  _ | d  k	 r¹ x> t t | ƒ ƒ D]' } | | d k r‹ | | |  _ Pq‹ q‹ Wn  d  S(   Nt   _(   t   namet   modulet   defst   usest   globalst   paramst   freest   cellst   childrent   Nonet   nestedt	   generatort   klasst   ranget   len(   t   selfR
   R   R   t   i(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   __init__   s"    												c         C   s   d |  j  j |  j f S(   Ns   <%s: %s>(   t	   __class__t   __name__R
   (   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   __repr__%   s    c         C   s#   |  j  d  k r | St | |  j  ƒ S(   N(   R   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   (   s    c         C   s   d |  j  |  j | ƒ <d  S(   Ni   (   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   add_def-   s    c         C   s   d |  j  |  j | ƒ <d  S(   Ni   (   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   add_use0   s    c         C   sy   |  j  | ƒ } | |  j k s0 | |  j k r0 n  | |  j k rX t d | |  j f ‚ n  d |  j | <|  j j | ƒ d  S(   Ns    %s in %s is global and parameteri   (	   R   R   R   R   t   SyntaxErrorR
   R   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt
   add_global3   s    c         C   s-   |  j  | ƒ } d |  j | <d |  j | <d  S(   Ni   (   R   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt	   add_param=   s    c         C   s@   i  } | j  |  j ƒ | j  |  j ƒ | j  |  j ƒ | j ƒ  S(   N(   t   updateR   R   R   t   keys(   R   t   d(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt	   get_namesB   s
    c         C   s   |  j  j | ƒ d  S(   N(   R   t   append(   R   t   child(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt	   add_childI   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   get_childrenL   s    c         C   s—   t  j |  j I|  j r d p" d IJt  j d I|  j IJt  j d I|  j IJt  j d I|  j IJt  j d I|  j IJt  j d I|  j IJd  S(   NR   t    s
   	globals: s   	cells: s   	defs: s   	uses: s   	frees:(	   t   syst   stderrR
   R   R   R   R   R   R   (   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   DEBUGO   s    %c         C   sy   | |  j  k r t S| |  j k r& t S| |  j k r9 t S|  j rd | |  j k s` | |  j k rd t	 S|  j rq t
 St Sd S(   sb   Return scope of name.

        The scope of a name could be LOCAL, GLOBAL, FREE, or CELL.
        N(   R   R   R   R   R   R   R   R   R   R   R   R   (   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt
   check_nameW   s    '	c         C   sr   |  j  s d Si  } | j |  j ƒ xB |  j j ƒ  D]1 } | |  j k r3 | |  j k r3 d | | <q3 q3 W| j ƒ  S(   Ni   (    (   R   R$   R   R   R%   R   R   (   R   t   freeR
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   get_free_varsi   s    	c         C   sQ   xJ |  j  D]? } | j ƒ  } |  j | ƒ } x | D] } | j | ƒ q2 Wq
 Wd  S(   N(   R   R2   t	   add_freest   force_global(   R   R)   R   R   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   handle_childrens   s
    c         C   sf   d |  j  | <| |  j k r) |  j | =n  x6 |  j D]+ } | j | ƒ t k r3 | j | ƒ q3 q3 Wd S(   sÞ  Force name to be global in scope.

        Some child of the current node had a free reference to name.
        When the child was processed, it was labelled a free
        variable.  Now that all its enclosing scope have been
        processed, the name is known to be a global or builtin.  So
        walk back down the child chain and set the name to be global
        rather than free.

        Be careful to stop if a child does not think the name is
        free.
        i   N(   R   R   R   R0   R   R4   (   R   R
   R)   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR4   z   s    c         C   s  g  } xû | D]ó } |  j  | ƒ } |  j rÈ | t k sR | t k sR t |  t ƒ rb d |  j | <q | t k r~ | j | ƒ q t |  t	 ƒ r© | t
 k r© d |  j | <q | t k r | j | ƒ q q | t
 k rä d |  j | <q | t k r | j | ƒ q q W| S(   sþ   Process list of free vars from nested scope.

        Returns a list of names that are either 1) declared global in the
        parent or 2) undefined in a top-level parent.  In either case,
        the nested scope should treat them as globals.
        i   (   R0   R   R   R   t
   isinstancet
   ClassScopeR   R   R(   t   FunctionScopeR   R   R   (   R   t   namest   child_globalsR
   t   sc(    (    s&   /usr/lib/python2.7/compiler/symbols.pyR3   Ž   s$    	c         C   s   |  j  j ƒ  S(   N(   R   R%   (   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   get_cell_vars©   s    N(   R   t
   __module__R   R   R   R   R   R    R"   R#   R'   R*   R+   R/   R0   R2   R5   R4   R3   R<   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR      s"   					
							
			t   ModuleScopec           B   s   e  Z e j Z d  „  Z RS(   c         C   s   |  j  d |  ƒ d  S(   Nt   global(   t   _ModuleScope__super_init(   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   ¯   s    (   R   R=   R   R   R@   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR>   ¬   s   	R8   c           B   s   e  Z RS(    (   R   R=   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR8   ²   s   t   GenExprScopec           B   s,   e  Z e j Z d  Z d d „ Z d „  Z RS(   i   c         C   s@   |  j  } |  j  d 7_  |  j d | | | ƒ |  j d ƒ d  S(   Ni   s   generator expression<%d>s   .0(   t   _GenExprScope__countert   _GenExprScope__super_initR#   (   R   R   R   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   º   s    	c         C   s   t  j |  ƒ } | S(   N(   R   R'   (   R   R%   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR'   À   s    N(   R   R=   R   R   RC   RB   R   R'   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyRA   µ   s   	t   LambdaScopec           B   s#   e  Z e j Z d  Z d d „ Z RS(   i   c         C   s3   |  j  } |  j  d 7_  |  j d | | | ƒ d  S(   Ni   s	   lambda.%d(   t   _LambdaScope__countert   _LambdaScope__super_init(   R   R   R   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   É   s    	N(   R   R=   R   R   RF   RE   R   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyRD   Ä   s   	R7   c           B   s   e  Z e j Z d  „  Z RS(   c         C   s   |  j  | | | ƒ d  S(   N(   t   _ClassScope__super_init(   R   R
   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   Ñ   s    (   R   R=   R   R   RG   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR7   Î   s   	t   SymbolVisitorc           B   s  e  Z d  „  Z d „  Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d „ Z
 d	 „  Z d
 „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d d „ Z d d „ Z d „  Z e j e j e j f Z d „  Z d „  Z RS(   c         C   s   i  |  _  d  |  _ d  S(   N(   t   scopesR   R   (   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyR   Õ   s    	c         C   s2   t  ƒ  } |  _ |  j | <|  j | j | ƒ d  S(   N(   R>   R   RI   t   visitt   node(   R   RK   t   scope(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitModuleÛ   s    c         C   sÙ   | j  r |  j | j  | ƒ n  | j | j ƒ x! | j D] } |  j | | ƒ q9 Wt | j |  j |  j ƒ } | j s† t	 | t ƒ r’ d | _ n  | |  j
 | <|  j | | j ƒ |  j | j | ƒ |  j | | ƒ d  S(   Ni   (   t
   decoratorsRJ   R   R
   t   defaultsR8   R   R   R   R6   RI   t   _do_argst   argnamest   codet   handle_free_vars(   R   RK   t   parentt   nRL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitFunctioná   s    	c         C   s|   t  |  j |  j ƒ } | j s< t | t ƒ s< t | t  ƒ rH d | _ n  | |  j | <|  j | j | ƒ |  j	 | | ƒ d  S(   Ni   (
   RA   R   R   R   R6   R8   RI   RJ   RR   RS   (   R   RK   RT   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitGenExprï   s    c         C   s;   x! | j  D] } |  j | | ƒ q
 W|  j | j | ƒ d  S(   N(   t   qualsRJ   t   expr(   R   RK   RL   t   genfor(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitGenExprInnerú   s    c         C   sQ   |  j  | j | d ƒ |  j  | j | ƒ x! | j D] } |  j  | | ƒ q3 Wd  S(   Ni   (   RJ   t   assignt   itert   ifs(   R   RK   RL   t   if_(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitGenExprFor   s    c         C   s   |  j  | j | ƒ d  S(   N(   RJ   t   test(   R   RK   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitGenExprIf  s    i    c         C   s±   | s t  ‚ x! | j D] } |  j | | ƒ q Wt |  j |  j ƒ } | j s^ t | t ƒ rj d | _ n  | |  j	 | <|  j
 | | j ƒ |  j | j | ƒ |  j | | ƒ d  S(   Ni   (   t   AssertionErrorRO   RJ   RD   R   R   R   R6   R8   RI   RP   RQ   RR   RS   (   R   RK   RT   R\   RU   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitLambda	  s    c         C   sJ   xC | D]; } t  | ƒ t j k r5 |  j | | ƒ q | j | ƒ q Wd  S(   N(   t   typet   typest	   TupleTypeRP   R#   (   R   RL   t   argsR
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyRP     s    c         C   s   | j  | ƒ | j ƒ  d  S(   N(   R*   R5   (   R   RL   RT   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyRS      s    c         C   së   | j  | j ƒ x! | j D] } |  j | | ƒ q Wt | j |  j ƒ } | j sa t | t ƒ rm d | _ n  | j	 d  k	 rŒ | j  d ƒ n  | j  d ƒ | |  j | <|  j } | j |  _ |  j | j | ƒ | |  _ |  j | | ƒ d  S(   Ni   t   __doc__R=   (   R   R
   t   basesRJ   R7   R   R   R6   R8   t   docR   RI   R   RR   RS   (   R   RK   RT   RU   RL   t   prev(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt
   visitClass$  s    		c         C   s-   | r | j  | j ƒ n | j | j ƒ d  S(   N(   R   R
   R    (   R   RK   RL   R\   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt	   visitName;  s    c         C   s_   |  j  | j | d ƒ |  j  | j | ƒ |  j  | j | ƒ | j r[ |  j  | j | ƒ n  d  S(   Ni   (   RJ   R\   t   listt   bodyt   else_(   R   RK   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitForC  s
    	c         C   sC   x< | j  D]1 \ } } | d k r( q
 n  | j | p7 | ƒ q
 Wd  S(   Nt   *(   R9   R   (   R   RK   RL   R
   t   asname(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt	   visitFromJ  s    c         C   sY   xR | j  D]G \ } } | j d ƒ } | d k r> | |  } n  | j | pM | ƒ q
 Wd  S(   Nt   .iÿÿÿÿ(   R9   t   findR   (   R   RK   RL   R
   Rt   R   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitImportP  s
    c         C   s%   x | j  D] } | j | ƒ q
 Wd  S(   N(   R9   R"   (   R   RK   RL   R
   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitGlobalW  s    c         C   s>   x$ | j  D] } |  j | | d ƒ q
 W|  j | j | ƒ d S(   s.  Propagate assignment flag down to child nodes.

        The Assign node doesn't itself contains the variables being
        assigned to.  Instead, the children in node.nodes are visited
        with the assign flag set to true.  When the names occur in
        those nodes, they are marked as defs.

        Some names that occur in an assignment target are not bound by
        the assignment, e.g. a name occurring inside a slice.  The
        visitor handles these nodes specially; they do not propagate
        the assign flag to their children.
        i   N(   t   nodesRJ   RY   (   R   RK   RL   RU   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitAssign[  s    i   c         C   s   | j  | j ƒ d  S(   N(   R   R
   (   R   RK   RL   R\   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitAssNamel  s    c         C   s   |  j  | j | d ƒ d  S(   Ni    (   RJ   RY   (   R   RK   RL   R\   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitAssAttro  s    c         C   sA   |  j  | j | d ƒ x$ | j D] } |  j  | | d ƒ q  Wd  S(   Ni    (   RJ   RY   t   subs(   R   RK   RL   R\   RU   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitSubscriptr  s    c         C   s^   |  j  | j | d ƒ | j r8 |  j  | j | d ƒ n  | j rZ |  j  | j | d ƒ n  d  S(   Ni    (   RJ   RY   t   lowert   upper(   R   RK   RL   R\   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt
   visitSlicew  s
    		c         C   sX   |  j  | j | ƒ t | j t j ƒ rA |  j  | j | d ƒ n  |  j  | j | ƒ d  S(   Ni   (   RJ   RK   R6   R    t   NameRY   (   R   RK   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitAugAssign~  s    c         C   sœ   xv | j  D]k \ } } t | t j ƒ rU t | j ƒ |  j k rU | j sR q
 qR qU n  |  j | | ƒ |  j | | ƒ q
 W| j r˜ |  j | j | ƒ n  d  S(   N(	   t   testsR6   R    t   ConstRe   t   valuet   _const_typesRJ   Rq   (   R   RK   RL   Ra   Rp   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   visitIfŠ  s    		c         C   s    d | _  |  j | j | ƒ d  S(   Ni   (   R   RJ   R‡   (   R   RK   RL   (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt
   visitYield—  s    	(    R   R=   R   RM   t   visitExpressionRV   RW   R[   R`   Rb   Rd   RP   RS   Rm   Rn   Rr   Ru   Rx   Ry   R{   R|   R}   R   R‚   R„   Rf   t
   StringTypet   IntTypet	   FloatTypeRˆ   R‰   RŠ   (    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyRH   Ô   s4   																
	c         C   s   t  |  ƒ t  | ƒ k S(   N(   t   sorted(   t   l1t   l2(    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   list_eq›  s    t   __main__(   t	   parseFilet   walkc         C   sT   g  g  |  j  ƒ  D] } | j ƒ  ^ q D]* } | j d ƒ pG | j d ƒ s& | ^ q& S(   Ns   _[Rv   (   t   get_symbolst   get_namet
   startswith(   t   symst   s(    (    s&   /usr/lib/python2.7/compiler/symbols.pyR'   £  s    ,i   t   exect   oopst   skippingi    (    (    (6   Ri   t   compilerR    t   compiler.constsR   R   R   R   R   R   t   compiler.miscR   Rf   R-   t
   MANGLE_LENR   R>   R8   RA   RD   R7   RH   R’   R   R”   R•   t   symtableR'   t   argvt   filet   opent   ft   readt   buft   closeR™   t	   mod_namest   treeRš   RI   t   names2R   t   exitR&   R$   t   valuesR–   t   is_namespaceR;   R
   R—   t   lR   t   get_namespace(    (    (    s&   /usr/lib/python2.7/compiler/symbols.pyt   <module>   sj   .ž
Ç		
		!