From 50daf1b4c89f40e835478723eaea2021f980e3d5 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 24 Apr 2013 22:34:14 +0200 Subject: [PATCH] Make temp attribute more solid and add unit test on regression Signed-off-by: Julien Danjou --- hy/compiler.py | 2 +- hy/util.py | 6 ++++-- tests/compilers/test_ast.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index daa3aae..c6f63ad 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -66,7 +66,7 @@ class HyTypeError(TypeError): self.expression = expression def __str__(self): - return (self.message + " (line %s, column %d)" + return (super(HyTypeError, self).__str__() + " (line %s, column %d)" % (self.expression.start_line, self.expression.start_column)) diff --git a/hy/util.py b/hy/util.py index 6b26543..faa36f9 100644 --- a/hy/util.py +++ b/hy/util.py @@ -34,8 +34,10 @@ def temporary_attribute_value(obj, attribute, value): """Temporarily switch an object attribute value to another value.""" original_value = getattr(obj, attribute) setattr(obj, attribute, value) - yield - setattr(obj, attribute, original_value) + try: + yield + finally: + setattr(obj, attribute, original_value) def flatten_literal_list(entry): diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 59e01ba..bb0eaed 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -391,3 +391,14 @@ def test_ast_unicode_strings(): assert _compile_string("test") == "test" assert _compile_string("\u03b1\u03b2") == "\u03b1\u03b2" assert _compile_string("\xc3\xa9") == "\xc3\xa9" + + +def test_compile_error(): + """Ensure we get compile error in tricky cases""" + try: + hy_compile(tokenize("(fn [] (= 1))")) + except HyCompileError as e: + assert(str(e) + == "`=' needs at least 2 arguments, got 1 (line 1, column 8)") + else: + assert(False)