From 5bb4132464c44e5b6bcb628dc936e7bf95cbc53a Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 14:23:41 +0200 Subject: [PATCH 1/6] Added test for the first fix: getting del to work --- test/test_codegen.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/test_codegen.py diff --git a/test/test_codegen.py b/test/test_codegen.py new file mode 100644 index 0000000..edf4105 --- /dev/null +++ b/test/test_codegen.py @@ -0,0 +1,12 @@ +import codegen +import ast + +def to_ast_and_back_again(source): + return codegen.to_source(ast.parse(source)) + +def test_del(): + source = "del l[0]" + assert source == to_ast_and_back_again(source) + source = "del obj.x" + assert source == to_ast_and_back_again(source) + assert source == to_ast_and_back_again(source) From b347e63413daca8259a62e0ff149bec40e434f3d Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 14:34:53 +0200 Subject: [PATCH 2/6] Fixed the bug concerning the del keyword --- codegen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen.py b/codegen.py index 113d9be..833b782 100644 --- a/codegen.py +++ b/codegen.py @@ -297,7 +297,7 @@ def visit_Print(self, node): def visit_Delete(self, node): self.newline(node) self.write('del ') - for idx, target in enumerate(node): + for idx, target in enumerate(node.targets): if idx: self.write(', ') self.visit(target) From ede19265b7a9fd12d9fbbf08c174a4d91777fe9b Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 14:56:29 +0200 Subject: [PATCH 3/6] Added test for the next fix: try-except --- test/test_codegen.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_codegen.py b/test/test_codegen.py index edf4105..6328349 100644 --- a/test/test_codegen.py +++ b/test/test_codegen.py @@ -9,4 +9,17 @@ def test_del(): assert source == to_ast_and_back_again(source) source = "del obj.x" assert source == to_ast_and_back_again(source) + +def test_try_expect(): + source = ("try:\n" + " '#'[2]\n" + "except IndexError:\n" + " print 'What did you expect?!'") + assert source == to_ast_and_back_again(source) + source = ("try:\n" + " l = []\n" + " l[1]\n" + "except IndexError, index_error:\n" + " print index_error") + assert source == to_ast_and_back_again(source) assert source == to_ast_and_back_again(source) From 4b892fa8e75b8ec437ce9f56d16a3ae07e6d80e3 Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 14:59:40 +0200 Subject: [PATCH 4/6] Fixed the bug concerning the try-except-syntax --- codegen.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/codegen.py b/codegen.py index 833b782..2f2899c 100644 --- a/codegen.py +++ b/codegen.py @@ -309,6 +309,16 @@ def visit_TryExcept(self, node): for handler in node.handlers: self.visit(handler) + def visit_ExceptHandler(self, node): + self.newline(node) + self.write('except ') + self.visit(node.type) + if node.name is not None: + self.write(', ') + self.visit(node.name) + self.write(':') + self.body(node.body) + def visit_TryFinally(self, node): self.newline(node) self.write('try:') From 151b88fc4e494326f4ced486cbe5633a8992b335 Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 15:05:30 +0200 Subject: [PATCH 5/6] Added a test for the "import ... as ..."-syntax --- test/test_codegen.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_codegen.py b/test/test_codegen.py index 6328349..91de0c0 100644 --- a/test/test_codegen.py +++ b/test/test_codegen.py @@ -22,4 +22,9 @@ def test_try_expect(): "except IndexError, index_error:\n" " print index_error") assert source == to_ast_and_back_again(source) + +def test_import(): + source = "import intertools as iterators" + assert source == to_ast_and_back_again(source) + source = "from math import floor as fl, ceil as cl" assert source == to_ast_and_back_again(source) From 4375bbc8b895670bf7c327a472d532270c95b497 Mon Sep 17 00:00:00 2001 From: gemoe100 Date: Thu, 4 Apr 2013 15:08:09 +0200 Subject: [PATCH 6/6] codegen can handle "import ... as ..."-syntax now. --- codegen.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/codegen.py b/codegen.py index 2f2899c..07ac543 100644 --- a/codegen.py +++ b/codegen.py @@ -169,7 +169,10 @@ def visit_ImportFrom(self, node): for idx, item in enumerate(node.names): if idx: self.write(', ') - self.write(item) + self.write(item.name) + if item.asname is not None: + self.write(' as ') + self.write(item.asname) def visit_Import(self, node): self.newline(node)