add-determinism 0.7.3

RPM buildroot helper to strip nondeterministic bits in files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
a

Z��^���@s�dZddlZddlTddlmZmZddlmZmZd\ddd	�d
d�Z	dd
�Z
d]dd�dd�Zdd�Zdd�Z
d^dd�Zdd�Zdd�Zd_dd�Zdd �Zd!d"�Zdd#�d$d%�Zd&d'�ZGd(d)�d)e�ZGd*d+�d+e�Zd,d-�Zd.d/�Zeee�e_eee�e_Gd0d1�d1e�Z d2d3�Z!Gd4d5�d5ee d6�Z"Gd7d8�d8ee d6�Z#Gd9d:�d:ee d6�Z$Gd;d<�d<ee d6�Z%Gd=d>�d>ee d6�Z&e"e'e(e)fe#e*fe$e+fe%ed�e,fe&ed?�fiZ-e"e,fiZ.e,d<ed�d<e'd5e(d5e)d5e*d8e+d:ed?�d>iZ/Gd@dA�dAe0�Z1GdBdC�dCe1�Z2GdDdE�dEe1�Z3dFdG�Z4dHdI�Z5ee4e5�e6_7GdJdK�dKe8�Z9GdLdM�dMe:�Z;GdNdO�dOe:�Z<GdPdQ�dQe:�Z=dRe>ej?j@d�ZAGdSdT�dTe�ZBGdUdV�dVe�ZCdWdX�ZDdYdZ�ZEeFd[k�r�eE�dS)`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.
�N)�*)�contextmanager�nullcontext)�IntEnum�auto�	<unknown>�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)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_nodeAsz+literal_eval.<locals>._raise_malformed_nodecs,t|t�rt|j�tttfvr&�|�|jS�N)r�Constant�type�value�int�float�complexr)rrr�_convert_numCsz"literal_eval.<locals>._convert_numcsDt|t�r<t|jttf�r<�|j�}t|jt�r6|
S|S�|�Sr )rZUnaryOp�op�UAdd�USub�operand)rr+)r'rr�_convert_signed_numGs
z)literal_eval.<locals>._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,rrrrDOs<




���


�

zliteral_eval.<locals>._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<t||d�du�r<q�|	��\}	}
|�oR|
}|�d||	f�q�|�r�t|�d	k�r�d
|j	j
d�|�f|fSd|j	j
||�|�fdfSt|t��r�|�s�d
Sd||���fdd�|D��fdfSt
|�dfS)N��
z,
��, T.z%s=%srz%s(%s)z%s(%s%s)F)z[]Tz[%s%s]c3s|]}�|��dVqdS)rNr)�.0�x)�_format�levelrr�	<genexpr>��z(dump.<locals>._format.<locals>.<genexpr>)r�ASTr"�_fields�getattr�AttributeError�append�_attributesr:�	__class__�__name__�joinr2�repr)rrP�prefix�sep�clsr7Z	allsimpler8�namer#�simple�rO�annotate_fields�include_attributesrH)rPrrOxsX




&zdump.<locals>._formatzexpected AST, got %rN� )r)rrS�	TypeErrorrYrZrE)rrcrdrHrrbr�dumpls0
rgcCsBdD]8}||jvr||jvrt||d�}|durt|||�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_offsetN)rXrU�setattr)�new_nodeZold_node�attrr#rrr�
copy_location�srocs �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)Nrhrjrirk)rX�hasattrrhrUrjrirk�iter_child_nodes)rrhrirjrk�child��_fixrrrt�s$





z#fix_missing_locations.<locals>._fixrIrrrrrsr�fix_missing_locations�srurIcCsJt|�D]<}d|jvr(t|dd�||_d|jvrt|dd�||_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.
    rhrrj)�walkrXrUrhrj)r�nrrrrr�increment_lineno�s

rxc	cs8|jD],}z|t||�fVWqty0Yq0qdS)zs
    Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
    that is present on *node*.
    N)rTrUrV)r�fieldrrr�iter_fields�s

rzccsLt|�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)rzrrSr2)rr`ry�itemrrrrq�s


rqcCs�t|ttttf�s"td|jj��|jr8t|jdt	�s<dS|jdj
}t|t�rZ|j}n"t|t
�rxt|j
t�rx|j
}ndS|r�ddl}|�|�}|S)aC
    Return the docstring for the given node or None if no docstring can
    be found.  If the node provided does not have docstrings a TypeError
    will be raised.

    If *clean* is `True`, all tabs are expanded to spaces and any whitespace
    that can be uniformly removed from the second line onwards is removed.
    z%r can't have docstringsrN)r�AsyncFunctionDef�FunctionDef�ClassDef�ModulerfrYrZrF�Exprr#�Str�sr!rE�inspectZcleandoc)rZclean�textr�rrr�
get_docstrings	

r�cCs�d}g}d}|t|�krx||}||7}|d7}|dkr`|t|�kr`||dkr`|d7}|d7}|dvr|�|�d}q|r�|�|�|S)z}Split a string into lines ignoring form feed and other chars.

    This mimics how the Python parser splits source code.
    rrKrI�
rJz
)r:rW)r�idx�linesZ	next_line�crrr�_splitlines_no_ff!s  

r�cCs,d}|D]}|dvr||7}q|d7}q|S)z6Replace all chars except '\f\t' in a line with spaces.rKz	rer)r�resultr�rrr�_pad_whitespace:s

r�)�paddedcCsz>|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)rjrkrhrirVr��encode�decoder��insertrWr[)rrr�rhrjrirkr�Zpadding�firstZlastrrr�get_source_segmentEs*	



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�extendrq)rr�Ztodorrrrvjs
rvc@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_)rYrZrU�
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)rzrr2rSr�)r�rryr#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"�itemsrrUrV�warnings�warn�DeprecationWarningr�)	r�rr#�	type_namer_r`r�r�r�rrr�visit_Constant�s&
�zNodeVisitor.visit_ConstantN)rZ�
__module__�__qualname__�__doc__r�r�r�rrrrr�xs
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 )	rzrr2rSr�r�rW�delattrrl)r�rry�	old_valueZ
new_valuesr#rmrrrr��s&






zNodeTransformer.generic_visitN)rZr�r�r�r�rrrrr��s#r�cCs|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#rV�_const_types_notr�r"�__instancecheck__)r_�instr#rrrr��s

�z_ABC.__instancecheck__N)rZr�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 )rT�indexr:rfrZr�r!�__new__)r_r7�kwargs�key�posrrr�_news
r�c@seZdZdZeZdS)�Num)rwN�rZr�r�rTr�r�rrrrr�sr�)�	metaclassc@seZdZdZeZdS)r��r�Nr�rrrrr�sr�c@seZdZdZeZdS)�Bytesr�Nr�rrrrr�"sr�c@seZdZeZdS)�NameConstantN)rZr�r�r�r�rrrrr�&sr�c@seZdZdZdd�ZdS)�EllipsisrcOs6|turtdg|�Ri|��Stj|g|�Ri|��S)N.)r�r!r�)r_r7r�rrrr�,szEllipsis.__new__N)rZr�r�rTr�rrrrr�)sr�.c@seZdZdZdS)�slicezDeprecated AST node class.N�rZr�r�r�rrrrr�Gsr�c@seZdZdZdd�ZdS)�Indexz@Deprecated AST node class. Use the index value directly instead.cKs|Sr r)r_r#r�rrrr�Lsz
Index.__new__N�rZr�r�r�r�rrrrr�Jsr�c@seZdZdZddd�ZdS)�ExtSlicez1Deprecated AST node class. Use ast.Tuple instead.rcKstt|�t�fi|��Sr )r.r2ZLoad)r_�dimsr�rrrr�QszExtSlice.__new__N)rr�rrrrr�Osr�cCs|jS)zDeprecated. Use elts instead.�r0r�rrr�_dims_getterTsr�cCs
||_dSr r�r�rrr�_dims_setterXsr�c@seZdZdZdS)�Suite�/Deprecated AST node class.  Unused in Python 3.Nr�rrrrr�]sr�c@seZdZdZdS)�AugLoadr�Nr�rrrrr�`sr�c@seZdZdZdS)�AugStorer�Nr�rrrrr�csr�c@seZdZdZdS)�Paramr�Nr�rrrrr�fsr�Z1ec@s�eZdZdZe�Ze�Ze�Ze�Ze�Z	e�Z
e�Ze�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)rYrr�rrr�next�sz_Precedence.nextN)rZr�r�r�r�TUPLE�YIELD�TEST�OR�AND�NOT�CMP�EXPR�BOR�BXOR�BAND�SHIFT�ARITH�TERM�FACTOR�POWER�AWAIT�ATOMr�rrrrr�ns(r�cs�eZdZdZdd�Zdd�Zdd�Zdd	�Zd�dd�Zd
d�Z	dd�Z
edd��Ze
dd�dd��Ze
dd��Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Z�fd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Z d;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�Z0d[d\�Z1d]d^�Z2d_d`�Z3dadb�Z4dcdd�Z5dedf�Z6dgdh�Z7didj�Z8dkdl�Z9dmdn�Z:dodp�Z;dqdr�Z<dsdt�Z=dudv�Z>dwdx�Z?dydz�Z@d{d|�ZAd}d~�ZBdd��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�d��ZHd�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d�d�d�d��ZNeOjPeOjQeOjQeOjQd��ZRd�d��ZSd�d�d�d�d�d�d�d�d�d�d�d�d�d��
ZTeOjUeOjUeOjVeOjVeOjVeOjVeOjWeOjWeOjXeOjYeOjZeOjVeOj[d��
Z\e]d��Z^d�d��Z_d�d�d�d�d�d�d�d�d�d�d��
Z`d�d��Zad�d�d��ZbeOjceOjdd��Zed�d��Zfd�d��Zgd�dÄZhd�dńZid�dDŽZjd�dɄZkd�d˄Zld�d̈́Zmd�dτZnd�dфZod�dӄZpd�dՄZqd�dׄZr�ZsS)��	_Unparserz�Methods in this class recursively traverse an AST and
    output source code for the abstract syntax; original formatting
    is disregarded.cCs"g|_g|_i|_i|_d|_dS)Nr)�_source�_buffer�_precedences�
_type_ignores�_indentr�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�seqrNrrr�
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�<lambda>�rRz&_Unparser.items_view.<locals>.<lambda>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)r�r�r��r�r�rrr�fill�sz_Unparser.fillcCs|j�|�dS)zAppend a piece of textN)r�rWr�rrrr��sz_Unparser.writecCs|j�|�dSr )r�rWr�rrr�
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 )rr)r�rr�	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<dS|j}t|t	�r\t|jt
�r\|SdS)z�If a docstring node is found in the body of the *node* parameter,
        return that docstring node, None otherwise.

        Logic mirrored from ``_PyAST_GetDocString``.rINr)rr|r}r~rr:rFr�r#r!rEr
rrr�get_raw_docstring�s��

z_Unparser.get_raw_docstringcCs*|j�|j�p|j}|dur&d|��SdS)Nz	 # type: )r�r�rh�type_comment)r�rZcommentrrr�get_type_comment�sz_Unparser.get_type_commentcs0t|t�r |D]}|�|�qnt��|�dSr )rr2�traverse�superr�)r�rr{�rYrrrs
z_Unparser.traversecCsg|_|�|�d�|j�S)z�Outputs a source code string that, if converted back to an ast
        (using ast.parse) will generate an AST equivalent to *node*rK)r�rr[r
rrrr�	s
z_Unparser.visitcCs>|�|�}r.|�|�|�|jdd��n|�|j�dSr�)r�_write_docstringrrF)r�rZ	docstringrrr�"_write_docstring_and_traverse_bodys
z,_Unparser._write_docstring_and_traverse_bodycCs*dd�|jD�|_|�|�|j��dS)NcSsi|]}|jd|j���qS)�ignore)rh�tag)rMrrrr�
<dictcomp>s�z*_Unparser.visit_Module.<locals>.<dictcomp>)�type_ignoresr�rr�r
rrr�visit_Modules
�
z_Unparser.visit_Modulecs`��dd��*���fdd��j|j�Wd�n1s<0Y��d���|j�dS)Nrr	cs
��d�Sr�r�rr�rrr�"rRz._Unparser.visit_FunctionType.<locals>.<lambda>� -> )rr�r�argtypesr��returnsr
rr�r�visit_FunctionTypes�"
z_Unparser.visit_FunctionTypecCs(|��|�tj|j�|�|j�dSr )r�rr�r�r#rr
rrr�
visit_Expr(sz_Unparser.visit_ExprcCsh|�tj|��F|�tj|j|j�|�|j�|�d�|�|j�Wd�n1sZ0YdS)Nz := )	rr�r�rr��targetr#rr�r
rrr�visit_NamedExpr-s

z_Unparser.visit_NamedExprcs(��d����fdd��j|j�dS)Nzimport cs
��d�Sr�r�rr�rrr�6rRz(_Unparser.visit_Import.<locals>.<lambda>)r�r�r�namesr
rr�r�visit_Import4s
z_Unparser.visit_ImportcsT��d���d|j�|jr,��|j���d����fdd��j|j�dS)Nzfrom �.z import cs
��d�Sr�r�rr�rrr�>rRz,_Unparser.visit_ImportFrom.<locals>.<lambda>)r�r�rP�moduler�rr#r
rr�r�visit_ImportFrom8s

z_Unparser.visit_ImportFromcCsP|��|jD]}|�|�|�d�q|�|j�|�|�}rL|�|�dS)N� = )r��targetsrr�r#r)r�rr!rrrr�visit_Assign@s

z_Unparser.visit_AssigncCsB|��|�|j�|�d|j|jjjd�|�|j�dS)Nrez= )	r�rr!r��binopr(rYrZr#r
rrr�visit_AugAssignIsz_Unparser.visit_AugAssigncCs�|��|�dd|jo"t|jt���|�|j�Wd�n1sH0Y|�d�|�|j�|j	r�|�d�|�|j	�dS)Nrr	�: r()
r�rrarr!r5rr��
annotationr#r
rrr�visit_AnnAssignOs *

z_Unparser.visit_AnnAssigncCs*|�d�|jr&|�d�|�|j�dS)N�returnre)r�r#r�rr
rrr�visit_ReturnYs

z_Unparser.visit_ReturncCs|�d�dS)N�pass�r�r
rrr�
visit_Pass_sz_Unparser.visit_PasscCs|�d�dS)N�breakr3r
rrr�visit_Breakbsz_Unparser.visit_BreakcCs|�d�dS)N�continuer3r
rrr�visit_Continueesz_Unparser.visit_Continuecs(��d����fdd��j|j�dS)Nzdel cs
��d�Sr�r�rr�rrr�jrRz(_Unparser.visit_Delete.<locals>.<lambda>)r�r�rr)r
rr�r�visit_Deletehs
z_Unparser.visit_DeletecCs6|�d�|�|j�|jr2|�d�|�|j�dS)Nzassert rL)r�r�test�msgr�r
rrr�visit_Assertls


z_Unparser.visit_Assertcs(��d����fdd��j|j�dS)Nzglobal cs
��d�Sr�r�rr�rrr�urRz(_Unparser.visit_Global.<locals>.<lambda>�r�r�r�r#r
rr�r�visit_Globalss
z_Unparser.visit_Globalcs(��d����fdd��j|j�dS)Nz	nonlocal cs
��d�Sr�r�rr�rrr�yrRz*_Unparser.visit_Nonlocal.<locals>.<lambda>r=r
rr�r�visit_Nonlocalws
z_Unparser.visit_NonlocalcCsh|�tj|��F|�d�|jrF|�d�|�tj|j�|�|j�Wd�n1sZ0YdS)N�awaitre)rr�r�r�r#rr�rr
rrr�visit_Await{s

z_Unparser.visit_AwaitcCsh|�tj|��F|�d�|jrF|�d�|�tj|j�|�|j�Wd�n1sZ0YdS)N�yieldre)rr�r�r�r#rr�rr
rrr�visit_Yield�s

z_Unparser.visit_YieldcCsf|�tj|��D|�d�|js(td��|�tj|j�|�|j�Wd�n1sX0YdS)Nzyield from z-Node can't be used without a value attribute.)	rr�r�r�r#rrr�rr
rrr�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.rez from )r��exc�causerr�rr
rrr�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)r�rrrF�handlers�orelse�	finalbody)r�r�exrrr�	visit_Try�s

*


*

z_Unparser.visit_TrycCsz|�d�|jr&|�d�|�|j�|jrB|�d�|�|j�|���|�|j�Wd�n1sl0YdS)N�exceptre� as )r�r"r�rr`rrFr
rrr�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	)rFrLT)r��decorator_listr�rr`r�basesr8r�rr)r�r�deco�comma�errr�visit_ClassDef�s&



*
z_Unparser.visit_ClassDefcCs|�|d�dS)N�def��_function_helperr
rrr�visit_FunctionDef�sz_Unparser.visit_FunctionDefcCs|�|d�dS)Nz	async defr\r
rrr�visit_AsyncFunctionDef�sz _Unparser.visit_AsyncFunctionDefcCs�|��|jD]}|�d�|�|�q|d|j}|�|�|�dd��|�|j�Wd�n1sn0Y|jr�|�d�|�|j�|j	|�
|�d��|�|�Wd�n1s�0YdS)NrTrerr	rr�)r�rUr�rr`rr7rr�rrr)r�rZfill_suffixrWZdef_strrrrr]�s


*
z_Unparser._function_helpercCs|�d|�dS)Nzfor ��_for_helperr
rrr�	visit_For�sz_Unparser.visit_ForcCs|�d|�dS)Nz
async for r`r
rrr�visit_AsyncFor�sz_Unparser.visit_AsyncForcCs�|�|�|�|j�|�d�|�|j�|j|�|�d��|�|j�Wd�n1s`0Y|jr�|�d�|���|�|j�Wd�n1s�0YdS)N� in r�rJ)	r�rr!r�r�rrrFrM)r�r�rrrrra�s

*

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 rJ)	r�rr:rrFrMr:rZIfr
rrr�visit_If�s

*$


,

z_Unparser.visit_IfcCs�|�d�|�|j�|���|�|j�Wd�n1s@0Y|jr�|�d�|���|�|j�Wd�n1s�0YdS)Nzwhile rJ)r�rr:rrFrMr
rrr�visit_While
s

*

z_Unparser.visit_Whilecsf��d����fdd��j|j��j��|�d����|j�Wd�n1sX0YdS)Nzwith cs
��d�Sr�r�rr�rrr�rRz&_Unparser.visit_With.<locals>.<lambda>r��r�r�rr�rrrFr
rr�r�
visit_Withs
z_Unparser.visit_Withcsf��d����fdd��j|j��j��|�d����|j�Wd�n1sX0YdS)Nzasync with cs
��d�Sr�r�rr�rrr�rRz+_Unparser.visit_AsyncWith.<locals>.<lambda>r�rgr
rr�r�visit_AsyncWiths
z_Unparser.visit_AsyncWithcCs,|�d�|�||j�|�t|j��dS�Nr�)r��_fstring_JoinedStrr�r\r�r
rrr�visit_JoinedStr s
z_Unparser.visit_JoinedStrcCs,|�d�|�||j�|�t|j��dSrj)r��_fstring_FormattedValuer�r\r�r
rrr�visit_FormattedValue%s
z_Unparser.visit_FormattedValuecCs.|jD]"}t|dt|�j�}|||�qdS)N�	_fstring_)r<rUr"rZ)r�rr�r#�methrrrrk*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|��}|�tj��|j�|�|j�}|�d�rD|d�||�|jdkr~t	|j�}|dvrpt
d��|d|���|jr�|d�t|dt|j�j
�}||j|�|d	�dS)
NrqrerZsrazUnknown f-string conversion.�!rrorr)r"rr�r�r�r#r��
startswith�
conversion�chrr�format_specrUrZ)r�rr��unparser�exprrwrprrrrm5s"



z!_Unparser._fstring_FormattedValuecCs|�|j�dSr )r�r6r
rrr�
visit_NameHsz_Unparser.visit_NamecCszdd�}|��|jdkr$|�d�|j}|rdd�t||��}|ddkrX|�ddd�}|�dd	�}|�d|�d��dS)
NcSs|dvr|S|�d��d�S)N)rJ�	Zunicode_escape�ascii)r�r�)r�rrr�esc_charLsz,_Unparser._write_docstring.<locals>.esc_char�urKr�"z\"z"""z""\")r��kindr�r#r[r/rs)r�rrr#rrrrKs	

z_Unparser._write_docstringcCs8t|ttf�r&|�t|��dt��n|�t|��dS)N�inf)rr%r&r�r\rs�_INFSTRr�rrr�_write_constantcsz_Unparser._write_constantcCs�|j}t|t�rL|�dd��|�|j|�Wd�q�1s@0Yn4|dur`|�d�n |jdkrt|�d�|�|j�dS)Nrr	.�...r�)r#rrrr�r�r�r�)r�rr#rrrr�js
.

z_Unparser.visit_ConstantcsJ��dd��*���fdd��j|j�Wd�n1s<0YdS)N�[�]cs
��d�Sr�r�rr�rrr�xrRz&_Unparser.visit_List.<locals>.<lambda>)rr�rr0r
rr�r�
visit_Listvsz_Unparser.visit_ListcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS)Nr�r��rr�elt�
generators�r�r�genrrr�visit_ListCompzs
z_Unparser.visit_ListCompcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS�Nrr	r�r�rrr�visit_GeneratorExp�s
z_Unparser.visit_GeneratorExpcCsR|�dd��2|�|j�|jD]}|�|�q Wd�n1sD0YdS)Nrqrrr�r�rrr�
visit_SetComp�s
z_Unparser.visit_SetCompcCsh|�dd��H|�|j�|�d�|�|j�|jD]}|�|�q6Wd�n1sZ0YdS)Nrqrrr-)rrr�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 rd� 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 )
rr�r�rr�rFr:rr�rMr
rrr�visit_IfExp�s

z_Unparser.visit_IfExpcsX|jstd����dd��*���fdd��j|j�Wd�n1sJ0YdS)Nz&Set node should have at least one itemrqrrcs
��d�Sr�r�rr�rrr��rRz%_Unparser.visit_Set.<locals>.<lambda>)r0rrr�rr
rr�r�	visit_Set�sz_Unparser.visit_Setcsj�fdd����fdd�}��dd��0���fdd�|t|j|j��Wd�n1s\0YdS)	Ncs"��|���d���|�dS�Nr-)rr�)�k�vr�rr�write_key_value_pair�s

z2_Unparser.visit_Dict.<locals>.write_key_value_paircsB|\}}|dur4��d���tj|���|�n
�||�dS)N�**)r�rr�r�r)r{r�r��r�r�rr�
write_item�s
z(_Unparser.visit_Dict.<locals>.write_itemrqrrcs
��d�Sr�r�rr�rrr��rRz&_Unparser.visit_Dict.<locals>.<lambda>)rr�r>r;r<)r�rr�rr�r�
visit_Dict�s�z_Unparser.visit_DictcCs@|�dd�� |�|j|j�Wd�n1s20YdSr�)rr�rr0r
rrr�visit_Tuple�sz_Unparser.visit_Tuple�~�not�+�-)ZInvertZNotr)r*)r�r�r�r�cCs�|j|jjj}|j|}|�||��H|�|�|tjurF|�d�|�	||j
�|�|j
�Wd�n1st0YdS�Nre)�unopr(rYrZ�unop_precedencerr�r�r�rr+r)r�r�operator�operator_precedencerrr�
visit_UnaryOp�s



z_Unparser.visit_UnaryOprrT�/�%�<<�>>�|�^�&�//r�)
r?r@ZMultZMatMultZDivZModZLShiftZRShiftZBitOrZBitXorZBitAndZFloorDivZPow)
r�r�rrTr�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�)
r+r(rYrZ�binop_precedencer�binop_rassocr�rrArr�rB)r�rr�r�Zleft_precedenceZright_precedencerrr�visit_BinOps

z_Unparser.visit_BinOpz==z!=�<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�)rr�r�rr�rA�comparatorsrr>�opsr��cmpopsrYrZ)r�r�orYrrr�
visit_Compares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.<locals>.increasing_level_traverserecs
����Sr r�r)r�r�rrr�6rRz(_Unparser.visit_BoolOp.<locals>.<lambda>)�boolopsr(rYrZ�boolop_precedencerr�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)Nrer%)
rr�r�r#rrr!r$r�rnr
rrr�visit_Attribute8s

z_Unparser.visit_AttributecCs�|�tj|j�|�|j�|�dd��hd}|jD]"}|rH|�d�nd}|�|�q4|jD]"}|rr|�d�nd}|�|�q^Wd�n1s�0YdS)Nrr	FrLT)	rr�r�r4rrr7r�r8)r�rrXrYrrr�
visit_CallCs

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)rMr�rrrrQ]rRzE_Unparser.visit_Subscript.<locals>.is_simple_tuple.<locals>.<genexpr>)rr.r0�any)Zslice_valuerrr�is_simple_tupleVs

��z2_Unparser.visit_Subscript.<locals>.is_simple_tupler�r�)	rr�r�r#rrr�r�r0)r�rr�rrr�visit_SubscriptUs

z_Unparser.visit_SubscriptcCs*|�d�|�tj|j�|�|j�dS)Nr)r�rr�r�r#rr
rrr�
visit_Starredhs
z_Unparser.visit_StarredcCs|�d�dS)Nr�r�r
rrr�visit_Ellipsismsz_Unparser.visit_EllipsiscCsN|jr|�|j�|�d�|jr.|�|j�|jrJ|�d�|�|j�dS)Nr)�lowerrr��upper�stepr
rrr�visit_Sliceps

z_Unparser.visit_SlicecCs,|�|j�|jr(|�d�|�|j�dSr�)r��argr.rr
rrr�	visit_argzs
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
�|jjr�|�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, /rr-r�)�posonlyargsr7r:�defaults�	enumerater>r�r�vararg�
kwonlyargsr�r.�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#r
rrr�
visit_keyword�s


z_Unparser.visit_keywordcCsn|�tj|��L|�d�|�|j�|�d�|�tj|j�|�|j�Wd�n1s`0YdS)Nzlambda r-)rr�r�r�rr7rrFr
rrr�visit_Lambda�s

z_Unparser.visit_LambdacCs&|�|j�|jr"|�d|j�dS�NrR)r�r`�asnamer
rrr�visit_alias�sz_Unparser.visit_aliascCs,|�|j�|jr(|�d�|�|j�dSr�)r�context_expr�
optional_varsr�r
rrr�visit_withitem�s
z_Unparser.visit_withitem)rK)trZr�r�r�r�r�r�r�r�r�r��propertyr�rrrrrr
rrrrr�rrrr r"r$r'r*r,r/r1r4r6r8r9r<r>r?rArCrDrHrPrSrZr^r_r]rbrcrarerfrhrirlrnrkrtrmr|rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r+r�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_objrzrrr�unparse�sr�cCs�ddl}|jdd�}|jd|jdd�ddd	d
�|jddd
ddd�|jddddd�|jddddd�|jddtddd�|��}|j�}|��}Wd�n1s�0Yt||jj	|j
|jd �}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--indentrz'indentation of nodes (number of spaces))r"r�r�)r	)rdrH)�argparse�ArgumentParser�add_argument�FileTyper$�
parse_argsr��readrr`rZno_type_comments�printrgrdrH)r��parserr7r�rZtreerrr�main�s0�
�
�
��&r��__main__)rr)TF)rI)T)Gr��sysZ_ast�
contextlibrr�enumrrrrGrgrorurxrzrqr�r�r�r�rv�objectr�r�r�r�r�r!rwr�r"r�r�r�r�r�r�r�r$r%r&rE�bytes�boolr�r�r�rSr�r�r�r�r�r.r��modr�Zexpr_contextr�r�r�r\�
float_info�
max_10_expr�r�r�r�r�rZrrrr�<module>s��6C
#

%:>	

���I