def visit_Expr(self, node): try: return Expr(self.visit(node.value)) except Exception as ex: from hope._tosource import tosource ex.args = ((ex.args[0] if ex.args else "") + "\nin line " + tosource(node),) + ex.args[1:] raise
def visit_Assign(self, node): try: if len(node.targets) != 1: raise UnsupportedFeatureException("Only single target assignments are supported") target, value = self.visit(node.targets[0]), self.visit(node.value) if isinstance(target, NewVariable): self.variables[target.name] = Variable(target.name, copy.deepcopy(value.shape), value.dtype) target = self.variables[target.name] elif isinstance(target, Variable) and len(target.shape) == 0: pass elif not isinstance(target, View): raise Exception("Assignments are only allowed to views") # TODO: should we check dtypes? if len(target.shape) > 0 and len(value.shape) == 0: pass elif len(target.shape) != len(value.shape): raise Exception("Invalid shapes: {0!s} {1!s}".format(self.dump_shape(target.shape), self.dump_shape(value.shape))) else: self.merge_shapes(target, value) return Assign(target, value) except Exception as ex: from hope._tosource import tosource ex.args = ((ex.args[0] if ex.args else "") + "\nin line " + tosource(node),) + ex.args[1:] raise
def test_simple(self): mod_ast = _transformer.get_fkt_ast(dummy) mod_source = _tosource.tosource(mod_ast) # with open("dummy.py", "w") as f: # f.write(mod_source) mod_ast2 = ast.parse(mod_source) assert compare_ast(mod_ast, mod_ast2.body[0])
def visit_AugAssign(self, node): try: target, value = self.visit(node.target), self.visit(node.value) if len(value.shape) == 0: pass elif len(target.shape) != len(value.shape): raise Exception("Invalid shapes: {0!s} {1!s}".format(self.dump_shape(target.shape), self.dump_shape(value.shape))) else: self.merge_shapes(target, value) # TODO: should we check dtypes? return AugAssign(node.op, target, value) except Exception as ex: from hope._tosource import tosource ex.args = ((ex.args[0] if ex.args else "") + "\nin line " + tosource(node),) + ex.args[1:] raise
def exec_test_on_mod(self, mod): modpath = mod.__file__ if modpath.endswith('.pyc'): modpath = modpath[:-1] with open(modpath, "r") as f: source = f.read() mod_ast = ast.parse(source) mod_source = _tosource.tosource(mod_ast) mod_source = mod_source.replace("'\\n'", "@@@@") mod_source = mod_source.replace("\\n", "\n") mod_source = mod_source.replace("@@@@", "'\\n'") # with open("dummy.py", "w") as f: # f.write(mod_source) mod_ast2 = ast.parse(mod_source) assert compare_ast(mod_ast, mod_ast2)
def visit_Assign(self, node): try: if len(node.targets) != 1: raise UnsupportedFeatureException("Only single target assignments are supported") target, value = self.visit(node.targets[0]), self.visit(node.value) if isinstance(target, NewVariable): self.variables[target.name] = Variable(target.name, copy.deepcopy(value.shape), value.dtype) target = self.variables[target.name] elif isinstance(target, Variable) and len(target.shape) == 0: pass elif isinstance(target, ObjectAttr): pass elif not isinstance(target, View): raise Exception("Assignments are only allowed to views or variables") # TODO: should we check dtypes? if len(target.shape) > 0 and len(value.shape) == 0: pass elif len(target.shape) != len(value.shape): raise Exception("Invalid shapes: {0!s} {1!s}".format(self.dump_shape(target.shape), self.dump_shape(value.shape))) else: self.merge_shapes(target, value) if isinstance(value, ObjectAttr) and not isinstance(target, View): # reference to member name = ".".join(value.getTrace()) self.variables[name] = Variable(name, shape=copy.deepcopy(value.shape), dtype=value.dtype, scope="body") if not isinstance(target, ObjectAttr): # avoid that target is allocated self.variables[target.name].scope = "body" self.variables[target.name].allocated = True return Reference(target, value) return Assign(target, value) except Exception as ex: from hope._tosource import tosource ex.args = ((ex.args[0] if ex.args else "") + "\nin line " + tosource(node),) + ex.args[1:] raise
def body_visit(self, stmts): outerexprs, self.exprs, blocks, last_shape = self.exprs, [], [], "-1" for stmt in stmts: try: self.exprs.append(self.visit(stmt)) except Exception as ex: if "\nin body " not in (ex.args[0] if ex.args else ""): from hope._tosource import tosource ex.args = ((ex.args[0] if ex.args else "") + "\nin body " + tosource(stmt),) + ex.args[1:] raise for expr in self.exprs: segstrs = ["{0}:{1}".format("" if seg[0] is None else self.dumper.visit(seg[0]), self.dumper.visit(seg[1])) for seg in expr.shape] for segstr in segstrs: if not segstr in self.merged: self.merged[segstr] = len(self.merged) shapestr = "/".join([repr(self.merged[segstr]) for segstr in segstrs]) if len(shapestr) and len(blocks) > 0 and last_shape == shapestr: blocks[-1].body.append(expr) else: blocks.append(Block(expr)) last_shape = shapestr self.exprs = outerexprs return Body(blocks)