From 4138c660cc040aac1177086e3a236c7585e5c91f Mon Sep 17 00:00:00 2001 From: Ewald Grusk Date: Mon, 15 Jun 2015 22:42:02 +0200 Subject: [PATCH 1/3] Raise more appropriate error on missing kwarg value --- hy/compiler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 8e9893c..05a2fd5 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -459,8 +459,9 @@ class HyASTCompiler(object): try: value = next(exprs_iter) except StopIteration: - msg = "Keyword argument {kw} needs a value" - raise HyCompileError(msg.format(kw=str(expr))) + raise HyTypeError(expr, + "Keyword argument {kw} needs " + "a value.".format(kw=str(expr))) compiled_value = self.compile(value) ret += compiled_value From e407b0a605f42c7d266d9334dcc59e3558313d16 Mon Sep 17 00:00:00 2001 From: Ewald Grusk Date: Mon, 22 Jun 2015 20:18:04 +0200 Subject: [PATCH 2/3] New test: Ensure the compiler chokes on missing keyword argument values --- tests/compilers/test_ast.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 4491f83..633c3bb 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -439,6 +439,16 @@ def test_lambda_list_keywords_mixed(): " (list x xs kwxs kwoxs))") +def test_missing_keyword_argument_value(): + """Ensure the compiler chokes on missing keyword argument values.""" + try: + can_compile("((fn [x] x) :x)") + except HyTypeError as e: + assert(e.message == "Keyword argument \ufdd0:x needs a value.") + else: + assert(False) + + def test_ast_unicode_strings(): """Ensure we handle unicode strings correctly""" From b36294336572d6c4238613bf5be3288192d40fd3 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Tue, 11 Aug 2015 10:43:00 +0200 Subject: [PATCH 3/3] compiler: Fix the kw argument needs value exception Strip the \ufdd0 prefix from the keyword argument before turning it into a string: the same representation the user entered looks better, and is printable too, thus Python2 doesn't choke on it. Signed-off-by: Gergely Nagy --- hy/compiler.py | 2 +- tests/compilers/test_ast.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 05a2fd5..41119f1 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -461,7 +461,7 @@ class HyASTCompiler(object): except StopIteration: raise HyTypeError(expr, "Keyword argument {kw} needs " - "a value.".format(kw=str(expr))) + "a value.".format(kw=str(expr[1:]))) compiled_value = self.compile(value) ret += compiled_value diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 633c3bb..dd6ae59 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -444,7 +444,7 @@ def test_missing_keyword_argument_value(): try: can_compile("((fn [x] x) :x)") except HyTypeError as e: - assert(e.message == "Keyword argument \ufdd0:x needs a value.") + assert(e.message == "Keyword argument :x needs a value.") else: assert(False)