From 83bb1513db59e72b362688b6082868d0d36676e7 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Sat, 2 Nov 2013 20:11:18 +0100 Subject: [PATCH] Make apply api-compatible with python (args is not mandatory) --- hy/compiler.py | 40 ++++++++++++++++------------------ tests/native_tests/language.hy | 4 +++- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index a1ef47d..cd69df2 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1255,35 +1255,33 @@ class HyASTCompiler(object): return ret - # apply only needs to be defined for python3 - if sys.version_info[0] >= 3: - @builds("apply") - @checkargs(min=2, max=3) - def compile_apply_expression(self, expr): - expr.pop(0) # apply - call = self.compile(expr.pop(0)) - call = ast.Call(func=call.expr, - args=[], - keywords=[], - starargs=None, - kwargs=None, - lineno=expr.start_line, - col_offset=expr.start_column) - ret = call + @builds("apply") + @checkargs(min=1, max=3) + def compile_apply_expression(self, expr): + expr.pop(0) # apply + call = self.compile(expr.pop(0)) + call = ast.Call(func=call.expr, + args=[], + keywords=[], + starargs=None, + kwargs=None, + lineno=expr.start_line, + col_offset=expr.start_column) + ret = call - #add star args if any + if expr: stargs = expr.pop(0) if stargs is not None: stargs = self.compile(stargs) call.starargs = stargs.force_expr ret = stargs + ret - if expr != []: - kwargs = self.compile(expr.pop(0)) - call.kwargs = kwargs.force_expr - ret = kwargs + ret + if expr: + kwargs = self.compile(expr.pop(0)) + call.kwargs = kwargs.force_expr + ret = kwargs + ret - return ret + return ret @builds("not") @builds("~") diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 4bf35d5..6521589 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -168,7 +168,9 @@ (assert (= (apply sumit [1] {"b" 2 "c" 3}) 6)) (assert (= (apply sumit [1 2 2]) 5)) (assert (= (apply sumit [] {"a" 1 "b" 1 "c" 2}) 4)) - (assert (= (apply sumit ((fn [] [1 1])) {"c" 1}) 3))) + (assert (= (apply sumit ((fn [] [1 1])) {"c" 1}) 3)) + (defn noargs [] [1 2 3]) + (assert (= (apply noargs) [1 2 3]))) (defn test-dotted [] "NATIVE: test dotted invocation"