This commit is contained in:
blueloveTH 2024-08-15 01:24:40 +08:00
parent 3e7af41e1f
commit 2f14689e2f
4 changed files with 34 additions and 39 deletions

View File

@ -543,7 +543,11 @@ PK_EXPORT bool py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) PY_RAISE;
/// -1: error, 0: not found, 1: found (and deleted) /// -1: error, 0: not found, 1: found (and deleted)
PK_EXPORT int py_dict_delitem(py_Ref self, py_Ref key) PY_RAISE; PK_EXPORT int py_dict_delitem(py_Ref self, py_Ref key) PY_RAISE;
/// -1: error, 0: not found, 1: found /// -1: error, 0: not found, 1: found
PK_EXPORT int py_dict_contains(py_Ref self, py_Ref key) PY_RAISE; PK_EXPORT int py_dict_getitem_by_str(py_Ref self, const char* key) PY_RAISE PY_RETURN;
/// true: success, false: error
PK_EXPORT bool py_dict_setitem_by_str(py_Ref self, const char* key, py_Ref val) PY_RAISE;
/// -1: error, 0: not found, 1: found (and deleted)
PK_EXPORT int py_dict_delitem_by_str(py_Ref self, const char* key) PY_RAISE;
/// true: success, false: error /// true: success, false: error
PK_EXPORT bool PK_EXPORT bool
py_dict_apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE; py_dict_apply(py_Ref self, bool (*f)(py_Ref key, py_Ref val, void* ctx), void* ctx) PY_RAISE;

View File

@ -392,11 +392,9 @@ static bool
co->name->data); co->name->data);
} else { } else {
// add to **kwargs // add to **kwargs
py_Ref tmp = py_pushtmp(); bool ok = py_dict_setitem_by_str(&buffer[decl->starred_kwarg],
c11_sv key_sv = py_name2sv(key); py_name2str(key),
py_newstrn(tmp, key_sv.data, key_sv.size); &p1[2 * j + 1]);
bool ok = py_dict_setitem(&buffer[decl->starred_kwarg], tmp, &p1[2 * j + 1]);
py_pop();
if(!ok) return false; if(!ok) return false;
} }
} }

View File

@ -521,25 +521,40 @@ int py_dict_getitem(py_Ref self, py_Ref key) {
return 0; return 0;
} }
int py_dict_delitem(py_Ref self, py_Ref key) {
assert(py_isdict(self));
Dict* ud = py_touserdata(self);
return Dict__pop(ud, key);
}
bool py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) { bool py_dict_setitem(py_Ref self, py_Ref key, py_Ref val) {
assert(py_isdict(self)); assert(py_isdict(self));
Dict* ud = py_touserdata(self); Dict* ud = py_touserdata(self);
return Dict__set(ud, key, val); return Dict__set(ud, key, val);
} }
int py_dict_contains(py_Ref self, py_Ref key) { int py_dict_delitem(py_Ref self, py_Ref key) {
assert(py_isdict(self)); assert(py_isdict(self));
Dict* ud = py_touserdata(self); Dict* ud = py_touserdata(self);
DictEntry* entry; return Dict__pop(ud, key);
bool ok = Dict__try_get(ud, key, &entry); }
if(!ok) return -1;
return entry ? 1 : 0; int py_dict_getitem_by_str(py_Ref self, const char *key){
py_Ref tmp = py_pushtmp();
py_newstr(tmp, key);
int res = py_dict_getitem(self, tmp);
py_pop();
return res;
}
bool py_dict_setitem_by_str(py_Ref self, const char *key, py_Ref val){
py_Ref tmp = py_pushtmp();
py_newstr(tmp, key);
bool res = py_dict_setitem(self, tmp, val);
py_pop();
return res;
}
int py_dict_delitem_by_str(py_Ref self, const char *key){
py_Ref tmp = py_pushtmp();
py_newstr(tmp, key);
int res = py_dict_delitem(self, tmp);
py_pop();
return res;
} }
int py_dict_len(py_Ref self) { int py_dict_len(py_Ref self) {

View File

@ -1,22 +0,0 @@
# @szdytom favored testing, set BUILD_TESTING to enable it
# You can use scripts/run_tests.py as an alternative
# Note: the CI uses scripts/run_tests.py to run the tests
cmake_minimum_required(VERSION 3.10)
function(pkpy_add_test pyfile)
get_filename_component(test_name ${pyfile} NAME_WE)
add_test(
NAME ${test_name}
COMMAND $<TARGET_FILE:main> ${pyfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/..
)
endfunction()
message("Testing enabled")
file(GLOB PK_PYTHON_TESTCASES_FILES RELATIVE ${CMAKE_CURRENT_LIST_DIR}/.. "*.py")
foreach(pyfile ${PK_PYTHON_TESTCASES_FILES})
pkpy_add_test(${pyfile})
endforeach()