changeset 65:ce5e27a3f277

Add ClassDef.decorator_list support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 13 Jun 2016 01:27:10 +0100
parents 53817b39f139
children 32550e12aedf
files src/ast_convert.rs src/ast_dump.rs tests/test_parse_files/test_functiondef_decorators.py
diffstat 3 files changed, 29 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -553,13 +553,14 @@ fn parse_statement(py: Python, ast: PyOb
         let bases = ast.getattr(py, "bases").unwrap();
         //let keywords = ast.getattr(py, "keywords").unwrap();
         let body = ast.getattr(py, "body").unwrap();
-        //let decorator_list = ast.getattr(py, "decorator_list").unwrap();
+        let decorator_list = ast.getattr(py, "decorator_list").unwrap();
 
         let name = get_str(py, name);
         let bases = parse_list(py, bases, parse_expr);
         let body = parse_list(py, body, parse_statement);
+        let decorator_list = parse_list(py, decorator_list, parse_expr);
 
-        stmt::ClassDef(name, bases, vec!(), body, vec!())
+        stmt::ClassDef(name, bases, vec!(), body, decorator_list)
     } else if is_instance(&ast, &function_def_type) {
         let name = ast.getattr(py, "name").unwrap();
         let args = ast.getattr(py, "args").unwrap();
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -221,21 +221,35 @@ fn make_indent(indent: usize) -> String 
     iter::repeat("    ").take(indent).collect()
 }
 
+fn decorator_list_to_string(decorator_list: Vec<expr>, indent: usize) -> String {
+    let current_indent = make_indent(indent);
+    let decorators = vec_to_strings_vec(decorator_list);
+    if decorators.is_empty() {
+        String::new()
+    } else {
+        format!("{}@{}\n",
+            current_indent,
+            decorators.join(format!("\n{}@", current_indent).as_str())
+        )
+    }
+}
+
 impl stmt {
     fn to_string(&self, indent: usize) -> String {
         let current_indent = make_indent(indent);
         match self.clone() {
-            stmt::ClassDef(name, bases, keywords, body, decorator_list) => format!("{}class {}({}):\n{}", current_indent, name, vec_to_strings_vec(bases).join(", "), statements_to_string(indent, body)),
+            stmt::ClassDef(name, bases, keywords, body, decorator_list) => {
+                format!("{}{}class {}({}):\n{}",
+                    decorator_list_to_string(decorator_list, indent),
+                    current_indent,
+                    name,
+                    vec_to_strings_vec(bases).join(", "),
+                    statements_to_string(indent, body)
+                )
+            },
             stmt::FunctionDef(name, arguments, body, decorator_list, returns) => {
                 format!("{}{}def {}({}){}:\n{}",
-                    {
-                        let decorators = vec_to_strings_vec(decorator_list);
-                        if decorators.is_empty() {
-                            String::new()
-                        } else {
-                            format!("{}@{}\n", current_indent, decorators.join(format!("\n{}@", current_indent).as_str()))
-                        }
-                    },
+                    decorator_list_to_string(decorator_list, indent),
                     current_indent,
                     name,
                     arguments.to_string(),
--- a/tests/test_parse_files/test_functiondef_decorators.py
+++ b/tests/test_parse_files/test_functiondef_decorators.py
@@ -4,4 +4,7 @@ import cython
 @cython.cfunc
 def id(a):
     return a
+@cython.cclass
+class Coucou(object):
+    pass
 print(id(5))