U e5d"Kã@s¸dZddlTd=dddœdd „Zd d „Zd>d d„Zdd„Zdd„Zd?dd„Zdd„Zdd„Z d@dd„Z dd„Z dd„Z dd œd!d"„Z d#d$„ZGd%d&„d&eƒZGd'd(„d(eƒZd)d*„Zd+d,„Zeeeƒe_eeeƒe_Gd-d.„d.eƒZd/d0„ZGd1d2„d2eed3�ZGd4d5„d5eed3�ZGd6d7„d7eed3�ZGd8d9„d9eed3�ZGd:d;„d;eed3�Zee e!e"fee#fee$feedƒe%feed<ƒfiZ&ee%fiZ'e%d9edƒd9e d2e!d2e"d2e#d5e$d7ed<ƒd;iZ(dS)AaH ast ~~~ The `ast` module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like and allows modifications of it. An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as a flag to the `compile()` builtin function or by using the `parse()` function from this module. The result will be a tree of objects whose classes all inherit from `ast.AST`. A modified abstract syntax tree can be compiled into a Python code object using the built-in `compile()` function. Additionally various helper functions are provided that make working with the trees simpler. The main intention of the helper functions and this module in general is to provide an easy to use interface for libraries that work tightly with the python syntax (template engines for example). :copyright: Copyright 2008 by Armin Ronacher. :license: Python License. é)Ú*ú ÚexecFN)Ú type_commentsÚfeature_versioncCsRt}|r|tO}t|tƒr4|\}}|dks.t‚|}n |dkr@d}t|||||d�S)z½ Parse the source into an AST node. Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). Pass type_comments=True to get back type comments where the syntax allows. éNéÿÿÿÿ)Ú_feature_version)Z PyCF_ONLY_ASTZPyCF_TYPE_COMMENTSÚ isinstanceÚtupleÚAssertionErrorÚcompile)ÚsourceÚfilenameÚmoderrÚflagsÚmajorÚminor©rú/usr/lib64/python3.8/ast.pyÚparses   ÿrcs`t|tƒrt|dd�}t|tƒr&|j}dd„‰‡fdd„‰‡fdd„‰‡‡‡‡fd d „‰ˆ|ƒS) a Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None. Úeval)rcSstd|›�ƒ‚dS)Nzmalformed node or string: )Ú ValueError©ÚnoderrrÚ_raise_malformed_node>sz+literal_eval.._raise_malformed_nodecs,t|tƒrt|jƒtttfkr&ˆ|ƒ|jS©N)r ÚConstantÚtypeÚvalueÚintÚfloatÚcomplexr)rrrÚ _convert_num@sz"literal_eval.._convert_numcsDt|tƒr._convert_signed_numcst|tƒr|jSt|tƒr*ttˆ|jƒƒSt|tƒrDttˆ|jƒƒSt|t ƒr^t tˆ|jƒƒSt|t ƒr¢t |j ƒt |jƒkr„ˆ|ƒtttˆ|j ƒtˆ|jƒƒƒSt|tƒ�rt|jttfƒ�rˆ|jƒ}ˆ|jƒ}t|ttfƒ�rt|tƒ�rt|jtƒ�r||S||Sˆ|ƒSr)r rrZTupler ÚmapZeltsZListÚlistÚSetÚsetZDictÚlenÚkeysÚvaluesÚdictÚzipZBinOpr$ZAddZSubÚleftÚrightr r!r")rr0r1©Ú_convertr#r&rrrr3Ls,      ÿ  zliteral_eval.._convert)r ÚstrrZ ExpressionÚbody)Znode_or_stringrr2rÚ literal_eval3s     r6Tcs2‡‡‡fdd„‰t|tƒs*td|jjƒ‚ˆ|ƒS)aÝ Return a formatted dump of the tree in node. This is mainly useful for debugging purposes. If annotate_fields is true (by default), the returned string will show the names and the values for fields. If annotate_fields is false, the result string will be more compact by omitting unambiguous field names. Attributes such as line numbers and column offsets are not dumped by default. If this is wanted, include_attributes can be set to true. c st|tƒrÒg}ˆ}|jD]V}zt||ƒ}Wntk rBd}YqX|r`| d|ˆ|ƒf¡q| ˆ|ƒ¡qˆr¼|jr¼|jD]:}z | d|ˆt||ƒƒf¡Wq€tk r¸Yq€Xq€d|jjd  |¡fSt|t ƒrødd  ‡fdd„|Dƒ¡St |ƒS)NTz%s=%sz%s(%s)z, z[%s]c3s|]}ˆ|ƒVqdSrr)Ú.0Úx)Ú_formatrrÚ †sz(dump.._format..) r ÚASTÚ_fieldsÚgetattrÚAttributeErrorÚappendÚ _attributesÚ __class__Ú__name__Újoinr(Úrepr)rÚargsÚkeywordsÚfieldrÚa©r9Úannotate_fieldsÚinclude_attributesrrr9ps*       zdump.._formatzexpected AST, got %r)r r;Ú TypeErrorrArB)rrJrKrrIrÚdumpfs  rMcCsVdD]L}||jkr||jkrt||dƒ}|dk sDt||ƒr| d¡rt|||ƒq|S)z¬ Copy source location (`lineno`, `col_offset`, `end_lineno`, and `end_col_offset` attributes) from *old_node* to *new_node* if possible, and return *new_node*. )ÚlinenoÚ col_offsetÚ end_linenoÚend_col_offsetNZend_)r@r=ÚhasattrÚ startswithÚsetattr)Únew_nodeZold_nodeÚattrrrrrÚ copy_location�s ÿÿrWcs ‡fdd„‰ˆ|ddddƒ|S)a{ When you compile a node tree with compile(), the compiler expects lineno and col_offset attributes for every node that supports them. This is rather tedious to fill in for generated nodes, so this helper adds these attributes recursively where not already set, by setting them to the values of the parent node. It works recursively starting at *node*. csªd|jkr"t|dƒs||_n|j}d|jkrDt|dƒs>||_n|j}d|jkrft|dƒs`||_n|j}d|jkrˆt|dƒs‚||_n|j}t|ƒD]}ˆ|||||ƒq�dS)NrNrPrOrQ)r@rRrNrPrOrQÚiter_child_nodes)rrNrOrPrQÚchild©Ú_fixrrr[¦s$         z#fix_missing_locations.._fixérrrrrZrÚfix_missing_locationsžs r]r\cCsVt|ƒD]H}d|jkr(t|ddƒ||_d|jkrt|ddƒ}dk r|||_q|S)z´ Increment the line number and end line number of each node in the tree starting at *node* by *n*. This is useful to "move code" to a different location in a file. rNrrPN)Úwalkr@r=rNrP)rÚnrYrPrrrÚincrement_linenoÁs  ÿþ r`c cs:|jD].}z|t||ƒfVWqtk r2YqXqdS)zs Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` that is present on *node*. N)r<r=r>)rrGrrrÚ iter_fieldsÒs  raccsLt|ƒD]>\}}t|tƒr"|Vqt|tƒr|D]}t|tƒr0|Vq0qdS)z‰ Yield all direct child nodes of *node*, that is, all fields that are nodes and all items of fields that are lists of nodes. N)rar r;r()rÚnamerGÚitemrrrrXÞs   rXcCs–t|ttttfƒs"td|jjƒ‚|jr8t|jdt ƒsroÚencodeÚdecoderrÚinsertr?rC) rrrsrNrPrOrQrmZpaddingÚfirstZlastrrrÚget_source_segment*s&      rxccs<ddlm}||gƒ}|r8| ¡}| t|ƒ¡|VqdS)zå Recursively yield all descendant nodes in the tree starting at *node* (including *node* itself), in no specified order. This is useful if you only want to modify nodes in place and don't care about the context. r)ÚdequeN)Ú collectionsryÚpopleftÚextendrX)rryZtodorrrr^Ms   r^c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú NodeVisitora< A node visitor base class that walks the abstract syntax tree and calls a visitor function for every node found. This function may return a value which is forwarded by the `visit` method. This class is meant to be subclassed, with the subclass adding visitor methods. Per default the visitor functions for the nodes are ``'visit_'`` + class name of the node. So a `TryFinally` node visit function would be `visit_TryFinally`. This behavior can be changed by overriding the `visit` method. If no visitor function exists for a node (return value `None`) the `generic_visit` visitor is used instead. Don't use the `NodeVisitor` if you want to apply changes to nodes during traversing. For this a special visitor exists (`NodeTransformer`) that allows modifications. cCs"d|jj}t|||jƒ}||ƒS)z Visit a node.Úvisit_)rArBr=Ú generic_visit)ÚselfrÚmethodÚvisitorrrrÚvisitos zNodeVisitor.visitcCsTt|ƒD]F\}}t|tƒr:|D]}t|tƒr| |¡qqt|tƒr| |¡qdS)z9Called if no explicit visitor function exists for a node.N)rar r(r;rƒ)r€rrGrrcrrrrus   zNodeVisitor.generic_visitc Cs¢|j}t t|ƒ¡}|dkr@t ¡D]\}}t||ƒr$|}q@q$|dk r˜d|}zt||ƒ}Wntk rrYn&Xddl}|  |›d�t d¡||ƒS|  |¡S)Nr~rz" is deprecated; add visit_Constanté) rÚ_const_node_type_namesÚgetrÚitemsr r=r>ÚwarningsÚwarnÚPendingDeprecationWarningr) r€rrZ type_nameÚclsrbr�r‚rˆrrrÚvisit_Constants(  ÿzNodeVisitor.visit_ConstantN)rBÚ __module__Ú __qualname__Ú__doc__rƒrrŒrrrrr}[s r}c@seZdZdZdd„ZdS)ÚNodeTransformeraG A :class:`NodeVisitor` subclass that walks the abstract syntax tree and allows modification of nodes. The `NodeTransformer` will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is ``None``, the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place. Here is an example transformer that rewrites all occurrences of name lookups (``foo``) to ``data['foo']``:: class RewriteName(NodeTransformer): def visit_Name(self, node): return Subscript( value=Name(id='data', ctx=Load()), slice=Index(value=Str(s=node.id)), ctx=node.ctx ) Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth:`generic_visit` method for the node first. For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node. Usually you use the transformer like this:: node = YourTransformer().visit(node) cCs°t|ƒD]¢\}}t|tƒrvg}|D]D}t|tƒr\| |¡}|dkrFq"nt|tƒs\| |¡q"| |¡q"||dd…<qt|tƒr| |¡}|dkržt||ƒqt|||ƒq|Sr) rar r(r;rƒr|r?ÚdelattrrT)r€rrGÚ old_valueZ new_valuesrrUrrrr¹s&         zNodeTransformer.generic_visitN)rBr�rŽr�rrrrrr�•s#r�cCs|jSr©r)r€rrrÚ_getterÓsr”cCs ||_dSrr“)r€rrrrÚ_setterÖsr•c@seZdZdd„ZdS)Ú_ABCcCsft|tƒsdS|tkrZz |j}Wntk r6YdSXt|t|ƒoXt|t |d¡ƒ St ||¡S)NFr) r rÚ _const_typesrr>Ú_const_types_notr†rÚ__instancecheck__)r‹Úinstrrrrr™Þs  þz_ABC.__instancecheck__N)rBr�rŽr™rrrrr–Üsr–cOsf|D]<}||jkrq|j |¡}|t|ƒkrt|j›d|›�ƒ‚q|tkrTt||ŽStj|f|ž|ŽS)Nz" got multiple values for argument )r<Úindexr+rLrBr—rÚ__new__)r‹rEÚkwargsÚkeyÚposrrrÚ_newís    r c@seZdZdZeZdS)ÚNum)r_N©rBr�rŽr<r rœrrrrr¡ùsr¡)Ú metaclassc@seZdZdZeZdS)rd©reNr¢rrrrrdýsrdc@seZdZdZeZdS)ÚBytesr¤Nr¢rrrrr¥sr¥c@seZdZeZdS)Ú NameConstantN)rBr�rŽr rœrrrrr¦sr¦c@seZdZdZdd„ZdS)ÚEllipsisrcOs(|tkrtd|ž|ŽStj|f|ž|ŽS)N.).)r§rrœ)r‹rEr�rrrrœ szEllipsis.__new__N)rBr�rŽr<rœrrrrr§sr§.)rr)TF)r\)T))r�Ú_astrr6rMrWr]r`rarXrhrorrrxr^Úobjectr}r�r”r•Úpropertyrr_rerr–r r¡rdr¥r¦r§r r!r"r4ÚbytesÚboolr—r˜r…rrrrÚszÿ 3 '#    #:>     ûÿø