# HG changeset patch # User Bastien Orivel # Date 1465296696 -7200 # Node ID 38f59b0efc2c77a83818c8ca9927cedc89173bd3 # Parent b3f47c8150c00666af0da3f8880967675445efd2 Handle Import and add a test for it. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -434,6 +434,7 @@ fn parse_statement(py: Python, ast: PyOb let aug_assign_type = ast_module.get(py, "AugAssign").unwrap(); let return_type = ast_module.get(py, "Return").unwrap(); let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); + let import_type = ast_module.get(py, "Import").unwrap(); let if_type = ast_module.get(py, "If").unwrap(); let while_type = ast_module.get(py, "While").unwrap(); let for_type = ast_module.get(py, "For").unwrap(); @@ -545,6 +546,11 @@ fn parse_statement(py: Python, ast: PyOb let names = parse_list(py, names, parse_alias); stmt::ImportFrom(module, names, None) + } else if is_instance(&ast, &import_type) { + let names = ast.getattr(py, "names").unwrap(); + let names = parse_list(py, names, parse_alias); + + stmt::Import(names) } else if is_instance(&ast, &return_type) { let value = ast.getattr(py, "value").unwrap(); if value == py.None() { diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -200,6 +200,16 @@ impl stmt { } names_.join(", ") }), + stmt::Import(names) => format!("{}import {}", current_indent, { + let mut names_ = vec!(); + for name in names { + match name.asname { + None => names_.push(name.name), + Some(asname) => names_.push(format!("{} as {}", name.name, asname)) + } + } + names_.join(", ") + }), stmt::Expr(expr) => format!("{}{}", current_indent, expr.to_string()), stmt::Break => format!("{}break", current_indent), stmt::Delete(targets) => format!("{}del {}", current_indent, args_to_string(targets)) diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -55,7 +55,7 @@ pub enum stmt { //Assert(expr, Option) // Import(alias* names) - //Import(Vec) + Import(Vec), // ImportFrom(identifier? module, alias* names, int? level) ImportFrom(String, Vec, Option), // TODO: check the size of level. diff --git a/tests/test_parse_files/test_import.py b/tests/test_parse_files/test_import.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_import.py @@ -0,0 +1,1 @@ +import a, b as c