# HG changeset patch # User Bastien Orivel # Date 1464904074 -7200 # Node ID f1c77634a2eb4b24bb0f97d9a0e0dd634e743ecb # Parent b21a246349f3fae939233baf53296802eacebbc6 Add ast.Tuple diff --git a/example/dictcomp.py b/example/dictcomp.py --- a/example/dictcomp.py +++ b/example/dictcomp.py @@ -1,1 +1,1 @@ -d = {key: 4 for key in imaginary_list if key % 12} +d = {key: value for key, value in imaginary_dict.items() if key % 12} diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -217,6 +217,7 @@ fn parse_expr(py: Python, ast: PyObject) let call_type = ast_module.get(py, "Call").unwrap(); let listcomp_type = ast_module.get(py, "ListComp").unwrap(); let dictcomp_type = ast_module.get(py, "DictComp").unwrap(); + let tuple_type = ast_module.get(py, "Tuple").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -352,6 +353,17 @@ fn parse_expr(py: Python, ast: PyObject) new_gens.push(gen); } expr::DictComp(Box::new(key), Box::new(value), new_gens) + } else if is_instance(&ast, &tuple_type) { + let elts = ast.getattr(py, "elts").unwrap(); + let elts = elts.iter(py).unwrap(); + + let mut new_elts = vec!(); + for elt in elts { + let elt = elt.unwrap(); + let elt = parse_expr(py, elt); + new_elts.push(elt); + } + expr::Tuple(new_elts, expr_context::Load) } else { println!("expr {}", ast); unreachable!() diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -148,7 +148,8 @@ impl expr { expr::Name(name, ctx) => format!("{}", name), expr::List(elts, ctx) => format!("[{}]", args_to_string(elts)), expr::ListComp(elt, generators) => format!("[{} {}]", elt.to_string(), generators_to_string(generators)), - expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), generators_to_string(generators)) + expr::DictComp(key, value, generators) => format!("{{{}: {} {}}}", key.to_string(), value.to_string(), generators_to_string(generators)), + expr::Tuple(elts, ctx) => format!("({})", args_to_string(elts)) } } } diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -111,7 +111,7 @@ pub enum expr { //Starred(Box, expr_context), Name(String, expr_context), List(Vec, expr_context), - //Tuple(Vec, expr_context), + Tuple(Vec, expr_context), } #[derive(Clone, Debug, PartialEq, Eq, Hash)]