From 13bfc829aa2b1a77d04727e981a370ec9cdcd3ff Mon Sep 17 00:00:00 2001 From: Lyon Date: Sun, 10 Sep 2023 10:22:58 +0800 Subject: [PATCH] support `in` op for **kw --- examples/builtins/kw.py | 12 ++++++++++++ package/PikaStdLib/PikaStdData_Dict.c | 13 +++---------- port/linux/.vscode/launch.json | 2 +- .../pikascript-lib/PikaStdLib/PikaStdData_Dict.c | 13 +++---------- port/linux/test/python/builtins/kw.py | 12 ++++++++++++ src/PikaVersion.h | 2 +- 6 files changed, 32 insertions(+), 22 deletions(-) create mode 100644 examples/builtins/kw.py create mode 100644 port/linux/test/python/builtins/kw.py diff --git a/examples/builtins/kw.py b/examples/builtins/kw.py new file mode 100644 index 000000000..f026c56c7 --- /dev/null +++ b/examples/builtins/kw.py @@ -0,0 +1,12 @@ +def test_kw(**kw): + if 'a' in kw: + return kw['a'] + if 'b' in kw: + return kw['b'] + +a = test_kw(a=1) +b = test_kw(b=2) +assert a == 1 +assert b == 2 + +print('PASS') diff --git a/package/PikaStdLib/PikaStdData_Dict.c b/package/PikaStdLib/PikaStdData_Dict.c index 29f9d5736..2b343360c 100644 --- a/package/PikaStdLib/PikaStdData_Dict.c +++ b/package/PikaStdLib/PikaStdData_Dict.c @@ -189,16 +189,9 @@ int PikaStdData_dict_keys___len__(PikaObj* self) { } int dict_contains(PikaDict* dict, Arg* key) { - int i = 0; - while (PIKA_TRUE) { - Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i); - if (NULL == item) { - break; - } - if (arg_isEqual(item, key)) { - return PIKA_TRUE; - } - i++; + Hash hash = hash_time33(arg_getStr(key)); + if (args_isArgExist_hash(_OBJ2DICT(dict), hash)) { + return pika_true; } return pika_false; } diff --git a/port/linux/.vscode/launch.json b/port/linux/.vscode/launch.json index f8e1e9b01..488cd9e9c 100644 --- a/port/linux/.vscode/launch.json +++ b/port/linux/.vscode/launch.json @@ -12,7 +12,7 @@ // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain", "args": [ // "--gtest_filter=vm.keyword_2" - "--gtest_filter=builtin.repl_mode" + // "--gtest_filter=builtin.kw" ], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c index 29f9d5736..2b343360c 100644 --- a/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c +++ b/port/linux/package/pikascript/pikascript-lib/PikaStdLib/PikaStdData_Dict.c @@ -189,16 +189,9 @@ int PikaStdData_dict_keys___len__(PikaObj* self) { } int dict_contains(PikaDict* dict, Arg* key) { - int i = 0; - while (PIKA_TRUE) { - Arg* item = args_getArgByIndex(_OBJ2KEYS(dict), i); - if (NULL == item) { - break; - } - if (arg_isEqual(item, key)) { - return PIKA_TRUE; - } - i++; + Hash hash = hash_time33(arg_getStr(key)); + if (args_isArgExist_hash(_OBJ2DICT(dict), hash)) { + return pika_true; } return pika_false; } diff --git a/port/linux/test/python/builtins/kw.py b/port/linux/test/python/builtins/kw.py new file mode 100644 index 000000000..f026c56c7 --- /dev/null +++ b/port/linux/test/python/builtins/kw.py @@ -0,0 +1,12 @@ +def test_kw(**kw): + if 'a' in kw: + return kw['a'] + if 'b' in kw: + return kw['b'] + +a = test_kw(a=1) +b = test_kw(b=2) +assert a == 1 +assert b == 2 + +print('PASS') diff --git a/src/PikaVersion.h b/src/PikaVersion.h index 547fcdb5d..05e2fcbb9 100644 --- a/src/PikaVersion.h +++ b/src/PikaVersion.h @@ -2,4 +2,4 @@ #define PIKA_VERSION_MINOR 12 #define PIKA_VERSION_MICRO 5 -#define PIKA_EDIT_TIME "2023/09/07 14:46:48" +#define PIKA_EDIT_TIME "2023/09/10 10:22:49"