U e5d"K@sdZddlTd=ddddd Zd d Zd>d dZddZddZd?ddZddZddZ d@ddZ ddZ ddZ dd d!d"Z d#d$ZGd%d&d&eZGd'd(d(eZd)d*Zd+d,Zeeee_eeee_Gd-d.d.eZd/d0ZGd1d2d2eed3ZGd4d5d5eed3ZGd6d7d7eed3ZGd8d9d9eed3ZGd:d;d;eed3Zee e!e"fee#fee$feede%feed<fiZ&ee%fiZ'e%d9edd9e 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_commentsfeature_versioncCsFt}|r|tO}t|tr(|\}}|}n |dkr4d}t|||||dS)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 isinstancetuplecompile)sourcefilenamemoderrflagsmajorminorr/usr/lib64/python3.8/ast.pyparses  rcs`t|trt|dd}t|tr&|j}ddfddfddfd 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: ) ValueErrornoderrr_raise_malformed_node>sz+literal_eval.._raise_malformed_nodecs,t|trt|jtttfkr&||jSN)r Constanttypevalueintfloatcomplexr)rrr _convert_num@sz"literal_eval.._convert_numcsDt|tr._convert_signed_numcst|tr|jSt|tr*tt|jSt|trDtt|jSt|t r^t t|jSt|t rt |j t |jkr|ttt|j t|jSt|trt|jttfr|j}|j}t|ttfrt|trt|jtr||S||S|Sr)r rrZTupler mapZeltsZListlistSetsetZDictlenkeysvaluesdictzipZBinOpr"ZAddZSubleftrightrrr )rr.r/_convertr!r$rrrr1Ls,        zliteral_eval.._convert)r strrZ Expressionbody)Znode_or_stringrr0r literal_eval3s     r4Tcs2fddt|ts*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|trg}}|jD]V}zt||}Wntk rBd}YqX|r`|d||fq||qr|jr|jD]:}z |d|t||fWqtk rYqXqd|jjd |fSt|t rdd fdd|DSt |S)NTz%s=%sz%s(%s)z, z[%s]c3s|]}|VqdSrr).0x)_formatrr sz(dump.._format..) r AST_fieldsgetattrAttributeErrorappend _attributes __class____name__joinr&repr)rargskeywordsfieldrar7annotate_fieldsinclude_attributesrrr7ps*       zdump.._formatzexpected AST, got %r)r r9 TypeErrorr?r@)rrHrIrrGrdumpfs  rKcCsVdD]L}||jkr||jkrt||d}|dk sDt||r|drt|||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_linenoend_col_offsetNZend_)r>r;hasattr startswithsetattr)new_nodeZold_nodeattrrrrr copy_locations rUcs 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*. csd|jkr"t|ds||_n|j}d|jkrDt|ds>||_n|j}d|jkrft|ds`||_n|j}d|jkrt|ds||_n|j}t|D]}|||||qdS)NrLrNrMrO)r>rPrLrNrMrOiter_child_nodes)rrLrMrNrOchild_fixrrrYs$         z#fix_missing_locations.._fixrrrrrXrfix_missing_locationss r[rZcCsVt|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. rLrrNN)walkr>r;rLrN)rnrWrNrrrincrement_linenos   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<)rrErrr iter_fieldss  r_ccsLt|D]>\}}t|tr"|Vqt|tr|D]}t|tr0|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)r_r r9r&)rnamerEitemrrrrVs   rVcCst|ttttfs"td|jj|jr8t|jdt ssz 3 '#    #:>