# HG changeset patch # User Emmanuel Gil Peyrot # Date 1465777630 -3600 # Node ID ce5e27a3f277660e861ad6dd2f300b2b66ef3ddc # Parent 53817b39f13952dbfe972f5c5a83a23643b39627 Add ClassDef.decorator_list support. diff --git a/src/ast_convert.rs b/src/ast_convert.rs --- 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(); diff --git a/src/ast_dump.rs b/src/ast_dump.rs --- 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, 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(), diff --git a/tests/test_parse_files/test_functiondef_decorators.py b/tests/test_parse_files/test_functiondef_decorators.py --- 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))