comparison 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
comparison
equal deleted inserted replaced
85:09f5e0d7bcf3 86:a7b1db623f41
1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice}; 1 use python_ast::{Module, stmt, expr, expr_context, cmpop, boolop, operator, unaryop, arguments, arg, alias, comprehension, keyword, withitem, excepthandler, slice, name_constant};
2 2
3 use cpython::{Python, PyObject}; 3 use cpython::{Python, PyObject, PyBool, PyResult};
4 use cpython::ObjectProtocol; //for call method 4 use cpython::ObjectProtocol; //for call method
5 5
6 fn get_str(py: Python, object: PyObject) -> String { 6 fn get_str(py: Python, object: PyObject) -> String {
7 let pystring = object.str(py).unwrap(); 7 let pystring = object.str(py).unwrap();
8 let mut string = pystring.to_string(py).unwrap(); 8 let mut string = pystring.to_string(py).unwrap();
367 let id = ast.getattr(py, "id").unwrap(); 367 let id = ast.getattr(py, "id").unwrap();
368 let id = get_str(py, id); 368 let id = get_str(py, id);
369 expr::Name(id, get_ctx(py, ast)) 369 expr::Name(id, get_ctx(py, ast))
370 } else if is_instance(&ast, &name_constant_type) { 370 } else if is_instance(&ast, &name_constant_type) {
371 let value = ast.getattr(py, "value").unwrap(); 371 let value = ast.getattr(py, "value").unwrap();
372 let value = get_str(py, value); 372 let boolean: PyResult<PyBool> = value.extract(py);
373 expr::NameConstant(value) 373 let constant = match boolean {
374 Ok(boolean) => if boolean.is_true() {
375 name_constant::True
376 } else {
377 name_constant::False
378 },
379 Err(_) => {
380 assert!(value == py.None());
381 name_constant::None_
382 }
383 };
384 expr::NameConstant(constant)
374 } else if is_instance(&ast, &num_type) { 385 } else if is_instance(&ast, &num_type) {
375 let n = ast.getattr(py, "n").unwrap(); 386 let n = ast.getattr(py, "n").unwrap();
376 let n = get_str(py, n); 387 let n = get_str(py, n);
377 expr::Num(n) 388 expr::Num(n)
378 } else if is_instance(&ast, &str_type) { 389 } else if is_instance(&ast, &str_type) {