Skip to content

Commit 4ef65d8

Browse files
committed
Add bytearray.append
1 parent b542b39 commit 4ef65d8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

tests/snippets/bytearray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@
6565
pass
6666
else:
6767
assert False
68+
69+
a = bytearray(b'appen')
70+
assert len(a) == 5
71+
a.append(100)
72+
assert len(a) == 6
73+
assert a.pop() == 100

vm/src/obj/objbytearray.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn init(context: &PyContext) {
7979
"istitle" =>context.new_rustfunc(PyByteArrayRef::istitle),
8080
"isupper" => context.new_rustfunc(PyByteArrayRef::isupper),
8181
"lower" => context.new_rustfunc(PyByteArrayRef::lower),
82+
"append" => context.new_rustfunc(PyByteArrayRef::append),
8283
"pop" => context.new_rustfunc(PyByteArrayRef::pop),
8384
"upper" => context.new_rustfunc(PyByteArrayRef::upper)
8485
});
@@ -213,6 +214,10 @@ impl PyByteArrayRef {
213214
self.value.borrow_mut().clear();
214215
}
215216

217+
fn append(self, x: u8, _vm: &VirtualMachine) {
218+
self.value.borrow_mut().push(x);
219+
}
220+
216221
fn pop(self, vm: &VirtualMachine) -> PyResult<u8> {
217222
let mut bytes = self.value.borrow_mut();
218223
bytes

0 commit comments

Comments
 (0)