def to_node(self): if len(self.operator) == 0: return self.left.to_node() else: right_nodes = [] comp_operator = [] for i in range(len(self.right)): right_nodes.append(self.right[i].to_node()) if self.operator[i] in ['<', 'is lower than']: comp_operator.append(ast.Lt()) elif self.operator[i] in ['<=', 'is lower or equal to']: comp_operator.append(ast.LtE()) elif self.operator[i] in ['>', 'is greater than']: comp_operator.append(ast.Gt()) elif self.operator[i] in ['>=', 'is greater or equal to']: comp_operator.append(ast.GtE()) elif self.operator[i] in ['==', 'is equal to']: comp_operator.append(ast.Eq()) elif self.operator[i] in ['!=', 'is different from', 'is not equal to']: comp_operator.append(ast.NotEq()) elif self.operator[i] == 'in': comp_operator.append(ast.In()) elif self.operator[i] == 'not in': comp_operator.append(ast.NotIn()) elif self.operator[i] == 'is': comp_operator.append(ast.Is()) elif self.operator[i] == 'is not': comp_operator.append(ast.IsNot()) else: raise Exception("Unrecognized argument in Comparison") return ast.Compare(left=self.left.to_node(), ops=comp_operator, comparators=right_nodes)
def test_multi_not_in_array(self): assert self.run([ ast.Compare( ast.Num(4), [ast.NotIn()], [ast.List([ ast.Num(1), ast.Num(2), ast.Num(3), ], ast.Load())]), ], None, bool) is True
def visit_Compare(self, node): # cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn print(" in MyTransformer.visit_Compare()") print(" node =", node) print(" op =", node.ops[0]) curr_op = node.ops[0] comp_negate = curr_op rand_num = random.randint(1, 10) if rand_num >= 7: print(" negating...") if isinstance(curr_op, ast.Eq): comp_negate = ast.NotEq() elif isinstance(curr_op, ast.NotEq): comp_negate = ast.Eq() elif isinstance(curr_op, ast.Lt): comp_negate = ast.GtE() elif isinstance(curr_op, ast.LtE): comp_negate = ast.Gt() elif isinstance(curr_op, ast.Gt): comp_negate = ast.LtE() elif isinstance(curr_op, ast.GtE): comp_negate = ast.Lt() elif isinstance(curr_op, ast.Is): comp_negate = ast.IsNot() elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Is() elif isinstance(curr_op, ast.In): comp_negate = ast.NotIn() elif isinstance(curr_op, ast.NotIn): comp_negate = ast.In() else: comp_negate = ast.Eq() else: print(" mixing up...") if isinstance(curr_op, ast.Lt): comp_negate = ast.LtE() elif isinstance(curr_op, ast.LtE): comp_negate = ast.And() elif isinstance(curr_op, ast.Gt): comp_negate = ast.Or() elif isinstance(curr_op, ast.GtE): comp_negate = ast.Gt() elif isinstance(curr_op, ast.Is): comp_negate = ast.Gt() elif isinstance(curr_op, ast.IsNot): comp_negate = ast.Lt() elif isinstance(curr_op, ast.In): comp_negate = ast.In( ) #leave the same for for loops (for x in this) elif isinstance(curr_op, ast.NotIn): comp_negate = ast.Lt() else: comp_negate = ast.Eq() print(" new comparator =", comp_negate) # create negated node | Compare(expr left, cmpop* ops, expr* comparators) new_node = node new_node.ops = [comp_negate] ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) return new_node
def test_multi_not_in_dict(self): assert self.run([ ast.Compare(ast.Str('d'), [ast.NotIn()], [ ast.Dict([ ast.Str('a'), ast.Str('b'), ast.Str('c'), ], [ ast.Num(1), ast.Num(2), ast.Num(3), ]) ]), ], None, bool) is True
def p_comp_op(p): '''comp_op : ">" | "<" | OP_EQ | OP_GE | OP_LE | OP_NE | OP_NNE | TAG_IN | TAG_NOT TAG_IN | TAG_IS | TAG_IS TAG_NOT''' if len(p) == 2: if p.get_item(1).type == 'OP_EQ': p[0] = ast.Eq() elif p.get_item(1).type == '>': p[0] = ast.Gt() elif p.get_item(1).type == '<': p[0] = ast.Lt() elif p.get_item(1).type == 'OP_GE': p[0] = ast.GtE() elif p.get_item(1).type == 'OP_LE': p[0] = ast.LtE() elif p.get_item(1).type == 'OP_NE': p[0] = ast.NotEq() elif p.get_item(1).type == 'OP_NNE': p[0] = ast.NotEq() elif p[1] == 'is': p[0] = ast.Is() elif p[1] == 'in': p[0] = ast.In() elif len(p) == 3: if p[1] == 'is': p[0] = ast.IsNot() elif p[1] == 'not': p[0] = ast.NotIn() return
def _visit_Assign(self, assign_node, dummy=None): def search_ids(node): ids = [] for elt in node.elts: if isinstance(elt, ast.Name): ids.append(elt.id) else: ids.extend(search_ids(elt)) return ids nodes = [] for idx, target in enumerate(assign_node.targets): target_ids = [] if not isinstance(target, ast.Name): target_ids.append(search_ids(target)) else: target_ids.append([target.id]) compares = [] for ids in target_ids: target_compares = [] for _id in ids: compare = ast.Compare( left=ast.Str(s=_id), ops=[ast.NotIn()], comparators=[ ast.Call(func=ast.Name(id="globals", ctx=ast.Load()), args=[], keywords=[]) ]) target_compares.append(compare) compares.append(target_compares) for target_compares in compares: if len(target_compares) == 1: test = target_compares[0] new_target = [assign_node.targets[0]] else: test = ast.BoolOp(op=ast.Or(), values=target_compares) new_target = [assign_node.targets[idx]] new_assign = ast.Assign(targets=new_target, value=assign_node.value) nodes.append(ast.If(test=test, body=[new_assign], orelse=[])) return nodes
def _revert_compare(node): """ Helper function to revert a compare node to its negation. """ rev_node = copy.deepcopy(node) op = rev_node.ops[0] if isinstance(op, ast.Is): rev_node.ops = [ast.IsNot()] elif isinstance(op, ast.Gt): rev_node.ops = [ast.LtE()] elif isinstance(op, ast.Lt): rev_node.ops = [ast.GtE()] elif isinstance(op, ast.Eq): rev_node.ops = [ast.NotEq()] elif isinstance(op, ast.In): rev_node.ops = [ast.NotIn()] else: raise ConstraintError('Unknown operator: %s' % op) return rev_node
def visit_CmpOp(self, node: CmpOp, *args, **kwargs) -> C.cmpop: if node == CmpOp.Eq: return C.Eq() elif node == CmpOp.NotEq: return C.NotEq() elif node == CmpOp.Lt: return C.Lt() elif node == CmpOp.Lte: return C.Lte() elif node == CmpOp.Gt: return C.Gt() elif node == CmpOp.Gte: return C.Gte() elif node == CmpOp.Is: return C.Is() elif node == CmpOp.IsNot: return C.IsNot() elif node == CmpOp.In: return C.In() elif node == CmpOp.NotIn: return C.NotIn() else: raise Exception(f'unknown CmpOp {node!r}')
def comparison(self) -> ast.expr: left: ast.expr = self.bit_or() operators: list[ast.cmpop] = [] extra: list[ast.expr] = [] while self.match_(*TokenGroup.SINGLE_COMPARISON, TokenType.NOT): if self.previous().type == TokenType.IS: if self.match_(TokenType.NOT): operator = ast.IsNot() else: operator = ast.Is() elif self.previous().type == TokenType.NOT: self.consume(TokenType.IN, "'in' must follow 'not' in comparison.") operator = ast.NotIn() else: operator = COMPARISON_OPERATORS[self.previous().type]() right = self.bit_or() operators.append(operator) extra.append(right) if operators: return ast.Compare(left, operators, extra, **self.get_loc(left, extra[-1])) else: return left
def __init__(self, str, lineno=0): self.value = str self.lineno = lineno op_ast_map = { '+': ast.Add(), '-': ast.Sub(), '*': ast.Mult(), '/': ast.Div(), '%': ast.Mod(), '**': ast.Pow(), '<<': ast.LShift(), '>>': ast.RShift(), '|': ast.BitOr(), '^^': ast.BitXor(), '&&': ast.BitAnd(), '//': ast.FloorDiv(), '==': ast.Eq(), '!=': ast.NotEq(), '<': ast.Lt(), '<=': ast.LtE(), '>': ast.Gt(), '>=': ast.GtE(), 'is': ast.Is(), 'is_not': ast.IsNot(), 'in': ast.In(), 'not_in': ast.NotIn(), 'and': ast.And(), 'or': ast.Or() }
def mutate_In(self, node): return ast.NotIn()
def compile_with(node): body = [] matched_var_name = node.context_expr.args[0].id for expr in node.body: assert isinstance(expr.value, ast.BinOp) assert isinstance(expr.value.op, ast.RShift) processor = expr.value.right check_node = build_matcher(expr.value.left, matched_var_name) # val = do_match(.....) match_node = ast.Assign(targets=[ast.Name(id='__vals', ctx=ast.Store())], value=check_node) processor_call = VarTransformer(var_re, lambda x : ast.Subscript( value=ast.Name(id='__vals', ctx=ast.Load()), slice=ast.Index(value=ast.Str(s=x.group(1))), ctx=ast.Load()) ).visit(processor) raise_result = ast.Raise(type=ast.Call( func=ast.Attribute( value=ast.Name(id="python_match", ctx=ast.Load()), attr='Value', ctx=ast.Load()), args=[processor_call], keywords=[], starargs=None, kwargs=None), inst=None, tback=None ) if_node = ast.If(test=ast.Compare( left=ast.Name(id='__vals', ctx=ast.Load()), ops=[ast.NotIn()], comparators=[ast.Tuple( elts=[ast.Name(id='None', ctx=ast.Load()), ast.Name(id='False', ctx=ast.Load())], ctx=ast.Load())] ), body=[raise_result], orelse=[]) for new_node in (match_node, if_node): ast.copy_location(new_node, expr) ast.fix_missing_locations(new_node) body.append(new_node) node_body = ast.Call( func=ast.Attribute( value=ast.Name(id="python_match", ctx=ast.Load()), attr='marked_match__', ctx=ast.Load()), args=node.context_expr.args, keywords=node.context_expr.keywords, starargs=node.context_expr.starargs, kwargs=node.context_expr.kwargs ) new_node = ast.With(body=body, context_expr=node_body, optional_vars=node.optional_vars) ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) #print ast.dump(node) return new_node
Symbol.new("cadr"): lambda p: ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(1)), ast.Load()), Symbol.new("car"): lambda p: ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(0)), ast.Load()), Symbol.new("cdar"): lambda p: ast.Subscript(ast.Subscript(build_ast(p[1]), ast.Index(ast.Num(0)), ast.Load()), ast.Slice(ast.Num(1), None, None), ast.Load()), #Symbol.new("cddr"): lambda p: compiler.ast.Slice(build_ast(p[1]), 0, compiler.ast.Const(2), None), Symbol.new("cdr"): lambda p: ast.Subscript(build_ast(p[1]), ast.Slice(ast.Num(1), None, None), ast.Load()), Symbol.new("cons"): lambda p: ast.BinOp(ast.List([build_ast(p[1])], ast.Load()), ast.Add(), build_ast(p[2])), #Symbol.new("append"): lambda p: ast.Call(ast.Attribute(ast.Name("functools", ast.Load()), "reduce", ast.Load()), [ast.Attribute(ast.Name("operator", ast.Load()), "add", ast.Load()), build_ast(p[1])], [], None, None), #Symbol.new("apply"): lambda p: ast.Call(build_ast(p[1]), [build_ast(p[2])], [], None, None), Symbol.new("if"): lambda p: ast.IfExp(build_ast(p[1]), build_ast(p[2]), build_ast(p[3]) if len(p) >= 4 else ast.Name("None", ast.Load())), Symbol.new("in"): lambda p: ast.Compare(build_ast(p[1]), [ast.In() for x in p[1::2]], [build_ast(x) for x in p[2::2]]), Symbol.new("index"): lambda p: ast.Subscript(build_ast(p[1]), ast.Index(build_ast(p[2])), ast.Load()), Symbol.new("lambda"): compile_lambda, Symbol.new("list"): lambda p: ast.List([build_ast(x) for x in p[1:]], ast.Load()), #Symbol.new("make-list"): lambda p: ast.Call(ast.Name("list", ast.Load()), [build_ast(x) for x in p[1:]], [], None, None), Symbol.new("not"): lambda p: ast.UnaryOp(ast.Not(), build_ast(p[1])), Symbol.new("not-in"): lambda p: ast.Compare(build_ast(p[1]), [ast.NotIn() for x in p[1::2]], [build_ast(x) for x in p[2::2]]), Symbol.new("quote"): compile_quote, Symbol.new("reverse"): lambda p: ast.Call(ast.Name("reversed", ast.Load()), [build_ast(p[1])], [], None, None), Symbol.new("set!"): lambda p: ast.Assign([ast.Name(p[1].name, ast.Store())], build_ast(p[2])), Symbol.new("slice"): lambda p: ast.Subscript(build_ast(p[1]), ast.Slice(build_ast(p[2]), build_ast(p[3]), None), ast.Load()), Symbol.new("string->symbol"): lambda p: ast.Call(ast.Name("intern"), [build_ast(p[1])], None, None, None), } def build_ast(p, tail = False): """ #>>> build_ast(parse(tokenise("(+ 2 3)"))) #Add((Const(2), Const(3))) """ if isinstance(p, list): if isinstance(p[0], Symbol): f = CompileFuncs.get(p[0])
def __init__(self, base_node): BaseMutator.__init__(self, base_node) self.original_ops = base_node.ops index_count = 0 for op in base_node.ops: if type(op) in [ ast.Gt, ast.GtE, ast.Lt, ast.LtE, ast.Eq, ast.NotEq, ast.Is, ast.IsNot, ast.In, ast.NotIn ]: if type(op) is ast.Eq: ops_mutant_Eq = copy.deepcopy(base_node.ops) ops_mutant_Eq[index_count] = ast.NotEq() self.mutations.append({"ops": ops_mutant_Eq}) if type(op) is ast.NotEq: ops_mutant_NotEq = copy.deepcopy(base_node.ops) ops_mutant_NotEq[index_count] = ast.Eq() self.mutations.append({"ops": ops_mutant_NotEq}) if type(op) is ast.LtE: ops_mutant_LtE = copy.deepcopy(base_node.ops) ops_mutant_LtE[index_count] = ast.Gt() self.mutations.append({"ops": ops_mutant_LtE}) if type(op) is ast.GtE: ops_mutant_GtE = copy.deepcopy(base_node.ops) ops_mutant_GtE[index_count] = ast.Lt() self.mutations.append({"ops": ops_mutant_GtE}) if type(op) is ast.Lt: ops_mutant_Lt = copy.deepcopy(base_node.ops) ops_mutant_Lt[index_count] = ast.GtE() self.mutations.append({"ops": ops_mutant_Lt}) if type(op) is ast.Gt: ops_mutant_Gt = copy.deepcopy(base_node.ops) ops_mutant_Gt[index_count] = ast.LtE() self.mutations.append({"ops": ops_mutant_Gt}) if type(op) is ast.Is: ops_mutant_Is = copy.deepcopy(base_node.ops) ops_mutant_Is[index_count] = ast.IsNot() self.mutations.append({"ops": ops_mutant_Is}) if type(op) is ast.IsNot: ops_mutant_IsNot = copy.deepcopy(base_node.ops) ops_mutant_IsNot[index_count] = ast.Is() self.mutations.append({"ops": ops_mutant_IsNot}) if type(op) is ast.In: ops_mutant_In = copy.deepcopy(base_node.ops) ops_mutant_In[index_count] = ast.NotIn() self.mutations.append({"ops": ops_mutant_In}) if type(op) is ast.NotIn: ops_mutant_NotIn = copy.deepcopy(base_node.ops) ops_mutant_NotIn[index_count] = ast.In() self.mutations.append({"ops": ops_mutant_NotIn}) index_count += 1
import ast from django.template import defaulttags from . import generator_flt_expr, ast_builder OPERATORS = { 'or': lambda x, y: ast.BoolOp(op=ast.Or(), values=[x, y]), 'and': lambda x, y: ast.BoolOp(op=ast.And(), values=[x, y]), 'not': lambda x: ast.UnaryOp(op=ast.Not(), operand=x), 'in': lambda x, y: ast.Compare(left=x, ops=[ast.In()], comparators=[y]), 'not in': lambda x, y: ast.Compare(left=x, ops=[ast.NotIn()], comparators=[y]), '=': lambda x, y: ast.Compare(left=x, ops=[ast.Eq()], comparators=[y]), '==': lambda x, y: ast.Compare(left=x, ops=[ast.Eq()], comparators=[y]), '!=': lambda x, y: ast.Compare(left=x, ops=[ast.NotEq()], comparators=[y]), '>': lambda x, y: ast.Compare(left=x, ops=[ast.Gt()], comparators=[y]), '>=': lambda x, y: ast.Compare(left=x, ops=[ast.GtE()], comparators=[y]), '<': lambda x, y: ast.Compare(left=x, ops=[ast.Lt()], comparators=[y]), '<=': lambda x, y: ast.Compare(left=x, ops=[ast.LtE()], comparators=[y]), } def generate_condition(condition, state): need_try_catch = condition.id in OPERATORS cond = _do_generate_condition(condition, state) if need_try_catch: try_stmt = ast_builder.build_stmt( state, lambda b: b.try_(b.return_(cond),
def not_in(self, other: Any) -> Compare: # not a dunder, use with Expr.not_in(a, other) return Compare(self, [ast.NotIn()], [other])
def not_in(self, left, right): return ast.Compare(left=to_ast(left), ops=[ast.NotIn()], comparators=[to_ast(right)])
def visit_Compare(self, node): if isinstance(node.ops, ast.LtE): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = [ast.Gt()] print('changing lessThanEqual {} to greaterThan.'.format( self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.GtE): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.op = [ast.Lt()] print('changing GTE {} to LT.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.Gt): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = [ast.LtE()] print('changing GT {} to LtE.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.Lt): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = [ast.GtE()] print('changing LT {} to GtE.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.Eq): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = [ast.NotEq()] print('changing eq {} to neq.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.NotEq): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = ast.Eq() print('changing neq {} to eq.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.Is): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = ast.IsNot() print('changing is {} to isnot.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.IsNot): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = ast.Is() print('changing isnot {} to is.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.In): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = ast.NotIn() print('changing In {} to InNot.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging if isinstance(node.ops, ast.NotIn): self.counter += 1 if self.counter == self.nodeToMutate: new_node = ast.Compare() new_node.left = node.left new_node.comparators = node.comparators new_node.ops = ast.In() print('changing Notin {} to in.'.format(self.counter)) return ast.copy_location(new_node, node) # helps debugging return self.generic_visit(node)
def _generate_init_file(services, modules): """Generate the __init__.py file which contains the ServicsMixin for the client. This is mostly to automate the addition of new services. """ nodes = [] nodes.append( ast.Import(names=[ast.alias(name="typing", asname=None)], level=0)) # Collect all submodules submodules = {} for service in services.values(): module_name = snakeit(service.context_name) service_name = service.context_name + "Service" info = modules[module_name] key = ".%s" % info["name"] submodules[key] = { "module_name": info["name"], "class_name": service.context_name + "Service", "var_name": snakeit(service.context_name), } # Add manual generated files (TODO) submodules[".project"] = { "module_name": "project", "class_name": "ProjectService", "var_name": "project", } # Generate TYPE_CHECKING import statements (these will be sorted by isort). if_node = ast.If( test=ast.Attribute(value=ast.Name(id="typing"), attr="TYPE_CHECKING"), body=[], orelse=[], ) nodes.append(if_node) for name, service in submodules.items(): node = ast.ImportFrom( module=name, names=[ast.alias(name=service["class_name"], asname=None)], level=0, ) if_node.body.append(node) # Import ClientProtocol node = ast.ImportFrom( module="commercetools.protocols", names=[ast.alias(name="ClientProtocol", asname=None)], level=0, ) if_node.body.append(node) module_varnames = sorted(submodules.values(), key=operator.itemgetter("var_name")) # Return the class + properties class_node = ast.ClassDef(name="ServicesMixin", bases=[], keywords=[], decorator_list=[], body=[]) for name, service in submodules.items(): fname = service["module_name"] node = ast.FunctionDef( name=fname, args=ast.arguments( args=[ ast.arg(arg="self", annotation=ast.Str(s="ClientProtocol", kind=None)) ], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[], ), body=[], decorator_list=[ast.Name(id="property")], returns=ast.Str(s=service["class_name"], kind=None), ) if_stmt = ast.If( test=ast.Compare( left=ast.Constant(value=fname, kind=None), ops=[ast.NotIn()], comparators=[ ast.Attribute(value=ast.Name(id="self"), attr="__dict__") ], ), body=[], orelse=[], ) if_stmt.body.append( ast.ImportFrom( module=name, names=[ast.alias(name=service["class_name"], asname=None)], level=0, )) if_stmt.body.append( ast.Assign( targets=[ ast.Subscript( value=ast.Attribute(value=ast.Name(id="self"), attr="__dict__"), slice=ast.Index( value=ast.Constant(value=fname, kind=None)), ) ], value=ast.Call( func=ast.Name(id=service["class_name"]), args=[ast.Name(id="self")], keywords=[], ), )) node.body.append(if_stmt) node.body.append( ast.Return(value=ast.Subscript( value=ast.Attribute(value=ast.Name( id="self"), attr="__dict__"), slice=ast.Index(value=ast.Constant(value=fname, kind=None)), ))) class_node.body.append(node) nodes.append(class_node) return ast.Module(body=nodes)
def not_cmp_trans(self, op): trans=dict([(ast.Eq,ast.Eq()),(ast.NotEq,ast.NotEq()),(ast.Lt,ast.Lt()),(ast.Gt,ast.Gt()),(ast.LtE,ast.LtE()),(ast.GtE,ast.GtE()),(ast.In,ast.In()),(ast.NotIn,ast.NotIn())]) v = [(ast.Eq, ast.NotEq), (ast.Lt, ast.GtE), (ast.Gt, ast.LtE), (ast.Is, ast.IsNot), (ast.In, ast.NotIn)] vv = v + [(j,i) for (i,j) in v] d = dict(vv) return trans[d[type(op)]]
def test_empty_init(self): # Jython 2.5.0 did not allow empty constructors for many ast node types # but CPython ast nodes do allow this. For the moment, I don't see a # reason to allow construction of the super types (like ast.AST and # ast.stmt) as well as the op types that are implemented as enums in # Jython (like boolop), but I've left them in but commented out for # now. We may need them in the future since CPython allows this, but # it may fall under implementation detail. #ast.AST() ast.Add() ast.And() ast.Assert() ast.Assign() ast.Attribute() ast.AugAssign() ast.AugLoad() ast.AugStore() ast.BinOp() ast.BitAnd() ast.BitOr() ast.BitXor() ast.BoolOp() ast.Break() ast.Call() ast.ClassDef() ast.Compare() ast.Continue() ast.Del() ast.Delete() ast.Dict() ast.Div() ast.Ellipsis() ast.Eq() ast.Exec() ast.Expr() ast.Expression() ast.ExtSlice() ast.FloorDiv() ast.For() ast.FunctionDef() ast.GeneratorExp() ast.Global() ast.Gt() ast.GtE() ast.If() ast.IfExp() ast.Import() ast.ImportFrom() ast.In() ast.Index() ast.Interactive() ast.Invert() ast.Is() ast.IsNot() ast.LShift() ast.Lambda() ast.List() ast.ListComp() ast.Load() ast.Lt() ast.LtE() ast.Mod() ast.Module() ast.Mult() ast.Name() ast.Not() ast.NotEq() ast.NotIn() ast.Num() ast.Or() ast.Param() ast.Pass() ast.Pow() ast.Print() ast.RShift() ast.Raise() ast.Repr() ast.Return() ast.Slice() ast.Store() ast.Str() ast.Sub() ast.Subscript() ast.Suite() ast.TryExcept() ast.TryFinally() ast.Tuple() ast.UAdd() ast.USub() ast.UnaryOp() ast.While() ast.With() ast.Yield() ast.alias() ast.arguments() #ast.boolop() #ast.cmpop() ast.comprehension() #ast.excepthandler() #ast.expr() #ast.expr_context() ast.keyword()
def test_operators(self): boolop0 = ast.BoolOp() boolop1 = ast.BoolOp(ast.And(), [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())]) boolop2 = ast.BoolOp(ast.And(), [ ast.Name('True', ast.Load()), ast.Name('False', ast.Load()), ast.Name('a',ast.Load())], 0, 0) binop0 = ast.BinOp() binop1 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3)) binop2 = ast.BinOp(ast.Str('xy'), ast.Mult(), ast.Num(3), 0, 0) unaryop0 = ast.UnaryOp() unaryop1 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load())) unaryop2 = ast.UnaryOp(ast.Not(), ast.Name('True',ast.Load()), 0, 0) lambda0 = ast.Lambda() lambda1 = ast.Lambda(ast.arguments([ast.Name('x', ast.Param())], None, None, []), ast.Name('x', ast.Load())) ifexp0 = ast.IfExp() ifexp1 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0)) ifexp2 = ast.IfExp(ast.Name('True',ast.Load()), ast.Num(1), ast.Num(0), 0, 0) dict0 = ast.Dict() dict1 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')]) dict2 = ast.Dict([ast.Num(1), ast.Num(2)], [ast.Str('a'), ast.Str('b')], 0, 0) set0 = ast.Set() set1 = ast.Set([ast.Num(1), ast.Num(2)]) set2 = ast.Set([ast.Num(1), ast.Num(2)], 0, 0) lc0 = ast.ListComp() lc1 = ast.ListComp( ast.Name('x',ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])]) lc2 = ast.ListComp( ast.Name('x',ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])], 0, 0) setcomp0 = ast.SetComp() setcomp1 = ast.SetComp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Str('abracadabra'), [ast.Compare(ast.Name('x', ast.Load()), [ast.NotIn()], [ast.Str('abc')])])]) comprehension0 = ast.comprehension() comprehension1 = ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), []) # "{i : chr(65+i) for i in (1,2)}") dictcomp0 = ast.DictComp() dictcomp1 = ast.DictComp(ast.Name('i', ast.Load()), ast.Call(ast.Name('chr', ast.Load()), [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))], [], None, None), [ast.comprehension(ast.Name('i', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])]) dictcomp2 = ast.DictComp(ast.Name('i', ast.Load()), ast.Call(ast.Name('chr', ast.Load()), [ast.BinOp(ast.Num(65), ast.Add(), ast.Name('i', ast.Load()))], [], None, None), [ast.comprehension(ast.Name('i', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(n=2)], ast.Load()), [])],0,0) # (x for x in (1,2)) genexp0 = ast.GeneratorExp() genexp1 = ast.GeneratorExp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])]) genexp2 = ast.GeneratorExp(ast.Name('x', ast.Load()), [ast.comprehension(ast.Name('x', ast.Store()), ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()), [])],0,0) # yield 2 yield0 = ast.Yield() yield1 = ast.Yield(ast.Num(2)) yield2 = ast.Yield(ast.Num(2),0,0) yield20 = ast.Yield(lineno=0, col_offset=0) # a>0 compare0 = ast.Compare() compare1 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)]) compare2 = ast.Compare(ast.Name('a', ast.Load()), [ast.Gt()], [ast.Num(0)],0,0) # chr(65) call0 = ast.Call() call1 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None) call2 = ast.Call(ast.Name('chr', ast.Load()), [ast.Num(65)], [], None, None, 0, 0) call20 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], []) call21 = ast.Call(ast.Name('f', ast.Load()), [ast.Num(0)], [], lineno=0, col_offset=0) # 0 num0 = ast.Num() num1 = ast.Num(0) num2 = ast.Num(0,0,0) # "foo" str0 = ast.Str() str1 = ast.Str("foo") str2 = ast.Str("foo",0,0) # TODO: come back repr0 = ast.Repr() repr1 = ast.Repr(ast.Num(0)) repr2 = ast.Repr(ast.Num(0),0,0) # foo.bar attr0 = ast.Attribute() attr1 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load()) attr2 = ast.Attribute(ast.Name('foo', ast.Load()), 'bar', ast.Load(), 0,0) # a[1:2] subscript0 = ast.Subscript() subscript1 = ast.Subscript(ast.Name('a', ast.Load()), ast.Slice(ast.Num(1), ast.Num(2)), ast.Load()) subscript2 = ast.Subscript(ast.Name('a', ast.Load()), ast.ExtSlice([ast.Num(1), ast.Num(2)]), ast.Load(), 0, 0) # name name0 = ast.Name() name1 = ast.Name("name", ast.Load()) name2 = ast.Name("name", ast.Load(),0,0) # [1,2] list0 = ast.List() list1 = ast.List([ast.Num(1), ast.Num(2)], ast.Load()) list2 = ast.List([ast.Num(1), ast.Num(2)], ast.Load(),0,0) # (1,2) tuple0 = ast.Tuple() tuple1 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load()) tuple2 = ast.Tuple([ast.Num(1), ast.Num(2)], ast.Load(), 0, 0)
def as_ast(dct): """See https://docs.python.org/2/library/ast.html""" if dct['ast_type'] == "Module": return ast.Module(dct["body"]) elif dct['ast_type'] == "Interactive": return ast.Interactive(dct["body"]) elif dct['ast_type'] == "Expression": return ast.Expression(dct["body"]) elif dct['ast_type'] == "Suite": return ast.Suite(dct["body"]) elif dct['ast_type'] == "FunctionDef": return ast.FunctionDef(dct["name"], dct["args"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "ClassDef": return ast.ClassDef(dct["name"], dct["bases"], dct["body"], dct["decorator_list"]) elif dct['ast_type'] == "Return": return ast.Return(dct["value"]) elif dct['ast_type'] == "Delete": return ast.Delete(dct["targets"]) elif dct['ast_type'] == "Assign": return ast.Assign(dct["targets"], dct["value"]) elif dct['ast_type'] == "AugAssign": return ast.AugAssign(dct["target"], dct["op"], dct["value"]) elif dct['ast_type'] == "Print": return ast.Print(dct["dest"], dct["values"], dct["nl"]) elif dct['ast_type'] == "For": return ast.For(dct["target"], dct["iter"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "While": return ast.While(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "If": return ast.If(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "With": return ast.With(dct["context_expr"], dct["optional_vars"], dct["body"]) elif dct['ast_type'] == "Raise": return ast.Raise(dct["type"], dct["inst"], dct["tback"]) elif dct['ast_type'] == "TryExcept": return ast.TryExcept(dct["body"], dct["handlers"], dct["orelse"]) elif dct['ast_type'] == "TryFinally": return ast.TryFinally(dct["body"], dct["finalbody"]) elif dct['ast_type'] == "Assert": return ast.Assert(dct["test"], dct["msg"]) elif dct['ast_type'] == "Import": return ast.Import(dct["names"]) elif dct['ast_type'] == "ImportFrom": return ast.ImportFrom(dct["module"], dct["names"], dct["level"]) elif dct['ast_type'] == "Exec": return ast.Exec(dct["body"], dct["globals"], dct["locals"]) elif dct['ast_type'] == "Global": return ast.Global(dct["names"]) elif dct['ast_type'] == "Expr": return ast.Expr(dct["value"]) elif dct['ast_type'] == "Pass": return ast.Pass() elif dct['ast_type'] == "Break": return ast.Break() elif dct['ast_type'] == "Continue": return ast.Continue() elif dct['ast_type'] == "BoolOp": return ast.BoolOp(dct["op"], dct["values"]) elif dct['ast_type'] == "BinOp": return ast.BinOp(dct["left"], dct["op"], dct["right"]) elif dct['ast_type'] == "UnaryOp": return ast.UnaryOp(dct["op"], dct["operand"]) elif dct['ast_type'] == "Lambda": return ast.Lambda(dct["args"], dct["body"]) elif dct['ast_type'] == "IfExp": return ast.IfExp(dct["test"], dct["body"], dct["orelse"]) elif dct['ast_type'] == "Dict": return ast.Dict(dct["keys"], dct["values"]) elif dct['ast_type'] == "Set": return ast.Set(dct["elts"]) elif dct['ast_type'] == "ListComp": return ast.ListComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "SetComp": return ast.SetComp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "DictComp": return ast.DictComp(dct["key"], dct["value"], dct["generators"]) elif dct['ast_type'] == "GeneratorExp": return ast.GeneratorExp(dct["elt"], dct["generators"]) elif dct['ast_type'] == "Yield": return ast.Yield(dct["value"]) elif dct['ast_type'] == "Compare": return ast.Compare(dct["left"], dct["ops"], dct["comparators"]) elif dct['ast_type'] == "Call": return ast.Call(dct["func"], dct["args"], dct["keywords"], dct["starargs"], dct["kwargs"]) elif dct['ast_type'] == "Repr": return ast.Repr(dct["value"]) elif dct['ast_type'] == "Num": return ast.Num(dct["n"]) elif dct['ast_type'] == "Str": # Converting to ASCII return ast.Str(dct["s"].encode('ascii', 'ignore')) elif dct['ast_type'] == "Attribute": return ast.Attribute(dct["value"], dct["attr"], dct["ctx"]) elif dct['ast_type'] == "Subscript": return ast.Subscript(dct["value"], dct["slice"], dct["ctx"]) elif dct['ast_type'] == "Name": return ast.Name(dct["id"], dct["ctx"]) elif dct['ast_type'] == "List": return ast.List(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Tuple": return ast.Tuple(dct["elts"], dct["ctx"]) elif dct['ast_type'] == "Load": return ast.Load() elif dct['ast_type'] == "Store": return ast.Store() elif dct['ast_type'] == "Del": return ast.Del() elif dct['ast_type'] == "AugLoad": return ast.AugLoad() elif dct['ast_type'] == "AugStore": return ast.AugStore() elif dct['ast_type'] == "Param": return ast.Param() elif dct['ast_type'] == "Ellipsis": return ast.Ellipsis() elif dct['ast_type'] == "Slice": return ast.Slice(dct["lower"], dct["upper"], dct["step"]) elif dct['ast_type'] == "ExtSlice": return ast.ExtSlice(dct["dims"]) elif dct['ast_type'] == "Index": return ast.Index(dct["value"]) elif dct['ast_type'] == "And": return ast.And() elif dct['ast_type'] == "Or": return ast.Or() elif dct['ast_type'] == "Add": return ast.Add() elif dct['ast_type'] == "Sub": return ast.Sub() elif dct['ast_type'] == "Mult": return ast.Mult() elif dct['ast_type'] == "Div": return ast.Div() elif dct['ast_type'] == "Mod": return ast.Mod() elif dct['ast_type'] == "Pow": return ast.Pow() elif dct['ast_type'] == "LShift": return ast.LShift() elif dct['ast_type'] == "RShift": return ast.RShift() elif dct['ast_type'] == "BitOr": return ast.BitOr() elif dct['ast_type'] == "BitXor": return ast.BitXor() elif dct['ast_type'] == "BitAnd": return ast.BitAnd() elif dct['ast_type'] == "FloorDiv": return ast.FloorDiv() elif dct['ast_type'] == "Invert": return ast.Invert() elif dct['ast_type'] == "Not": return ast.Not() elif dct['ast_type'] == "UAdd": return ast.UAdd() elif dct['ast_type'] == "USub": return ast.USub() elif dct['ast_type'] == "Eq": return ast.Eq() elif dct['ast_type'] == "NotEq": return ast.NotEq() elif dct['ast_type'] == "Lt": return ast.Lt() elif dct['ast_type'] == "LtE": return ast.LtE() elif dct['ast_type'] == "Gt": return ast.Gt() elif dct['ast_type'] == "GtE": return ast.GtE() elif dct['ast_type'] == "Is": return ast.Is() elif dct['ast_type'] == "IsNot": return ast.IsNot() elif dct['ast_type'] == "In": return ast.In() elif dct['ast_type'] == "NotIn": return ast.NotIn() elif dct['ast_type'] == "comprehension": return ast.comprehension(dct["target"], dct["iter"], dct["ifs"]) elif dct['ast_type'] == "ExceptHandler": return ast.ExceptHandler(dct["type"], dct["name"], dct["body"]) elif dct['ast_type'] == "arguments": return ast.arguments(dct["args"], dct["vararg"], dct["kwarg"], dct["defaults"]) elif dct['ast_type'] == "keyword": return ast.keyword(dct["arg"], dct["value"]) elif dct['ast_type'] == "alias": return ast.alias(dct["name"], dct["asname"]) else: return dct
from hypothesis._strategies import booleans from hypothesis.searchstrategy import SearchStrategy from hypothesis.strategies import integers, lists, binary, sampled_from, recursive, dictionaries from hypothesis.strategies import text, composite, one_of, floats, complex_numbers, characters, none comparison_operators = sampled_from([ ast.Eq(), ast.NotEq(), ast.Lt(), ast.LtE(), ast.Gt(), ast.GtE(), ast.Is(), ast.IsNot(), ast.In(), ast.NotIn() ]) # region: Literals @composite def Num(draw) -> ast.AST: def to_node(n) -> ast.AST: if isinstance(n, int): return ast.Num(n) if n >= 0 else ast.UnaryOp( ast.USub(), ast.Num(abs(n))) elif isinstance(n, float): return ast.Num(n) if math.copysign(1.0, n) > 0.0 else ast.UnaryOp( ast.USub(), ast.Num(abs(n))) elif isinstance(n, complex):