Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def generate_Assign(self):
"""Generate an Assign node."""
# Generate left-hand side
target_node = self.generate_Name(gast.Store())
# Generate right-hand side
value_node = self.generate_expression()
# Put it all together
node = gast.Assign(targets=[target_node], value=value_node)
return node
for child in node.body:
for n in gast.walk(child):
if isinstance(n, gast.Name) and isinstance(n.ctx, gast.Load):
loads[n.id].append(n)
if isinstance(child, gast.Assign):
name = child.targets[0].id
if name in loads:
if name in lcds:
raise NotImplementedError("cannot process LCD "
"stored to twice")
lcds.add(name)
node = SplitAttributes().visit(node)
synchronizes = []
for name in lcds:
synchronize = gast.Assign(
[gast.Name(name, gast.Store(), None)],
gast.Call(
gast.Attribute(
gast.Name(name, gast.Load(), None),
gast.Name('_synchronize', gast.Load(), None),
None),
[], []))
synchronizes.append(synchronize)
node.body.extend(synchronizes)
return node
def visit_Return(self, node):
modified_node = self.generic_visit(node)
if node.value is None:
node_value = gast.NameConstant(value=None)
else:
node_value = node.value
self.func_returned_stack[-1] = True
returned_id = len(self.func_returned_stack)
replacement = [gast.Assign(targets=[gast.Name(id=self.returned_flag + str(returned_id), ctx=gast.Store(), annotation=None)], value=gast.NameConstant(value=True)),
gast.Assign(targets=[gast.Name(id=self.returned_value_key, ctx=gast.Store(), annotation=None)], value=node_value)]
if isinstance(modified_node, gast.If): #TODO: Add location to returned value.
modified_node.body = replacement
return modified_node
else:
return replacement
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
elif do_rename:
new_name = namer.function_name(f.__name__)
else:
new_name = f.__name__
entity_info = transformer.EntityInfo(
source_code=source,
source_file='',
future_features=future_features,
namespace=namespace)
context = converter.EntityContext(namer, entity_info, program_ctx, new_name)
node = node_to_graph(node, context)
if isinstance(node, gast.Lambda):
node = gast.Assign(
targets=[gast.Name(new_name, gast.Store(), None)], value=node)
elif do_rename:
node.name = new_name
else:
assert node.name == new_name
return (node,), new_name, entity_info
n = len(self.new_functions)
status_n = "$status{}".format(n)
return_n = "$return{}".format(n)
cont_n = "$cont{}".format(n)
if has_return:
cont_ass = self.make_control_flow_handlers(cont_n, status_n,
expected_return,
has_cont, has_break)
cmpr = ast.Compare(ast.Name(status_n, ast.Load(), None),
[ast.Eq()], [ast.Num(EARLY_RET)])
fast_return = [ast.Name(status_n, ast.Store(), None),
ast.Name(return_n, ast.Store(), None),
ast.Name(cont_n, ast.Store(), None)]
return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
actual_call),
ast.If(cmpr,
[ast.Return(ast.Name(return_n, ast.Load(), None))],
cont_ass)]
elif has_break or has_cont:
cont_ass = self.make_control_flow_handlers(cont_n, status_n,
expected_return,
has_cont, has_break)
fast_return = [ast.Name(status_n, ast.Store(), None),
ast.Name(cont_n, ast.Store(), None)]
return [ast.Assign([ast.Tuple(fast_return, ast.Store())],
actual_call)] + cont_ass
def visit_Subscript(self, node):
if isinstance(node.value, (gast.Name, gast.Num)) and node.value.id == 'd':
if (not isinstance(node.slice, gast.Index) or
not isinstance(node.slice.value,
(gast.Subscript, gast.Name, gast.Str))):
# This happens when the gradient of a constant is taken
if self.replace_grad == Replace.TANGENT:
new_node = gast.Num(0)
else:
new_node = gast.Name(id='_', ctx=None, annotation=None)
self.remove(new_node)
elif (self.replace_grad in (Replace.FULL, Replace.TANGENT) or
isinstance(node.ctx, gast.Load)):
new_node = create.create_grad(node.slice.value, self.namer,
self.tangent)
elif isinstance(node.ctx, gast.Store):
new_node = create.create_temp_grad(node.slice.value, self.namer,
self.tangent)
else:
raise ValueError
new_node.ctx = node.ctx
if isinstance(new_node, gast.Tuple):
for elt in new_node.elts:
elt.ctx = node.ctx
node = new_node
return node
new_id = '{}_m{}'.format(node.left.id, i)
i += 1
rargs = range_.args.args
lower = rargs[0] if len(rargs) > 1 else ast.Constant(0, None)
header = ast.Assign([ast.Name(new_id, ast.Store(), None, None)],
ast.BinOp(
ast.BinOp(deepcopy(lower),
ast.Sub(),
ast.Constant(1, None)),
ast.Mod(),
deepcopy(node.right)))
incr = ast.BinOp(ast.Name(new_id, ast.Load(), None, None),
ast.Add(),
ast.Constant(1, None))
step = ast.Assign([ast.Name(new_id, ast.Store(), None, None)],
ast.IfExp(
ast.Compare(incr,
[ast.Eq()], [deepcopy(node.right)]),
ast.Constant(0, None),
deepcopy(incr)))
self.loops_mod.setdefault(loop, []).append((header, step))
self.update = True
return ast.Name(new_id, ast.Load(), None, None)
reversed(node.generators),
ast.Expr(
ast.Call(
reduce(lambda x, y: ast.Attribute(x, y,
ast.Load()),
path[1:],
ast.Name(path[0], ast.Load(), None, None)),
[ast.Name(starget, ast.Load(), None, None), node.elt],
[],
)
)
)
# add extra metadata to this node
metadata.add(body, metadata.Comprehension(starget))
init = ast.Assign(
[ast.Name(starget, ast.Store(), None, None)],
ast.Call(
ast.Attribute(
ast.Name('__builtin__', ast.Load(), None, None),
comp_type,
ast.Load()
),
[], [],)
)
result = ast.Return(ast.Name(starget, ast.Load(), None, None))
sargs = [ast.Name(arg, ast.Param(), None, None) for arg in args]
fd = ast.FunctionDef(name,
ast.arguments(sargs, [], None, [], [], None, []),
[init, body, result],
[], None, None)
metadata.add(fd, metadata.Local())
self.ctx.module.body.append(fd)