def test_visit_UnaryOp(self, get_logger_mock): node = MagicMock() node.operand = "operand" node.op = ast.Not() analyzer = ExpressionAnalyzer() self.assertIsNone(analyzer.visit_UnaryOp(node)) self.assertEqual(analyzer.parts, ["not", " ", "operand"]) node.op = ast.Invert() analyzer = ExpressionAnalyzer() self.assertIsNone(analyzer.visit_UnaryOp(node)) self.assertEqual(analyzer.parts, ["~", "operand"]) node.op = ast.UAdd() analyzer = ExpressionAnalyzer() self.assertIsNone(analyzer.visit_UnaryOp(node)) self.assertEqual(analyzer.parts, ["+", "operand"]) node.op = ast.USub() analyzer = ExpressionAnalyzer() self.assertIsNone(analyzer.visit_UnaryOp(node)) self.assertEqual(analyzer.parts, ["-", "operand"]) node.op = ast.Import() analyzer = ExpressionAnalyzer() self.assertIsNone(analyzer.visit_UnaryOp(node)) self.assertEqual(analyzer.parts, ["...", "operand"]) get_logger_mock().warning.assert_called_once_with(ANY)
def visit_Call(self, node): 'Replace custom function with bitwise operators' self.generic_visit(node) if isinstance(node.func, ast.Name): if len(node.args) == 2: if node.func.id == "mand": op = ast.BitAnd() elif node.func.id == "mxor": op = ast.BitXor() elif node.func.id == "mor": op = ast.BitOr() elif node.func.id == "mlshift": op = ast.LShift() elif node.func.id == "mrshift": op = ast.RShift() else: return node return ast.BinOp(node.args[0], op, node.args[1]) elif len(node.args) == 1 and node.func.id == "mnot": arg = node.args[0] self.generic_visit(node) return ast.UnaryOp(ast.Invert(), arg) return self.generic_visit(node)
def p_factor(p): '''factor : "+" factor | "-" factor | "~" factor | power''' if len(p) == 2: p[0] = p[1] else: item = p.get_item(1) if p[1] == "+": p[0] = ast.UnaryOp(op=ast.UAdd(), operand=p[2], lineno=item.lineno, col_offset=item.lexpos) elif p[1] == "-": p[0] = ast.UnaryOp(op=ast.USub(), operand=p[2], lineno=item.lineno, col_offset=item.lexpos) elif p[1] == "~": p[0] = ast.UnaryOp(op=ast.Invert(), operand=p[2], lineno=item.lineno, col_offset=item.lexpos) return
def visit_unaryop(self, node): # Converts 'not' into '~' which NumExpr supports if isinstance(node.op, ast.Not): return ast.UnaryOp(op=ast.Invert(), operand=self.visit(node.operand)) elif isinstance(node.op, ast.USub): return node raise NotImplementedError(type(node.op))
def test_UnaryOp(self): self.verify(ast.UnaryOp(ast.Invert(), ast.Num(42)), '~42') self.verify(ast.UnaryOp(ast.Not(), ast.Num(42)), 'not 42') self.verify(ast.UnaryOp(ast.UAdd(), ast.Num(42)), '+42') self.verify(ast.UnaryOp(ast.USub(), ast.Num(42)), '-42') precedence = ast.UnaryOp( ast.Not(), ast.BoolOp(ast.Or(), [ast.Num(n=1), ast.Num(2)])) self.verify(precedence, 'not (1 or 2)')
def test_unaryop(self): def f(a): return ~a expected = G(C(START_TICK, "a", 1, "value"), T(1, tasks.UnaryOpTask(ast.Invert()), {'quick': True}), C(1, "value", FINAL_TICK, "retval")) callee = translator.translate_function(f, "scheduler", False) utils.assert_graph_equal(expected, callee.graph)
def unary_action(s, loc, tokens): op_char, operand = tokens[0], tokens[1] if op_char == '+': return operand elif op_char == '-': return ast.UnaryOp(op=ast.USub(), operand=operand) elif op_char == '~': return ast.UnaryOp(op=ast.Invert(), operand=operand) else: # not return ast.UnaryOp(op=ast.Not(), operand=operand)
def to_node(self): value = self.value operator = self.operator if operator in ('+', 'plus'): return ast.UnaryOp(ast.UAdd(), value.to_node()) elif operator in ('-', 'minus'): return ast.UnaryOp(ast.USub(), value.to_node()) elif operator == '~': return ast.UnaryOp(ast.Invert(), value.to_node()) else: return value.to_node()
def check_not(self, target, pattern): 'Check NOT pattern node that could be in another form' if self.is_wildcard(pattern.operand): wkey = pattern.operand.id if isinstance(target, ast.Num): if wkey not in self.wildcards: mod = 2**self.nbits self.wildcards[wkey] = ast.Num((~target.n) % mod) return True else: wilds2 = self.wildcards[pattern.operand.id] num = ast.Num((~target.n) % 2**self.nbits) return asttools.Comparator().visit(wilds2, num) else: if wkey not in self.wildcards: self.wildcards[wkey] = ast.UnaryOp(ast.Invert(), target) return True return self.check_eq_z3(target, pattern) else: subpattern = pattern.operand newtarget = ast.UnaryOp(ast.Invert(), target) return self.check_eq_z3(newtarget, subpattern)
def p_expr_unaryop(self, p): '''expr : MINUS expr %prec UNARYOP | TILDE expr %prec UNARYOP | NOT expr %prec UNARYOP ''' op = None if p[1] == '-': op = ast.USub() elif p[1] == '~': op = ast.Invert() elif p[1] == 'not': op = ast.Not() p[0] = ast.UnaryOp(op=op, operand=p[2])
def visit_Compare(self, node): # TODO: Implement or/and. if len(node.ops) > 1: return node if isinstance(node.ops[0], ast.In): return ast.BinOp(left=node.left, op=ast.LShift(), right=node.comparators[0]) if isinstance(node.ops[0], ast.Eq): return ast.BinOp(left=node.left, op=ast.LShift(), right=ast.Set(node.comparators)) if isinstance(node.ops[0], ast.NotIn): node_copy = copy.deepcopy(node) node_copy.ops[0] = ast.In() return ast.UnaryOp(op=ast.Invert(), operand=self.visit_Compare(node_copy)) if isinstance(node.ops[0], ast.NotEq): node_copy = copy.deepcopy(node) node_copy.ops[0] = ast.Eq() return ast.UnaryOp(op=ast.Invert(), operand=self.visit_Compare(node_copy)) return node
def visit_UnaryOp(self, node): # unaryop = Invert | Not | UAdd | USub print(" in MyTransformer.visit_UnaryOp()") print(" node =", node) print(" op =", node.op) curr_op = node.op unary_negate = node.op if isinstance(curr_op, ast.UAdd): unary_negate = ast.USub() elif isinstance(curr_op, ast.USub): unary_negate = ast.UAdd() elif isinstance(curr_op, ast.Invert): unary_negate = ast.Not() elif isinstance(curr_op, ast.Not): unary_negate = ast.Invert() else: print(" did not find negation for", curr_op) print(" negation =", unary_negate) # create negated node | UnaryOp(unaryop op, expr operand) new_node = node new_node.op = unary_negate ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) return new_node
def __invert__(self): return self.apply(ast.Invert())
def __invert__(self) -> Column: ''' Invert, or logical NOT operation. ''' from .utils import _term_to_ast return Column( type(bool), ast.UnaryOp(op=ast.Invert(), operand=_term_to_ast(self, self)))
def __invert__(self) -> DataFrame: ''' Invert, or logical NOT operation. ''' self._test_for_extension('operator invert') child_expr = ast.UnaryOp(op=ast.Invert(), operand=ast_DataFrame(self)) return DataFrame(child_expr)
def make_invert(cursor_trail, tree): selected_node = core_logic.get_node_at_cursor(cursor_trail, tree) expr = selected_node if isinstance(selected_node, ast.expr) else make_expression() return ast.UnaryOp(op=ast.Invert(), operand=expr)
def visit_Not(self, node): return ast.Invert()
def factor(self, p): return ast.UnaryOp(op=ast.Invert(), operand=p.factor)
def not_test(self, p): return ast.UnaryOp(op=ast.Invert(), operand=p.not_test)
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 UnaryOp(draw, expression) -> ast.UnaryOp: op = draw(sampled_from([ast.USub(), ast.UAdd(), ast.Not(), ast.Invert()])) l = draw(expression) return ast.UnaryOp(op, l)
def visit_Not(self, node): self.generic_visit(node) return ast.Invert()
def test_UnaryInvert(self): py_ast = ast.UnaryOp(ast.Invert(), ast.Name('i', ast.Load())) c_ast = BitNot(SymbolRef('i')) self._check(py_ast, c_ast)
def visit_Not(self, node: ast.Not) -> ast.Invert: return ast.Invert()
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
def translateExpr(n): if isinstance(n, Token): if n.type.name == 'INTEGER': if n.arg > 2**63: return ast.NameConstant('__BIGINT__') return ast.Num(n.arg) if n.type.name == 'FLOAT': return ast.Num(n.arg) if n.type.name == 'IDENT': return ast.NameConstant(mungeName(n.arg)) if n.type.name == 'FALSE': return ast.NameConstant('False') if n.type.name == 'TRUE': return ast.NameConstant('True') if n.type.name == 'BITSTRING': if n.arg.find('x') >= 0: return ast.Call(ast.NameConstant('__BITSTRING_MATCH__'), [ast.Str(n.arg)], []) return ast.NameConstant('0b' + n.arg) raise Exception('q %s' % n) if isinstance(n, Type): return ast.Num(800) if n.type == 'e-land': return ast.BoolOp( ast.And(), [translateExpr(n.children[0]), translateExpr(n.children[1])]) elif n.type == 'e-lor': return ast.BoolOp( ast.Or(), [translateExpr(n.children[0]), translateExpr(n.children[1])]) elif n.type == 'e-eq': return ast.Compare(translateExpr(n.children[0]), [ast.Eq()], [translateExpr(n.children[1])]) elif n.type == 'e-ne': return ast.Compare(translateExpr(n.children[0]), [ast.NotEq()], [translateExpr(n.children[1])]) elif n.type == 'e-call': return ast.Call(translateExpr(n.children[0]), [translateExpr(x) for x in n.children[1]], []) elif n.type == 'e-range': return ast.Subscript(translateExpr(n.children[0]), translateRange(n.children[1]), None) elif n.type == 'e-not': return ast.UnaryOp(ast.Not(), translateExpr(n.children[0])) elif n.type == 'e-negate': return ast.UnaryOp(ast.USub(), translateExpr(n.children[0])) elif n.type == 'e-add': return ast.BinOp(translateExpr(n.children[0]), ast.Add(), translateExpr(n.children[1])) elif n.type == 'e-sub': return ast.BinOp(translateExpr(n.children[0]), ast.Sub(), translateExpr(n.children[1])) elif n.type == 'e-mul': return ast.BinOp(translateExpr(n.children[0]), ast.Mult(), translateExpr(n.children[1])) elif n.type == 'e-div': return ast.BinOp(translateExpr(n.children[0]), ast.FloorDiv(), translateExpr(n.children[1])) elif n.type == 'e-fdiv': return ast.BinOp(translateExpr(n.children[0]), ast.Div(), translateExpr(n.children[1])) elif n.type == 'e-rem': return ast.BinOp(translateExpr(n.children[0]), ast.Mod(), translateExpr(n.children[1])) elif n.type == 'e-lt': return ast.Compare(translateExpr(n.children[0]), [ast.Lt()], [translateExpr(n.children[1])]) elif n.type == 'e-le': return ast.Compare(translateExpr(n.children[0]), [ast.LtE()], [translateExpr(n.children[1])]) elif n.type == 'e-gt': return ast.Compare(translateExpr(n.children[0]), [ast.Gt()], [translateExpr(n.children[1])]) elif n.type == 'e-ge': return ast.Compare(translateExpr(n.children[0]), [ast.GtE()], [translateExpr(n.children[1])]) elif n.type == 'e-lsh': return ast.BinOp(translateExpr(n.children[0]), ast.LShift(), translateExpr(n.children[1])) elif n.type == 'e-rsh': return ast.BinOp(translateExpr(n.children[0]), ast.RShift(), translateExpr(n.children[1])) elif n.type == 'e-eor': return ast.BinOp(translateExpr(n.children[0]), ast.BitXor(), translateExpr(n.children[1])) elif n.type == 'e-or': return ast.BinOp(translateExpr(n.children[0]), ast.BitOr(), translateExpr(n.children[1])) elif n.type == 'e-and': return ast.BinOp(translateExpr(n.children[0]), ast.BitAnd(), translateExpr(n.children[1])) elif n.type == 'e-ternary': return ast.IfExp(translateExpr(n.children[0]), translateExpr(n.children[1]), translateExpr(n.children[2])) elif n.type == 'e-subrange': return ast.Num(113) elif n.type == 'e-lnegate': return ast.UnaryOp(ast.Invert(), translateExpr(n.children[0])) elif n.type == 'e-subscript': return ast.Attribute(translateExpr(n.children[0]), n.children[1].arg, None) elif n.type == 'e-indexempty': return ast.Subscript(translateExpr(n.children[0]), ast.Slice(None, None, None), None) elif n.type == 'e-implementation-defined': return ast.Call(ast.NameConstant('IMPLEMENTATION_DEFINED'), [], []) elif n.type == 'e-exp': return ast.BinOp(translateExpr(n.children[0]), ast.Pow(), translateExpr(n.children[1])) elif n.type == 'e-mod': return ast.BinOp(translateExpr(n.children[0]), ast.Mod(), translateExpr(n.children[1])) elif n.type == 'e-unknown': return ast.Call(ast.NameConstant('UNKNOWN'), [], []) elif n.type == 'e-index': return ast.Subscript(translateExpr(n.children[0]), ast.Slice(None, None, None), None) elif n.type == 'e-tuple': return ast.Tuple([translateExpr(x) for x in n.children[0]], None) elif n.type == 'e-set-in': return ast.Num(114) elif n.type == 'e-concat': return ast.Call( ast.NameConstant('_CONCAT_'), [translateExpr(n.children[0]), translateExpr(n.children[1])], []) elif n.type == 'tuple-nomatch': return ast.NameConstant('_') elif n.type == 'range': return ast.Num(122) #return translateRange(n) else: raise Exception('unknown node type: %s' % (n, )) return ast.Num(42)