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
# 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("~")

View File

@ -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"