Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 87844ff

Browse files
committed
Implement bytes.__mul__ and __rmul__
1 parent 94d1b65 commit 87844ff

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

tests/snippets/bytes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,12 @@
588588
b"they're bill's friends from the UK".title()
589589
== b"They'Re Bill'S Friends From The Uk"
590590
)
591+
592+
593+
# repeat by multiply
594+
a = b'abcd'
595+
assert a * 0 == b''
596+
assert a * -1 == b''
597+
assert a * 1 == b'abcd'
598+
assert a * 3 == b'abcdabcdabcd'
599+
assert 3 * a == b'abcdabcdabcd'

vm/src/obj/objbytes.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,16 @@ impl PyBytesRef {
403403
fn title(self, vm: &VirtualMachine) -> PyResult {
404404
Ok(vm.ctx.new_bytes(self.inner.title()))
405405
}
406+
407+
#[pymethod(name = "__mul__")]
408+
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
409+
Ok(vm.ctx.new_bytes(self.inner.repeat(n, vm)?))
410+
}
411+
412+
#[pymethod(name = "__rmul__")]
413+
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
414+
self.repeat(n, vm)
415+
}
406416
}
407417

408418
#[pyclass]

0 commit comments

Comments
 (0)