# HG changeset patch # User Bastien Orivel # Date 1465833259 -7200 # Node ID a73eaf42bea1d0d074e8e83f11257f92eca7e7f6 # Parent c59ad5ccd8a67bb743c75159fef5228c567ee053 Add ast.AsyncDefFunction. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -525,6 +525,7 @@ fn parse_statement(py: Python, ast: PyOb let ast_type = ast_module.get(py, "AST").unwrap(); let class_def_type = ast_module.get(py, "ClassDef").unwrap(); let function_def_type = ast_module.get(py, "FunctionDef").unwrap(); + let async_function_def_type = ast_module.get(py, "AsyncFunctionDef").unwrap(); let global_type = ast_module.get(py, "Global").unwrap(); let nonlocal_type = ast_module.get(py, "Nonlocal").unwrap(); let assign_type = ast_module.get(py, "Assign").unwrap(); @@ -575,7 +576,7 @@ fn parse_statement(py: Python, ast: PyOb let decorator_list = parse_list(py, decorator_list, parse_expr); stmt::ClassDef(name, bases, keywords, body, decorator_list) - } else if is_instance(&ast, &function_def_type) { + } else if is_instance(&ast, &function_def_type) || is_instance(&ast, &async_function_def_type) { let name = ast.getattr(py, "name").unwrap(); let args = ast.getattr(py, "args").unwrap(); let body = ast.getattr(py, "body").unwrap(); @@ -588,7 +589,7 @@ fn parse_statement(py: Python, ast: PyOb let decorator_list = parse_list(py, decorator_list, parse_expr); let returns = parse_optional_expr(py, returns); - stmt::FunctionDef(name, args, body, decorator_list, returns) + stmt::FunctionDef(name, args, body, decorator_list, returns, is_instance(&ast, &async_function_def_type)) } else if is_instance(&ast, &global_type) { let names = ast.getattr(py, "names").unwrap(); let names = parse_list(py, names, get_str); diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -272,9 +272,10 @@ impl stmt { statements_to_string(indent, body) ) }, - stmt::FunctionDef(name, arguments, body, decorator_list, returns) => { - format!("{}{}def {}({}){}:\n{}", + stmt::FunctionDef(name, arguments, body, decorator_list, returns, async) => { + format!("{}{}{}def {}({}){}:\n{}", decorator_list_to_string(decorator_list, indent), + if async { "async " } else { "" }, current_indent, name, arguments.to_string(), diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -9,10 +9,9 @@ pub struct Module { #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub enum stmt { // FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) - FunctionDef(String, arguments, Vec, Vec, Option), - // AsyncFunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns) - //AsyncFunctionDef(String/*, arguments, Vec), + // If the last field is true, then it's an AsyncFunctionDef. + FunctionDef(String, arguments, Vec, Vec, Option, bool), // ClassDef(identifier name, expr* bases, keyword* keywords, stmt* body, expr* decorator_list) ClassDef(String, Vec, Vec, Vec, Vec), diff --git a/tests/test_parse_files/test_async_function_def.py b/tests/test_parse_files/test_async_function_def.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_async_function_def.py @@ -0,0 +1,8 @@ +async def func0(): + pass +async def func1(a, b: str) -> None: + pass +async def func2(a, *, b: composite.type, c=2, d: int=3): + pass +async def func3(a, c=1, *args, b=2, **kwargs): + pass