o w[e'@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)codefilename compile_typetopr/usr/lib/python3.10/symtable.pyr s rc@s$eZdZddZddZddZdS)SymbolTableFactorycCst|_dSN)weakrefWeakValueDictionary_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$ 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/r2nameformatr3)r$kindrrr__repr__1s   zSymbolTable.__repr__cCsX|jjtjkr dS|jjtjkrdS|jjtjkrdS|jjdvs*Jd|jjdS)zwReturn the type of the symbol table. The values retuned are 'class', 'module' and 'function'. modulefunctionclass)zunexpected type: {0}N)r2r&r TYPE_MODULEr'r(r9r#rrrget_type>s zSymbolTable.get_typecC|jjS)z,Return an identifier for the table. )r2idr#rrrget_idMszSymbolTable.get_idcCrD)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. )r2r8r#rrrget_nameRszSymbolTable.get_namecCrD)zPReturn the number of the first line in the block for the table. )r2linenor#rrr get_lineno[szSymbolTable.get_linenocCst|jjtjkS)zJReturn *True* if the locals in the table are optimizable. )boolr2r&rr'r#rrr is_optimizedaszSymbolTable.is_optimizedcC t|jjS)zAReturn *True* if the block is a nested class or function.)rJr2nestedr#rrr is_nestedg zSymbolTable.is_nestedcCrL)z:Return *True* if the block has nested namespaces. )rJr2childrenr#rrr has_childrenlrOzSymbolTable.has_childrencCs |jjS)zKReturn a view object containing the names of symbols in the table. )r2symbolskeysr#rrrget_identifiersqrOzSymbolTable.get_identifierscCsT|j|}|dur(|jj|}||}|jjdk}t||||d}|j|<|S)zLLookup a *name* in the table. Returns a *Symbol* instance. Nr module_scope)r4r+r2rR_SymbolTable__check_childrenr8r)r$r8symflags namespacesrVrrrlookupvs    zSymbolTable.lookupcsfddDS)zLReturn a list of *Symbol* instances for names in the table. csg|]}|qSr)r[.0identr#rr sz+SymbolTable.get_symbols..)rTr#rr#r get_symbolsszSymbolTable.get_symbolscsfddjjDS)Ncs"g|] }|jkrt|jqSr)r8rr3r]str8r$rrr_s   z0SymbolTable.__check_children..r2rP)r$r8rrcr__check_childrens zSymbolTable.__check_childrencsfddjjDS)z3Return a list of the nested symbol tables. csg|]}t|jqSr)rr3rar#rrr_sz,SymbolTable.get_children..rdr#rr#r get_childrens zSymbolTable.get_childrenN)r/r0r1r%r;rCrFrGrIrKrNrQrTr[r`rWrfrrrrr*s   rc@sPeZdZdZdZdZdZdZddZddZ ddZ dd Z d d Z d d Z dS)rNcstfddDS)Nc3s$|] }jj|r|VqdSr)r2rRr\r$ test_funcrr sz-Function.__idents_matching..)tuplerTrgrrgr__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 rnlocsrrrpz%Function.get_locals..)_Function__localsr rrsr$testrryr get_locals   zFunction.get_localscrv)z3Return a tuple of globals in the function. Ncrwrrxrnglobrrrpr{z&Function.get_globals..)_Function__globalsrrrsr}rrr get_globalsrzFunction.get_globalscCrl)z5Return a tuple of nonlocals in the function. NcSrmr)rrnrrrrprqz(Function.get_nonlocals..)_Function__nonlocalsrsr#rrr get_nonlocalsruzFunction.get_nonlocalscCs$|jdurdd}|||_|jS)z:Return a tuple of free variables in the function. NcSs|t?t@tkSr)r r r rnrrrrpr{z$Function.get_frees..)_Function__freesrs)r$is_freerrr get_freess  zFunction.get_frees)r/r0r1rrr|rrrrsrtrrrrrrrrrs  rc@seZdZdZddZdS)rNcCs6|jduri}|jjD]}d||j<q t||_|jS)z9Return a tuple of methods declared in the class. Nr?)_Class__methodsr2rPr8rj)r$drbrrr get_methodss    zClass.get_methods)r/r0r1rrrrrrrs rc@seZdZd$ddddZddZdd Zd d Zd d ZddZddZ ddZ ddZ ddZ ddZ ddZddZddZd d!Zd"d#ZdS)%rNFrUcCs.||_||_|t?t@|_|pd|_||_dS)Nr) _Symbol__name_Symbol__flagsr r _Symbol__scope_Symbol__namespaces_Symbol__module_scope)r$r8rYrZrVrrrr%s   zSymbol.__init__cCs d|jS)Nz)r9rr#rrrr;s zSymbol.__repr__cC|jS)z#Return a name of a symbol. )rr#rrrrGszSymbol.get_namecCst|jtj@S)zBReturn *True* if the symbol is used in its block. )rJrrrr#rrr is_referencedszSymbol.is_referencedcCt|jt@S)z4Return *True* if the symbol is a parameter. )rJrrr#rrr is_parameterzSymbol.is_parametercC"t|jttfvp|jo|jt@S)z0Return *True* if the sysmbol is global. )rJrrrrrrr#rrr is_globalzSymbol.is_globalcCr)z(Return *True* if the symbol is nonlocal.)rJrrr#rrr is_nonlocalzSymbol.is_nonlocalcCt|jtkS)zOReturn *True* if the symbol is declared global with a global statement.)rJrrr#rrris_declared_globalrzSymbol.is_declared_globalcCr)z.Return *True* if the symbol is local. )rJrr rrrrr#rrris_localrzSymbol.is_localcCr)z2Return *True* if the symbol is annotated. )rJrr r#rrr is_annotated rzSymbol.is_annotatedcCr)zIReturn *True* if a referenced symbol is not assigned to. )rJrr r#rrrrzSymbol.is_freecCr)zQReturn *True* if the symbol is created from an import statement. )rJrrr#rrr is_importedrzSymbol.is_importedcCr)z)Return *True* if a symbol is assigned to.)rJrrr#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. )rJrr#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/r0r1r%r;rGrrrrrrrrrrrrrrrrrrs" r__main__r?exec),__doc__rrrrrrrrr r r r r rrrr __all__rrrrrrrr/ossysopenargvfreadsrcpathsplitmodrTr^r[infoprintrrrrrrs.D l6`