diff src/ast_convert.rs @ 53:1a815946c2e5

Implement keywords in expr::Call, and add some tests.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 12 Jun 2016 02:37:28 +0100
parents d9838e8b3ec5
children c3cc16b933d2
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -1,4 +1,4 @@
-use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension};
+use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword};
 
 use cpython::{Python, PyObject};
 use cpython::ObjectProtocol; //for call method
@@ -168,6 +168,21 @@ fn parse_comprehension(py: Python, ast: 
     comprehension {target: target, iter: iter, ifs: ifs}
 }
 
+fn parse_keyword(py: Python, ast: PyObject) -> keyword {
+    let arg = ast.getattr(py, "arg").unwrap();
+    let value = ast.getattr(py, "value").unwrap();
+
+    let arg = if arg == py.None() {
+        None
+    } else {
+        let arg = get_str(py, arg);
+        Some(arg)
+    };
+    let value = parse_expr(py, value);
+
+    keyword {arg: arg, value: value}
+}
+
 fn parse_operator(py: Python, ast: PyObject) -> operator {
     let builtins_module = py.import("builtins").unwrap();
     let isinstance = builtins_module.get(py, "isinstance").unwrap();
@@ -320,16 +335,9 @@ fn parse_expr(py: Python, ast: PyObject)
 
         let func = parse_expr(py, func);
         let args = parse_list(py, args, parse_expr);
+        let keywords = parse_list(py, keywords, parse_keyword);
 
-        /*
-        let mut kwargs = vec!();
-        for arg in keywords.iter(py).unwrap() {
-            let arg = arg.unwrap();
-            kwargs.push(parse_expr(py, arg));
-        }
-        */
-
-        expr::Call(Box::new(func), args, vec!())
+        expr::Call(Box::new(func), args, keywords)
     } else if is_instance(&ast, &compare_type) {
         let left = ast.getattr(py, "left").unwrap();
         let ops = ast.getattr(py, "ops").unwrap();