Skip to content

Commit 0c731e8

Browse files
committed
Add as_integer_ratio to PyInt
1 parent 37fe202 commit 0c731e8

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

tests/snippets/ints.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
with assert_raises(ZeroDivisionError):
6262
(0).__rmod__(10)
6363

64+
# as_integer_ratio
65+
# TODO uncomment the following tests once #1705 lands (or when the CPython version in test pipeline is upgraded to 3.8)
66+
# assert (42).as_integer_ratio() == (42, 1)
67+
# assert (-17).as_integer_ratio() == (-17, 1)
68+
# assert (0).as_integer_ratio() == (0, 1)
69+
6470
# real/imag attributes
6571
assert (1).real == 1
6672
assert (1).imag == 0

vm/src/obj/objint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ impl PyInt {
521521
size_of::<Self>() + ((self.value.bits() + 7) & !7) / 8
522522
}
523523

524+
#[pymethod(name = "as_integer_ratio")]
525+
fn as_integer_ratio(&self, vm: &VirtualMachine) -> PyResult {
526+
Ok(vm.ctx.new_tuple(vec![
527+
vm.ctx.new_bigint(&self.value),
528+
vm.ctx.new_bigint(&BigInt::one()),
529+
]))
530+
}
531+
524532
#[pymethod]
525533
fn bit_length(&self) -> usize {
526534
self.value.bits()

0 commit comments

Comments
 (0)