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_ExceptHandler(self, node):
name = self.visit(node.name) if node.name else None
body = [self.visit(m) for m in node.body]
if isinstance(node.type, ast.Tuple):
return [ExceptHandler(p.attr, Block(body), name)
for p in node.type.elts]
else:
return [ExceptHandler(
node.type and node.type.attr,
Block(body),
name)]
def visit_While(self, node):
"""
Create While node for Cxx generation.
It is a cxx_loop to handle else clause.
"""
test = self.visit(node.test)
body = [self.visit(n) for n in node.body]
stmt = While(test, Block(body))
return self.process_omp_attachements(node, stmt)
This function also handle assignment for local variables.
We can notice that three kind of loop are possible:
- Normal for loop on iterator
- Autofor loop.
- Normal for loop using integer variable iteration
Kind of loop used depend on OpenMP, yield use and variable scope.
"""
if not isinstance(node.target, ast.Name):
raise PythranSyntaxError(
"Using something other than an identifier as loop target",
node.target)
target = self.visit(node.target)
# Handle the body of the for loop
loop_body = Block([self.visit(stmt) for stmt in node.body])
# Declare local variables at the top of the loop body
loop_body = self.process_locals(node, loop_body, node.target.id)
iterable = self.visit(node.iter)
if self.can_use_c_for(node):
header, loop = self.gen_c_for(node, target, loop_body)
else:
if self.can_use_autofor(node):
header = []
self.ldecls.remove(node.target.id)
autofor = AutoFor(target, iterable, loop_body)
loop = [self.process_omp_attachements(node, autofor)]
else:
# Iterator declaration
def visit_If(self, node):
test = self.visit(node.test)
body = [self.visit(n) for n in node.body]
orelse = [self.visit(n) for n in node.orelse]
# compound statement required for some OpenMP Directives
if isnum(node.test) and node.test.value == 1:
stmt = Block(body)
else:
stmt = If(test, Block(body), Block(orelse) if orelse else None)
return self.process_locals(node,
self.process_omp_attachements(node, stmt))
name_fmt = pythran_ward + "{0}::{1}::type{2}"
args_list = ", ".join(arguments_types)
specialized_fname = name_fmt.format(module_name,
internal_func_name,
"<{0}>".format(args_list)
if arguments_names else "")
result_type = "typename %s::result_type" % specialized_fname
mod.add_pyfunction(
FunctionBody(
FunctionDeclaration(
Value(
result_type,
numbered_function_name),
[Value(t + '&&', a)
for t, a in zip(arguments_types, arguments)]),
Block([Statement("""
PyThreadState *_save = PyEval_SaveThread();
try {{
auto res = {0}()({1});
PyEval_RestoreThread(_save);
return res;
}}
catch(...) {{
PyEval_RestoreThread(_save);
throw;
}}
""".format(warded(module_name,
internal_func_name),
', '.join(arguments)))])
),
function_name,
arguments_types,
Declare variable local to node and insert declaration before.
Not possible for function yielding values.
"""
local_vars = self.scope[node].difference(skipped)
local_vars = local_vars.difference(self.openmp_deps)
if not local_vars:
return node_visited # no processing
locals_visited = []
for varname in local_vars:
vartype = self.typeof(varname)
decl = Statement("{} {}".format(vartype, varname))
locals_visited.append(decl)
self.ldecls.difference_update(local_vars)
return Block(locals_visited + [node_visited])
# block -----------------------------------------------------------------------
class Block(object):
def __init__(self, contents=None):
if contents is None:
contents = []
self.contents = contents
def generate(self):
yield "{"
for item in self.contents:
for item_line in item.generate():
yield " " + item_line
yield "}"
class Module(Block):
def generate(self):
for c in self.contents:
for line in c.generate():
yield line
class Namespace(Block):
def __init__(self, name, contents=None):
Block.__init__(self, contents)
self.name = name
def generate(self):
yield "namespace " + self.name
yield "{"
for item in self.contents:
for item_line in item.generate():
FunctionDeclaration(
Value("pythonic::types::generator_iterator<{0}>"
.format(next_name),
"begin"),
[]),
Block([Statement("next()"),
ReturnStatement(
"pythonic::types::generator_iterator<{0}>"
"(*this)".format(next_name))])),
FunctionBody(
FunctionDeclaration(
Value("pythonic::types::generator_iterator<{0}>"
.format(next_name),
"end"),
[]),
Block([ReturnStatement(
"pythonic::types::generator_iterator<{0}>()"
.format(next_name))]))
]
next_signature = templatize(
FunctionDeclaration(
Value(
"typename {0}::result_type".format(
instanciated_next_name),
"{0}::next".format(instanciated_next_name)),
[]),
formal_types)
next_body = operator_body
# the dispatch table at the entry point
next_body.insert(0, Statement("switch({0}) {{ {1} }}".format(
CxxGenerator.StateHolder,