def mutate_FunctionDef(self, node): if not self.should_mutate(node): raise MutationResign() index, stmt = self.get_super_call(node) if index is not None: raise MutationResign() node.body.insert(0, self.create_super_call(node)) return node
def mutate_FunctionDef(self, node): if not self.should_mutate(node): raise MutationResign() index, _ = self.get_super_call(node) if index is None: raise MutationResign() node.body[index] = ast.Pass() return node
def mutate_Attribute(self, node): try: if node.value.id == 'self': return ast.Name(id=node.attr, ctx=ast.Load()) else: raise MutationResign() except AttributeError: raise MutationResign()
def mutate_Assign(self, node): if len(node.targets) > 1: raise MutationResign() if isinstance(node.targets[0], ast.Name) and self.is_overridden( node, name=node.targets[0].id): return ast.Pass() elif isinstance(node.targets[0], ast.Tuple) and isinstance( node.value, ast.Tuple): return self.mutate_unpack(node) else: raise MutationResign()
def mutate_FunctionDef(self, node): if not self.should_mutate(node): raise MutationResign() index, stmt = self.get_super_call(node) if index is None: raise MutationResign() super_call = node.body[index] del node.body[index] if index == 0: node.body.append(super_call) else: node.body.insert(0, super_call) return node
def mutate_FunctionDef(self, node): if not isinstance(node.parent, ast.ClassDef): raise MutationResign() for decorator in node.decorator_list: if isinstance(decorator, ast.Call): decorator_name = decorator.func.id elif isinstance(decorator, ast.Attribute): decorator_name = decorator.value.id else: decorator_name = decorator.id if decorator_name == self.get_decorator_name(): raise MutationResign() decorator = ast.Name(id=self.get_decorator_name(), ctx=ast.Load()) node.decorator_list.append(decorator) return node
def help_str(self, node): if utils.is_docstring(node): raise MutationResign() if node.s != self.FIRST_CONST_STRING: return self.FIRST_CONST_STRING else: return self.SECOND_CONST_STRING
def mutate_Str(self, node): if utils.is_docstring(node): raise MutationResign() if node.s != self.FIRST_CONST_STRING: return ast.Str(s=self.FIRST_CONST_STRING) else: return ast.Str(s=self.SECOND_CONST_STRING)
def mutate_FunctionDef(self, node): if not self.should_mutate(node): raise MutationResign() index, stmt = self.get_super_call(node) if index is None: raise MutationResign() super_call = node.body[index] del node.body[index] if index == 0: self.set_lineno(super_call, node.body[-1].lineno) self.shift_lines(node.body, -1) node.body.append(super_call) else: self.set_lineno(super_call, node.body[0].lineno) self.shift_lines(node.body, 1) node.body.insert(0, super_call) return node
def is_overridden(self, node, name=None): if not isinstance(node.parent, ast.ClassDef): raise MutationResign() if not name: name = node.name parent = node.parent parent_names = [] while parent: if not isinstance(parent, ast.Module): parent_names.append(parent.name) if not isinstance(parent, ast.ClassDef) and not isinstance( parent, ast.Module): raise MutationResign() parent = parent.parent getattr_rec = lambda obj, attr: functools.reduce(getattr, attr, obj) try: klass = getattr_rec(self.module, reversed(parent_names)) except AttributeError: raise MutationResign() for base_klass in type.mro(klass)[1:-1]: if hasattr(base_klass, name): return True return False
def mutate_unpack(self, node): target = node.targets[0] value = node.value new_targets = [] new_values = [] for target_element, value_element in zip(target.elts, value.elts): if not self.is_overridden(node, getattr(target_element, 'id', None)): new_targets.append(target_element) new_values.append(value_element) if len(new_targets) == len(target.elts): raise MutationResign() if not new_targets: return ast.Pass() elif len(new_targets) == 1: node.targets = new_targets node.value = new_values[0] return node else: target.elts = new_targets value.elts = new_values return node
def mutate_Call(self, node): if isinstance(node.func, ast.Name): print("Name: ", ast.dump(node)) if node.func.id in self.equivalent_gates(): print('Mutating quantum gate: ', node.func.id) equiv_gates = self.equivalent_gates()[node.func.id] equiv_gates.remove(node.func.id) if equiv_gates is None: return node new_gate = random.choice(list(equiv_gates)) mutated_qgate = ast.Name(new_gate, node.func.ctx) print( "Mutated: ", ast.dump(ast.Call(mutated_qgate, node.args, node.keywords))) return ast.Call(mutated_qgate, node.args, node.keywords) if isinstance(node.func, ast.Attribute): print("Attribute: ", ast.dump(node)) if node.func.attr in self.equivalent_gates(): print('Mutating quantum gate: ', node.func.attr) equiv_gates = self.equivalent_gates()[node.func.attr] equiv_gates.remove(node.func.attr) if equiv_gates is None: return node new_gate = random.choice(list(equiv_gates)) mutated_qgate = ast.Attribute(node.func.value, new_gate, node.func.ctx) print( "Mutated: ", ast.dump(ast.Call(mutated_qgate, node.args, node.keywords))) return ast.Call(mutated_qgate, node.args, node.keywords) raise MutationResign()
def mutate_Mult_to_Div(self, node): if self.should_mutate(node): return ast.Div() raise MutationResign()
def mutate_Sub(self, node): if self.should_mutate(node): return ast.Add() raise MutationResign()
def help_str_empty(self, node): if not node.s or utils.is_docstring(node): raise MutationResign() return ''
def mutate_Constant_num(self, node): if isinstance(node.value, (int, float)) and not isinstance(node.value, bool): return ast.Constant(n=node.n + 1) else: raise MutationResign()
def mutate_Constant_str_empty(self, node): if isinstance(node.value, str): return ast.Constant(s=self.help_str_empty(node)) else: raise MutationResign()
def mutate_ExceptHandler(self, node): if node.body and isinstance(node.body[0], ast.Raise): raise MutationResign() return ast.ExceptHandler(type=node.type, name=node.name, body=[ast.Raise()])
def mutate_ExceptHandler(self, node): if len(node.body) == 1 and isinstance(node.body[0], ast.Pass): raise MutationResign() return self._replace_exception_body( node, [ast.Pass(lineno=node.body[0].lineno)])
def mutate_FloorDiv_to_Mult(self, node): if self.should_mutate(node): return ast.Mult() raise MutationResign()
def mutate_Slice_remove_step(self, node): if not node.step: raise MutationResign() return ast.Slice(lower=node.lower, upper=node.upper, step=None)
def mutate_Str_empty(self, node): if not node.s or utils.is_docstring(node): raise MutationResign() return ast.Str(s='')
def mutate_Pow(self, node): if self.should_mutate(node): return ast.Mult() raise MutationResign()
def mutate_Expr(self, node): if utils.is_docstring(node.value): raise MutationResign() return ast.Pass()
def mutate_FunctionDef(self, node): if node.decorator_list: node.decorator_list = [] return node else: raise MutationResign()
def mutate_FunctionDef(self, node): if self.is_overridden(node): return ast.Pass() raise MutationResign()