
    (g*                     n   d Z ddlZddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ ddlZg dZd Z G d d      Z e       Z G d d	      Z G d
 de      Z G d de      Z G d d      Zedk(  rddlZddlZ eej<                  d         5 ZejA                         Z!ddd        ee!ejD                  jG                  ej<                  d         d   d      Z$e$jK                         D ]9  Z&e$jO                  e&      Z( e)e(e(jU                         e(jW                                ; yy# 1 sw Y   xY w)z2Interface to the compiler's internal symbol tables    N)USE
DEF_GLOBALDEF_NONLOCAL	DEF_LOCAL	DEF_PARAM
DEF_IMPORT	DEF_BOUND	DEF_ANNOT	SCOPE_OFF
SCOPE_MASKFREELOCALGLOBAL_IMPLICITGLOBAL_EXPLICITCELL)symtableSymbolTableClassFunctionSymbolc                 H    t        j                  | ||      }t        ||      S )z Return the toplevel *SymbolTable* for the source code.

    *filename* is the name of the file with the code
    and *compile_type* is the *compile()* mode argument.
    )	_symtabler   _newSymbolTable)codefilenamecompile_typetops       /usr/lib/python3.12/symtable.pyr   r      s%     

T8\
:C3))    c                       e Zd Zd Zd Zd Zy)SymbolTableFactoryc                 6    t        j                         | _        y N)weakrefWeakValueDictionary_SymbolTableFactory__memoselfs    r   __init__zSymbolTableFactory.__init__   s    113r   c                     |j                   t        j                  k(  rt        ||      S |j                   t        j                  k(  rt        ||      S t        ||      S r#   )typer   TYPE_FUNCTIONr   
TYPE_CLASSr   r   )r(   tabler   s      r   newzSymbolTableFactory.new   sM    ::000E8,,::---))5(++r   c                     ||f}| j                   j                  |d       }|!| j                  ||      x}| j                   |<   |S r#   )r&   getr/   )r(   r.   r   keyobjs        r   __call__zSymbolTableFactory.__call__    sG    Xokkooc4(;%)XXeX%>>C$++c"
r   N)__name__
__module____qualname__r)   r/   r4    r   r   r!   r!      s    4,r   r!   c                   `    e 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y)r   c                 .    || _         || _        i | _        y r#   )_table	_filename_symbols)r(   	raw_tabler   s      r   r)   zSymbolTable.__init__,   s    !r   c                 ,   | j                   t        k(  rd}nd| j                   j                  z  }| j                  j                  dk(  rdj                  || j                        S dj                  || j                  j                  | j                        S )N z%s r   z<{0}SymbolTable for module {1}>z<{0}SymbolTable for {1} in {2}>)	__class__r   r5   r;   nameformatr<   )r(   kinds     r   __repr__zSymbolTable.__repr__1   s{    >>[(D4>>222D;;u$4;;D$..QQ4;;D<@KK<L<L<@NNL Lr   c                 n   | j                   j                  t        j                  k(  ry| j                   j                  t        j                  k(  ry| j                   j                  t        j
                  k(  ry| j                   j                  t        j                  k(  ry| j                   j                  t        j                  k(  ry| j                   j                  t        j                  k(  ry| j                   j                  t        j                  k(  ryJ d| j                   j                          )	zReturn the type of the symbol table.

        The values returned are 'class', 'module', 'function',
        'annotation', 'TypeVar bound', 'type alias', and 'type parameter'.
        modulefunctionclass
annotationzTypeVar boundz
type aliasztype parameterzunexpected type: )
r;   r+   r   TYPE_MODULEr,   r-   TYPE_ANNOTATIONTYPE_TYPE_VAR_BOUNDTYPE_TYPE_ALIASTYPE_TYPE_PARAMr'   s    r   get_typezSymbolTable.get_type>   s     ;;y444;;y666;;y333;;y888;;y<<<";;y888;;y888#<)$++*:*:);<<ur   c                 .    | j                   j                  S )z,Return an identifier for the table.
        )r;   idr'   s    r   get_idzSymbolTable.get_idT   s     {{~~r   c                 .    | j                   j                  S )zReturn the table's name.

        This corresponds to the name of the class, function
        or 'top' if the table is for a class, function or
        global respectively.
        )r;   rB   r'   s    r   get_namezSymbolTable.get_nameY   s     {{r   c                 .    | j                   j                  S )zPReturn the number of the first line in the
        block for the table.
        )r;   linenor'   s    r   
get_linenozSymbolTable.get_linenob   s     {{!!!r   c                 b    t        | j                  j                  t        j                  k(        S )zJReturn *True* if the locals in the table
        are optimizable.
        )boolr;   r+   r   r,   r'   s    r   is_optimizedzSymbolTable.is_optimizedh   s$     DKK$$	(?(??@@r   c                 @    t        | j                  j                        S )zAReturn *True* if the block is a nested class
        or function.)rZ   r;   nestedr'   s    r   	is_nestedzSymbolTable.is_nestedn   s     DKK&&''r   c                 @    t        | j                  j                        S )z:Return *True* if the block has nested namespaces.
        )rZ   r;   childrenr'   s    r   has_childrenzSymbolTable.has_childrens   s     DKK(())r   c                 J    | j                   j                  j                         S )zKReturn a view object containing the names of symbols in the table.
        )r;   symbolskeysr'   s    r   get_identifierszSymbolTable.get_identifiersx   s     {{""''))r   c                    | j                   j                  |      }|a| j                  j                  |   }| j	                  |      }| j                  j
                  dk(  }t        ||||      x}| j                   |<   |S )zLLookup a *name* in the table.

        Returns a *Symbol* instance.
        r   module_scope)r=   r1   r;   rc   _SymbolTable__check_childrenrB   r   )r(   rB   symflags
namespacesrh   s         r   lookupzSymbolTable.lookup}   s    
 mm%;KK''-E..t4J KK,,5L(.tUJ<H)J JC$--%
r   c                 f    | j                         D cg c]  }| j                  |       c}S c c}w )zLReturn a list of *Symbol* instances for
        names in the table.
        )re   rm   )r(   idents     r   get_symbolszSymbolTable.get_symbols   s*     150D0D0FGuE"GGGs   .c                     | j                   j                  D cg c]'  }|j                  |k(  rt        || j                        ) c}S c c}w r#   )r;   r`   rB   r   r<   )r(   rB   sts      r   __check_childrenzSymbolTable.__check_children   sB    ++..$77d?  DNN3 $ 	$ $s   ,Ac                 |    | j                   j                  D cg c]  }t        || j                         c}S c c}w )z3Return a list of the nested symbol tables.
        )r;   r`   r   r<   )r(   rr   s     r   get_childrenzSymbolTable.get_children   s9     ++..0  DNN3 0 	0 0s   9N)r5   r6   r7   r)   rE   rP   rS   rU   rX   r[   r^   ra   re   rm   rp   ri   ru   r8   r   r   r   r   *   sK    
L=,
 "A(
*
*
H$
0r   r   c                   D    e 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y)r   Nc                 L     t         fd j                         D              S )Nc              3   `   K   | ]%  } j                   j                  |         r| ' y wr#   )r;   rc   ).0ro   r(   	test_funcs     r   	<genexpr>z-Function.__idents_matching.<locals>.<genexpr>   s/      ?u!$++"5"5e"<=  ?s   +.)tuplere   )r(   rz   s   ``r   __idents_matchingzFunction.__idents_matching   s%     ?(<(<(> ? ? 	?r   c                 `    | j                   | j                  d       | _         | j                   S )z6Return a tuple of parameters to the function.
        c                     | t         z  S r#   )r   xs    r   <lambda>z)Function.get_parameters.<locals>.<lambda>   s
    A	M r   )_Function__params_Function__idents_matchingr'   s    r   get_parameterszFunction.get_parameters   s+     ==  223IJDM}}r   c                     | j                   't        t        ffd}| j                  |      | _         | j                   S )z2Return a tuple of locals in the function.
        c                 (    | t         z	  t        z  v S r#   r   r   )r   locss    r   r   z%Function.get_locals.<locals>.<lambda>   s    qI~;D r   )_Function__localsr   r   r   )r(   testr   s     @r   
get_localszFunction.get_locals   s9     == 4=DDD 2248DM}}r   c                     | j                   't        t        ffd}| j                  |      | _         | j                   S )z3Return a tuple of globals in the function.
        c                 (    | t         z	  t        z  v S r#   r   )r   globs    r   r   z&Function.get_globals.<locals>.<lambda>   s    a9n
:tC r   )_Function__globalsr   r   r   )r(   r   r   s     @r   get_globalszFunction.get_globals   s:     >>!#_5DCD!33D9DN~~r   c                 `    | j                   | j                  d       | _         | j                   S )z5Return a tuple of nonlocals in the function.
        c                     | t         z  S r#   )r   r   s    r   r   z(Function.get_nonlocals.<locals>.<lambda>   s    q<?O r   )_Function__nonlocalsr   r'   s    r   get_nonlocalszFunction.get_nonlocals   s0     ##556OPDr   c                 d    | j                   d }| j                  |      | _         | j                   S )z:Return a tuple of free variables in the function.
        c                 0    | t         z	  t        z  t        k(  S r#   )r   r   r   r   s    r   r   z$Function.get_frees.<locals>.<lambda>   s    i: =$F r   )_Function__freesr   )r(   is_frees     r   	get_freeszFunction.get_frees   s/     <<FG11':DL||r   )r5   r6   r7   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   r      s;     HHGIK? r   r   c                       e Zd ZdZd Zy)r   Nc                     | j                   <i }| j                  j                  D ]  }d||j                  <    t	        |      | _         | j                   S )z9Return a tuple of methods declared in the class.
           )_Class__methodsr;   r`   rB   r|   )r(   drr   s      r   get_methodszClass.get_methods   sN     >>!Akk** "''
"1XDN~~r   )r5   r6   r7   r   r   r8   r   r   r   r      s    Ir   r   c                   t    e Zd Zdd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y)r   NFrg   c                n    || _         || _        |t        z	  t        z  | _        |xs d| _        || _        y )Nr8   )_Symbol__name_Symbol__flagsr   r   _Symbol__scope_Symbol__namespaces_Symbol__module_scope)r(   rB   rk   rl   rh   s        r   r)   zSymbol.__init__   s6    *j8&,"*r   c                 8    dj                  | j                        S )Nz<symbol {0!r}>)rC   r   r'   s    r   rE   zSymbol.__repr__   s    &&t{{33r   c                     | j                   S )z#Return a name of a symbol.
        )r   r'   s    r   rU   zSymbol.get_name   s     {{r   c                 N    t        | j                  t        j                  z        S )zBReturn *True* if the symbol is used in
        its block.
        )rZ   r   r   r   r'   s    r   is_referencedzSymbol.is_referenced   s     DLL9==011r   c                 :    t        | j                  t        z        S )z4Return *True* if the symbol is a parameter.
        )rZ   r   r   r'   s    r   is_parameterzSymbol.is_parameter        DLL9,--r   c                     t        | j                  t        t        fv xs! | j                  xr | j
                  t        z        S )z/Return *True* if the symbol is global.
        )rZ   r   r   r   r   r   r	   r'   s    r   	is_globalzSymbol.is_global   s@     DLL_o$FF J++Hy0HK 	Kr   c                 :    t        | j                  t        z        S )z(Return *True* if the symbol is nonlocal.)rZ   r   r   r'   s    r   is_nonlocalzSymbol.is_nonlocal  s    DLL</00r   c                 :    t        | j                  t        k(        S )zOReturn *True* if the symbol is declared global
        with a global statement.)rZ   r   r   r'   s    r   is_declared_globalzSymbol.is_declared_global  s     DLLO344r   c                     t        | j                  t        t        fv xs! | j                  xr | j
                  t        z        S )z.Return *True* if the symbol is local.
        )rZ   r   r   r   r   r   r	   r'   s    r   is_localzSymbol.is_local  s?     DLLUDM1 J++Hy0HK 	Kr   c                 :    t        | j                  t        z        S )z2Return *True* if the symbol is annotated.
        )rZ   r   r
   r'   s    r   is_annotatedzSymbol.is_annotated  r   r   c                 :    t        | j                  t        k(        S )zIReturn *True* if a referenced symbol is
        not assigned to.
        )rZ   r   r   r'   s    r   r   zSymbol.is_free  s     DLLD())r   c                 :    t        | j                  t        z        S )zQReturn *True* if the symbol is created from
        an import statement.
        )rZ   r   r   r'   s    r   is_importedzSymbol.is_imported  s     DLL:-..r   c                 :    t        | j                  t        z        S )z)Return *True* if a symbol is assigned to.)rZ   r   r   r'   s    r   is_assignedzSymbol.is_assigned$  s    DLL9,--r   c                 ,    t        | j                        S )a  Returns *True* if name binding introduces new namespace.

        If the name is used as the target of a function or class
        statement, this will be true.

        Note that a single name can be bound to multiple objects.  If
        is_namespace() is true, the name may also be bound to other
        objects, like an int or list, that does not introduce a new
        namespace.
        )rZ   r   r'   s    r   is_namespacezSymbol.is_namespace(  s     D%%&&r   c                     | j                   S )z.Return a list of namespaces bound to this name)r   r'   s    r   get_namespaceszSymbol.get_namespaces5  s       r   c                     t        | j                        dk(  rt        d      t        | j                        dkD  rt        d      | j                  d   S )zReturn the single namespace bound to this name.

        Raises ValueError if the name is bound to multiple namespaces
        or no namespace.
        r   z#name is not bound to any namespacesr   z$name is bound to multiple namespaces)lenr   
ValueErrorr'   s    r   get_namespacezSymbol.get_namespace9  sS     t  !Q&BCC""#a'CDD$$Q''r   r#   )r5   r6   r7   r)   rE   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   r      s[    +U +4
2.
K15
K.
*/.'!(r   r   __main__r   exec),__doc__r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r$   __all__r   r!   r   r   r   r   r   r5   ossysopenargvfreadsrcpathsplitmodre   ro   rm   infoprintr   r   r8   r   r   <module>r      s'   8 4 4 4 4 4 
D* $ %&p0 p0f3{ 3lK b( b(H z	chhqk	 affh
3chhqk215v
>C$$& :zz% dDMMOT%6%6%89:  s   D++D4