Move clear method from bytes to bytearray

This commit is contained in:
janczer
2019-02-19 07:11:13 +01:00
parent 58369b6fbc
commit d5c91a8de0
3 changed files with 18 additions and 14 deletions

View File

@@ -30,6 +30,6 @@ assert not bytearray(b'tuPpEr').isupper()
assert bytearray(b'Is Title Case').istitle()
assert not bytearray(b'is Not title casE').istitle()
a = b'abcd'
a = bytearray(b'abcd')
a.clear()
assert len(a) == 0

View File

@@ -95,6 +95,11 @@ pub fn init(context: &PyContext) {
"istitle",
context.new_rustfunc(bytearray_istitle),
);
context.set_attr(
&bytearray_type,
"clear",
context.new_rustfunc(bytearray_clear),
);
}
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -261,3 +266,15 @@ fn bytearray_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
let data = String::from_utf8(value.to_vec()).unwrap();
Ok(vm.new_str(format!("bytearray(b'{}')", data)))
}
fn bytearray_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_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

@@ -41,7 +41,6 @@ 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 {
@@ -209,15 +208,3 @@ 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())),
}
}