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_Call(self, node):
# TODO(mdan): Refactor converted_call as a 'Call' operator.
# Calls to the internal 'ag__' module are never converted (though their
# arguments might be).
full_name = str(anno.getanno(node.func, anno.Basic.QN, default=''))
if full_name.startswith('ag__.'):
return self.generic_visit(node)
if (full_name == 'print' and
not self.ctx.program.options.uses(converter.Feature.BUILTIN_FUNCTIONS)):
return self.generic_visit(node)
if isinstance(node.func, gast.Attribute):
func = gast.Str(node.func.attr)
owner = node.func.value
else:
func = node.func
owner = parser.parse_expression('None')
starred_arg = None
normal_args = []
for a in node.args:
if isinstance(a, gast.Starred):
assert starred_arg is None, 'Multiple *args should be impossible.'
starred_arg = a
else:
a = self.visit(a)
normal_args.append(a)
if starred_arg is None:
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)
return ast.Call(
ast.Name(name, ast.Load(), None, None),
def make_dispatcher(static_expr, func_true, func_false,
imported_ids):
dispatcher_args = [static_expr,
ast.Name(func_true.name, ast.Load(), None),
ast.Name(func_false.name, ast.Load(), None)]
dispatcher = ast.Call(
ast.Attribute(
ast.Attribute(
ast.Name("__builtin__", ast.Load(), None),
"pythran",
ast.Load()),
"static_if",
ast.Load()),
dispatcher_args, [])
actual_call = ast.Call(
dispatcher,
[ast.Name(ii, ast.Load(), None) for ii in imported_ids],
[])
return actual_call
def _process_variable_assignment(self, target, value):
# Constructors
if isinstance(value, gast.Call):
func = value.func
if anno.hasanno(func, 'live_val'):
func_obj = anno.getanno(func, 'live_val')
if tf_inspect.isclass(func_obj):
anno.setanno(value, 'is_constructor', True)
anno.setanno(value, 'type', func_obj)
anno.setanno(value, 'type_fqn', anno.getanno(func, 'fqn'))
# TODO(mdan): Raise an error if constructor has side effects.
# We can have a whitelist of no-side-effects constructors.
# We can also step inside the constructor and further analyze.
if isinstance(target, (gast.Name, gast.Attribute)):
target_symbol = anno.getanno(target, anno.Basic.QN)
self.scope.setval(target_symbol, value)
elif isinstance(target, gast.Subscript):
pass
else:
raise ValueError('assignment target has unknown type: %s' % target)
def visit_Return(self, node):
if not node.value and not self.yield_points:
none = ast.Attribute(ast.Name("__builtin__", ast.Load(), None, None),
'None', ast.Load())
node.value = none
self.update = True
return node
def pushmask(mask_expr):
return gast.Expr(gast.Call(
gast.Attribute(gast.Name('matchbox', gast.Load(), None),
gast.Name('push_execution_mask', gast.Load(), None),
gast.Load()),
[mask_expr], []))
def sub():
return ast.Call(func=ast.Attribute(
ast.Attribute(
ast.Name('__builtin__', ast.Load(), None, None),
'str',
ast.Load()),
'join', ast.Load()),
args=[ast.Constant(Placeholder(1), None),
ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())],
keywords=[])
>>> pm = passmanager.PassManager("test")
>>> _, node = pm.apply(Square, node)
>>> print(pm.dump(backend.Python, node))
import numpy as __pythran_import_numpy
__pythran_import_numpy.square(a)
>>> node = ast.parse('__pythran_import_numpy.power(a,2)')
>>> pm = passmanager.PassManager("test")
>>> _, node = pm.apply(Square, node)
>>> print(pm.dump(backend.Python, node))
import numpy as __pythran_import_numpy
__pythran_import_numpy.square(a)
"""
POW_PATTERN = ast.BinOp(AST_any(), ast.Pow(), ast.Constant(2, None))
POWER_PATTERN = ast.Call(
ast.Attribute(ast.Name(mangle('numpy'), ast.Load(), None, None),
'power',
ast.Load()),
[AST_any(), ast.Constant(2, None)],
[])
def __init__(self):
Transformation.__init__(self)
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], [])
def visit_Module(self, node):
self.need_import = False
def _get_full_name(node):
"""
Return full dotted name for Attribute or Name nodes.
If Attribute contains nodes other than Attribute or Name, return None.
"""
if isinstance(node, gast.Name):
return node.id
elif isinstance(node, gast.Attribute):
vn = _get_full_name(node.value)
if vn is not None:
return '.'.join((vn, node.attr))
def make_dispatcher(static_expr, func_true, func_false,
imported_ids):
dispatcher_args = [static_expr,
ast.Name(func_true.name, ast.Load(), None),
ast.Name(func_false.name, ast.Load(), None)]
dispatcher = ast.Call(
ast.Attribute(
ast.Attribute(
ast.Name("__builtin__", ast.Load(), None),
"pythran",
ast.Load()),
"static_if",
ast.Load()),
dispatcher_args, [])
actual_call = ast.Call(
dispatcher,
[ast.Name(ii, ast.Load(), None) for ii in imported_ids],
[])
return actual_call