Skip to content

Commit d5c91a8

Browse files
committed
Move clear method from bytes to bytearray
1 parent 58369b6 commit d5c91a8

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

tests/snippets/bytearray.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
assert bytearray(b'Is Title Case').istitle()
3131
assert not bytearray(b'is Not title casE').istitle()
3232

33-
a = b'abcd'
33+
a = bytearray(b'abcd')
3434
a.clear()
3535
assert len(a) == 0

vm/src/obj/objbytearray.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ pub fn init(context: &PyContext) {
9595
"istitle",
9696
context.new_rustfunc(bytearray_istitle),
9797
);
98+
context.set_attr(
99+
&bytearray_type,
100+
"clear",
101+
context.new_rustfunc(bytearray_clear),
102+
);
98103
}
99104

100105
fn bytearray_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -261,3 +266,15 @@ fn bytearray_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
261266
let data = String::from_utf8(value.to_vec()).unwrap();
262267
Ok(vm.new_str(format!("bytearray(b'{}')", data)))
263268
}
269+
270+
fn bytearray_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
271+
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytearray_type()))]);
272+
let mut mut_obj = zelf.borrow_mut();
273+
match mut_obj.payload {
274+
PyObjectPayload::Bytes { ref mut value } => {
275+
value.clear();
276+
Ok(vm.get_none())
277+
}
278+
_ => Err(vm.new_type_error("".to_string())),
279+
}
280+
}

vm/src/obj/objbytes.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ pub fn init(context: &PyContext) {
4141
"__doc__",
4242
context.new_str(bytes_doc.to_string()),
4343
);
44-
context.set_attr(bytes_type, "clear", context.new_rustfunc(bytes_clear));
4544
}
4645

4746
fn bytes_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -209,15 +208,3 @@ fn bytes_iter(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
209208

210209
Ok(iter_obj)
211210
}
212-
213-
fn bytes_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
214-
arg_check!(vm, args, required = [(zelf, Some(vm.ctx.bytes_type()))]);
215-
let mut mut_obj = zelf.borrow_mut();
216-
match mut_obj.payload {
217-
PyObjectPayload::Bytes { ref mut value } => {
218-
value.clear();
219-
Ok(vm.get_none())
220-
}
221-
_ => Err(vm.new_type_error("".to_string())),
222-
}
223-
}

0 commit comments

Comments
 (0)