Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def visit_Assign(self, node):
node = self.generic_visit(node)
# Only convert lists when they are assigned to a variable, e.g.:
# l = []
# TODO(mdan): This rule should be improved.
if len(node.targets) != 1:
return node
if not isinstance(node.value, gast.List):
return node
if not isinstance(node.value.ctx, gast.Load):
return node
if node.value.elts:
node.value = self._pre_populated_list(node.value)
else:
node.value = self._empty_list(node.value)
return node
def visit_Destructured(self, node):
dnode = self.chains.setdefault(node, Def(node))
tmp_store = ast.Store()
for elt in node.elts:
if isinstance(elt, ast.Name):
tmp_store, elt.ctx = elt.ctx, tmp_store
self.visit(elt)
tmp_store, elt.ctx = elt.ctx, tmp_store
elif isinstance(elt, ast.Subscript):
self.visit(elt)
elif isinstance(elt, (ast.List, ast.Tuple)):
self.visit_Destructured(elt)
return dnode
def visit_For(self, node):
if isinstance(node.iter, (ast.List, ast.Tuple)):
range_params = self.isrange(node.iter.elts)
if range_params:
node.iter = ast.Call(ast.Attribute(
ast.Name('__builtin__', ast.Load(), None, None),
'xrange',
node.iter.ctx),
[ast.Constant(param, None) for param in range_params],
[])
self.update = True
return self.generic_visit(node)
def _to_reference_list(self, names):
return gast.List(
[self._to_reference(name) for name in names], ctx=gast.Load())
def visit_DictComp(self, node):
# this is a quickfix to match visit_AnyComp signature
# potential source of improvement there!
node.elt = ast.List(
[ast.Tuple([node.key, node.value], ast.Load())],
ast.Load()
)
return self.visit_AnyComp(node, "dict", "__dispatch__", "update")
if isinstance(node, gast.UnaryOp):
return "{}{}".format(unaryop_to_str(node.op), expr_to_str(node.operand))
if isinstance(node, gast.Call):
return "{}({})".format(expr_to_str(node.func),
intercalate([expr_to_str(arg) for arg in node.args], ", "))
if isinstance(node, gast.Num):
return str(node.n)
if isinstance(node, gast.Str):
return "\"...\"" # sometimes it is too long
if isinstance(node, gast.Attribute):
return "{}.{}".format(expr_to_str(node.value), node.attr)
if isinstance(node, gast.Subscript):
return "{}[{}]".format(expr_to_str(node.value), slice_to_str(node.slice))
if isinstance(node, gast.Name):
return node.id
if isinstance(node, gast.List):
return "[" + intercalate([expr_to_str(e) for e in node.elts], ", ") + "]"
if isinstance(node, gast.Tuple):
return "(" + intercalate([expr_to_str(e) for e in node.elts], ", ") + ")"
return ""
return "{}({})".format(expr_to_str(node.func), intercalate(args, ", "))
if isinstance(node, gast.Num):
return str(node.n)
if isinstance(node, gast.Str):
if len(node.s) < 20:
return "\'" + node.s + "\'"
return "\"...\"" # sometimes it is too long
if isinstance(node, gast.Attribute):
return "{}.{}".format(expr_to_str(node.value), node.attr)
if isinstance(node, gast.Subscript):
return "{}[{}]".format(expr_to_str(node.value), slice_to_str(node.slice))
if isinstance(node, gast.NameConstant):
return str(node.value)
if isinstance(node, gast.Name):
return node.id
if isinstance(node, gast.List):
return "[" + intercalate([expr_to_str(e) for e in node.elts], ", ") + "]"
if isinstance(node, gast.Tuple):
return "(" + intercalate([expr_to_str(e) for e in node.elts], ", ") + ")"
return ""
r = ast.Call(ast.Attribute(ast.Name('__builtin__', ast.Load(),
None, None),
'list', ast.Load()),
[r], [])
return r
if isinstance(node.elt, ast.Constant) and len(node.generators) == 1:
gen = node.generators[0]
if not gen.ifs and isinstance(gen.iter, ast.Call):
try:
path = attr_to_path(gen.iter.func)[1]
range_path = 'pythonic', '__builtin__', 'functor', 'range'
if path == range_path and len(gen.iter.args) == 1:
self.update = True
return ast.BinOp(
ast.List([node.elt], ast.Load()),
ast.Mult(),
ast.Call(path_to_attr(('__builtin__', 'len')),
[gen.iter],
[]))
except TypeError:
pass
return self.visitComp(node, makeattr)
Examples
--------
>> from numpy.linalg import det
>> det(a)
Becomes
>> numpy.linalg.det(a)
"""
if node.id in self.symbols:
symbol = path_to_node(self.symbols[node.id])
if not getattr(symbol, 'isliteral', lambda: False)():
parent = self.ancestors[node][-1]
blacklist = (ast.Tuple,
ast.List,
ast.Set,
ast.Return)
if isinstance(parent, blacklist):
raise PythranSyntaxError(
"Unsupported module identifier manipulation",
node)
new_node = path_to_attr(self.symbols[node.id])
new_node.ctx = node.ctx
ast.copy_location(new_node, node)
return new_node
return node