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_BinOp(self, node):
# replace "str" % (...) by __builtin__.str.__mod__(...)
# the reason why we do this is that % formatting is handled by
# a third party library that's relatively costly to load, so using a
# function name instead of an operator overload makes it possible to
# load it only when needed. The drawback is that % formatting is no
# longer supported when lhs is not a literal
self.generic_visit(node)
if isinstance(node.op, ast.Mod) and isstr(node.left):
self.update = True
return ast.Call(
ast.Attribute(
ast.Attribute(
ast.Name('__builtin__', ast.Load(), None, None),
'str',
ast.Load()),
'__mod__',
ast.Load()),
[node.left, node.right],
[])
return node
def visit_Name(self, node):
self.generic_visit(node)
qn = anno.getanno(node, anno.Basic.QN)
if isinstance(node.ctx, gast.Param):
self._process_function_arg(qn)
elif isinstance(node.ctx, gast.Load) and self.scope.hasval(qn):
# E.g. if we had
# a = b
# then for future references to `a` we should have definition = `b`
definition = self.scope.getval(qn)
if anno.hasanno(definition, 'type'):
anno.setanno(node, 'type', anno.getanno(definition, 'type'))
anno.setanno(node, 'type_fqn', anno.getanno(definition, 'type_fqn'))
if anno.hasanno(definition, 'element_type'):
anno.setanno(node, 'element_type',
anno.getanno(definition, 'element_type'))
return node
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 replace(self, value):
self.update = self.need_import = True
module_name = ast.Name(mangle('numpy'), ast.Load(), None, None)
return ast.Call(ast.Attribute(module_name, 'square', ast.Load()),
[value], [])
# gast.Attribute(
# gast.Name('matchbox', gast.Load(), None),
# gast.Name('EXECUTION_MASK', gast.Load(), None),
# gast.Load()),
# [gast.IsNot()],
# [gast.NameConstant(None)]),
gast.Call(gast.Name('isinstance', gast.Load(), None),
[node.targets[0], gast.Tuple(
[gast.Attribute(
gast.Name('matchbox', gast.Load(), None),
gast.Name('MaskedBatch', gast.Load(), None),
gast.Load()),
gast.Attribute(
gast.Name('matchbox', gast.Load(), None),
gast.Name('TENSOR_TYPE', gast.Load(), None),
gast.Load())], gast.Load())], [])]),
gast.Call(
gast.Attribute(
gast.Name(node.targets[0].id, gast.Load(), None),
gast.Name('_update', gast.Load(), None),
gast.Load()),
[node.value, gast.Attribute(
gast.Name('matchbox', gast.Load(), None),
gast.Name('EXECUTION_MASK', gast.Load(), None),
gast.Load())], []),
node.value)
return node
def interprocedural_type_translator(s, n):
translated_othernode = ast.Name(
'__fake__', ast.Load(), None, None)
s.result[translated_othernode] = (
parametric_type.instanciate(
s.current,
[s.result[arg] for arg in n.args]))
# look for modified argument
for p, effective_arg in enumerate(n.args):
formal_arg = args[p]
if formal_arg.id == node_id:
translated_node = effective_arg
break
try:
s.combine(translated_node,
translated_othernode,
op, unary_op, register=True,
aliasing_type=True)
except NotImplementedError:
if is_whitelisted_for_graph(base):
alias = namer.new_symbol(base.__name__, ())
output_nodes.append(
gast.ImportFrom(
module=base.__module__,
names=[gast.alias(name=base.__name__, asname=alias)],
level=0))
else:
# This will trigger a conversion into a class with this name.
alias = namer.compiled_class_name(base.__name__, base)
base_names.append(alias)
renames[qual_names.QN(base.__name__)] = qual_names.QN(alias)
program_ctx.update_name_map(namer)
# Generate the definition of the converted class.
bases = [gast.Name(n, gast.Load(), None) for n in base_names]
class_def = gast.ClassDef(
class_name,
bases=bases,
keywords=[],
body=list(converted_members.values()),
decorator_list=[])
# Make a final pass to replace references to the class or its base classes.
# Most commonly, this occurs when making super().__init__() calls.
# TODO(mdan): Making direct references to superclass' superclass will fail.
class_def = qual_names.resolve(class_def)
renames[qual_names.QN(c.__name__)] = qual_names.QN(class_name)
class_def = ast_util.rename_symbols(class_def, renames)
output_nodes.append(class_def)
return output_nodes, class_name, class_namespace
return_alias=lambda args: {
ast.Subscript(args[0],
ast.Index(args[1]),
ast.Load())
}.union({args[2]} if len(args) == 3 else set())
),
def make_Iterator(self, gen):
if gen.ifs:
ldFilter = ast.Lambda(
ast.arguments([ast.Name(gen.target.id, ast.Param(), None, None)],
[], None, [], [], None, []),
ast.BoolOp(ast.And(), gen.ifs)
if len(gen.ifs) > 1 else gen.ifs[0])
self.use_itertools |= MODULE == 'itertools'
ifilterName = ast.Attribute(
value=ast.Name(id=ASMODULE,
ctx=ast.Load(),
annotation=None, type_comment=None),
attr=IFILTER, ctx=ast.Load())
return ast.Call(ifilterName, [ldFilter, gen.iter], [])
else:
return gen.iter
def inlineFixedSizeArrayUnaryOp(self, node):
if isinstance(node.operand, (ast.Constant, ast.List, ast.Tuple)):
return node
base, size = self.fixedSizeArray(node.operand)
if not base:
return node
self.update = True
operands = [ast.UnaryOp(type(node.op)(),
self.make_array_index(base, size, i))
for i in range(size)]
res = ast.Call(path_to_attr(('numpy', 'array')),
[ast.Tuple(operands, ast.Load())],
[])
self.aliases[res.func] = {path_to_node(('numpy', 'array'))}
return res