o ?Og'@sXdZddlZddlmZmZmZmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlZgdZddZGdddZeZGd d d ZGd d d eZGd ddeZGdddZedkrddlZddlZeejd Ze Z!Wdn1s~wYee!ej"#ejdddZ$e$%D]Z&e$'e&Z(e)e(e(*e(+qdSdS)z2Interface to the compiler's internal symbol tablesN)USE DEF_GLOBAL DEF_NONLOCAL DEF_LOCAL DEF_PARAM DEF_IMPORT DEF_BOUND DEF_ANNOT SCOPE_OFF SCOPE_MASKFREELOCALGLOBAL_IMPLICITGLOBAL_EXPLICITCELL)symtable SymbolTableClassFunctionSymbolcCst|||}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)codefilenameZ compile_typetopr//opt/alt/python310/lib64/python3.10/symtable.pyr s rc@s$eZdZddZddZddZdS)SymbolTableFactorycCst|_dSN)weakrefZWeakValueDictionary_SymbolTableFactory__memoselfrrr__init__szSymbolTableFactory.__init__cCs6|jtjkr t||S|jtjkrt||St||Sr)typer TYPE_FUNCTIONr TYPE_CLASSrr)r"tablerrrrnews     zSymbolTableFactory.newcCs8||f}|j|d}|dur|||}|j|<|Sr)r getr()r"r'rkeyobjrrr__call__ s zSymbolTableFactory.__call__N)__name__ __module__ __qualname__r#r(r,rrrrrs rc@s|eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZdS)rcCs||_||_i|_dSr)_table _filename_symbols)r"Z raw_tablerrrrr#,s zSymbolTable.__init__cCsJ|jtkrd}nd|jj}|jjdkrd||jSd||jj|jS)Nz%s rz<{0}SymbolTable for module {1}>z<{0}SymbolTable for {1} in {2}>) __class__rr-r0nameformatr1)r"kindrrr__repr__1s   zSymbolTable.__repr__cCs:|jjtjkr dS|jjtjkrdS|jjtjkrdSdS)zwReturn the type of the symbol table. The values retuned are 'class', 'module' and 'function'. moduleZfunctionclassN)r0r$rZ TYPE_MODULEr%r&r!rrrget_type>szSymbolTable.get_typecC|jjS)z,Return an identifier for the table. )r0idr!rrrget_idMszSymbolTable.get_idcCr<)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. )r0r5r!rrrget_nameRszSymbolTable.get_namecCr<)zPReturn the number of the first line in the block for the table. )r0linenor!rrr get_lineno[szSymbolTable.get_linenocCst|jjtjkS)zJReturn *True* if the locals in the table are optimizable. )boolr0r$rr%r!rrr is_optimizedaszSymbolTable.is_optimizedcC t|jjS)zAReturn *True* if the block is a nested class or function.)rBr0nestedr!rrr is_nestedg zSymbolTable.is_nestedcCrD)z:Return *True* if the block has nested namespaces. )rBr0childrenr!rrr has_childrenlrGzSymbolTable.has_childrencCs |jjS)zKReturn a view object containing the names of symbols in the table. )r0symbolskeysr!rrrget_identifiersqrGzSymbolTable.get_identifierscCsT|j|}|dur(|jj|}||}|jjdk}t||||d}|j|<|S)zLLookup a *name* in the table. Returns a *Symbol* instance. Nr module_scope)r2r)r0rJ_SymbolTable__check_childrenr5r)r"r5Zsymflags namespacesrNrrrlookupvs    zSymbolTable.lookupcsfddDS)zLReturn a list of *Symbol* instances for names in the table. csg|]}|qSr)rR.0identr!rr sz+SymbolTable.get_symbols..)rLr!rr!r get_symbolsszSymbolTable.get_symbolscsfddjjDS)Ncs"g|] }|jkrt|jqSr)r5rr1rTstr5r"rrrVs   z0SymbolTable.__check_children..r0rH)r"r5rrZrZ__check_childrens zSymbolTable.__check_childrencsfddjjDS)z3Return a list of the nested symbol tables. csg|]}t|jqSr)rr1rXr!rrrVsz,SymbolTable.get_children..r[r!rr!r get_childrens zSymbolTable.get_childrenN)r-r.r/r#r8r;r>r?rArCrFrIrLrRrWrOr\rrrrr*s   rc@sPeZdZdZdZdZdZdZddZddZ ddZ dd Z d d Z d d Z dS)rNcstfddDS)Nc3s$|] }jj|r|VqdSr)r0rJrSr"Z test_funcrr sz-Function.__idents_matching..)tuplerLr]rr]rZ__idents_matchingszFunction.__idents_matchingcC |jdur |dd|_|jS)z6Return a tuple of parameters to the function. NcS|t@Sr)rxrrrz)Function.get_parameters..)_Function__params_Function__idents_matchingr!rrrget_parameters zFunction.get_parametersc0|jdurttffdd}|||_|jS)z2Return a tuple of locals in the function. Nc|t?t@vSrr r rbZlocsrrrdz%Function.get_locals..)_Function__localsr rrgr"testrrmr get_locals   zFunction.get_localscrj)z3Return a tuple of globals in the function. NcrkrrlrbZglobrrrdrnz&Function.get_globals..)_Function__globalsrrrgrprrtr get_globalsrszFunction.get_globalscCr`)z5Return a tuple of nonlocals in the function. NcSrar)rrbrrrrdrez(Function.get_nonlocals..)_Function__nonlocalsrgr!rrr get_nonlocalsrizFunction.get_nonlocalscCs$|jdurdd}|||_|jS)z:Return a tuple of free variables in the function. NcSs|t?t@tkSr)r r r rbrrrrdrnz$Function.get_frees..)_Function__freesrg)r"is_freerrr get_freess  zFunction.get_frees)r-r.r/rfroryrurwrgrhrrrvrxr{rrrrrs  rc@seZdZdZddZdS)rNcCs6|jduri}|jjD]}d||j<q t||_|jS)z9Return a tuple of methods declared in the class. N)_Class__methodsr0rHr5r_)r"drYrrr get_methodss    zClass.get_methods)r-r.r/r}rrrrrrs rc@seZdZd$ddddZddZdd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZd d!Zd"d#ZdS)%rNFrMcCs.||_||_|t?t@|_|pd|_||_dS)Nr) _Symbol__name_Symbol__flagsr r _Symbol__scope_Symbol__namespaces_Symbol__module_scope)r"r5rPrQrNrrrr#s   zSymbol.__init__cCs d|jS)Nz)r6rr!rrrr8s zSymbol.__repr__cC|jS)z#Return a name of a symbol. )rr!rrrr?szSymbol.get_namecCst|jtj@S)zBReturn *True* if the symbol is used in its block. )rBrrrr!rrr is_referencedszSymbol.is_referencedcCt|jt@S)z4Return *True* if the symbol is a parameter. )rBrrr!rrr is_parameterzSymbol.is_parametercC"t|jttfvp|jo|jt@S)z0Return *True* if the sysmbol is global. )rBrrrrrrr!rrr is_globalzSymbol.is_globalcCr)z(Return *True* if the symbol is nonlocal.)rBrrr!rrr is_nonlocalzSymbol.is_nonlocalcCt|jtkS)zOReturn *True* if the symbol is declared global with a global statement.)rBrrr!rrris_declared_globalrzSymbol.is_declared_globalcCr)z.Return *True* if the symbol is local. )rBrr rrrrr!rrris_localrzSymbol.is_localcCr)z2Return *True* if the symbol is annotated. )rBrr r!rrr is_annotated rzSymbol.is_annotatedcCr)zIReturn *True* if a referenced symbol is not assigned to. )rBrr r!rrrrzzSymbol.is_freecCr)zQReturn *True* if the symbol is created from an import statement. )rBrrr!rrr is_importedrzSymbol.is_importedcCr)z)Return *True* if a symbol is assigned to.)rBrrr!rrr is_assignedrzSymbol.is_assignedcCs t|jS)aReturns *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. )rBrr!rrr is_namespace!s zSymbol.is_namespacecCr)z.Return a list of namespaces bound to this name)rr!rrrget_namespaces.szSymbol.get_namespacescCs t|jdkr td|jdS)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!rrr get_namespace2s zSymbol.get_namespacer)r-r.r/r#r8r?rrrrrrrrzrrrrrrrrrrs" r__main__r|exec),__doc__rrrrrrrrr r r r r rrrr__all__rrrrrrrr-ossysopenargvfreadsrcpathsplitmodrLrUrRinfoprintrrrrrrs.D l6`