# HG changeset patch # User Bastien Orivel # Date 1465748475 -7200 # Node ID e5a808ec31c07c51828c39a9fa6c6f32e1fca51c # Parent c3cc16b933d200e7cca4375441e54d21029b2ebb Add ast.Assert. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -512,6 +512,7 @@ fn parse_statement(py: Python, ast: PyOb let delete_type = ast_module.get(py, "Delete").unwrap(); let pass_type = ast_module.get(py, "Pass").unwrap(); let continue_type = ast_module.get(py, "Continue").unwrap(); + let assert_type = ast_module.get(py, "Assert").unwrap(); assert!(is_instance(&ast, &ast_type)); @@ -653,6 +654,17 @@ fn parse_statement(py: Python, ast: PyOb stmt::Pass } else if is_instance(&ast, &continue_type) { stmt::Continue + } else if is_instance(&ast, &assert_type) { + let test = ast.getattr(py, "test").unwrap(); + let test = parse_expr(py, test); + let msg = ast.getattr(py, "msg").unwrap(); + + if msg == py.None() { + stmt::Assert(test, None) + } else { + let msg = parse_expr(py, msg); + stmt::Assert(test, Some(msg)) + } } else { println!("stmt {}", ast); panic!() diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- a/src/ast_dump.rs +++ b/src/ast_dump.rs @@ -265,7 +265,19 @@ impl stmt { stmt::Break => format!("{}break", current_indent), stmt::Delete(targets) => format!("{}del {}", current_indent, vec_to_strings_vec(targets).join(", ")), stmt::Pass => format!("{}pass", current_indent), - stmt::Continue => format!("{}continue", current_indent) + stmt::Continue => format!("{}continue", current_indent), + stmt::Assert(test, msg) => { + format!("{}assert {}{}", + current_indent, + test.to_string(), + { + match msg { + Some(msg) => format!(", {}", msg.to_string()), + None => format!("") + } + } + ) + } } } } diff --git a/src/python_ast.rs b/src/python_ast.rs --- a/src/python_ast.rs +++ b/src/python_ast.rs @@ -52,7 +52,7 @@ pub enum stmt { //Try(Vec, Vec, Vec, Vec) // Assert(expr test, expr? msg) - //Assert(expr, Option) + Assert(expr, Option), // Import(alias* names) Import(Vec), diff --git a/tests/test_parse_files/test_assert.py b/tests/test_parse_files/test_assert.py new file mode 100644 --- /dev/null +++ b/tests/test_parse_files/test_assert.py @@ -0,0 +1,3 @@ +assert True +assert a != b, "error" +assert a != b, (a, b)