Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../usr/lib64/python3....
File: ast.py
"""
[0] Fix | Delete
ast
[1] Fix | Delete
~~~
[2] Fix | Delete
[3] Fix | Delete
The `ast` module helps Python applications to process trees of the Python
[4] Fix | Delete
abstract syntax grammar. The abstract syntax itself might change with
[5] Fix | Delete
each Python release; this module helps to find out programmatically what
[6] Fix | Delete
the current grammar looks like and allows modifications of it.
[7] Fix | Delete
[8] Fix | Delete
An abstract syntax tree can be generated by passing `ast.PyCF_ONLY_AST` as
[9] Fix | Delete
a flag to the `compile()` builtin function or by using the `parse()`
[10] Fix | Delete
function from this module. The result will be a tree of objects whose
[11] Fix | Delete
classes all inherit from `ast.AST`.
[12] Fix | Delete
[13] Fix | Delete
A modified abstract syntax tree can be compiled into a Python code object
[14] Fix | Delete
using the built-in `compile()` function.
[15] Fix | Delete
[16] Fix | Delete
Additionally various helper functions are provided that make working with
[17] Fix | Delete
the trees simpler. The main intention of the helper functions and this
[18] Fix | Delete
module in general is to provide an easy to use interface for libraries
[19] Fix | Delete
that work tightly with the python syntax (template engines for example).
[20] Fix | Delete
[21] Fix | Delete
[22] Fix | Delete
:copyright: Copyright 2008 by Armin Ronacher.
[23] Fix | Delete
:license: Python License.
[24] Fix | Delete
"""
[25] Fix | Delete
from _ast import *
[26] Fix | Delete
[27] Fix | Delete
[28] Fix | Delete
def parse(source, filename='<unknown>', mode='exec', *,
[29] Fix | Delete
type_comments=False, feature_version=None):
[30] Fix | Delete
"""
[31] Fix | Delete
Parse the source into an AST node.
[32] Fix | Delete
Equivalent to compile(source, filename, mode, PyCF_ONLY_AST).
[33] Fix | Delete
Pass type_comments=True to get back type comments where the syntax allows.
[34] Fix | Delete
"""
[35] Fix | Delete
flags = PyCF_ONLY_AST
[36] Fix | Delete
if type_comments:
[37] Fix | Delete
flags |= PyCF_TYPE_COMMENTS
[38] Fix | Delete
if isinstance(feature_version, tuple):
[39] Fix | Delete
major, minor = feature_version # Should be a 2-tuple.
[40] Fix | Delete
assert major == 3
[41] Fix | Delete
feature_version = minor
[42] Fix | Delete
elif feature_version is None:
[43] Fix | Delete
feature_version = -1
[44] Fix | Delete
# Else it should be an int giving the minor version for 3.x.
[45] Fix | Delete
return compile(source, filename, mode, flags,
[46] Fix | Delete
_feature_version=feature_version)
[47] Fix | Delete
[48] Fix | Delete
[49] Fix | Delete
def literal_eval(node_or_string):
[50] Fix | Delete
"""
[51] Fix | Delete
Safely evaluate an expression node or a string containing a Python
[52] Fix | Delete
expression. The string or node provided may only consist of the following
[53] Fix | Delete
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
[54] Fix | Delete
sets, booleans, and None.
[55] Fix | Delete
"""
[56] Fix | Delete
if isinstance(node_or_string, str):
[57] Fix | Delete
node_or_string = parse(node_or_string, mode='eval')
[58] Fix | Delete
if isinstance(node_or_string, Expression):
[59] Fix | Delete
node_or_string = node_or_string.body
[60] Fix | Delete
def _raise_malformed_node(node):
[61] Fix | Delete
raise ValueError(f'malformed node or string: {node!r}')
[62] Fix | Delete
def _convert_num(node):
[63] Fix | Delete
if not isinstance(node, Constant) or type(node.value) not in (int, float, complex):
[64] Fix | Delete
_raise_malformed_node(node)
[65] Fix | Delete
return node.value
[66] Fix | Delete
def _convert_signed_num(node):
[67] Fix | Delete
if isinstance(node, UnaryOp) and isinstance(node.op, (UAdd, USub)):
[68] Fix | Delete
operand = _convert_num(node.operand)
[69] Fix | Delete
if isinstance(node.op, UAdd):
[70] Fix | Delete
return + operand
[71] Fix | Delete
else:
[72] Fix | Delete
return - operand
[73] Fix | Delete
return _convert_num(node)
[74] Fix | Delete
def _convert(node):
[75] Fix | Delete
if isinstance(node, Constant):
[76] Fix | Delete
return node.value
[77] Fix | Delete
elif isinstance(node, Tuple):
[78] Fix | Delete
return tuple(map(_convert, node.elts))
[79] Fix | Delete
elif isinstance(node, List):
[80] Fix | Delete
return list(map(_convert, node.elts))
[81] Fix | Delete
elif isinstance(node, Set):
[82] Fix | Delete
return set(map(_convert, node.elts))
[83] Fix | Delete
elif isinstance(node, Dict):
[84] Fix | Delete
if len(node.keys) != len(node.values):
[85] Fix | Delete
_raise_malformed_node(node)
[86] Fix | Delete
return dict(zip(map(_convert, node.keys),
[87] Fix | Delete
map(_convert, node.values)))
[88] Fix | Delete
elif isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)):
[89] Fix | Delete
left = _convert_signed_num(node.left)
[90] Fix | Delete
right = _convert_num(node.right)
[91] Fix | Delete
if isinstance(left, (int, float)) and isinstance(right, complex):
[92] Fix | Delete
if isinstance(node.op, Add):
[93] Fix | Delete
return left + right
[94] Fix | Delete
else:
[95] Fix | Delete
return left - right
[96] Fix | Delete
return _convert_signed_num(node)
[97] Fix | Delete
return _convert(node_or_string)
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
def dump(node, annotate_fields=True, include_attributes=False):
[101] Fix | Delete
"""
[102] Fix | Delete
Return a formatted dump of the tree in node. This is mainly useful for
[103] Fix | Delete
debugging purposes. If annotate_fields is true (by default),
[104] Fix | Delete
the returned string will show the names and the values for fields.
[105] Fix | Delete
If annotate_fields is false, the result string will be more compact by
[106] Fix | Delete
omitting unambiguous field names. Attributes such as line
[107] Fix | Delete
numbers and column offsets are not dumped by default. If this is wanted,
[108] Fix | Delete
include_attributes can be set to true.
[109] Fix | Delete
"""
[110] Fix | Delete
def _format(node):
[111] Fix | Delete
if isinstance(node, AST):
[112] Fix | Delete
args = []
[113] Fix | Delete
keywords = annotate_fields
[114] Fix | Delete
for field in node._fields:
[115] Fix | Delete
try:
[116] Fix | Delete
value = getattr(node, field)
[117] Fix | Delete
except AttributeError:
[118] Fix | Delete
keywords = True
[119] Fix | Delete
else:
[120] Fix | Delete
if keywords:
[121] Fix | Delete
args.append('%s=%s' % (field, _format(value)))
[122] Fix | Delete
else:
[123] Fix | Delete
args.append(_format(value))
[124] Fix | Delete
if include_attributes and node._attributes:
[125] Fix | Delete
for a in node._attributes:
[126] Fix | Delete
try:
[127] Fix | Delete
args.append('%s=%s' % (a, _format(getattr(node, a))))
[128] Fix | Delete
except AttributeError:
[129] Fix | Delete
pass
[130] Fix | Delete
return '%s(%s)' % (node.__class__.__name__, ', '.join(args))
[131] Fix | Delete
elif isinstance(node, list):
[132] Fix | Delete
return '[%s]' % ', '.join(_format(x) for x in node)
[133] Fix | Delete
return repr(node)
[134] Fix | Delete
if not isinstance(node, AST):
[135] Fix | Delete
raise TypeError('expected AST, got %r' % node.__class__.__name__)
[136] Fix | Delete
return _format(node)
[137] Fix | Delete
[138] Fix | Delete
[139] Fix | Delete
def copy_location(new_node, old_node):
[140] Fix | Delete
"""
[141] Fix | Delete
Copy source location (`lineno`, `col_offset`, `end_lineno`, and `end_col_offset`
[142] Fix | Delete
attributes) from *old_node* to *new_node* if possible, and return *new_node*.
[143] Fix | Delete
"""
[144] Fix | Delete
for attr in 'lineno', 'col_offset', 'end_lineno', 'end_col_offset':
[145] Fix | Delete
if attr in old_node._attributes and attr in new_node._attributes:
[146] Fix | Delete
value = getattr(old_node, attr, None)
[147] Fix | Delete
# end_lineno and end_col_offset are optional attributes, and they
[148] Fix | Delete
# should be copied whether the value is None or not.
[149] Fix | Delete
if value is not None or (
[150] Fix | Delete
hasattr(old_node, attr) and attr.startswith("end_")
[151] Fix | Delete
):
[152] Fix | Delete
setattr(new_node, attr, value)
[153] Fix | Delete
return new_node
[154] Fix | Delete
[155] Fix | Delete
[156] Fix | Delete
def fix_missing_locations(node):
[157] Fix | Delete
"""
[158] Fix | Delete
When you compile a node tree with compile(), the compiler expects lineno and
[159] Fix | Delete
col_offset attributes for every node that supports them. This is rather
[160] Fix | Delete
tedious to fill in for generated nodes, so this helper adds these attributes
[161] Fix | Delete
recursively where not already set, by setting them to the values of the
[162] Fix | Delete
parent node. It works recursively starting at *node*.
[163] Fix | Delete
"""
[164] Fix | Delete
def _fix(node, lineno, col_offset, end_lineno, end_col_offset):
[165] Fix | Delete
if 'lineno' in node._attributes:
[166] Fix | Delete
if not hasattr(node, 'lineno'):
[167] Fix | Delete
node.lineno = lineno
[168] Fix | Delete
else:
[169] Fix | Delete
lineno = node.lineno
[170] Fix | Delete
if 'end_lineno' in node._attributes:
[171] Fix | Delete
if not hasattr(node, 'end_lineno'):
[172] Fix | Delete
node.end_lineno = end_lineno
[173] Fix | Delete
else:
[174] Fix | Delete
end_lineno = node.end_lineno
[175] Fix | Delete
if 'col_offset' in node._attributes:
[176] Fix | Delete
if not hasattr(node, 'col_offset'):
[177] Fix | Delete
node.col_offset = col_offset
[178] Fix | Delete
else:
[179] Fix | Delete
col_offset = node.col_offset
[180] Fix | Delete
if 'end_col_offset' in node._attributes:
[181] Fix | Delete
if not hasattr(node, 'end_col_offset'):
[182] Fix | Delete
node.end_col_offset = end_col_offset
[183] Fix | Delete
else:
[184] Fix | Delete
end_col_offset = node.end_col_offset
[185] Fix | Delete
for child in iter_child_nodes(node):
[186] Fix | Delete
_fix(child, lineno, col_offset, end_lineno, end_col_offset)
[187] Fix | Delete
_fix(node, 1, 0, 1, 0)
[188] Fix | Delete
return node
[189] Fix | Delete
[190] Fix | Delete
[191] Fix | Delete
def increment_lineno(node, n=1):
[192] Fix | Delete
"""
[193] Fix | Delete
Increment the line number and end line number of each node in the tree
[194] Fix | Delete
starting at *node* by *n*. This is useful to "move code" to a different
[195] Fix | Delete
location in a file.
[196] Fix | Delete
"""
[197] Fix | Delete
for child in walk(node):
[198] Fix | Delete
if 'lineno' in child._attributes:
[199] Fix | Delete
child.lineno = getattr(child, 'lineno', 0) + n
[200] Fix | Delete
if (
[201] Fix | Delete
"end_lineno" in child._attributes
[202] Fix | Delete
and (end_lineno := getattr(child, "end_lineno", 0)) is not None
[203] Fix | Delete
):
[204] Fix | Delete
child.end_lineno = end_lineno + n
[205] Fix | Delete
return node
[206] Fix | Delete
[207] Fix | Delete
[208] Fix | Delete
def iter_fields(node):
[209] Fix | Delete
"""
[210] Fix | Delete
Yield a tuple of ``(fieldname, value)`` for each field in ``node._fields``
[211] Fix | Delete
that is present on *node*.
[212] Fix | Delete
"""
[213] Fix | Delete
for field in node._fields:
[214] Fix | Delete
try:
[215] Fix | Delete
yield field, getattr(node, field)
[216] Fix | Delete
except AttributeError:
[217] Fix | Delete
pass
[218] Fix | Delete
[219] Fix | Delete
[220] Fix | Delete
def iter_child_nodes(node):
[221] Fix | Delete
"""
[222] Fix | Delete
Yield all direct child nodes of *node*, that is, all fields that are nodes
[223] Fix | Delete
and all items of fields that are lists of nodes.
[224] Fix | Delete
"""
[225] Fix | Delete
for name, field in iter_fields(node):
[226] Fix | Delete
if isinstance(field, AST):
[227] Fix | Delete
yield field
[228] Fix | Delete
elif isinstance(field, list):
[229] Fix | Delete
for item in field:
[230] Fix | Delete
if isinstance(item, AST):
[231] Fix | Delete
yield item
[232] Fix | Delete
[233] Fix | Delete
[234] Fix | Delete
def get_docstring(node, clean=True):
[235] Fix | Delete
"""
[236] Fix | Delete
Return the docstring for the given node or None if no docstring can
[237] Fix | Delete
be found. If the node provided does not have docstrings a TypeError
[238] Fix | Delete
will be raised.
[239] Fix | Delete
[240] Fix | Delete
If *clean* is `True`, all tabs are expanded to spaces and any whitespace
[241] Fix | Delete
that can be uniformly removed from the second line onwards is removed.
[242] Fix | Delete
"""
[243] Fix | Delete
if not isinstance(node, (AsyncFunctionDef, FunctionDef, ClassDef, Module)):
[244] Fix | Delete
raise TypeError("%r can't have docstrings" % node.__class__.__name__)
[245] Fix | Delete
if not(node.body and isinstance(node.body[0], Expr)):
[246] Fix | Delete
return None
[247] Fix | Delete
node = node.body[0].value
[248] Fix | Delete
if isinstance(node, Str):
[249] Fix | Delete
text = node.s
[250] Fix | Delete
elif isinstance(node, Constant) and isinstance(node.value, str):
[251] Fix | Delete
text = node.value
[252] Fix | Delete
else:
[253] Fix | Delete
return None
[254] Fix | Delete
if clean:
[255] Fix | Delete
import inspect
[256] Fix | Delete
text = inspect.cleandoc(text)
[257] Fix | Delete
return text
[258] Fix | Delete
[259] Fix | Delete
[260] Fix | Delete
def _splitlines_no_ff(source):
[261] Fix | Delete
"""Split a string into lines ignoring form feed and other chars.
[262] Fix | Delete
[263] Fix | Delete
This mimics how the Python parser splits source code.
[264] Fix | Delete
"""
[265] Fix | Delete
idx = 0
[266] Fix | Delete
lines = []
[267] Fix | Delete
next_line = ''
[268] Fix | Delete
while idx < len(source):
[269] Fix | Delete
c = source[idx]
[270] Fix | Delete
next_line += c
[271] Fix | Delete
idx += 1
[272] Fix | Delete
# Keep \r\n together
[273] Fix | Delete
if c == '\r' and idx < len(source) and source[idx] == '\n':
[274] Fix | Delete
next_line += '\n'
[275] Fix | Delete
idx += 1
[276] Fix | Delete
if c in '\r\n':
[277] Fix | Delete
lines.append(next_line)
[278] Fix | Delete
next_line = ''
[279] Fix | Delete
[280] Fix | Delete
if next_line:
[281] Fix | Delete
lines.append(next_line)
[282] Fix | Delete
return lines
[283] Fix | Delete
[284] Fix | Delete
[285] Fix | Delete
def _pad_whitespace(source):
[286] Fix | Delete
r"""Replace all chars except '\f\t' in a line with spaces."""
[287] Fix | Delete
result = ''
[288] Fix | Delete
for c in source:
[289] Fix | Delete
if c in '\f\t':
[290] Fix | Delete
result += c
[291] Fix | Delete
else:
[292] Fix | Delete
result += ' '
[293] Fix | Delete
return result
[294] Fix | Delete
[295] Fix | Delete
[296] Fix | Delete
def get_source_segment(source, node, *, padded=False):
[297] Fix | Delete
"""Get source code segment of the *source* that generated *node*.
[298] Fix | Delete
[299] Fix | Delete
If some location information (`lineno`, `end_lineno`, `col_offset`,
[300] Fix | Delete
or `end_col_offset`) is missing, return None.
[301] Fix | Delete
[302] Fix | Delete
If *padded* is `True`, the first line of a multi-line statement will
[303] Fix | Delete
be padded with spaces to match its original position.
[304] Fix | Delete
"""
[305] Fix | Delete
try:
[306] Fix | Delete
lineno = node.lineno - 1
[307] Fix | Delete
end_lineno = node.end_lineno - 1
[308] Fix | Delete
col_offset = node.col_offset
[309] Fix | Delete
end_col_offset = node.end_col_offset
[310] Fix | Delete
except AttributeError:
[311] Fix | Delete
return None
[312] Fix | Delete
[313] Fix | Delete
lines = _splitlines_no_ff(source)
[314] Fix | Delete
if end_lineno == lineno:
[315] Fix | Delete
return lines[lineno].encode()[col_offset:end_col_offset].decode()
[316] Fix | Delete
[317] Fix | Delete
if padded:
[318] Fix | Delete
padding = _pad_whitespace(lines[lineno].encode()[:col_offset].decode())
[319] Fix | Delete
else:
[320] Fix | Delete
padding = ''
[321] Fix | Delete
[322] Fix | Delete
first = padding + lines[lineno].encode()[col_offset:].decode()
[323] Fix | Delete
last = lines[end_lineno].encode()[:end_col_offset].decode()
[324] Fix | Delete
lines = lines[lineno+1:end_lineno]
[325] Fix | Delete
[326] Fix | Delete
lines.insert(0, first)
[327] Fix | Delete
lines.append(last)
[328] Fix | Delete
return ''.join(lines)
[329] Fix | Delete
[330] Fix | Delete
[331] Fix | Delete
def walk(node):
[332] Fix | Delete
"""
[333] Fix | Delete
Recursively yield all descendant nodes in the tree starting at *node*
[334] Fix | Delete
(including *node* itself), in no specified order. This is useful if you
[335] Fix | Delete
only want to modify nodes in place and don't care about the context.
[336] Fix | Delete
"""
[337] Fix | Delete
from collections import deque
[338] Fix | Delete
todo = deque([node])
[339] Fix | Delete
while todo:
[340] Fix | Delete
node = todo.popleft()
[341] Fix | Delete
todo.extend(iter_child_nodes(node))
[342] Fix | Delete
yield node
[343] Fix | Delete
[344] Fix | Delete
[345] Fix | Delete
class NodeVisitor(object):
[346] Fix | Delete
"""
[347] Fix | Delete
A node visitor base class that walks the abstract syntax tree and calls a
[348] Fix | Delete
visitor function for every node found. This function may return a value
[349] Fix | Delete
which is forwarded by the `visit` method.
[350] Fix | Delete
[351] Fix | Delete
This class is meant to be subclassed, with the subclass adding visitor
[352] Fix | Delete
methods.
[353] Fix | Delete
[354] Fix | Delete
Per default the visitor functions for the nodes are ``'visit_'`` +
[355] Fix | Delete
class name of the node. So a `TryFinally` node visit function would
[356] Fix | Delete
be `visit_TryFinally`. This behavior can be changed by overriding
[357] Fix | Delete
the `visit` method. If no visitor function exists for a node
[358] Fix | Delete
(return value `None`) the `generic_visit` visitor is used instead.
[359] Fix | Delete
[360] Fix | Delete
Don't use the `NodeVisitor` if you want to apply changes to nodes during
[361] Fix | Delete
traversing. For this a special visitor exists (`NodeTransformer`) that
[362] Fix | Delete
allows modifications.
[363] Fix | Delete
"""
[364] Fix | Delete
[365] Fix | Delete
def visit(self, node):
[366] Fix | Delete
"""Visit a node."""
[367] Fix | Delete
method = 'visit_' + node.__class__.__name__
[368] Fix | Delete
visitor = getattr(self, method, self.generic_visit)
[369] Fix | Delete
return visitor(node)
[370] Fix | Delete
[371] Fix | Delete
def generic_visit(self, node):
[372] Fix | Delete
"""Called if no explicit visitor function exists for a node."""
[373] Fix | Delete
for field, value in iter_fields(node):
[374] Fix | Delete
if isinstance(value, list):
[375] Fix | Delete
for item in value:
[376] Fix | Delete
if isinstance(item, AST):
[377] Fix | Delete
self.visit(item)
[378] Fix | Delete
elif isinstance(value, AST):
[379] Fix | Delete
self.visit(value)
[380] Fix | Delete
[381] Fix | Delete
def visit_Constant(self, node):
[382] Fix | Delete
value = node.value
[383] Fix | Delete
type_name = _const_node_type_names.get(type(value))
[384] Fix | Delete
if type_name is None:
[385] Fix | Delete
for cls, name in _const_node_type_names.items():
[386] Fix | Delete
if isinstance(value, cls):
[387] Fix | Delete
type_name = name
[388] Fix | Delete
break
[389] Fix | Delete
if type_name is not None:
[390] Fix | Delete
method = 'visit_' + type_name
[391] Fix | Delete
try:
[392] Fix | Delete
visitor = getattr(self, method)
[393] Fix | Delete
except AttributeError:
[394] Fix | Delete
pass
[395] Fix | Delete
else:
[396] Fix | Delete
import warnings
[397] Fix | Delete
warnings.warn(f"{method} is deprecated; add visit_Constant",
[398] Fix | Delete
PendingDeprecationWarning, 2)
[399] Fix | Delete
return visitor(node)
[400] Fix | Delete
return self.generic_visit(node)
[401] Fix | Delete
[402] Fix | Delete
[403] Fix | Delete
class NodeTransformer(NodeVisitor):
[404] Fix | Delete
"""
[405] Fix | Delete
A :class:`NodeVisitor` subclass that walks the abstract syntax tree and
[406] Fix | Delete
allows modification of nodes.
[407] Fix | Delete
[408] Fix | Delete
The `NodeTransformer` will walk the AST and use the return value of the
[409] Fix | Delete
visitor methods to replace or remove the old node. If the return value of
[410] Fix | Delete
the visitor method is ``None``, the node will be removed from its location,
[411] Fix | Delete
otherwise it is replaced with the return value. The return value may be the
[412] Fix | Delete
original node in which case no replacement takes place.
[413] Fix | Delete
[414] Fix | Delete
Here is an example transformer that rewrites all occurrences of name lookups
[415] Fix | Delete
(``foo``) to ``data['foo']``::
[416] Fix | Delete
[417] Fix | Delete
class RewriteName(NodeTransformer):
[418] Fix | Delete
[419] Fix | Delete
def visit_Name(self, node):
[420] Fix | Delete
return Subscript(
[421] Fix | Delete
value=Name(id='data', ctx=Load()),
[422] Fix | Delete
slice=Index(value=Str(s=node.id)),
[423] Fix | Delete
ctx=node.ctx
[424] Fix | Delete
)
[425] Fix | Delete
[426] Fix | Delete
Keep in mind that if the node you're operating on has child nodes you must
[427] Fix | Delete
either transform the child nodes yourself or call the :meth:`generic_visit`
[428] Fix | Delete
method for the node first.
[429] Fix | Delete
[430] Fix | Delete
For nodes that were part of a collection of statements (that applies to all
[431] Fix | Delete
statement nodes), the visitor may also return a list of nodes rather than
[432] Fix | Delete
just a single node.
[433] Fix | Delete
[434] Fix | Delete
Usually you use the transformer like this::
[435] Fix | Delete
[436] Fix | Delete
node = YourTransformer().visit(node)
[437] Fix | Delete
"""
[438] Fix | Delete
[439] Fix | Delete
def generic_visit(self, node):
[440] Fix | Delete
for field, old_value in iter_fields(node):
[441] Fix | Delete
if isinstance(old_value, list):
[442] Fix | Delete
new_values = []
[443] Fix | Delete
for value in old_value:
[444] Fix | Delete
if isinstance(value, AST):
[445] Fix | Delete
value = self.visit(value)
[446] Fix | Delete
if value is None:
[447] Fix | Delete
continue
[448] Fix | Delete
elif not isinstance(value, AST):
[449] Fix | Delete
new_values.extend(value)
[450] Fix | Delete
continue
[451] Fix | Delete
new_values.append(value)
[452] Fix | Delete
old_value[:] = new_values
[453] Fix | Delete
elif isinstance(old_value, AST):
[454] Fix | Delete
new_node = self.visit(old_value)
[455] Fix | Delete
if new_node is None:
[456] Fix | Delete
delattr(node, field)
[457] Fix | Delete
else:
[458] Fix | Delete
setattr(node, field, new_node)
[459] Fix | Delete
return node
[460] Fix | Delete
[461] Fix | Delete
[462] Fix | Delete
# The following code is for backward compatibility.
[463] Fix | Delete
# It will be removed in future.
[464] Fix | Delete
[465] Fix | Delete
def _getter(self):
[466] Fix | Delete
return self.value
[467] Fix | Delete
[468] Fix | Delete
def _setter(self, value):
[469] Fix | Delete
self.value = value
[470] Fix | Delete
[471] Fix | Delete
Constant.n = property(_getter, _setter)
[472] Fix | Delete
Constant.s = property(_getter, _setter)
[473] Fix | Delete
[474] Fix | Delete
class _ABC(type):
[475] Fix | Delete
[476] Fix | Delete
def __instancecheck__(cls, inst):
[477] Fix | Delete
if not isinstance(inst, Constant):
[478] Fix | Delete
return False
[479] Fix | Delete
if cls in _const_types:
[480] Fix | Delete
try:
[481] Fix | Delete
value = inst.value
[482] Fix | Delete
except AttributeError:
[483] Fix | Delete
return False
[484] Fix | Delete
else:
[485] Fix | Delete
return (
[486] Fix | Delete
isinstance(value, _const_types[cls]) and
[487] Fix | Delete
not isinstance(value, _const_types_not.get(cls, ()))
[488] Fix | Delete
)
[489] Fix | Delete
return type.__instancecheck__(cls, inst)
[490] Fix | Delete
[491] Fix | Delete
def _new(cls, *args, **kwargs):
[492] Fix | Delete
for key in kwargs:
[493] Fix | Delete
if key not in cls._fields:
[494] Fix | Delete
# arbitrary keyword arguments are accepted
[495] Fix | Delete
continue
[496] Fix | Delete
pos = cls._fields.index(key)
[497] Fix | Delete
if pos < len(args):
[498] Fix | Delete
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function