o
     JAf'                     @   sX  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d ZG dd dZe ZG d	d
 d
ZG dd deZG dd deZG dd dZedkrddlZddlZeejd Ze  Z!W d   n1 s~w   Y  ee!ej"#ejd d dZ$e$% D ]Z&e$'e&Z(e)e(e(* e(+  qdS dS )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                 C   s   t | ||}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filenameZcompile_typetop r   /usr/lib/python3.10/symtable.pyr      s   
r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )SymbolTableFactoryc                 C   s   t  | _d S N)weakrefWeakValueDictionary_SymbolTableFactory__memoselfr   r   r   __init__   s   zSymbolTableFactory.__init__c                 C   s6   |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   r   r   r   new   s
   


zSymbolTableFactory.newc                 C   s8   ||f}| j |d }|d u r| || }| j |< |S r   )r!   getr)   )r#   r(   r   keyobjr   r   r   __call__    s
   zSymbolTableFactory.__call__N)__name__
__module____qualname__r$   r)   r-   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 )r   c                 C   s   || _ || _i | _d S r   )_table	_filename_symbols)r#   Z	raw_tabler   r   r   r   r$   ,   s   
zSymbolTable.__init__c                 C   sJ   | j tkrd}nd| j j }| jjdkrd|| jS d|| jj| jS )N z%s r   z<{0}SymbolTable for module {1}>z<{0}SymbolTable for {1} in {2}>)	__class__r   r.   r1   nameformatr2   )r#   kindr   r   r   __repr__1   s   
zSymbolTable.__repr__c                 C   sX   | j jtjkr	dS | j jtjkrdS | j jtjkrdS | j jdv s*J d| j jdS )zwReturn the type of the symbol table.

        The values retuned are 'class', 'module' and
        'function'.
        modulefunctionclass)         zunexpected type: {0}N)r1   r%   r   ZTYPE_MODULEr&   r'   r7   r"   r   r   r   get_type>   s   zSymbolTable.get_typec                 C      | j jS )z,Return an identifier for the table.
        )r1   idr"   r   r   r   get_idM   s   zSymbolTable.get_idc                 C   rA   )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.
        )r1   r6   r"   r   r   r   get_nameR   s   zSymbolTable.get_namec                 C   rA   )zPReturn the number of the first line in the
        block for the table.
        )r1   linenor"   r   r   r   
get_lineno[   s   zSymbolTable.get_linenoc                 C   s   t | jjtjkS )zJReturn *True* if the locals in the table
        are optimizable.
        )boolr1   r%   r   r&   r"   r   r   r   is_optimizeda   s   zSymbolTable.is_optimizedc                 C      t | jjS )zAReturn *True* if the block is a nested class
        or function.)rG   r1   nestedr"   r   r   r   	is_nestedg      zSymbolTable.is_nestedc                 C   rI   )z:Return *True* if the block has nested namespaces.
        )rG   r1   childrenr"   r   r   r   has_childrenl   rL   zSymbolTable.has_childrenc                 C   s   | j j S )zKReturn a view object containing the names of symbols in the table.
        )r1   symbolskeysr"   r   r   r   get_identifiersq   rL   zSymbolTable.get_identifiersc                 C   sT   | j |}|du r(| jj| }| |}| jjdk}t||||d }| j |< |S )zLLookup a *name* in the table.

        Returns a *Symbol* instance.
        Nr   module_scope)r3   r*   r1   rO   _SymbolTable__check_childrenr6   r   )r#   r6   Zsymflags
namespacesrS   r   r   r   lookupv   s   
zSymbolTable.lookupc                    s    fdd   D S )zLReturn a list of *Symbol* instances for
        names in the table.
        c                    s   g | ]}  |qS r   )rW   .0identr"   r   r   
<listcomp>   s    z+SymbolTable.get_symbols.<locals>.<listcomp>)rQ   r"   r   r"   r   get_symbols   s   zSymbolTable.get_symbolsc                    s    fddj jD S )Nc                    s"   g | ]}|j  krt|jqS r   )r6   r   r2   rY   str6   r#   r   r   r[      s
    

z0SymbolTable.__check_children.<locals>.<listcomp>r1   rM   )r#   r6   r   r_   r   Z__check_children   s   zSymbolTable.__check_childrenc                    s    fdd j jD S )z3Return a list of the nested symbol tables.
        c                    s   g | ]}t | jqS r   )r   r2   r]   r"   r   r   r[      s    z,SymbolTable.get_children.<locals>.<listcomp>r`   r"   r   r"   r   get_children   s   
zSymbolTable.get_childrenN)r.   r/   r0   r$   r9   r@   rC   rD   rF   rH   rK   rN   rQ   rW   r\   rT   ra   r   r   r   r   r   *   s    	r   c                   @   sP   e Z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
d Zdd ZdS )r   Nc                    s   t  fdd  D S )Nc                 3   s$    | ]} j j| r|V  qd S r   )r1   rO   rX   r#   Z	test_funcr   r   	<genexpr>   s    z-Function.__idents_matching.<locals>.<genexpr>)tuplerQ   rb   r   rb   r   Z__idents_matching   s   zFunction.__idents_matchingc                 C       | j du r| dd | _ | j S )z6Return a tuple of parameters to the function.
        Nc                 S      | t @ S r   )r   xr   r   r   <lambda>       z)Function.get_parameters.<locals>.<lambda>)_Function__params_Function__idents_matchingr"   r   r   r   get_parameters      
zFunction.get_parametersc                    0   | j du rttf  fdd}| || _ | j S )z2Return a tuple of locals in the function.
        Nc                       | t ? t@  v S r   r
   r   rg   Zlocsr   r   ri          z%Function.get_locals.<locals>.<lambda>)_Function__localsr   r   rl   r#   testr   rr   r   
get_locals   
   
zFunction.get_localsc                    ro   )z3Return a tuple of globals in the function.
        Nc                    rp   r   rq   rg   globr   r   ri      rs   z&Function.get_globals.<locals>.<lambda>)_Function__globalsr   r   rl   ru   r   ry   r   get_globals   rx   zFunction.get_globalsc                 C   re   )z5Return a tuple of nonlocals in the function.
        Nc                 S   rf   r   )r   rg   r   r   r   ri      rj   z(Function.get_nonlocals.<locals>.<lambda>)_Function__nonlocalsrl   r"   r   r   r   get_nonlocals   rn   zFunction.get_nonlocalsc                 C   s$   | j du rdd }| || _ | j S )z:Return a tuple of free variables in the function.
        Nc                 S   s   | t ? t@ tkS r   )r
   r   r   rg   r   r   r   ri      rs   z$Function.get_frees.<locals>.<lambda>)_Function__freesrl   )r#   is_freer   r   r   	get_frees   s   
zFunction.get_frees)r.   r/   r0   rk   rt   r   r{   r}   rl   rm   rw   r|   r~   r   r   r   r   r   r      s    		r   c                   @   s   e Zd ZdZdd ZdS )r   Nc                 C   s6   | j du ri }| jjD ]}d||j< qt|| _ | j S )z9Return a tuple of methods declared in the class.
        Nr=   )_Class__methodsr1   rM   r6   rd   )r#   dr^   r   r   r   get_methods   s   

zClass.get_methods)r.   r/   r0   r   r   r   r   r   r   r      s    r   c                   @   s   e Zd Zd$dd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S )%r   NFrR   c                C   s.   || _ || _|t? t@ | _|pd| _|| _d S )Nr   )_Symbol__name_Symbol__flagsr
   r   _Symbol__scope_Symbol__namespaces_Symbol__module_scope)r#   r6   rU   rV   rS   r   r   r   r$      s
   

zSymbol.__init__c                 C   s   d | jS )Nz<symbol {0!r}>)r7   r   r"   r   r   r   r9      s   zSymbol.__repr__c                 C      | j S )z#Return a name of a symbol.
        )r   r"   r   r   r   rD      s   zSymbol.get_namec                 C   s   t | jtj@ S )zBReturn *True* if the symbol is used in
        its block.
        )rG   r   r   r   r"   r   r   r   is_referenced   s   zSymbol.is_referencedc                 C      t | jt@ S )z4Return *True* if the symbol is a parameter.
        )rG   r   r   r"   r   r   r   is_parameter      zSymbol.is_parameterc                 C   "   t | jttfv p| jo| jt@ S )z0Return *True* if the sysmbol is global.
        )rG   r   r   r   r   r   r   r"   r   r   r   	is_global      zSymbol.is_globalc                 C   r   )z(Return *True* if the symbol is nonlocal.)rG   r   r   r"   r   r   r   is_nonlocal      zSymbol.is_nonlocalc                 C      t | jtkS )zOReturn *True* if the symbol is declared global
        with a global statement.)rG   r   r   r"   r   r   r   is_declared_global  r   zSymbol.is_declared_globalc                 C   r   )z.Return *True* if the symbol is local.
        )rG   r   r   r   r   r   r   r"   r   r   r   is_local  r   zSymbol.is_localc                 C   r   )z2Return *True* if the symbol is annotated.
        )rG   r   r	   r"   r   r   r   is_annotated  r   zSymbol.is_annotatedc                 C   r   )zIReturn *True* if a referenced symbol is
        not assigned to.
        )rG   r   r   r"   r   r   r   r        zSymbol.is_freec                 C   r   )zQReturn *True* if the symbol is created from
        an import statement.
        )rG   r   r   r"   r   r   r   is_imported  r   zSymbol.is_importedc                 C   r   )z)Return *True* if a symbol is assigned to.)rG   r   r   r"   r   r   r   is_assigned  r   zSymbol.is_assignedc                 C   s
   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.
        )rG   r   r"   r   r   r   is_namespace!  s   
zSymbol.is_namespacec                 C   r   )z.Return a list of namespaces bound to this name)r   r"   r   r   r   get_namespaces.  s   zSymbol.get_namespacesc                 C   s    t | jdkrtd| jd S )zReturn the single namespace bound to this name.

        Raises ValueError if the name is bound to multiple namespaces.
        r=   z$name is bound to multiple namespacesr   )lenr   
ValueErrorr"   r   r   r   get_namespace2  s   
zSymbol.get_namespacer   )r.   r/   r0   r$   r9   rD   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s"    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   r.   ossysopenargvfreadsrcpathsplitmodrQ   rZ   rW   infoprintr   r   r   r   r   r   <module>   s.    D	l6`

