Skip to content

Commit e819b9a

Browse files
committed
Add int.__rdivmod__
1 parent f486f30 commit e819b9a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

tests/snippets/ints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
assert (-2).__divmod__(3) == (-1, 1)
4141
with assertRaises(ZeroDivisionError):
4242
(2).__divmod__(0)
43+
assert (-3).__rdivmod__(2) == (-1, -1)
4344
assert (2).__pow__(3) == 8
4445
assert (10).__pow__(-1) == 0.1
4546
assert (2).__rpow__(3) == 9

vm/src/obj/objint.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ impl PyInt {
415415
}
416416
}
417417

418+
#[pymethod(name = "__rdivmod__")]
419+
fn rdivmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
420+
if objtype::isinstance(&other, &vm.ctx.int_type()) {
421+
let other = other.payload::<PyInt>().unwrap();
422+
inner_divmod(&other, self, vm)
423+
} else {
424+
Ok(vm.ctx.not_implemented())
425+
}
426+
}
427+
418428
#[pymethod(name = "__neg__")]
419429
fn neg(&self, _vm: &VirtualMachine) -> BigInt {
420430
-(&self.value)

0 commit comments

Comments
 (0)