diff src/ast_convert.rs @ 91:859d44f143b8

Implement Bytes in the AST.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 25 Jun 2016 02:08:50 +0100
parents 5923cd4bfc36
children
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, name_constant};
 
-use cpython::{Python, PyObject, PyBool, PyResult};
+use cpython::{Python, PyObject, PyBool, PyResult, PyBytes};
 use cpython::ObjectProtocol; //for call method
 
 fn get_str(py: Python, object: PyObject) -> String {
@@ -338,6 +338,7 @@ fn parse_expr(py: Python, ast: PyObject)
     let name_type = ast_module.get(py, "Name").unwrap();
     let num_type = ast_module.get(py, "Num").unwrap();
     let str_type = ast_module.get(py, "Str").unwrap();
+    let bytes_type = ast_module.get(py, "Bytes").unwrap();
     let list_type = ast_module.get(py, "List").unwrap();
     let compare_type = ast_module.get(py, "Compare").unwrap();
     let call_type = ast_module.get(py, "Call").unwrap();
@@ -396,6 +397,11 @@ fn parse_expr(py: Python, ast: PyObject)
         let s = ast.getattr(py, "s").unwrap();
         let s = get_str(py, s);
         expr::Str(s)
+    } else if is_instance(&ast, &bytes_type) {
+        let s = ast.getattr(py, "s").unwrap();
+        let s: PyBytes = s.extract(py).unwrap();
+        let s = s.as_slice(py).to_vec();
+        expr::Bytes(s)
     } else if is_instance(&ast, &list_type) {
         let elts = ast.getattr(py, "elts").unwrap();
         let elements = parse_list(py, elts, parse_expr);