Mercurial > python-compiler.rs
diff src/ast_convert.rs @ 86:a7b1db623f41
Implement NameConstant correctly.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 22 Jun 2016 22:55:55 +0100 |
parents | e59cd5754268 |
children | 624393ed3b0b |
line wrap: on
line diff
--- a/src/ast_convert.rs +++ b/src/ast_convert.rs @@ -1,6 +1,6 @@ -use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice}; +use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice, name_constant}; -use cpython::{Python, PyObject}; +use cpython::{Python, PyObject, PyBool, PyResult}; use cpython::ObjectProtocol; //for call method fn get_str(py: Python, object: PyObject) -> String { @@ -369,8 +369,19 @@ fn parse_expr(py: Python, ast: PyObject) expr::Name(id, get_ctx(py, ast)) } else if is_instance(&ast, &name_constant_type) { let value = ast.getattr(py, "value").unwrap(); - let value = get_str(py, value); - expr::NameConstant(value) + let boolean: PyResult<PyBool> = value.extract(py); + let constant = match boolean { + Ok(boolean) => if boolean.is_true() { + name_constant::True + } else { + name_constant::False + }, + Err(_) => { + assert!(value == py.None()); + name_constant::None_ + } + }; + expr::NameConstant(constant) } else if is_instance(&ast, &num_type) { let n = ast.getattr(py, "n").unwrap(); let n = get_str(py, n);