From a73d460beb1f5a4f658dff2a45ca02b6c35dfa77 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 7 Apr 2013 19:06:02 +0200 Subject: [PATCH 1/2] try: allow empty body Signed-off-by: Julien Danjou --- hy/compiler.py | 9 +++++++-- tests/compilers/test_ast.py | 6 +----- tests/native_tests/language.hy | 3 +++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index cf0de49..ca4be73 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -176,7 +176,6 @@ class HyASTCompiler(object): tback=None) @builds("try") - @checkargs(min=1) def compile_try_expression(self, expr): expr.pop(0) # try @@ -186,10 +185,16 @@ class HyASTCompiler(object): else: Try = ast.TryExcept + if len(expr) == 0: + body = [ast.Pass(lineno=expr.start_line, + col_offset=expr.start_column)] + else: + body = self._code_branch(self.compile(expr.pop(0))) + return Try( lineno=expr.start_line, col_offset=expr.start_column, - body=self._code_branch(self.compile(expr.pop(0))), + body=body, handlers=[self.compile(s) for s in expr], finalbody=[], orelse=[]) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 31daf14..cfafca2 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -116,14 +116,10 @@ def test_ast_bad_raise(): def test_ast_good_try(): "Make sure AST can compile valid try" + hy_compile(tokenize("(try)")) hy_compile(tokenize("(try 1)")) -def test_ast_bad_try(): - "Make sure AST can't compile invalid try" - cant_compile("(try)") - - def test_ast_good_catch(): "Make sure AST can compile valid catch" hy_compile(tokenize("(catch)")) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 9eaf0f5..cbd5254 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -162,6 +162,9 @@ (defn test-exceptions [] "NATIVE: test Exceptions" + + (try) + (try (raise (KeyError)) (catch [[IOError]] (assert false)) From 0d3500f6e534cceb257b71d368f8cfa6b3a5d0dd Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 8 Apr 2013 00:36:08 +0200 Subject: [PATCH 2/2] try: accept a simpler form Signed-off-by: Julien Danjou --- hy/compiler.py | 17 ++++++++++++++++- tests/compilers/test_ast.py | 3 +++ tests/native_tests/language.hy | 6 ++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index ca4be73..fff1e60 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -186,16 +186,31 @@ class HyASTCompiler(object): Try = ast.TryExcept if len(expr) == 0: + # (try) body = [ast.Pass(lineno=expr.start_line, col_offset=expr.start_column)] else: + # (try something…) body = self._code_branch(self.compile(expr.pop(0))) + if len(expr) == 0: + # (try) or (try body) + handlers = [ast.ExceptHandler( + lineno=expr.start_line, + col_offset=expr.start_column, + type=None, + name=None, + body=[ast.Pass(lineno=expr.start_line, + col_offset=expr.start_column)])] + else: + # (try body except except…) + handlers = [self.compile(s) for s in expr] + return Try( lineno=expr.start_line, col_offset=expr.start_column, body=body, - handlers=[self.compile(s) for s in expr], + handlers=handlers, finalbody=[], orelse=[]) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index cfafca2..3a2d7ca 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -1,4 +1,5 @@ # Copyright (c) 2013 Paul Tagliamonte +# Copyright (c) 2013 Julien Danjou # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -118,6 +119,8 @@ def test_ast_good_try(): "Make sure AST can compile valid try" hy_compile(tokenize("(try)")) hy_compile(tokenize("(try 1)")) + hy_compile(tokenize("(try 1 bla)")) + hy_compile(tokenize("(try 1 bla bla)")) def test_ast_good_catch(): diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index cbd5254..588e3af 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -165,6 +165,12 @@ (try) + (try (pass)) + + (try (pass) (except)) + + (try (pass) (except [IOError]) (except)) + (try (raise (KeyError)) (catch [[IOError]] (assert false))