Esempio n. 1
0
def atom_expr_rewrite(a: t.Optional[Tokenizer], atom: ast.AST,
                      trailers: t.List[t.Callable[[ast.AST], ast.Suite]]):
    for each in trailers:
        atom = each(atom)

    if a:
        atom = ast.Await(**(loc @ a), value=atom)
    return atom
Esempio n. 2
0
def r_await(node: ast.Subscript, ctx: Context, clauses: list):
    ctx.assert_clause_num_at_most(clauses, 2)

    await_clause = clauses[0]
    ctx.assert_no_head(await_clause)
    ctx.assert_single_body(await_clause)
    value = ctx.compile(await_clause.unwrap_body())

    return copy_lineinfo(node, ast.Await(value=value))
Esempio n. 3
0
def p_call_expr(p):
    '''expression : expression tuple'''
    #| PURE expression tuple'''
    if len(p) == 3:
        runner = ast.Name("_hio_interpret_call", ast.Load())
        args = list(p[2])
        args.insert(0, p[1])
        call = ast.Call(runner, args, [])
        p[0] = ast.Await(call)
    else:
        p[0] = ast.Call(p[2], list(p[3]), [])
Esempio n. 4
0
 def p_atom_expr4(self, p):
     ''' atom_expr : AWAIT atom trailer_list '''
     root = p[2]
     for node in p[3]:
         if isinstance(node, ast.Call):
             node.func = root
         elif isinstance(node, ast.Attribute):
             node.value = root
         elif isinstance(node, ast.Subscript):
             node.value = root
         else:
             raise TypeError('Unexpected trailer node: %s' % node)
         root = node
     p[0] = ast.Await(value=node)
Esempio n. 5
0
 def visit_Call(self, node):
     if type(node.func) is ast.Name and node.func.id.startswith(
             '__await__'):
         if self.params[get_await_param(node.func.id)]:  # pragma: no cover
             if ASYNC_AWAIT:
                 return ast.Await(value=node.args[0],
                                  lineno=node.lineno,
                                  col_offset=node.col_offset)
             else:
                 return ast.YieldFrom(value=node.args[0],
                                      lineno=node.lineno,
                                      col_offset=node.col_offset)
         else:
             return node.args[0]
     return self.generic_visit(node)
Esempio n. 6
0
def build_ret(method, rtype_arg):
    tmpl = (
        "return self._make_request(self.b.artist(artist_id=artist_id), Artist)"
    )
    ret = ast.parse(tmpl).body[0]

    ret.value.args[1] = rtype_arg  # type: ignore

    # TODO: Skip first arg (it's self)
    ret.value.args[0].args = []  # type: ignore
    ret.value.args[0].keywords = signature_to_keywords(method)  # type: ignore
    ret.value.args[0].func.attr = method.name  # type: ignore

    async_ret = deepcopy(ret)
    async_ret.value = ast.Await(ret.value)  # type: ignore
    return ret, async_ret
Esempio n. 7
0
def add_extras(klass: ast.ClassDef):
    if not klass.name.endswith("Auth"):
        return
    assign_methods = [
        (
            "authorize_user",
            "get_token_from_code",
            "token",
            _doc_authorize_user,
        ),
        (
            "authorize_client",
            "get_token_from_client_credentials",
            "token",
            _doc_authorize_client,
        ),
        (
            "refresh_authorization",
            "get_token_from_refresh_token",
            "token",
            _doc_refresh_authorization,
        ),
    ]
    tmpl = 'self._assign_result("token", self.method_name())'
    for methname, mirroredname, assignto, docstring in assign_methods:
        mirrored = find_method(klass, mirroredname)
        meth = deepcopy(mirrored)
        meth.name = methname
        keywords = signature_to_keywords(mirrored)
        assignment = ast.parse(tmpl).body[0]

        assignment.value.args[0].s = assignto  # type: ignore
        assignment.value.args[1].func.attr = mirrored.name  # type: ignore
        assignment.value.args[1].keywords = keywords  # type: ignore
        if isinstance(meth, ast.AsyncFunctionDef):
            assignment = ast.Expr(ast.Await(assignment.value))  # type: ignore

        body = [assignment]
        if docstring:
            docobj = ast.Expr(ast.Str(docstring))
            body.insert(0, docobj)

        meth.body = body
        meth.returns = None
        klass.body.append(meth)
Esempio n. 8
0
 def visit_YieldFrom(self, node):
     return ast.Await(node.value)
Esempio n. 9
0
                [ast.ImportFrom("__future__", names=[ast.alias(name="lol")])]),
            "'lol' is not defined",
        ),
        (
            ast.Assign(targets=[ast.Starred(), ast.Starred()]),
            "More then one starred expression",
        ),
        (
            ast.Assign(targets=([ast.Name("i")] * 500 + [ast.Starred()])),
            "Too many expressions",
        ),
        (
            ast.Assign(targets=([ast.Name("i")] * 500 + [ast.Starred()])),
            "Too many expressions",
        ),
        (ast.comprehension(is_async=1), "inside of a coroutine"),
        (ast.Yield(), "inside of a function"),
        (
            ast.AsyncFunctionDef(body=[ast.YieldFrom()]),
            "can't be used in a coroutine",
        ),
        (ast.Await(), "inside of a function"),
        (ast.FunctionDef(body=[ast.Await()]), "inside of a coroutine"),
    ],
)
def test_simple_ast_validator(node, message):
    validator = ContextualASTValidator()
    with pytest.raises(SyntaxError) as cm:
        validator.validate(ast.fix_missing_locations(node))
    assert cm.match(message)
Esempio n. 10
0
 def visit_Call(self, node):
     if self.is_magic_call(node):
         return ast.copy_location(ast.Await(value=node), node)
     return node
Esempio n. 11
0
 def p_atom_expr3(self, p):
     ''' atom_expr : AWAIT atom '''
     p[0] = ast.Await(value=p[2])
    def visit_Await(self, node: Await, *args, **kwargs) -> C.Await:
        value = self.visit(node.value, *args, **kwargs)

        return C.Await(value=value, )
def make_await():
    """Await(expr value)"""

    # TODO: Use the selected expression if possible
    return ast.Await(value=make_expression())
Esempio n. 14
0
def r_await(node: ast.Attribute, ctx: Context):
    value = ctx.compile(node.value)
    return copy_lineinfo(node, ast.Await(value=value))
Esempio n. 15
0
def generate_await(max_depth=None):
    return ast.Expr(ast.Await(generate_expression(max_depth=max_depth - 1)))
Esempio n. 16
0
def Await(draw, expression) -> ast.Await:
    return ast.Await(draw(expression))
Esempio n. 17
0
 def visit_Call(self, node: ast.Call) -> Union[ast.Call, ast.Await]:
     if ((not isinstance(node.func, ast.Name) or node.func.id != AWAIT_FUNC_NAME
          or len(node.args) != 1 or len(node.keywords) != 0)):
         return node
     return ast.copy_location(ast.Await(value=node.args[0]), node)