Add dict.clear and bytes.clear

This commit is contained in:
janczer
2019-02-18 18:21:39 +01:00
parent fcea8455fa
commit 85110f6fbf
4 changed files with 33 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ pub fn init(context: &PyContext) {
"__doc__",
context.new_str(bytes_doc.to_string()),
);
context.set_attr(bytes_type, "clear", context.new_rustfunc(bytes_clear));
}
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -208,3 +209,15 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
Ok(iter_obj)
}
fn bytes_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
let mut mut_obj = zelf.borrow_mut();
match mut_obj.payload {
PyObjectPayload::Bytes { ref mut value } => {
value.clear();
Ok(vm.get_none())
}
_ => Err(vm.new_type_error("".to_string())),
}
}

View File

@@ -244,6 +244,18 @@ fn dict_delitem(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
}
}
fn dict_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(dict, Some(vm.ctx.dict_type()))]);
let mut mut_obj = dict.borrow_mut();
match mut_obj.payload {
PyObjectPayload::Dict { ref mut elements } => {
elements.clear();
Ok(vm.get_none())
}
_ => Err(vm.new_type_error("".to_string())),
}
}
/// When iterating over a dictionary, we iterate over the keys of it.
fn dict_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(dict, Some(vm.ctx.dict_type()))]);
@@ -337,4 +349,5 @@ pub fn init(context: &PyContext) {
"__setitem__",
context.new_rustfunc(dict_setitem),
);
context.set_attr(&dict_type, "clear", context.new_rustfunc(dict_clear));
}