From eeca53e4f1a7aba51cce53bb9535520f486e3878 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Mon, 11 Jul 2022 10:03:00 +0900 Subject: [PATCH] Fix bad argument collection with optional positionals --- src/vm.c | 7 +++---- test/testOptionalAndCollected.krk | 9 +++++++++ test/testOptionalAndCollected.krk.expect | 6 ++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 test/testOptionalAndCollected.krk create mode 100644 test/testOptionalAndCollected.krk.expect diff --git a/src/vm.c b/src/vm.c index 520244f..9f7e705 100644 --- a/src/vm.c +++ b/src/vm.c @@ -890,13 +890,12 @@ _finishKwarg: argCountX = argCount - (!!(closure->function->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS) + !!(closure->function->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_KWS)); } else if ((size_t)argCount > potentialPositionalArgs && (closure->function->obj.flags & KRK_OBJ_FLAGS_CODEOBJECT_COLLECTS_ARGS)) { - krk_push(NONE_VAL()); krk_push(NONE_VAL()); krk_pop(); krk_pop(); KrkValue * startOfPositionals = &krk_currentThread.stackTop[-argCount]; - KrkValue tmp = krk_list_of(argCount - potentialPositionalArgs, - &startOfPositionals[potentialPositionalArgs], 0); + KrkValue tmp = krk_callNativeOnStack(argCount - potentialPositionalArgs, + &startOfPositionals[potentialPositionalArgs], 0, krk_list_of); startOfPositionals = &krk_currentThread.stackTop[-argCount]; startOfPositionals[offsetOfExtraArgs] = tmp; - argCount = closure->function->requiredArgs + 1; + argCount = potentialPositionalArgs + 1; argCountX = argCount - 1; while (krk_currentThread.stackTop > startOfPositionals + argCount) krk_pop(); } diff --git a/test/testOptionalAndCollected.krk b/test/testOptionalAndCollected.krk new file mode 100644 index 0000000..0d3faf0 --- /dev/null +++ b/test/testOptionalAndCollected.krk @@ -0,0 +1,9 @@ +def foo(a,b=42,*args,**kwargs): + print(a,b,args,kwargs) + +foo(1) +foo(1,2) +foo(1,2,3) +foo(1,2,3,4,5,6) +foo(1,2,f=7) +foo(b=2,a=7,k=96) diff --git a/test/testOptionalAndCollected.krk.expect b/test/testOptionalAndCollected.krk.expect new file mode 100644 index 0000000..5c62d36 --- /dev/null +++ b/test/testOptionalAndCollected.krk.expect @@ -0,0 +1,6 @@ +1 42 [] {} +1 2 [] {} +1 2 [3] {} +1 2 [3, 4, 5, 6] {} +1 2 [] {'f': 7} +7 2 [] {'k': 96}