Example #1
0
 def p_async_with_stmt(self, p):
     ''' async_with_stmt : ASYNC with_stmt '''
     async_with = ast.AsyncWith()
     with_node = p[2]
     for attr in tuple(with_node._fields) + ('lineno', 'col_offset'):
         setattr(async_with, attr, getattr(with_node, attr))
     p[0] = async_with
def make_async(cursor_trail, tree):

    definition = core_logic.get_node_at_cursor(cursor_trail, tree)

    if type(definition) == ast.FunctionDef:
        return ast.AsyncFunctionDef(name=definition.name,
                                    args=definition.args,
                                    body=definition.body,
                                    decorator_list=definition.decorator_list,
                                    returns=definition.returns,
                                    type_comment=definition.type_comment)

    if type(definition) == ast.AsyncFunctionDef:
        return ast.FunctionDef(name=definition.name,
                               args=definition.args,
                               body=definition.body,
                               decorator_list=definition.decorator_list,
                               returns=definition.returns,
                               type_comment=definition.type_comment)

    if type(definition) == ast.With:
        return ast.AsyncWith(items=definition.items,
                             body=definition.body,
                             type_comment=definition.type_comment)

    if type(definition) == ast.AsyncWith:
        return ast.With(items=definition.items,
                        body=definition.body,
                        type_comment=definition.type_comment)

    if isinstance(definition, ast.comprehension):
        # Toggle the async
        definition.is_async = 0 if definition.is_async == 1 else 1
        return definition
Example #3
0
 def visit_AsyncWith(self, node):
     new_node = ast.AsyncWith(
         self._visit(node.items),
         self._visit(node.body),
     )
     ast.copy_location(new_node, node)
     return new_node
Example #4
0
def generate_async_with(max_depth=None):
    num_items = random.randrange(1, 3)
    items = [
        _generate_with_item(max_depth=max_depth) for _ in range(num_items)
    ]
    body = generate_block(max_depth=max_depth)
    return ast.AsyncWith(items, body)
Example #5
0
 def visit_AsyncWith(self, node):
     new_body = self.if_exists(node.body, self.visit_list)
     new_with_items = self.if_exists(node.items, self.visit_list)
     return_list = self.new_stmts + [
         ast.copy_location(ast.AsyncWith(new_with_items, new_body), node)
     ]
     self.new_stmts.clear()
     return return_list
    def visit_AsyncWith(self, node: AsyncWith, *args, **kwargs) -> C.AsyncWith:
        items = self.visit(node.items, *args, **kwargs)
        body = self.visit(node.body, *args, **kwargs)
        type_comment = self.visit(node.type_comment, *args, **kwargs)

        return C.AsyncWith(
            items=items,
            body=body,
            type_comment=type_comment,
        )
Example #7
0
    def visit_With(self, node):
        change_node = False
        new_items = []
        for item in node.items:
            if isinstance(item.context_expr, ast.YieldFrom):
                new_item = ast.withitem(
                    item.context_expr.value,
                    item.optional_vars,
                )
                new_items.append(new_item)
                change_node = True
            else:
                new_items.append(item)

        if not change_node:
            return node

        return ast.AsyncWith(items=new_items, body=node.body)
Example #8
0
def AsyncWith(draw, statements) -> ast.AsyncWith:
    items = draw(lists(expression(), min_size=1, max_size=3))
    body = draw(lists(statements, min_size=1, max_size=3))
    return ast.AsyncWith(
        [ast.withitem(context_expr=i, optional_vars=None) for i in items],
        body)