Make apply api-compatible with python (args is not mandatory)

This commit is contained in:
Nicolas Dandrimont 2013-11-02 20:11:18 +01:00
parent f5754b404e
commit 83bb1513db
2 changed files with 22 additions and 22 deletions

View File

@ -1255,35 +1255,33 @@ class HyASTCompiler(object):
return ret return ret
# apply only needs to be defined for python3 @builds("apply")
if sys.version_info[0] >= 3: @checkargs(min=1, max=3)
@builds("apply") def compile_apply_expression(self, expr):
@checkargs(min=2, max=3) expr.pop(0) # apply
def compile_apply_expression(self, expr): call = self.compile(expr.pop(0))
expr.pop(0) # apply call = ast.Call(func=call.expr,
call = self.compile(expr.pop(0)) args=[],
call = ast.Call(func=call.expr, keywords=[],
args=[], starargs=None,
keywords=[], kwargs=None,
starargs=None, lineno=expr.start_line,
kwargs=None, col_offset=expr.start_column)
lineno=expr.start_line, ret = call
col_offset=expr.start_column)
ret = call
#add star args if any if expr:
stargs = expr.pop(0) stargs = expr.pop(0)
if stargs is not None: if stargs is not None:
stargs = self.compile(stargs) stargs = self.compile(stargs)
call.starargs = stargs.force_expr call.starargs = stargs.force_expr
ret = stargs + ret ret = stargs + ret
if expr != []: if expr:
kwargs = self.compile(expr.pop(0)) kwargs = self.compile(expr.pop(0))
call.kwargs = kwargs.force_expr call.kwargs = kwargs.force_expr
ret = kwargs + ret ret = kwargs + ret
return ret return ret
@builds("not") @builds("not")
@builds("~") @builds("~")

View File

@ -168,7 +168,9 @@
(assert (= (apply sumit [1] {"b" 2 "c" 3}) 6)) (assert (= (apply sumit [1] {"b" 2 "c" 3}) 6))
(assert (= (apply sumit [1 2 2]) 5)) (assert (= (apply sumit [1 2 2]) 5))
(assert (= (apply sumit [] {"a" 1 "b" 1 "c" 2}) 4)) (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 [] (defn test-dotted []
"NATIVE: test dotted invocation" "NATIVE: test dotted invocation"