Implement bytes.__mul__ and __rmul__

This commit is contained in:
Y. Sapir committed 2019-05-09 23:51:20 +03:00
1 parent 94d1b6585c
commit 87844ff434
2 files changed
+19

No files matched your search

+9
View File
@@ -588,3 +588,12 @@ assert (
b"they're bill's friends from the UK".title()
== b"They'Re Bill'S Friends From The Uk"
)
# repeat by multiply
a = b'abcd'
assert a * 0 == b''
assert a * -1 == b''
assert a * 1 == b'abcd'
assert a * 3 == b'abcdabcdabcd'
assert 3 * a == b'abcdabcdabcd'
+10
View File
@@ -403,6 +403,16 @@ impl PyBytesRef {
fn title(self, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(self.inner.title()))
}
#[pymethod(name = "__mul__")]
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(self.inner.repeat(n, vm)?))
}
#[pymethod(name = "__rmul__")]
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
self.repeat(n, vm)
}
}
#[pyclass]