Exemple #1
0
 def test_Try(self):
     # try/except where 'except' is only dead code.
     exc_clause = ast.ExceptHandler(None, None,
                                     [ast.Expr(ast.Str('dead code'))])
     try_exc = ast.Try([ast.Pass()], [exc_clause], [], [])
     expect = ast.Try([ast.Pass()],
                      [ast.ExceptHandler(None, None, [ast.Pass()])], [], [])
     self.check_transform(try_exc, expect)
Exemple #2
0
 def test_Try(self):
     except_ = ast.ExceptHandler(None, None, self.simple_stmt())
     # Always keep try/except.
     got = ast.Try([self.func], [except_], [], [])
     want = got
     self.check_transform(got, want)
     # try/except/finally where 'finally' is simple -> try/except
     got = ast.Try([self.func], [except_], [], self.simple_stmt())
     self.check_transform(got, want)
     # try/except/else where 'else' is simple -> try/except
     got = ast.Try([self.func], [except_], self.simple_stmt(), [])
     self.check_transform(got, want)
     # try/finally where 'finally' is simple -> unwrap 'try'
     got = ast.Module([ast.Try([self.func], [], [], self.simple_stmt())])
     want = ast.Module([self.func])
     self.check_transform(got, want)
Exemple #3
0
    def visit_FunctionDef(self, node):
        # Pre-hook.
        pre_hook_expr = ast.Expr(value=ast.Call(
            func=ast.Name(id='start', ctx=ast.Load()), args=[], keywords=[]))
        # Post-hook.
        finalbody = [
            ast.Global(names=[self._result_id]),
            ast.Assign(targets=[ast.Name(id=self._result_id, ctx=ast.Store())],
                       value=ast.Call(func=ast.Name(id='take_snapshot',
                                                    ctx=ast.Load()),
                                      args=[],
                                      keywords=[])),
            ast.Expr(value=ast.Call(func=ast.Name(id='stop', ctx=ast.Load()),
                                    args=[],
                                    keywords=[]))
        ]

        body_elems = [pre_hook_expr]
        body_elems.extend([elem for elem in node.body])
        node.body.clear()
        node.body.append(
            ast.Try(body=body_elems,
                    handlers=[],
                    orelse=[],
                    finalbody=finalbody))

        return ast.fix_missing_locations(node)
Exemple #4
0
        def generic_visit(self, node):
            import ast
            import sys
            ast.NodeTransformer.generic_visit(self, node)

            if isinstance(node,
                          ast.stmt) and not isinstance(node, ast.FunctionDef):
                if sys.version_info[0] == 3:
                    new_node = ast.Try(body=[node],
                                       handlers=[
                                           ast.ExceptHandler(type=None,
                                                             name=None,
                                                             body=[ast.Pass()])
                                       ],
                                       orelse=[],
                                       finalbody=[ast.Pass()])
                else:
                    new_node = ast.TryExcept(body=[node],
                                             handlers=[
                                                 ast.ExceptHandler(
                                                     type=None,
                                                     name=None,
                                                     body=[ast.Pass()])
                                             ],
                                             orelse=[])
                return ast.copy_location(new_node, node)
            return node
Exemple #5
0
    def visit_For(self, loop_):
        """
        >>> self = FirstPassFor(buffer='foo')
        >>> code = '''
        ...
        ... for i in range(5):
        ...     for j in range(5):
        ...         k = i + j
        ...         print(k)
        ...
        ... '''
        >>> tree = ast.parse(code)
        >>> loop_, = tree.body

        """
        loop = self.generic_visit(loop_)
        var = ast.Name(id=__random_string__(), ctx=ast.Store())
        assign = ast.Assign(targets=[var], value=ast.Call(func=ast.Name(id='iter', ctx=ast.Load()), args=[loop.iter], keywords=[]))
        first_pass = ast.Try(
            body=[ast.Assign(targets=[loop.target], value=ast.Call(func=ast.Name(id='next', ctx=ast.Load()), args=[ast.Name(id=var, ctx=ast.Load())], keywords=[]))],
            handlers=[ast.ExceptHandler(type=ast.Name(id='StopIteration', ctx=ast.Load()), name=None, body=[ast.Pass()])],
            orelse=loop.body,
            finalbody=[]
        )
        content = f'`for {astor.to_source(loop.target).strip()} in {astor.to_source(loop.iter).strip()} ...`'
        return [
            make_annotation(buffer=self.buffer, content=content, cell_type='2', lineno=loop.lineno),
            ast.Expr(loop.iter),
            assign,
            first_pass
        ]
Exemple #6
0
 def visit_Assign(self, node: ast.Assign) -> Any:
     self.generic_visit(node)
     target = node.targets[0]
     if not isinstance(target, ast.Name):
         return node
     expr = ast.Name(id=target.id, ctx=ast.Load())
     name = target.id
     check = ast.Try(
         body=[ast.Expr(expr)],
         handlers=[
             ast.ExceptHandler(
                 type=ast.Tuple(
                     elts=[ast.Name(id=_n(NameError), ctx=ast.Load())],
                     ctx=ast.Load(),
                 ),
                 name=None,
                 body=[ast.Expr(value=ast.Constant(value=Ellipsis))],
             )
         ],
         orelse=[
             ast.Raise(
                 exc=ast.Call(
                     func=ast.Name(
                         id=_n(ImmutabilityGuard.proxy_Assign), ctx=ast.Load()
                     ),
                     args=[ast.Constant(value=name)],
                     keywords=[],
                 ),
                 cause=None,
             )
         ],
         finalbody=[],
     )
     return [check, node]
Exemple #7
0
    def generic_visit(self, node):
        """Surround node statement with a try/except block to catch errors.

        This method is called for every node of the parsed code, and only
        changes statement lines.

        Args:
            node (ast.AST): node statement to surround.
        """
        super(ErrorsCatchTransformer, self).generic_visit(node)

        if (isinstance(node, ast.stmt)
                and not isinstance(node, ast.FunctionDef)):

            if sys.version_info > (3, 0):
                new_node = ast.Try(  # pylint: disable=no-member
                    orelse=[],
                    body=[node],
                    finalbody=[],
                    handlers=self.exception_handlers)

            else:
                new_node = ast.TryExcept(  # pylint: disable=no-member
                    orelse=[],
                    body=[node],
                    handlers=self.exception_handlers)

            return ast.copy_location(new_node, node)

        return node
Exemple #8
0
    def ast_nodes(self) -> List[ast.stmt]:
        """Get the list of generated AST nodes.

        In case the `wrap_nodes` property was set, the nodes will be wrapped in
        ```
        try:
            [nodes]
        except BaseException:
            pass
        ```

        Returns:
            A list of AST nodes
        """
        if self._wrap_nodes:
            nodes: List[ast.stmt] = [
                ast.Try(
                    body=self._ast_nodes,
                    handlers=[
                        ast.ExceptHandler(
                            body=[ast.Pass()],
                            name=None,
                            type=ast.Name(ctx=ast.Load(), id="BaseException"),
                        )
                    ],
                    orelse=[],
                    finalbody=[],
                )
            ]
            return nodes
        return self._ast_nodes
Exemple #9
0
    def wrap_starred_assign(self, n, targets):

        if PY2:
            return n

        starred = find_starred_variables(targets)

        if not starred:
            return n

        list_stmts = []

        for var in starred:

            call = ast.Call(func=ast.Name(id=b("__renpy__list__"),
                                          ctx=ast.Load()),
                            args=[ast.Name(id=var, ctx=ast.Load())],
                            keywords=[],
                            starargs=None,
                            kwargs=None)

            assign = ast.Assign(
                targets=[ast.Name(id=var, ctx=ast.Store())],
                value=call,
            )

            list_stmts.append(assign)

        return ast.Try(
            body=[n],
            handlers=[],
            orelse=[],
            finalbody=list_stmts,
        )
Exemple #10
0
 def visit_FunctionDef(self, node):
     if len(preconditions[node.name]) == 0:
         return node
     ast_name = ast.Name(id="PreconditionException", ctx=ast.Load())
     node_exception = ast.Raise(exc=ast_name, cause=None)
     precondition_node = preconditions[node.name][0]
     assert_node = ast.Assert(precondition_node,
                              lineno=node.lineno,
                              col_offset=node.col_offset,
                              end_lineno=node.lineno,
                              end_col_offset=node.end_col_offset)
     ast_exception = ast.Name(id="Exception", ctx=ast.Load())
     handler = ast.ExceptHandler(type=ast_exception,
                                 name=None,
                                 body=[node_exception])
     try_node = ast.Try(body=[assert_node],
                        handlers=[handler],
                        orelse=[],
                        finalbody=[node.body])
     #if_node = ast.If(precondition_node,[node.body],[node_exception])
     node_res = ast.FunctionDef(node.name,
                                node.args, [try_node],
                                node.decorator_list,
                                node.returns,
                                node.type_comment,
                                lineno=node.lineno,
                                col_offset=node.col_offset,
                                end_lineno=node.lineno,
                                end_col_offset=node.end_col_offset)
     ast.fix_missing_locations(node_res)
     return node_res
Exemple #11
0
    def gen_unpack_wrapper(self, node, target, ctx='store'):
        """Helper function to protect tuple unpacks.

		node: used to copy the locations for the new nodes.
		target: is the tuple which must be protected.
		ctx: Defines the context of the returned temporary node.

		It returns a tuple with two element.

		Element 1: Is a temporary name node which must be used to
				   replace the target.
				   The context (store, param) is defined
				   by the 'ctx' parameter..

		Element 2: Is a try .. finally where the body performs the
				   protected tuple unpack of the temporary variable
				   into the original target.
		"""

        # Generate a tmp name to replace the tuple with.
        tnam = self.tmpName

        # Generates an expressions which protects the unpack.
        # converter looks like 'wrapper(tnam)'.
        # 'wrapper' takes care to protect sequence unpacking with __rl_getiter__.
        converter = self.protect_unpack_sequence(target,
                                                 ast.Name(tnam, ast.Load()))

        # Assign the expression to the original names.
        # Cleanup the temporary variable.
        # Generates:
        # try:
        #	  # converter is 'wrapper(tnam)'
        #	  arg = converter
        # finally:
        #	  del tmp_arg
        try_body = [ast.Assign(targets=[target], value=converter)]
        finalbody = [self.gen_del_stmt(tnam)]

        cleanup = ast.Try(body=try_body,
                          finalbody=finalbody,
                          handlers=[],
                          orelse=[])

        if ctx == 'store':
            ctx = ast.Store()
        elif ctx == 'param':
            ctx = ast.Param()
        else:  # pragma: no cover
            # Only store and param are defined ctx.
            raise NotImplementedError('bad ctx "%s"' % type(ctx))

        # This node is used to catch the tuple in a tmp variable.
        tmp_target = ast.Name(tnam, ctx)

        copy_locations(tmp_target, node)
        copy_locations(cleanup, node)

        return (tmp_target, cleanup)
Exemple #12
0
 def 试试(主体, 处理, 片段):
     return ast.Try(
         body=主体,
         handlers=处理,
         orelse=[],
         finalbody=[],
         lineno=语法树.取行号(片段),
         col_offset=语法树.取列号(片段))
Exemple #13
0
 def visitTry(self, n, *args):
     body = self.dispatch_statements(n.body, *args)
     handlers = self.reduce(n.handlers, *args)
     orelse = self.dispatch_statements(n.orelse, *args)
     finalbody = self.dispatch_statements(n.finalbody, *args)
     return ast.Try(body=body,
                    handlers=handlers,
                    orelse=orelse,
                    finalbody=finalbody)
Exemple #14
0
 def gen_block_TryExcept(self, expr_try, expr_except):
     assert ((type(expr_try) and type(expr_except)) == list)
     # TODO: could be made in one-liner
     exception_handler = [
         ast.ExceptHandler(ast.Name("Exception", ast.Store()), "e",
                           expr_except)
     ]
     tryExcept_block = ast.Try(expr_try, exception_handler, [], [])
     return (tryExcept_block)
Exemple #15
0
def add_required_imports(tree: ast.Module,
                         required_imports: typ.Set[common.ImportDecl]) -> None:
    """Add imports required by fixers.

    Some fixers depend on modules which may not be imported in
    the source module. As an example, occurrences of 'map' might
    be replaced with 'itertools.imap', in which case,
    "import itertools" will be added in the module scope.

    A further quirk is that all reqired imports must be added
    before any other statment. This is because that statement
    could be subject to the fix which requires the import. As
    a side effect, a module may end up being imported twice, if
    the module is imported after some statement.
    """
    (future_imports_offset, imports_end_offset,
     found_imports) = parse_imports(tree)

    missing_imports = sorted(required_imports - found_imports)

    import_node: ast.stmt
    for import_decl in missing_imports:
        if import_decl.import_name is None:
            import_node = ast.Import(
                names=[ast.alias(name=import_decl.module_name, asname=None)])
        else:
            import_node = ast.ImportFrom(
                module=import_decl.module_name,
                level=0,
                names=[ast.alias(name=import_decl.import_name, asname=None)],
            )

        if import_decl.py2_module_name:
            asname = import_decl.import_name or import_decl.module_name
            fallback_import = ast.Import(names=[
                ast.alias(name=import_decl.py2_module_name, asname=asname)
            ])
            import_node = ast.Try(
                body=[import_node],
                handlers=[
                    ast.ExceptHandler(
                        type=ast.Name(id='ImportError', ctx=ast.Load()),
                        name=None,
                        body=[fallback_import],
                    )
                ],
                orelse=[],
                finalbody=[],
            )

        if import_decl.module_name == '__future__':
            tree.body.insert(future_imports_offset, import_node)
            future_imports_offset += 1
            imports_end_offset += 1
        else:
            tree.body.insert(imports_end_offset, import_node)
            imports_end_offset += 1
Exemple #16
0
def try_stmt_rewrite(mark, body, excs, rescues, orelse, final):
    excs = excs or []
    rescues = rescues or []

    def handlers():
        for (type, name), body in zip(excs, rescues):
            yield ast.ExceptHandler(type, name, body)

    return ast.Try(body, list(handlers()), orelse or [], final or [], **loc @ mark)
Exemple #17
0
def generate_try(max_depth=None):
    body = generate_block(max_depth=max_depth)
    num_handlers = random.randrange(1, 3)
    handlers = [
        _generate_except_handler(max_depth=max_depth)
        for _ in range(num_handlers)
    ]
    orelse = random.choice([generate_block(max_depth=max_depth), []])
    finalbody = random.choice([generate_block(max_depth=max_depth), []])
    return ast.Try(body, handlers, orelse, finalbody)
Exemple #18
0
 def visit_Try(self, try_):
     handlers = []
     for handler in try_.handlers:
         handlers.append(
             ast.ExceptHandler(type=handler.type,
                               name=None,
                               body=self._annotate_nodes(handler.body)))
     return ast.Try(body=self._annotate_nodes(try_.body),
                    handlers=handlers,
                    orelse=self._annotate_nodes(try_.orelse),
                    finalbody=self._annotate_nodes(try_.finalbody))
def _try_fallback(node: ast.stmt, fallback_node: ast.stmt) -> ast.Try:
    return ast.Try(
        body=[node],
        handlers=[
            ast.ExceptHandler(type=ast.Name(id="ImportError", ctx=ast.Load()),
                              name=None,
                              body=[fallback_node])
        ],
        orelse=[],
        finalbody=[],
    )
Exemple #20
0
    def visit_FunctionDef(self, node):
        node.parent = self.fundef
        self.fundef = node

        if len(list(filter(lambda n: isinstance(n, ast.Name) and n.id is 'rep_fun', node.decorator_list))) > 0:
            self.recs.append(node.name)

        self.generic_visit(node)

        r_args = {}

        for arg in node.args.args:
            arg_name = arg.arg
            try:
                if self.fundef.locals[arg_name] > 1:
                    r_args[arg_name] = self.freshName('x')
                # self.fundef.locals[arg_name] += 1
            except KeyError as e:
                pass

        # generate code to pre-initialize staged vars
        # we stage all vars that are written to more than once
        inits = [ast.Assign(targets=[ast.Name(id=id, ctx=ast.Store())],
                    value=ast.Call(
                        func=ast.Name(id='_var', ctx=ast.Load()),
                        args=[],
                        keywords=[])) \
                    for id in node.locals if node.locals[id] > 1]

        a_nodes = [ast.Expr(
                    ast.Call(
                        func=ast.Name(id='_assign', ctx=ast.Load()),
                        args=[ast.Name(id=arg,ctx=ast.Load()), ast.Name(id=r_args[arg],ctx=ast.Load())],
                        keywords=[])) \
                    for arg in r_args]

        new_node = ast.FunctionDef(name=node.name,
            args=ast.arguments(
                args=[ast.arg(arg=r_args[arg.arg],annotation=None) if arg.arg in r_args else arg for arg in node.args.args],
                vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), # node.args,
            body=[ast.Try(body=inits + a_nodes + node.body,
                        handlers=[ast.ExceptHandler(
                            type=ast.Name(id='NonLocalReturnValue', ctx=ast.Load()),
                            name='r',
                            body=[ast.Return(value=ast.Attribute(value=ast.Name(id='r', ctx=ast.Load()), attr='value', ctx=ast.Load()))])],
                        orelse=[],
                        finalbody=[])],
            decorator_list=list(filter(lambda n: isinstance(n, ast.Name) and n.id!='lms' and n.id!='rep_fun', node.decorator_list))
        )
        ast.copy_location(new_node, node)
        ast.fix_missing_locations(new_node)
        self.fundef = node.parent
        return new_node
    def visit_Try(self, node: Try, *args, **kwargs) -> C.Try:
        body = self.visit(node.body, *args, **kwargs)
        handlers = self.visit(node.handlers, *args, **kwargs)
        orelse = self.visit(node.orelse, *args, **kwargs)
        finalbody = self.visit(node.finalbody, *args, **kwargs)

        return C.Try(
            body=body,
            handlers=handlers,
            orelse=orelse,
            finalbody=finalbody,
        )
Exemple #22
0
 def test_Try(self):
     # except
     exc_clause = ast.ExceptHandler(ast.Name('X', ast.Load()), None,
                                    [ast.Pass()])
     exc_clause_2 = ast.ExceptHandler(None, None, [ast.Pass()])
     try_except = ast.Try([ast.Pass()], [exc_clause, exc_clause_2], None,
                          None)
     self.verify(try_except, 'try:pass\nexcept X:pass\nexcept:pass')
     # except/else
     try_except_else = ast.Try([ast.Pass()], [exc_clause, exc_clause_2],
                               [ast.Pass()], None)
     self.verify(try_except_else,
                 'try:pass\nexcept X:pass\nexcept:pass\nelse:pass')
     # except/finally
     exc_clause = ast.ExceptHandler(None, None, [ast.Pass()])
     try_except_finally = ast.Try([ast.Pass()], [exc_clause_2], None,
                                  [ast.Pass()])
     self.verify(try_except_finally, 'try:pass\nexcept:pass\nfinally:pass')
     # except/else/finally
     try_except_else_finally = ast.Try([ast.Pass()], [exc_clause_2],
                                       [ast.Pass()], [ast.Pass()])
     self.verify(try_except_else_finally,
                 'try:pass\nexcept:pass\nelse:pass\nfinally:pass')
     # else/finally
     try_else_finally = ast.Try([ast.Pass()], None, [ast.Pass()],
                                [ast.Pass()])
     self.verify(try_else_finally, 'try:pass\nelse:pass\nfinally:pass')
     # finally
     try_finally = ast.Try([ast.Pass()], None, None, [ast.Pass()])
     self.verify(try_finally, 'try:pass\nfinally:pass')
Exemple #23
0
def Try(draw, statements) -> ast.Try:
    body = draw(lists(statements, min_size=1, max_size=3))
    handlers = draw(lists(ExceptHandler(statements), min_size=0, max_size=2))
    if handlers:
        orelse = draw(lists(statements, min_size=1, max_size=3))
    else:
        orelse = []

    finalbody = draw(lists(statements, min_size=1, max_size=3))
    return ast.Try(body=body,
                   handlers=handlers,
                   orelse=orelse,
                   finalbody=finalbody)
def make_try():
    """Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)"""

    return ast.Try(
        body=[ast.Pass()],
        handlers=[make_exception_handler()],
        # The else and the finally are not that common
        # let's not include them by default
        orelse=[],
        # orelse=[ast.Pass()],
        finalbody=[],
        # finalbody=[ast.Pass()],
    )
Exemple #25
0
def _rewrite_if(tree, var_name=None, gen_sym=None, **kw_args):
    # TODO refactor into a _rewrite_switch and a _rewrite_if
    """
    Rewrite if statements to treat pattern matches as boolean
    expressions.

    Recall that normally a pattern match is a statement which will
    throw a PatternMatchException if the match fails.  We can
    therefore use try-blocks to produce the desired branching
    behavior.

    var_name is an optional parameter used for rewriting switch
    statements.

    If present, it will transform predicates which are expressions
    into pattern matches.
    """

    # with q as rewritten:
    #     try:
    #         with matching:
    #             u%(matchPattern)
    #         u%(successBody)
    #     except PatternMatchException:
    #         u%(_maybe_rewrite_if(failBody))
    # return rewritten
    if not isinstance(tree, ast.If):
        return tree

    if var_name:
        tree.test = ast.BinOp(tree.test, ast.LShift(),
                              ast.Name(var_name, ast.Load()))
    elif not (isinstance(tree.test, ast.BinOp)
              and isinstance(tree.test.op, ast.LShift)):
        return tree

    handler = ast.ExceptHandler(hq[PatternMatchException], None, tree.orelse)
    try_stmt = ast.Try(tree.body, [handler], [], [])

    match = [ast.Expr(tree.test)]

    _matching_walker.recurse(match, gen_sym=gen_sym)
    try_stmt.body = match + try_stmt.body

    if len(handler.body) == 1:  # (== tree.orelse)
        # Might be an elif
        handler.body = [_rewrite_if(handler.body[0], var_name, gen_sym)]
    elif not handler.body:
        handler.body = [ast.Pass()]

    return try_stmt
Exemple #26
0
 def visit_Module(self, node):
     setDoneNode = ast.Expr(ast.Call(ast.Attribute(ast.Name("finishedExecSig", ast.Load()), "set_result", ast.Load()), [ast.NameConstant(None)], []))
     setExceptionNode = ast.Expr(ast.Call(ast.Attribute(ast.Name("finishedExecSig", ast.Load()), "set_exception", ast.Load()), [ast.Name("e", ast.Load())], []))
     
     mainBody = node.body + [setDoneNode]
     
     tryExceptNode = ast.ExceptHandler(ast.Name("Exception", ast.Load()), "e", [setExceptionNode])
     
     tryNode = ast.Try(mainBody, [tryExceptNode], [], [])
     
     newNode = ast.Module([tryNode])
     ast.copy_location(newNode, node)
     ast.fix_missing_locations(newNode)
     return newNode
Exemple #27
0
def gen_try_except(try_body, exception_body):
    return ast.Try(
        body=try_body,
        handlers=[
            ast.ExceptHandler(
                type=ast.Name(id='Exception', ctx=ast.Load()),
                name=None,
                body=exception_body,
            )
        ],
        type_ignores=[],
        orelse=None,
        finalbody=[],
    )
Exemple #28
0
    def visit_Try(self, node):
        loop_guard = self.add_loop_block()
        self.curr_block = loop_guard
        self.add_stmt(loop_guard,
                      ast.Try(body=[], handlers=[], orelse=[], finalbody=[]))

        after_try_block = self.new_block()
        self.add_stmt(after_try_block,
                      ast.Name(id='handle errors', ctx=ast.Load()))
        self.populate_body(node.body, after_try_block.bid)

        self.curr_block = after_try_block

        if node.handlers:
            for handler in node.handlers:
                before_handler_block = self.new_block()
                self.curr_block = before_handler_block
                self.add_edge(
                    after_try_block.bid, before_handler_block.bid, handler.type
                    if handler.type else ast.Name(id='Error', ctx=ast.Load()))

                after_handler_block = self.new_block()
                self.add_stmt(after_handler_block,
                              ast.Name(id='end except', ctx=ast.Load()))
                self.populate_body(handler.body, after_handler_block.bid)
                self.add_edge(after_handler_block.bid, after_try_block.bid)

        if node.orelse:
            before_else_block = self.new_block()
            self.curr_block = before_else_block
            self.add_edge(after_try_block.bid, before_else_block.bid,
                          ast.Name(id='No Error', ctx=ast.Load()))

            after_else_block = self.new_block()
            self.add_stmt(after_else_block,
                          ast.Name(id='end no error', ctx=ast.Load()))
            self.populate_body(node.orelse, after_else_block.bid)
            self.add_edge(after_else_block.bid, after_try_block.bid)

        finally_block = self.new_block()
        self.curr_block = finally_block

        if node.finalbody:
            self.add_edge(after_try_block.bid, finally_block.bid,
                          ast.Name(id='Finally', ctx=ast.Load()))
            after_finally_block = self.new_block()
            self.populate_body(node.finalbody, after_finally_block.bid)
            self.curr_block = after_finally_block
        else:
            self.add_edge(after_try_block.bid, finally_block.bid)
    def ast_func(tree):
        hook_tree = ast.parse(fixindentation(inspect.getsource(globalify)))

        # try:
        #	f()
        # finally:
        #	hook()
        try_wrap = ast.Try(body=deepcopy(tree.body[0].body),
                           handlers=[],
                           orelse=[],
                           finalbody=deepcopy(hook_tree.body[0].body))

        tree.body[0].body = [try_wrap]
        return tree
Exemple #30
0
def convert_assign_py3(lhs, rhs):
    loaded = convert_name(lhs, ast.Load())
    return ast.Try(body=[
        ast.Expr(loaded),
    ],
                   handlers=[gen_except_handler(ast.Assign([lhs], rhs))],
                   orelse=[
                       ast.If(test=gen_hasattr_call(loaded, BOX_SIGNATURE),
                              body=[
                                  ast.Expr(gen_box_call(loaded, rhs)),
                              ],
                              orelse=[
                                  ast.Assign([lhs], rhs),
                              ])
                   ],
                   finalbody=[])