From b36018981a15db232b572dd9fe5e8f952e183dc3 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 5 Apr 2013 17:37:55 +0200 Subject: [PATCH] if: emit error on too many args Signed-off-by: Julien Danjou --- hy/compiler.py | 4 +++- tests/compilers/test_ast.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hy/compiler.py b/hy/compiler.py index 4a72b1a..4c19796 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -176,8 +176,10 @@ class HyASTCompiler(object): raise TypeError("if expects at least 2 arguments, got 1") body = self._code_branch(self.compile(body)) orel = [] - if len(expr) > 0: + if len(expr) == 1: orel = self._code_branch(self.compile(expr.pop(0))) + elif len(expr) > 1: + raise TypeError("if expects 2 or 3 arguments, got %d" % (len(expr) + 2)) return ast.If(test=test, body=body, diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 88208d1..87d7cc3 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -61,6 +61,15 @@ def test_ast_bad_if_1_arg(): pass +def test_ast_bad_if_too_much_arg(): + "Make sure AST can't compile invalid if" + try: + hy_compile(tokenize("(if 1 2 3 4 5)")) + assert False + except TypeError: + pass + + def test_ast_valid_if(): "Make sure AST can't compile invalid if" hy_compile(tokenize("(if foo bar)"))