changeset 51:ded1907b7a69

Add ast.Nonlocal.
author Bastien Orivel <eijebong@bananium.fr>
date Wed, 08 Jun 2016 17:26:09 +0200
parents 5edbc24b625f
children d9838e8b3ec5
files src/ast_convert.rs src/ast_dump.rs src/python_ast.rs tests/test_parse_files/test_nonlocal.py
diffstat 4 files changed, 9 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/ast_convert.rs
+++ b/src/ast_convert.rs
@@ -430,6 +430,7 @@ fn parse_statement(py: Python, ast: PyOb
     let class_def_type = ast_module.get(py, "ClassDef").unwrap();
     let function_def_type = ast_module.get(py, "FunctionDef").unwrap();
     let global_type = ast_module.get(py, "Global").unwrap();
+    let nonlocal_type = ast_module.get(py, "Nonlocal").unwrap();
     let assign_type = ast_module.get(py, "Assign").unwrap();
     let aug_assign_type = ast_module.get(py, "AugAssign").unwrap();
     let return_type = ast_module.get(py, "Return").unwrap();
@@ -489,6 +490,10 @@ fn parse_statement(py: Python, ast: PyOb
         let names = ast.getattr(py, "names").unwrap();
         let names = parse_list(py, names, get_str);
         stmt::Global(names)
+    } else if is_instance(&ast, &nonlocal_type) {
+        let names = ast.getattr(py, "names").unwrap();
+        let names = parse_list(py, names, get_str);
+        stmt::Nonlocal(names)
     } else if is_instance(&ast, &if_type) {
         let test = ast.getattr(py, "test").unwrap();
         let body = ast.getattr(py, "body").unwrap();
--- a/src/ast_dump.rs
+++ b/src/ast_dump.rs
@@ -181,6 +181,7 @@ impl stmt {
             stmt::ClassDef(name, bases, keywords, body, decorator_list) => format!("{}class {}({}):\n{}", current_indent, name, args_to_string(bases), statements_to_string(indent, body)),
             stmt::FunctionDef(name, arguments, body, decorator_list, returns) => format!("{}def {}({}):\n{}", current_indent, name, arguments_to_string(arguments), statements_to_string(indent, body)),
             stmt::Global(names) => format!("{}global {}", current_indent, names.join(", ")),
+            stmt::Nonlocal(names) => format!("{}nonlocal {}", current_indent, names.join(", ")),
             stmt::If(test, body, orelse) => format!("{}if {}:\n{}", current_indent, test.to_string(), if_else_statements_to_string(indent, body, orelse)),
             stmt::While(test, body, orelse) => format!("{}while {}:\n{}", current_indent, test.to_string(), if_else_statements_to_string(indent, body, orelse)),
             stmt::For(target, iter, body, orelse) => format!("{}for {} in {}:\n{}", current_indent, target.to_string(), iter.to_string(), if_else_statements_to_string(indent, body, orelse)),
--- a/src/python_ast.rs
+++ b/src/python_ast.rs
@@ -64,7 +64,7 @@ pub enum stmt {
     Global(Vec<String>),
 
     // Nonlocal(identifier* names)
-    //Nonlocal(Vec<String>),
+    Nonlocal(Vec<String>),
 
     // expr(expr value)
     Expr(expr),
new file mode 100644
--- /dev/null
+++ b/tests/test_parse_files/test_nonlocal.py
@@ -0,0 +1,2 @@
+def a():
+    nonlocal b, c