mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 11:14:38 +08:00
Implement bytes.__mul__ and __rmul__
This commit is contained in:
1 parent
94d1b6585c
commit
87844ff434
2 files changed
+19
No files matched your search
@@ -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'
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user