B -_1@sdZddlTd!ddZddZd"d d Zd dZddZd#ddZddZddZ d$ddZ ddZ Gddde Z Gddde Zd S)%aH 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. )* execcCst|||tS)zn Parse the source into an AST node. Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). )compileZ PyCF_ONLY_AST)sourcefilenamemoder /usr/lib/python3.7/ast.pyparsesr csRt|trt|dd}t|tr&|j}ddfddfdd|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)rcSsHt|tr$t|jtttfr4|jSnt|tr4|jStdt |dS)Nzmalformed node or string: ) isinstanceConstantvalueintfloatcomplexNumn ValueErrorrepr)noder r r _convert_num1s   z"literal_eval.._convert_numcsDt|tr._convert_signed_numcs.t|tr|jSt|ttfr$|jSt|tr4|jSt|trNt t |j St|t rht t |j St|trtt |j St|trttt |jt |jSt|tr|jSt|tr&t|jttfr&|j}|j}t|ttfr&t|tr&t|jtr||S||S|S)N)r rrStrZBytessrrZTupletuplemapZeltsZListlistSetsetZDictdictzipkeysvaluesZ NameConstantZBinOprZAddZSubleftrightrrr)rr'r()_convertrrr r r)@s2         zliteral_eval.._convert)r strr Z Expressionbody)Znode_or_stringr )r)rrr literal_eval&s    r,TFcs2fddt|ts*td|jj|S)a Return a formatted dump of the tree in *node*. This is mainly useful for debugging purposes. The returned string will show the names and the values for fields. This makes the code impossible to evaluate, so if evaluation is wanted *annotate_fields* must be set to False. Attributes such as line numbers and column offsets are not dumped by default. If this is wanted, *include_attributes* can be set to True. csttrfddtD}djjdr>dd|Dn dd|Df}rjr||rfdphd7}|dfd djD7}|d Sttrd dfd dDStS) Ncsg|]\}}||fqSr r ).0ab)_formatr r isz)dump.._format..z%s(%sz, css|]}d|VqdS)z%s=%sNr )r-fieldr r r ksz(dump.._format..css|]\}}|VqdS)Nr )r-r.r/r r r r3ms c3s$|]}d|t|fVqdS)z%s=%sN)getattr)r-r.)r0rr r r3qs)z[%s]c3s|]}|VqdS)Nr )r-x)r0r r r3us) r AST iter_fields __class____name__join _attributesr r)rZfieldsrv)r0annotate_fieldsinclude_attributes)rr r0gs    zdump.._formatzexpected AST, got %r)r r8 TypeErrorr:r;)rr?r@r )r0r?r@r dump^s  rBcCsBx||_n|j}xt|D]}|||qNWdS)NrCrD)r=rErCrDiter_child_nodes)rrCrDchild)_fixr r rLs    z#fix_missing_locations.._fixrr )rr )rLr fix_missing_locationss rNrMcCs2x,t|D] }d|jkr t|dd||_q W|S)z Increment the 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. rCr)walkr=r5rC)rrrKr r r increment_linenos rPc cs>x8|jD].}y|t||fVWqtk r4YqXqWdS)zs Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` that is present on *node*. N)_fieldsr5AttributeError)rr2r r r r9s  r9ccsTxNt|D]B\}}t|tr$|Vq t|tr x|D]}t|tr4|Vq4Wq WdS)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)r9r r8r )rnamer2itemr r r rJs    rJcCst|ttttfs"td|jj|jr8t|jdt ss 8     %