From 0c731e8a9be870ad0e89df99e810c4a2cce36a54 Mon Sep 17 00:00:00 2001 From: Do Nhat Minh Date: Mon, 10 Feb 2020 17:44:50 -0500 Subject: [PATCH] Add as_integer_ratio to PyInt --- tests/snippets/ints.py | 6 ++++++ vm/src/obj/objint.rs | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/tests/snippets/ints.py b/tests/snippets/ints.py index 6f42c9489c1..128b3d11a66 100644 --- a/tests/snippets/ints.py +++ b/tests/snippets/ints.py @@ -61,6 +61,12 @@ with assert_raises(ZeroDivisionError): (0).__rmod__(10) +# as_integer_ratio +# TODO uncomment the following tests once #1705 lands (or when the CPython version in test pipeline is upgraded to 3.8) +# assert (42).as_integer_ratio() == (42, 1) +# assert (-17).as_integer_ratio() == (-17, 1) +# assert (0).as_integer_ratio() == (0, 1) + # real/imag attributes assert (1).real == 1 assert (1).imag == 0 diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 9b5d0185b09..d3fa7702ca2 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -521,6 +521,14 @@ impl PyInt { size_of::() + ((self.value.bits() + 7) & !7) / 8 } + #[pymethod(name = "as_integer_ratio")] + fn as_integer_ratio(&self, vm: &VirtualMachine) -> PyResult { + Ok(vm.ctx.new_tuple(vec![ + vm.ctx.new_bigint(&self.value), + vm.ctx.new_bigint(&BigInt::one()), + ])) + } + #[pymethod] fn bit_length(&self) -> usize { self.value.bits()