Beispiel #1
0
def p_if_else_stmt(t):
    '''stmt : IF expression ELSE suite'''
    pass_ast = ast.Pass()
    pass_ast.lineno = t.lineno(1)
    pass_ast.col_offset = -1 # XXX
    t[0] = ast.If(t[2], [pass_ast], t[4])
    t[0].lineno = t.lineno(4)
    t[0].col_offset = -1  # XXX
Beispiel #2
0
    def pre_If(self):
        if hasattr(self.cur_node, 'custom_id') \
                and self.cur_node.custom_id == self.target \
                and MergeNestedIfStatement.is_applicable(self.cur_node):
            self.applied = True
            parent_list = self.parent
            cur_index = self.parent.index(self.cur_node)

            first_if = self.cur_node
            first_test = first_if.test
            first_body = first_if.body
            first_else = first_if.orelse

            second_if = [
                node for node in first_body if isinstance(node, ast.If)
            ][0]
            second_index = first_body.index(second_if)
            second_test = second_if.test
            second_body = second_if.body
            second_else = second_if.orelse
            results = list()

            if first_body[:second_index]:
                results.append(
                    ast.If(first_test, first_body[:second_index], []))

            if second_body:
                a_and_b = ast.BoolOp(ast.And(), [first_test, second_test])
                if_a_and_b = ast.If(a_and_b, second_body, [])
                results.append(if_a_and_b)
                if second_else:
                    a_and_not_b = ast.BoolOp(
                        ast.And(),
                        [first_test,
                         ast.UnaryOp(ast.Not(), second_test)])
                    if_a_and_not_b = ast.If(a_and_not_b, second_else, [])
                    if_a_and_b.orelse = [if_a_and_not_b]
            if first_body[second_index + 1:]:
                results.append(
                    ast.If(first_test, first_body[second_index + 1:],
                           first_else))
            elif first_else:
                results.append(
                    ast.If(ast.UnaryOp(ast.Not(), first_test), first_else, []))
            self.parent[cur_index:cur_index + 1] = results
Beispiel #3
0
 def visit_If(self, node):
     new_body = self.if_exists(node.body, self.visit_list)
     new_orelse = self.if_exists(node.orelse, self.visit_list)
     new_test = self.visit(node.test)
     return_list = self.new_stmts + [
         ast.copy_location(ast.If(new_test, new_body, new_orelse), node)
     ]
     self.new_stmts.clear()
     return return_list
Beispiel #4
0
def make_if(kwd, test, then, *rest):
    # (This'd be simpler with a different form of the grammar.)
    test = test(ast.Load())
    if not rest:         else_ = []
    elif len(rest) == 1: else_ = rest[0]
    else:                else_ = [make_if(*rest)]
    return ast.If(test, then, else_,
                  lineno=kwd.start[0],
                  col_offset=kwd.start[1])
Beispiel #5
0
    def pre_If(self):
        if hasattr(self.cur_node, 'custom_id') \
                and self.cur_node.custom_id == self.target \
                and SplitOrConditional.is_applicable(self.cur_node):
            self.applied = True
            parent_list = self.parent
            if_stmt = self.cur_node
            cur_index = parent_list.index(if_stmt)
            and_left = if_stmt.test.values[0]
            and_right = if_stmt.test.values[1]
            body_list = if_stmt.body
            orelse_list = if_stmt.orelse
            new_if = ast.If(and_left, body_list,
                            [ast.If(and_right, body_list, [])])
            # u.print_node(and_left)
            # u.print_node(and_right)

            self.replace(new_if)
Beispiel #6
0
 def visit_For(self, node):  #in testing:)
     #add more convincing junk for loop(random choice of a few seemingly important code lines that dont do anything)
     fortarget = self.visit(
         ast.Name(id=''.join([
             random.choice(string.ascii_letters)
             for i in range(random.randint(3, 8))
         ]),
                  ctx=ast.Load))
     junkfor = [
         ast.Assign(targets=[self.visit(node.target)],
                    ctx=ast.Store(),
                    value=self.visit(
                        ast.Num(n=id(''.join([
                            random.choice(string.ascii_letters)
                            for i in range(random.randint(3, 8))
                        ]))))),
         ast.If(test=ast.Compare(left=self.visit(node.target),
                                 ops=[ast.Eq()],
                                 comparators=[
                                     ast.Call(func=ast.Name(id='id',
                                                            ctx=ast.Load()),
                                              args=[fortarget],
                                              keywords=[],
                                              starargs=None,
                                              kwargs=None)
                                 ]),
                body=[
                    ast.Assign(targets=[self.visit(node.target)],
                               ctx=ast.Store(),
                               value=ast.BinOp(left=self.visit(node.target),
                                               op=ast.Sub(),
                                               right=self.visit(
                                                   ast.Num(n=1)))),
                    ast.Assign(targets=[fortarget],
                               ctx=ast.Store(),
                               value=ast.BinOp(left=self.visit(node.target),
                                               op=ast.Add(),
                                               right=self.visit(
                                                   node.target)))
                ],
                orelse=[])
     ]
     return ast.For(
         target=fortarget,
         iter=ast.Call(
             func=ast.Name(id='range', ctx=ast.Load()),
             args=[self.visit(ast.Num(n=random.randint(10, 100)))],
             keywords=[],
             starargs=None,
             kwargs=None),
         body=junkfor,
         orelse=[
             ast.For(target=self.visit(node.target),
                     iter=self.visit(node.iter),
                     body=[self.visit(i) for i in node.body],
                     orelse=[self.visit(i) for i in node.orelse])
         ])
Beispiel #7
0
    def visit_Assert(self, assert_):
        """Return the AST statements to replace the ast.Assert instance.

        This re-writes the test of an assertion to provide
        intermediate values and replace it with an if statement which
        raises an assertion error with a detailed explanation in case
        the expression is false.

        """
        if isinstance(assert_.test, ast.Tuple) and self.config is not None:
            fslocation = (self.module_path, assert_.lineno)
            self.config.warn('R1', 'assertion is always true, perhaps '
                             'remove parentheses?',
                             fslocation=fslocation)
        self.statements = []
        self.variables = []
        self.variable_counter = itertools.count()
        self.stack = []
        self.on_failure = []
        self.push_format_context()
        # Rewrite assert into a bunch of statements.
        top_condition, explanation = self.visit(assert_.test)
        # Create failure message.
        body = self.on_failure
        negation = ast.UnaryOp(ast.Not(), top_condition)
        self.statements.append(ast.If(negation, body, []))
        if assert_.msg:
            assertmsg = self.helper('format_assertmsg', assert_.msg)
            explanation = "\n>assert " + explanation
        else:
            assertmsg = ast.Str("")
            explanation = "assert " + explanation

        if _MARK_ASSERTION_INTROSPECTION:
            explanation = 'dessert* ' + explanation

        template = ast.BinOp(assertmsg, ast.Add(), ast.Str(explanation))
        msg = self.pop_format_context(template)
        fmt = self.helper("format_explanation", msg, assertmsg)
        err_name = ast.Name("AssertionError", ast.Load())
        exc = ast_Call(err_name, [fmt], [])
        if sys.version_info[0] >= 3:
            raise_ = ast.Raise(exc, None)
        else:
            raise_ = ast.Raise(exc, None, None)
        body.append(raise_)
        # Clear temporary variables by setting them to None.
        if self.variables:
            variables = [
                ast.Name(name, ast.Store()) for name in self.variables
            ]
            clear = ast.Assign(variables, _NameConstant(None))
            self.statements.append(clear)
        # Fix line numbers.
        for stmt in self.statements:
            set_location(stmt, assert_.lineno, assert_.col_offset)
        return self.statements
Beispiel #8
0
    def expand_ifexp(self, ifexp, ret_var):
        def assgn(value):
            return ast.Assign(targets=[make_name(ret_var)], value=value)

        return [
            ast.If(test=ifexp.test,
                   body=[assgn(ifexp.body)],
                   orelse=[assgn(ifexp.orelse)])
        ]
Beispiel #9
0
    def visit_TryExcept(self, node):
        if node.orelse:
            raise NotImplementedError('Try-except else handler not implemented')

        self.indent()
        self.output('try')
        self.block(node.body, context=BlockContext(self.stack[-1]))
        self.output(' catch($e) ')
        self.push(BlockContext(self.stack[-1]))

        if_start = None
        if_end = None

        for handler in node.handlers:
            if handler.type is not None:
                if handler.name is not None:
                    body = handler.body[:]
                    body.insert(0, ast.Assign(
                        [handler.name],
                        ast_call(ast_load('JS'), ast.Str('$e'))
                    ))
                else:
                    body = handler.body

                types = [handler.type] if isinstance(handler.type, _ast.Name) else handler.type
                conditions = [
                    ast_call(
                        ast_load('isinstance'),
                        ast_call(ast_load('JS'), ast.Str('$e')),
                        type_,
                    )
                    for type_ in types
                ]

                _if = ast.If(
                    ast.BoolOp(ast.Or(), conditions),
                    body,
                    []
                )
                if if_start is None:
                    if_start = if_end = _if
                else:
                    if_end.orelse, if_end = [_if], _if
            else:
                if handler is not node.handlers[-1]:
                    raise SyntaxError("default 'except:' must be last")
                if if_start is None:
                    self.block(handler.body)
                else:
                    if_end.orelse = handler.body

        if if_start is not None:
            self.visit(if_start)

        self.pop()
        self.output('\n')
Beispiel #10
0
def p_if_stmt(t):
    '''stmt : IF expression THEN suite elifs else_or_empty'''
    last = t[6]
    elifs = t[5]
    for elif_block in elifs:
        elif_block.orelse = last
        last = [elif_block]
    t[0] = ast.If(t[2], t[4], last)
    t[0].lineno = t.lineno(3)
    t[0].col_offset = -1  # XXX
Beispiel #11
0
def p_if_elif(t):
    '''elif : ELSE IF expression THEN suite
            | ELSEAND IF expression THEN suite'''
    # NOTE
    # 아니면 만약, 아니라면 만약 / 아니면서 만약
    # ELSE랑 충분히 헷갈릴 수 있으므로 일단 모두 처리가능하게
    elif_block = ast.If(t[3], t[5], [])
    elif_block.lineno = t.lineno(4)
    elif_block.col_offset = -1  # XXX
    t[0] = elif_block
    def visit_If(self, node: If, *args, **kwargs) -> C.If:
        test = self.visit(node.test, *args, **kwargs)
        body = self.visit(node.body, *args, **kwargs)
        orelse = self.visit(node.orelse, *args, **kwargs)

        return C.If(
            test=test,
            body=body,
            orelse=orelse,
        )
Beispiel #13
0
 def visit_If(self, iff):
     return [
         make_annotation(buffer=self.buffer, content=f'`if {astor.to_source(iff.test).strip()} ...`', cell_type='2'),
         make_annotation(iff.test, buffer=self.buffer),
         ast.If(
             test=iff.test,
             body=self._annotate_nodes(iff.body),
             orelse=self._annotate_nodes(iff.orelse)
         )
     ]
Beispiel #14
0
 def visit_ListComp(self, t):
     result_append = ast.Attribute(ast.Name('.0', load), 'append', load)
     body = ast.Expr(Call(result_append, [t.elt]))
     for loop in reversed(t.generators):
         for test in reversed(loop.ifs):
             body = ast.If(test, [body], [])
         body = ast.For(loop.target, loop.iter, [body], [])
     fn = [body, ast.Return(ast.Name('.0', load))]
     args = ast.arguments([ast.arg('.0', None)], None, [], None, [], [])
     return Call(Function('<listcomp>', args, fn), [ast.List([], load)])
Beispiel #15
0
 def test_If(self):
     # 'if' only
     if_ = ast.If(ast.Num(42), [ast.Pass()], [])
     self.verify(if_, 'if 42:pass')
     # if/else
     if_else = ast.If(ast.Num(42), [ast.Pass()], [ast.Pass()])
     self.verify(if_else, 'if 42:pass\nelse:pass')
     # if/elif/else
     if_elif_else = ast.If(ast.Num(6), [ast.Pass()], [if_else])
     self.verify(if_elif_else, 'if 6:pass\n' 'elif 42:pass\n' 'else:pass')
     # if/else w/ a leading 'if' clause + extra
     if_else_extra = ast.If(ast.Num(6), [ast.Pass()], [if_, ast.Pass()])
     self.verify(if_else_extra, 'if 6:pass\n'
                 'else:\n'
                 ' if 42:pass\n'
                 ' pass')
     # 'if' w/ a leading simple stmt but another non-simple stmt
     if_fancy_body = ast.If(ast.Num(6), [ast.Pass(), if_], [])
     self.verify(if_fancy_body, 'if 6:\n pass\n if 42:pass')
Beispiel #16
0
 def handle_goto_label(self, stmnt):
     assert isinstance(stmnt, GotoLabel)
     reset_ast = ast.Assign()
     reset_ast.targets = [ast.Name(id=self.gotoVarName, ctx=ast.Store())]
     reset_ast.value = ast.Name(id="None", ctx=ast.Load())
     test_ast = ast.Compare()
     test_ast.ops = [ast.Eq()]
     test_ast.left = ast.Name(id=self.gotoVarName, ctx=ast.Store())
     test_ast.comparators = [_ast_for_value(stmnt.label)]
     return [ast.If(test=test_ast, body=[reset_ast], orelse=[])]
Beispiel #17
0
    def insert_holes(the_ast, hole_id, site_map):

        if 'body' not in the_ast.__dict__ or len(the_ast.body) == 0:
            return False, the_ast, hole_id, site_map
        new_body = []
        for node in the_ast.body:
            site_map["if false : @R_{}@ = 1".format(hole_id)] = [
                "", 'transforms.AddDeadCode'
            ]
            new_body += [
                ast.If(test=ast.Name(id="False", ctx=ast.Load()),
                       body=[
                           ast.Assign(targets=[
                               ast.Name(id="REPLACEME" + str(hole_id),
                                        ctx=ast.Store())
                           ],
                                      value=ast.Num(n=1))
                       ],
                       orelse=[]), node
            ]
            hole_id += 1
        site_map["if false : @R_{}@ = 1".format(hole_id)] = [
            "", 'transforms.AddDeadCode'
        ]
        new_body.append(
            ast.If(test=ast.Name(id="False", ctx=ast.Load()),
                   body=[
                       ast.Assign(targets=[
                           ast.Name(id="REPLACEME" + str(hole_id),
                                    ctx=ast.Store())
                       ],
                                  value=ast.Num(n=1))
                   ],
                   orelse=[]))
        hole_id += 1
        the_ast.body = new_body

        for i in range(len(the_ast.body[1:])):
            if i % 2 == 0:
                node = the_ast.body[i + 1]
                _, _, hole_id, site_map = insert_holes(node, hole_id, site_map)
        return True, the_ast, hole_id, site_map
Beispiel #18
0
 def if_(self):
     self.expect('if')
     self.expect('(')
     cond = self.expression()
     self.expect(')')
     conseq = self.statement()
     alt = None
     if self.accept('else'):
         self.advance()
         alt = self.statement() 
     return ast.If(cond, conseq, alt)
Beispiel #19
0
 def visit_AnnAssign(self, node):
     name = node.target.id
     type_ = node.annotation.id
     return [
         node,
         ast.If(
             test=not_ast(isinstance_ast(name, type_)),
             body=[raise_type_violation_error_ast(name, type_)],
             orelse=[],
         )
     ]
Beispiel #20
0
 def stop_iteration(node):
     cmp_break = ast.Compare(left=mk_name(self.flag_id),
                             ops=[ast.Eq()],
                             comparators=[mk_str(self.BREAK)])
     cmp_return = ast.Compare(left=mk_name(self.flag_id),
                              ops=[ast.Eq()],
                              comparators=[mk_str(self.RETURN)])
     test = ast.BoolOp(op=ast.Or(), values=[cmp_break, cmp_return])
     break_stmt = ast.Break()
     ifstmt = ast.If(test=test, body=[break_stmt], orelse=[])
     node.body.append(ifstmt)
Beispiel #21
0
 def _translation_common(self, ctx, block_translator):
     stmt = self.stmt
     test, body_block, orelse_block = (stmt.test, stmt.body_block, 
                                       stmt.orelse_block)
     test_translation = ctx.translate(test)
     body_block_translation = block_translator(body_block, ctx)
     orelse_block_translation = block_translator(orelse_block, ctx)
     return [ast.If(
         test=test_translation,
         body=body_block_translation,
         orelse=orelse_block_translation)]
Beispiel #22
0
 def visit_Name(self, node):
     if node.id in self.replacements:
         replacement = self.replacements[node.id]
         if isinstance(replacement, list):
             if len(replacement) > 1:
                 return ast.If(test=ast.Num(n=1),
                               body=replacement,
                               orelse=[])
             return replacement[0]
         return replacement
     return self.generic_visit(node)
Beispiel #23
0
        def recurseIfs(conds, bodies, current):
            assert (len(conds) == len(bodies)), \
                'Length of conds %r and bodies %r mismatch' % (len(conds), len(bodies))

            if current >= len(conds):
                return []
            elif current == len(conds)-1:
                return bodies[current]
            return py.If(conds[current], 
                         bodies[current], 
                         recurseIfs(conds, bodies, current+1),
                         **pos(node))
Beispiel #24
0
 def recursive(values):
     if len(values) == 1:
         self.start_frame()
         stmt = mk_assign(target, self.visit(values[0]))
         return self.end_frame() + [stmt]
     else:
         self.start_frame()
         value = self.make_primitive(values[0])
         orelse = recursive(values[1:])
         body = [mk_assign(target, value)]
         ifstmt = ast.If(test=value, body=body, orelse=orelse)
         return self.end_frame() + [ifstmt]
Beispiel #25
0
def isl2py_ast(n):
    """ Transform a ISL AST node to a list of Python AST nodes (body) """
    isl_nty = n.get_type()
    if isl_nty == isl.ast_node_type.for_:
        return isl2py_for(n)

    elif isl_nty == isl.ast_node_type.if_:
        return [
            pyast.If(
                test=isl2py_exp(n.if_get_cond()),
                body=isl2py_ast(n.if_get_then()),
                orelse=isl2py_ast(n.if_get_else()) if n.if_has_else() else [],
            )
        ]

    elif isl_nty == isl.ast_node_type.block:
        nlist = n.block_get_children()
        nr_nodes = nlist.n_ast_node()
        ret = []
        for i in range(nr_nodes):
            ret.extend(isl2py_ast(nlist.get_ast_node(i)))
        return ret
        raise NotImplementedError
    elif isl_nty == isl.ast_node_type.mark:
        raise NotImplementedError
    elif isl_nty == isl.ast_node_type.user:
        e = n.user_get_expr()
        if (
            e.get_type() == isl.ast_expr_type.op
            and e.get_op_type() == isl.ast_expr_op_type.call
        ):
            # Replace call with a yield with the call arguments
            # I guess the first arg is the "function"
            nargs = e.get_op_n_arg()
            return [
                # NB: Ignore the first argument (for now)
                pyast.Expr(
                    pyast.Yield(
                        pyast.Tuple(
                            [
                                isl2py_exp(e.get_op_arg(i + 1))
                                for i in range(nargs - 1)
                            ],
                            pyast.Load(),
                        )
                    )
                )
            ]
        raise NotImplementedError
    elif isl_nty == isl.ast_node_type.error:
        raise NotImplementedError
    else:
        raise AssertionError("uknown ISL node type: %d" % (isl_nty,))
 def new_if(self, indent):
     iff = stdast.If()
     iff.lineno = self.i + 1
     self.expect('if')
     test = self.expression()
     if test is None:
         raise SyntaxError('Line {}: Expected expression.'.format(self.i))
     iff.test = test
     self.expect('colon')
     iff.body = self.new_body(indent + 1)
     iff.orelse = []
     return iff
Beispiel #27
0
 def handle_body(self, body):
     """
     :type body: list[ast.AST]
     :rtype: list[ast.AST]
     """
     parts = [[]]
     for s in body:
         if isinstance(s, GotoLabel):
             parts += [s, []]
         else:
             parts[-1].append(s)
     r = []
     for l in parts:
         if not l: continue
         if isinstance(l, GotoLabel):
             r += self.handle_goto_label(l)
         else:
             sr = []
             for s in l:
                 if isinstance(s, ast.If):
                     assert not s.orelse
                     assert len(s.body) == 1
                     assert isinstance(s.body[0], GotoStatement)
                     sr += [
                         ast.If(test=s.test,
                                orelse=[],
                                body=self.handle_goto_stmnt(s.body[0]))
                     ]
                 elif isinstance(s, (ast.While, ast.For)):
                     assert False, "not expected: %r" % s
                 elif isinstance(s, GotoStatement):
                     sr += self.handle_goto_stmnt(s)
                 else:
                     sr += [s]
             test_ast = ast.Compare()
             test_ast.ops = [ast.Is()]
             test_ast.left = ast.Name(id=self.gotoVarName, ctx=ast.Store())
             test_ast.comparators = [ast.Name(id="None", ctx=ast.Load())]
             r += [ast.If(test=test_ast, body=sr, orelse=[])]
     return r
Beispiel #28
0
    def get_kernel_embed():
        """A list of kernel embed nodes

        Returns:
            nodes (list): AST nodes which form the following code.

            ```
            import os
            pid = os.fork()
            if os.fork() == 0:
                open(f'{os.environ["HOME"]}/.pynt', 'a').close()
                import IPython
                IPython.start_kernel(user_ns={**locals(), **globals(), **vars()})
            os.waitpid(pid, 0)
            ```

        This is a purely functional method which always return the same thing.

        """
        return [
            ast.Import(names=[ast.alias(name='os', asname=None),]),
            ast.Assign(targets=[ast.Name(id='pid', ctx=ast.Store()),], value=ast.Call(func=ast.Attribute(value=ast.Name(id='os', ctx=ast.Load()), attr='fork', ctx=ast.Load()), args=[], keywords=[])),
            ast.If(
                test=ast.Compare(left=ast.Name(id='pid', ctx=ast.Load()), ops=[ast.Eq(),], comparators=[ast.Num(n=0),]),
                body=[
                    ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Call(func=ast.Name(id='open', ctx=ast.Load()), args=[
                        ast.JoinedStr(values=[
                            ast.FormattedValue(value=ast.Subscript(value=ast.Attribute(value=ast.Name(id='os', ctx=ast.Load()), attr='environ', ctx=ast.Load()), slice=ast.Index(value=ast.Str(s='HOME')), ctx=ast.Load()), conversion=-1, format_spec=None),
                            ast.Str(s='/.pynt'),
                        ]),
                        ast.Str(s='a'),
                    ], keywords=[]), attr='close', ctx=ast.Load()), args=[], keywords=[])),
                    ast.Import(names=[
                        ast.alias(name='IPython', asname=None),
                    ]),
                    ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id='IPython', ctx=ast.Load()), attr='start_kernel', ctx=ast.Load()), args=[], keywords=[
                        ast.keyword(arg='user_ns', value=ast.Dict(keys=[
                            None,
                            None,
                            None,
                        ], values=[
                            ast.Call(func=ast.Name(id='locals', ctx=ast.Load()), args=[], keywords=[]),
                            ast.Call(func=ast.Name(id='globals', ctx=ast.Load()), args=[], keywords=[]),
                            ast.Call(func=ast.Name(id='vars', ctx=ast.Load()), args=[], keywords=[]),
                        ])),
                    ])),
            ], orelse=[]),
            ast.Expr(value=ast.Call(func=ast.Attribute(value=ast.Name(id='os', ctx=ast.Load()), attr='waitpid', ctx=ast.Load()), args=[
                ast.Name(id='pid', ctx=ast.Load()),
                ast.Num(n=0),
            ], keywords=[])),
        ]
Beispiel #29
0
    def p_conditional(self, p):
        '''statement : IF expr LBRACE ifbody RBRACE
                     | IF expr LBRACE ifbody RBRACE ELSE LBRACE ifbody RBRACE
        '''
        test = p[2]
        iftrue = p[4]
        iftrue.lineno = p.lineno(4)
        iffalse = []
        if len(p) > 6:
            iffalse = p[8]
            iffalse.lineno = p.lineno(8)

        p[0] = ast.If(test=test, body=iftrue, orelse=iffalse)
 def getNodeTest(self):
     return ast.If(
         ast.Call(ast.Name('tutu', ast.Load()), [ast.List([], ast.Load())],
                  [], None, None),
         [
             ast.Print(
                 None,
                 ast.Call(ast.Name('toto', ast.Load()),
                          [ast.List([], ast.Load())], [], None, None),
                 False),
         ],
         [],
     )