mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-17 01:51:39 +09:00
Add dict.clear and bytes.clear
This commit is contained in:
@@ -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())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user