a �DOg���@s�dZddlZddlTddlmZmZddlmZmZd`ddd �d d �Z d d �Z dadd�dd�Z dd�Z dd�Z dbdd�Zdd�Zdd�Zdcdd�Zdd �Zd!d"�Zdd#�d$d%�Zd&d'�ZGd(d)�d)e�ZGd*d+�d+e�Zeed,��s d-d.�Zd/d0�Zeee�e_eee�e_Gd1d2�d2e �Z!d3d4�Z"Gd5d6�d6ee!d7�Z#Gd8d9�d9ee!d7�Z$Gd:d;�d;ee!d7�Z%Gdd?�d?ee!d7�Z'e#e(e)e*fe$e+fe%e,fe&e d�e-fe'e d@�fiZ.e#e-fiZ/e-d=e d�d=e(d6e)d6e*d6e+d9e,d;e d@�d?iZ0GdAdB�dBe1�Z2GdCdD�dDe2�Z3GdEdF�dFe2�Z4ee5dG��sFdHdI�Z6dJdK�Z7ee6e7�e5_8GdLdM�dMe9�Z:GdNdO�dOe;�ZdTe?ej@jAd�ZBGdUdV�dVe�ZCdWZDdXZEgeD�eE�RZFGdYdZ�dZe�ZGd[d\�ZHd]d^�ZIeJd_k�r�eI�dS)daH 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. �N)�*)�contextmanager� nullcontext)�IntEnum�auto� �execF)� type_comments�feature_versioncCsRt}|r|tO}t|t�r4|\}}|dks.J�|}n |dur@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�compile)�source�filename�moder r �flags�major�minor�r�/usr/lib64/python3.9/ast.py�parse!s   �rcs`t|t�rt|dd�}t|t�r&|j}dd���fdd���fdd������fd d ���|�S) aT Evaluate an expression node or a string containing only 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. Caution: A complex expression can overflow the C stack and cause a crash. �eval�rcSstd|����dS)Nzmalformed node or string: )� ValueError��noderrr�_raise_malformed_nodeCsz+literal_eval.._raise_malformed_nodecs,t|t�rt|j�tttfvr&�|�|jS�N)r�Constant�type�value�int�float�complexr)rrr� _convert_numEsz"literal_eval.._convert_numcsDt|t�r._convert_signed_numcsZt|t�r|jSt|t�r*tt�|j��St|t�rDtt�|j��St|t �r^t t�|j��St|t �r�t|j t �r�|j jdkr�|j|jkr�gkr�nnt �St|t�r�t|j�t|j�krȈ|�ttt�|j�t�|j���St|t��rRt|jttf��rR�|j�}�|j�}t|ttf��rRt|t��rRt|jt��rJ||S||S�|�S)N�set) rr!r#�Tupler�map�elts�List�list�Setr-ZCall�func�Name�id�args�keywords�Dict�len�keys�values�dict�zipZBinOpr(�Add�Sub�left�rightr$r%r&)rrArB��_convertr'r,rrrrDQs<     ���   �  zliteral_eval.._convert)r�strrZ Expression�body)Znode_or_stringrrCr� literal_eval6s     rGT)�indentcsTd����fdd� �t|t�s.td|jj���durHt�t�sHd���|�dS)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. If indent is a non-negative integer or string, then the tree will be pretty-printed with that indent level. None (the default) selects the single line representation. rc s��dur*�d7�d��}d��}nd}d}t|t��r�t|�}g}d}�}|jD]�}zt||�} Wnty�d}YqXYn0| dur�t||d�dur�d}qX�| ��\} } |o�| }|r�|�d|| f�qX|�| �qX��rh|j�rh|jD]t}zt||�} Wnt�yYq�Yn0| du�r��z(dump.._format..)r�ASTr"�_fields�getattr�AttributeError�append� _attributesr:� __class__�__name__�joinr2�repr) rrQ�prefix�sep�clsr7Z allsimpler8�namer#�simple�rP�annotate_fields�include_attributesrH)rQrrPzsX          &zdump.._formatzexpected AST, got %rN� )r)rrT� TypeErrorrZr[rE)rrdrerHrrcr�dumpns 0 rhcCsVdD]L}||jvr||jvrt||d�}|dusDt||�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_)rYrV�hasattr� startswith�setattr)�new_nodeZold_node�attrr#rrr� copy_location�s ��rrcs �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|jvr"t|d�s||_n|j}d|jvrJt|dd�durD||_n|j}d|jvrlt|d�sf||_n|j}d|jvr�t|dd�dur�||_n|j}t|�D]}�|||||�q�dS)Nrirkrjrl)rYrmrirVrkrjrl�iter_child_nodes)rrirjrkrl�child��_fixrrrv�s$       z#fix_missing_locations.._fixrIrrrrrur�fix_missing_locations�s rwrIcCsVt|�D]H}d|jvr(t|dd�||_d|jvrt|dd�}dur|||_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. rirrkN)�walkrYrVrirk)r�nrtrkrrr�increment_lineno�s  �� rzc cs8|jD],}z|t||�fVWqty0Yq0qdS)zs Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields`` that is present on *node*. N)rUrVrW)r�fieldrrr� iter_fields�s   r|ccsLt|�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)r|rrTr2)rrar{�itemrrrrss   rscCs�t|ttttf�s"td|jj��|jr8t|jdt �s|jdus|jdurWdS|jd}|jd}|j}|j}WntyRYdS0t|�}||kr|||��||���S|r�t||��d|����}nd}|||��|d���} ||��d|���} ||d|�}|� d| �|� | �d� |�S)aBGet source code segment of the *source* that generated *node*. If some location information (`lineno`, `end_lineno`, `col_offset`, or `end_col_offset`) is missing, return None. If *padded* is `True`, the first line of a multi-line statement will be padded with spaces to match its original position. NrIrKr) rkrlrirjrWr��encode�decoder��insertrXr\) rrr�rirkrjrlr�Zpadding�first�lastrrr�get_source_segmentNs*       r�ccs<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)� collectionsr��popleft�extendrs)rr�Ztodorrrrxss   rxc@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_)rZr[rV� generic_visit)�selfr�method�visitorrrr�visit�s 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)r|rr2rTr�)r�rr{r#r}rrrr��s   zNodeVisitor.generic_visitc Cs�|j}t�t|��}|dur@t��D]\}}t||�r$|}q@q$|dur�d|}zt||�}WntypYn&0ddl}|� |�d�t d�||�S|� |�S)Nr�rz" is deprecated; add visit_Constant�) r#�_const_node_type_names�getr"�itemsrrVrW�warnings�warn�DeprecationWarningr�) r�rr#� type_namer`rar�r�r�rrr�visit_Constant�s&   �zNodeVisitor.visit_ConstantN)r[� __module__� __qualname__�__doc__r�r�r�rrrrr��s r�c@seZdZdZdd�ZdS)�NodeTransformeraC 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=Constant(value=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\|�|�}|durFq"nt|t�s\|�|�q"|�|�q"||dd�<qt|t�r|�|�}|dur�t||�qt|||�q|Sr ) r|rr2rTr�r�rX�delattrro)r�rr{� old_valueZ new_valuesr#rprrrr��s&         zNodeTransformer.generic_visitN)r[r�r�r�r�rrrrr��s#r�rycCs|jS)zDeprecated. Use value instead.�r#�r�rrr�_getter�sr�cCs ||_dSr r��r�r#rrr�_setter�sr�c@seZdZdd�Zdd�ZdS)�_ABCcGs d|_dS)Nz3Deprecated AST node class. Use ast.Constant instead)r�)r`r7rrr�__init__sz _ABC.__init__cCsdt|t�sdS|tvrXz |j}Wnty4YdS0t|t|�oVt|t�|d�� St�||�S)NFr) rr!� _const_typesr#rW�_const_types_notr�r"�__instancecheck__)r`�instr#rrrr� s   �z_ABC.__instancecheck__N)r[r�r�r�r�rrrrr�sr�cOsp|D]<}||jvrq|j�|�}|t|�krt|j�d|����q|tvrXt|i|��Stj|g|�Ri|��S)Nz" got multiple values for argument )rU�indexr:rgr[r�r!�__new__)r`r7�kwargs�key�posrrr�_news   r�c@seZdZdZeZdS)�Num)ryN�r[r�r�rUr�r�rrrrr�%sr�)� metaclassc@seZdZdZeZdS)r��r�Nr�rrrrr�)sr�c@seZdZdZeZdS)�Bytesr�Nr�rrrrr�-sr�c@seZdZeZdS)� NameConstantN)r[r�r�r�r�rrrrr�1sr�c@seZdZdZdd�ZdS)�EllipsisrcOs6|turtdg|�Ri|��Stj|g|�Ri|��S)N.)r�r!r�)r`r7r�rrrr�7szEllipsis.__new__N)r[r�r�rUr�rrrrr�4sr�.c@seZdZdZdS)�slicezDeprecated AST node class.N�r[r�r�r�rrrrr�Rsr�c@seZdZdZdd�ZdS)�Indexz@Deprecated AST node class. Use the index value directly instead.cKs|Sr r)r`r#r�rrrr�Wsz Index.__new__N�r[r�r�r�r�rrrrr�Usr�c@seZdZdZddd�ZdS)�ExtSlicez1Deprecated AST node class. Use ast.Tuple instead.rcKstt|�t�fi|��Sr )r.r2ZLoad)r`�dimsr�rrrr�\szExtSlice.__new__N)rr�rrrrr�Zsr�r�cCs|jS)zDeprecated. Use elts instead.�r0r�rrr� _dims_getterdsr�cCs ||_dSr r�r�rrr� _dims_setterhsr�c@seZdZdZdS)�Suite�/Deprecated AST node class. Unused in Python 3.Nr�rrrrr�msr�c@seZdZdZdS)�AugLoadr�Nr�rrrrr�psr�c@seZdZdZdS)�AugStorer�Nr�rrrrr�ssr�c@seZdZdZdS)�Paramr�Nr�rrrrr�vsr�Z1ec@s�eZdZdZe�Ze�Ze�Ze�Ze�Z e�Z e�Z e�Z e Z e�Ze�Ze�Ze�Ze�Ze�Ze�Ze�Ze�Zdd�ZdS)� _Precedencez5Precedence table that originated from python grammar.cCs,z|�|d�WSty&|YS0dS�NrI)rZrr�rrr�next�s z_Precedence.nextN)r[r�r�r�r�TUPLE�YIELD�TEST�OR�AND�NOT�CMP�EXPR�BOR�BXOR�BAND�SHIFT�ARITH�TERM�FACTOR�POWER�AWAIT�ATOMr�rrrrr�~s(r�)�'�")z"""z'''cs�eZdZdZdd�dd�Zdd�Zdd �Zd d �Zd�d d�Zdd�Z dd�Z e dd��Z e dd�dd��Ze dd��Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Z�fd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Z d=d>�Z!d?d@�Z"dAdB�Z#dCdD�Z$dEdF�Z%dGdH�Z&dIdJ�Z'dKdL�Z(dMdN�Z)dOdP�Z*dQdR�Z+dSdT�Z,dUdV�Z-dWdX�Z.dYdZ�Z/d[d\�Z0d]d^�Z1d_d`�Z2dadb�Z3dcdd�Z4dedf�Z5dgdh�Z6didj�Z7dkdl�Z8dmdn�Z9dodp�Z:e;ddq�drds�Zdydz�Z?d{d|�Z@d}d~�ZAdd��ZBd�d��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZPd�d�d�d�d��ZQeRjSeRjTeRjTeRjTd��ZUd�d��ZVd�d�d�d�d�d�d�d�d�d�d�d�d�d�� ZWeRjXeRjXeRjYeRjYeRjYeRjYeRjZeRjZeRj[eRj\eRj]eRjYeRj^d�� Z_e`d��Zad�d��Zbd�d�d�d�d�d�d�d�d�d�d�� Zcd�d��Zdd�d�dĜZeeRjfeRjgdŜZhd�dDŽZid�dɄZjd�d˄Zkd�d̈́Zld�dτZmd�dфZnd�dӄZod�dՄZpd�dׄZqd�dلZrd�dۄZsd�d݄Ztd�d߄Zu�ZvS)�� _Unparserz�Methods in this class recursively traverse an AST and output source code for the abstract syntax; original formatting is disregarded.F��_avoid_backslashescCs(g|_g|_i|_i|_d|_||_dS)Nr)�_source�_buffer� _precedences� _type_ignores�_indentr�)r�r�rrrr��s z_Unparser.__init__cCsHt|�}z|t|��Wnty*Yn0|D]}|�||�q0dS)z7Call f on each item in seq, calling inter() in between.N)�iterr�� StopIteration)r�Zinter�f�seqrOrrr� interleave�s z_Unparser.interleavecs>t|�dkr$||d���d�n���fdd�||�dS)z�Traverse and separate the given *items* with a comma and append it to the buffer. If *items* is a single item sequence, a trailing comma will be added.rIr�,cs ��d�S�NrL��writerr�rr��rSz&_Unparser.items_view..N)r:r�r�)r�Z traverserr�rr�r� items_view�s   z_Unparser.items_viewcCs|jr|�d�dS)z8Adds a newline if it isn't the start of generated sourcerJN)r�r�r�rrr� maybe_newline�sz_Unparser.maybe_newlinerKcCs |��|�d|j|�dS)zXIndent a piece of text and append it, according to the current indentation levelz N)rr�r��r�r�rrr�fill�sz_Unparser.fillcCs|j�|�dS)zAppend a piece of textN)r�rXrrrrr��sz_Unparser.writecCs|j�|�dSr )r�rXrrrr� buffer_writer�sz_Unparser.buffer_writercCsd�|j�}|j��|S)NrK)r\r��clearr�rrr�buffer�s  z_Unparser.bufferN��extraccs>|�d�|r|�|�|jd7_dV|jd8_dS)aA context manager for preparing the source for blocks. It adds the character':', increases the indentation on enter and decreases the indentation on exit. If *extra* is given, it will be directly appended after the colon character. �:rIN)r�r�)r�rrrr�block�s   z_Unparser.blockccs|�|�dV|�|�dS)z�A context manager for preparing the source for expressions. It adds *start* to the buffer and enters, after exit it adds *end*.Nr�)r��start�endrrr�delimit�s z_Unparser.delimitcCs|r|�||�St�SdSr )r r)r�r r � conditionrrr� delimit_if�s z_Unparser.delimit_ifcCs|�dd|�|�|k�S)z,Shortcut to adding precedence related parens�(�))r�get_precedence)r�� precedencerrrr�require_parens�sz_Unparser.require_parenscCs|j�|tj�Sr )r�r�r�r��r�rrrrr�sz_Unparser.get_precedencecGs|D]}||j|<qdSr )r�)r�rZnodesrrrr�set_precedence�sz_Unparser.set_precedencecCs`t|ttttf�r t|j�dkr$dS|jd}t|t�s|�|�}r.|�|�|�|jdd��n |�|j�dSr�)r�_write_docstringrrF)r�rZ docstringrrr�"_write_docstring_and_traverse_body&s z,_Unparser._write_docstring_and_traverse_bodycCs*dd�|jD�|_|�|�|j��dS)NcSsi|]}|jd|j���qS)�ignore)ri�tag)rNrrrr� .s�z*_Unparser.visit_Module..)� type_ignoresr�rrrrrr� visit_Module-s � z_Unparser.visit_Modulecs`��dd��*���fdd��j|j�Wd�n1s<0Y��d���|j�dS)Nrrcs ��d�Sr�r�rr�rrr�8rSz._Unparser.visit_FunctionType..� -> )r r�r�argtypesr��returnsrrr�r�visit_FunctionType5s �" z_Unparser.visit_FunctionTypecCs(|��|�tj|j�|�|j�dSr )rrr�r�r#rrrrr� visit_Expr>sz_Unparser.visit_ExprcCsh|�tj|��F|�tj|j|j�|�|j�|�d�|�|j�Wd�n1sZ0YdS)Nz := ) rr�r�rr��targetr#rr�rrrr�visit_NamedExprCs   z_Unparser.visit_NamedExprcs(��d����fdd��j|j�dS)Nzimport cs ��d�Sr�r�rr�rrr�LrSz(_Unparser.visit_Import..)rr�r�namesrrr�r� visit_ImportJs z_Unparser.visit_ImportcsT��d���d|j�|jr,��|j���d����fdd��j|j�dS)Nzfrom �.z import cs ��d�Sr�r�rr�rrr�TrSz,_Unparser.visit_ImportFrom..)rr�rQ�moduler�rr*rrr�r�visit_ImportFromNs    z_Unparser.visit_ImportFromcCsP|��|jD]}|�|�|�d�q|�|j�|�|�}rL|�|�dS)N� = )r�targetsrr�r#r)r�rr(rrrr� visit_AssignVs    z_Unparser.visit_AssigncCsB|��|�|j�|�d|j|jjjd�|�|j�dS)Nrfz= ) rrr(r��binopr(rZr[r#rrrr�visit_AugAssign_s z_Unparser.visit_AugAssigncCs�|��|�dd|j o"t|jt���|�|j�Wd�n1sH0Y|�d�|�|j�|j r�|�d�|�|j �dS)Nrr�: r/) rrrbrr(r5rr�� annotationr#rrrr�visit_AnnAssignes *   z_Unparser.visit_AnnAssigncCs*|�d�|jr&|�d�|�|j�dS)N�returnrf)rr#r�rrrrr� visit_Returnos  z_Unparser.visit_ReturncCs|�d�dS)N�pass�rrrrr� visit_Passusz_Unparser.visit_PasscCs|�d�dS)N�breakr:rrrr� visit_Breakxsz_Unparser.visit_BreakcCs|�d�dS)N�continuer:rrrr�visit_Continue{sz_Unparser.visit_Continuecs(��d����fdd��j|j�dS)Nzdel cs ��d�Sr�r�rr�rrr��rSz(_Unparser.visit_Delete..)rr�rr0rrr�r� visit_Delete~s z_Unparser.visit_DeletecCs6|�d�|�|j�|jr2|�d�|�|j�dS)Nzassert rL)rr�test�msgr�rrrr� visit_Assert�s    z_Unparser.visit_Assertcs(��d����fdd��j|j�dS)Nzglobal cs ��d�Sr�r�rr�rrr��rSz(_Unparser.visit_Global..�rr�r�r*rrr�r� visit_Global�s z_Unparser.visit_Globalcs(��d����fdd��j|j�dS)Nz nonlocal cs ��d�Sr�r�rr�rrr��rSz*_Unparser.visit_Nonlocal..rDrrr�r�visit_Nonlocal�s z_Unparser.visit_NonlocalcCsh|�tj|��F|�d�|jrF|�d�|�tj|j�|�|j�Wd�n1sZ0YdS)N�awaitrf)rr�r�r�r#rr�rrrrr� visit_Await�s   z_Unparser.visit_AwaitcCsh|�tj|��F|�d�|jrF|�d�|�tj|j�|�|j�Wd�n1sZ0YdS)N�yieldrf)rr�r�r�r#rr�rrrrr� visit_Yield�s   z_Unparser.visit_YieldcCsf|�tj|��D|�d�|js(td��|�tj|j�|�|j�Wd�n1sX0YdS)Nz yield from z-Node can't be used without a value attribute.) rr�r�r�r#rrr�rrrrr�visit_YieldFrom�s  z_Unparser.visit_YieldFromcCsX|�d�|js"|jrtd��dS|�d�|�|j�|jrT|�d�|�|j�dS)N�raisez*Node can't use cause without an exception.rfz from )r�exc�causerr�rrrrr� visit_Raise�s    z_Unparser.visit_RaisecCs�|�d�|���|�|j�Wd�n1s40Y|jD]}|�|�qD|jr�|�d�|���|�|j�Wd�n1s�0Y|jr�|�d�|���|�|j�Wd�n1s�0YdS)N�try�else�finally)rr rrF�handlers�orelse� finalbody)r�r�exrrr� visit_Try�s  *    *  z_Unparser.visit_TrycCsz|�d�|jr&|�d�|�|j�|jrB|�d�|�|j�|���|�|j�Wd�n1sl0YdS)N�exceptrf� as )rr"r�rrar rFrrrr�visit_ExceptHandler�s      z_Unparser.visit_ExceptHandlercCs�|��|jD]}|�d�|�|�q|�d|j�|jdd|jpJ|jd��hd}|jD]"}|rp|�d�nd}|�|�q\|jD]"}|r�|�d�nd}|�|�q�Wd�n1s�0Y|� ��|� |�Wd�n1s�0YdS) N�@zclass rr)r FrLT) r�decorator_listrrrar�basesr8r�r r)r�r�deco�comma�errr�visit_ClassDef�s&        * z_Unparser.visit_ClassDefcCs|�|d�dS)N�def��_function_helperrrrr�visit_FunctionDef�sz_Unparser.visit_FunctionDefcCs|�|d�dS)Nz async defrcrrrr�visit_AsyncFunctionDef�sz _Unparser.visit_AsyncFunctionDefcCs�|��|jD]}|�d�|�|�q|d|j}|�|�|�dd��|�|j�Wd�n1sn0Y|jr�|�d�|�|j�|j |� |�d��|� |�Wd�n1s�0YdS)Nr[rfrrr#r) rr\rrrar r7r%r�r rr)r�rZ fill_suffixr^Zdef_strrrrrd�s    *  z_Unparser._function_helpercCs|�d|�dS)Nzfor �� _for_helperrrrr� visit_For�sz_Unparser.visit_ForcCs|�d|�dS)Nz async for rgrrrr�visit_AsyncFor�sz_Unparser.visit_AsyncForcCs�|�|�|�|j�|�d�|�|j�|j|�|�d��|�|j�Wd�n1s`0Y|jr�|�d�|���|�|j�Wd�n1s�0YdS)N� in rrQ) rrr(r�r�r rrFrT)r�rrrrrrhs    *  z_Unparser._for_helpercCs|�d�|�|j�|���|�|j�Wd�n1s@0Y|jr�t|j�dkr�t|jdt�r�|jd}|�d�|�|j�|���|�|j�Wd�qJ1s�0YqJ|j�r |�d�|���|�|j�Wd�n1�s0YdS)Nzif rIrzelif rQ) rrrAr rFrTr:rZIfrrrr�visit_Ifs   *$    ,  z_Unparser.visit_IfcCs�|�d�|�|j�|���|�|j�Wd�n1s@0Y|jr�|�d�|���|�|j�Wd�n1s�0YdS)Nzwhile rQ)rrrAr rFrTrrrr� visit_While s   *  z_Unparser.visit_Whilecsf��d����fdd��j|j��j��|�d����|j�Wd�n1sX0YdS)Nzwith cs ��d�Sr�r�rr�rrr�,rSz&_Unparser.visit_With..r�rr�rr�r rrFrrr�r� visit_With*s z_Unparser.visit_Withcsf��d����fdd��j|j��j��|�d����|j�Wd�n1sX0YdS)Nz async with cs ��d�Sr�r�rr�rrr�2rSz+_Unparser.visit_AsyncWith..rrnrrr�r�visit_AsyncWith0s z_Unparser.visit_AsyncWith�� quote_types�escape_special_whitespacecs��fdd�}d�t|����|}d�vr6dd�|D�}�fdd�|D�}|s�t���t�fdd �|D��d �}�d d �|gfS�r�|j�fd d�d�|d d �d kr�t|d �dks�J��dd �d�d ��|fS)z�Helper for writing string literals, minimizing escapes. Returns the tuple (string literal to write, possible quote types). cs4�s|dvr|S|dks |��s0|�d��d�S|S)Nz �\Zunicode_escape�ascii)� isprintabler�r�)r�)rsrr� escape_char<s  z2_Unparser._str_literal_helper..escape_charrKrJcSsg|]}|tvr|�qSr)� _MULTI_QUOTES�rN�qrrr� IrSz1_Unparser._str_literal_helper..csg|]}|�vr|�qSrrry��escaped_stringrrr{JrSc3s|]}�d|vr|VqdSrMrry)�stringrrrRPrSz0_Unparser._str_literal_helper..rrIr cs|d�dkS)Nrr r)rzr|rrr�TrSz/_Unparser._str_literal_helper..)r�r Nrt)r\r/r]r��sortr:)r�r~rrrsrwZpossible_quotes�quoter)rsr}r~r�_str_literal_helper6s  z_Unparser._str_literal_helper�rrcCs4|j||d�\}}|d}|�|�|�|���dS)zKWrite string literal value with a best effort attempt to avoid backslashes.r�rN)r�r�)r�r~rr� quote_typerrr�_write_str_avoiding_backslashes\sz)_Unparser._write_str_avoiding_backslashesc Cs�|�d�|jr.|�||j�|�|j�dSg}|jD]:}t|dt|�j �}|||j�|� |jt |t �f�q8g}t }|D]&\}}|j|||d�\}}|� |�q�d�|�}|d}|�|�|�|���dS)Nr�� _fstring_rqrKr)r�r��_fstring_JoinedStrrr�rr<rVr"r[rXrr!� _ALL_QUOTESr�r\) r�rrr#�methZ new_bufferrrZ is_constantr�rrr�visit_JoinedStrbs*     �   z_Unparser.visit_JoinedStrcCs(|�d�|�||j�|�|j�dS)Nr�)r��_fstring_FormattedValuerr�rrrrr�visit_FormattedValue�s z_Unparser.visit_FormattedValuecCs.|jD]"}t|dt|�j�}|||�qdS)Nr�)r<rVr"r[)r�rr�r#r�rrrr��s z_Unparser._fstring_JoinedStrcCs6t|jt�std��|j�dd��dd�}||�dS)Nz.Constants inside JoinedStr should be a string.�{z{{�}z}})rr#rEr�replace)r�rr�r#rrr�_fstring_Constant�s z_Unparser._fstring_ConstantcCs�|d�t|�dd�}|�tj��|j�|�|j�}|�d�rH|d�d|vrXtd��||�|j dkr�t |j �}|dvr�td ��|d |���|j r�|d �t |d t|j �j �}||j |�|d �dS)Nr�Tr�rfrtz5Unable to avoid backslash in f-string expression partr ZsrazUnknown f-string conversion.�!rr�r�)r"rr�r�r�r#r�rnr� conversion�chr� format_specrVr[)r�rr��unparser�exprr�r�rrrr��s&     z!_Unparser._fstring_FormattedValuecCs|�|j�dSr )r�r6rrrr� visit_Name�sz_Unparser.visit_NamecCs0|��|jdkr|�d�|j|jtd�dS)N�ur�)r�kindr�r�r#rxrrrrr�s  z_Unparser._write_docstringc Csjt|ttf�r<|�t|��dt��ddt�dt�d���n*|jrXt|t�rX|� |�n|�t|��dS)N�inf�nanr�-r) rr%r&r�r]r��_INFSTRr�rEr�r�rrr�_write_constant�s��� z_Unparser._write_constantcCs�|j}t|t�rL|�dd��|�|j|�Wd�q�1s@0Yn4|dur`|�d�n |jdkrt|�d�|�|j�dS)Nrr.�...r�)r#rrr r�r�r�r�)r�rr#rrrr��s .   z_Unparser.visit_ConstantcsJ��dd��*���fdd��j|j�Wd�n1s<0YdS)N�[�]cs ��d�Sr�r�rr�rrr��rSz&_Unparser.visit_List..)r r�rr0rrr�r� visit_List�sz_Unparser.visit_ListcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS)Nr�r��r r�elt� generators�r�r�genrrr�visit_ListComp�s  z_Unparser.visit_ListCompcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS�Nrrr�r�rrr�visit_GeneratorExp�s  z_Unparser.visit_GeneratorExpcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS)Nr�r�r�r�rrr� visit_SetComp�s  z_Unparser.visit_SetCompcCsh|�dd��H|�|j�|�d�|�|j�|jD]}|�|�q6Wd�n1sZ0YdS)Nr�r�r4)r rr�r�r#r�r�rrr�visit_DictComp�s     z_Unparser.visit_DictCompcCs�|jr|�d�n |�d�|�tj|j�|�|j�|�d�|jtj��|j g|j �R�|�|j �|j D]}|�d�|�|�qrdS)Nz async for z for rk� if ) �is_asyncr�rr�r�r(rr�r�r��ifs)r�rZ if_clauserrr�visit_comprehension�s       z_Unparser.visit_comprehensioncCs�|�tj|��p|�tj��|j|j�|�|j�|�d�|�|j�|�d�|�tj|j �|�|j �Wd�n1s�0YdS)Nr�z else ) rr�r�rr�rFrArr�rTrrrr� visit_IfExp�s    z_Unparser.visit_IfExpcs\|jrN��dd��*���fdd��j|j�Wd�qX1sB0Yn ��d�dS)Nr�r�cs ��d�Sr�r�rr�rrr�rSz%_Unparser.visit_Set..z{*()})r0r r�rr�rrr�r� visit_Set�s:z_Unparser.visit_Setcsj�fdd����fdd�}��dd��0���fdd�|t|j|j��Wd�n1s\0YdS) Ncs"��|���d���|�dS�Nr4)rr�)�k�vr�rr�write_key_value_pair s  z2_Unparser.visit_Dict..write_key_value_paircsB|\}}|dur4��d���tj|���|�n �||�dS)N�**)r�rr�r�r)r}r�r��r�r�rr� write_items   z(_Unparser.visit_Dict..write_itemr�r�cs ��d�Sr�r�rr�rrr�rSz&_Unparser.visit_Dict..)r r�r>r;r<)r�rr�rr�r� visit_Dicts   �z_Unparser.visit_DictcCs@|�dd�� |�|j|j�Wd�n1s20YdSr�)r r�rr0rrrr� visit_Tuplesz_Unparser.visit_Tuple�~�not�+r�)ZInvertZNotr)r*)r�r�r�r�cCs�|j|jjj}|j|}|�||��H|�|�|tjurF|�d�|� ||j �|� |j �Wd�n1st0YdS�Nrf) �unopr(rZr[�unop_precedencerr�r�r�rr+r)r�r�operator�operator_precedencerrr� visit_UnaryOp*s    z_Unparser.visit_UnaryOprr[�/�%�<<�>>�|�^�&�//r�) r?r@ZMultZMatMultZDivZModZLShiftZRShiftZBitOrZBitXorZBitAndZFloorDivZPow) r�r�rr[r�r�r�r�r�r�r�r�r�)r�cCs�|j|jjj}|j|}|�||��z||jvr@|��}|}n |}|��}|�||j �|� |j �|� d|�d��|�||j �|� |j �Wd�n1s�0YdSr�) r2r(rZr[�binop_precedencer� binop_rassocr�rrArr�rB)r�rr�r�Zleft_precedenceZright_precedencerrr� visit_BinOpWs   z_Unparser.visit_BinOpz==z!=�z>=�iszis not�inznot in) ZEqZNotEqZLtZLtEZGtZGtEZIsZIsNotZInZNotIncCs�|�tj|��x|jtj��|jg|j�R�|�|j�t|j |j�D].\}}|� d|j |j j d�|�|�qHWd�n1s�0YdSr�)rr�r�rr�rA� comparatorsrr>�opsr��cmpopsrZr[)r�r�or`rrr� visit_Compareus  z_Unparser.visit_Compare�and�or)ZAndZOr)r�r�cs~�j|jjj}�j|���fdd�}���|��6d|�d������fdd�||j�Wd�n1sp0YdS)Ncs"�������|���|�dSr )r�rrr)r�r�rr�increasing_level_traverse�s z9_Unparser.visit_BoolOp..increasing_level_traverserfcs ����Sr r�r)r�r�rrr��rSz(_Unparser.visit_BoolOp..)�boolopsr(rZr[�boolop_precedencerr�r<)r�rr�r�r)r�r�r�r� visit_BoolOp�s   z_Unparser.visit_BoolOpcCsZ|�tj|j�|�|j�t|jt�r@t|jjt�r@|�d�|�d�|�|j �dS)Nrfr,) rr�r�r#rrr!r$r�rqrrrr�visit_Attribute�s    z_Unparser.visit_AttributecCs�|�tj|j�|�|j�|�dd��hd}|jD]"}|rH|�d�nd}|�|�q4|jD]"}|rr|�d�nd}|�|�q^Wd�n1s�0YdS)NrrFrLT) rr�r�r4rr r7r�r8)r�rr_r`rrr� visit_Call�s      z_Unparser.visit_CallcCs~dd�}|�tj|j�|�|j�|�dd��:||j�rP|�|j|jj�n |�|j�Wd�n1sp0YdS)NcSs&t|t�o$|jo$tdd�|jD�� S)Ncss|]}t|t�VqdSr )rZStarred)rNr�rrrrR�rSzE_Unparser.visit_Subscript..is_simple_tuple..)rr.r0�any)Z slice_valuerrr�is_simple_tuple�s  ��z2_Unparser.visit_Subscript..is_simple_tupler�r�) rr�r�r#rr r�r�r0)r�rr�rrr�visit_Subscript�s   z_Unparser.visit_SubscriptcCs*|�d�|�tj|j�|�|j�dS)Nr)r�rr�r�r#rrrrr� visit_Starred�s z_Unparser.visit_StarredcCs|�d�dS)Nr�r�rrrr�visit_Ellipsis�sz_Unparser.visit_EllipsiscCsN|jr|�|j�|�d�|jr.|�|j�|jrJ|�d�|�|j�dS)Nr)�lowerrr��upper�steprrrr� visit_Slice�s    z_Unparser.visit_SlicecCs,|�|j�|jr(|�d�|�|j�dSr�)r��argr5rrrrr� visit_arg�s  z_Unparser.visit_argc Cs�d}|j|j}dgt|�t|j�|j}tt||�d�D]^\}}|\}}|rXd}n |�d�|�|�|r�|�d�|�|�|t|j�kr>|�d�q>|js�|j r�|r�d}n |�d�|�d�|jr�|�|jj �|jj r�|�d�|�|jj �|j �rLt|j |j �D]8\}}|�d�|�|�|�r|�d�|�|��q|j �r�|�r`d}n |�d�|�d |j j �|j j �r�|�d�|�|j j �dS) NTrIFrL�=z, /rr4r�)� posonlyargsr7r:�defaults� enumerater>r�r�vararg� kwonlyargsr�r5� kw_defaults�kwarg) r�rr�Zall_argsr�r��elements�a�drrr�visit_arguments�sN                z_Unparser.visit_argumentscCs<|jdur|�d�n|�|j�|�d�|�|j�dS)Nr�r�)r�r�rr#rrrr� visit_keyword s     z_Unparser.visit_keywordcCsn|�tj|��L|�d�|�|j�|�d�|�tj|j�|�|j�Wd�n1s`0YdS)Nzlambda r4)rr�r�r�rr7rrFrrrr� visit_Lambdas    z_Unparser.visit_LambdacCs&|�|j�|jr"|�d|j�dS�NrY)r�ra�asnamerrrr� visit_aliass z_Unparser.visit_aliascCs,|�|j�|jr(|�d�|�|j�dSr�)r� context_expr� optional_varsr�rrrr�visit_withitems  z_Unparser.visit_withitem)rK)wr[r�r�r�r�r�r�rrr�r�propertyrrr r rrrrrrrr�rr"r&r'r)r+r.r1r3r6r8r;r=r?r@rCrErFrHrJrKrOrWrZrarerfrdrirjrhrlrmrorpr�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r2r�r�r�r�r�r�r�r�� frozensetr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�� __classcell__rrrrr��s              � &   � ���    3r�cCst�}|�|�Sr )r�r�)Zast_objr�rrr�unparse$srcCs�ddl}|jdd�}|jd|jdd�ddd d �|jd d d ddd�|jddddd�|jddddd�|jddtddd�|��}|j�}|��}Wd�n1s�0Yt||jj |j |j d �}t t ||j|jd!��dS)"Nrz python -m ast)�prog�infile�rbr�?r�z$the file to parse; defaults to stdin)r"�nargs�default�helpz-mz--moder)rZsinglerZ func_typez(specify what kind of code must be parsed)r�choicesr z--no-type-commentsT� store_falsez)don't add information about type comments)r�actionr z-az--include-attributes� store_truez:include attributes such as line numbers and column offsets)r r z-iz--indentr z'indentation of nodes (number of spaces))r"rr )r )rerH)�argparse�ArgumentParser� add_argument�FileTyper$� parse_argsr�readrrarZno_type_comments�printrhrerH)r�parserr7rrZtreerrr�main)s0 � � � � �&r�__main__)rr)TF)rI)T)Kr��sysZ_ast� contextlibrr�enumrrrrGrhrrrwrzr|rsr�r�r�r�rx�objectr�r�rmr!r�r�r�ryr�r"r�r�r�r�r�r�r�r$r%r&rE�bytes�boolr�r�r�rTr�r�r�r.r�r�r��modr�Z expr_contextr�r�r�r]� float_info� max_10_expr�r�Z_SINGLE_QUOTESrxr�r�rrr[rrrr�s�� 8C#    %:<       ���