mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Move clear method from bytes to bytearray
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user