Skip to content

Commit 24dcc06

Browse files
committed
add dict.keys
1 parent 475476d commit 24dcc06

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

tests/snippets/dict.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ def dict_eq(d1, d2):
2929
assert a[key] == value
3030
count += 1
3131
assert count == len(a)
32+
33+
res = set()
34+
for key in a.keys():
35+
res.add(key)
36+
assert res == set(['a','b'])

vm/src/obj/objdict.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
252252
arg_check!(vm, args, required = [(dict, Some(vm.ctx.dict_type()))]);
253253

254254
let keys = get_elements(dict)
255-
.keys()
256-
.map(|k| vm.ctx.new_str(k.to_string()))
255+
.values()
256+
.map(|(k, _v)| k.clone())
257257
.collect();
258258
let key_list = vm.ctx.new_list(keys);
259259

@@ -387,4 +387,5 @@ pub fn init(context: &PyContext) {
387387
context.set_attr(&dict_type, "clear", context.new_rustfunc(dict_clear));
388388
context.set_attr(&dict_type, "values", context.new_rustfunc(dict_values));
389389
context.set_attr(&dict_type, "items", context.new_rustfunc(dict_items));
390+
context.set_attr(&dict_type, "keys", context.new_rustfunc(dict_iter));
390391
}

0 commit comments

Comments
 (0)