Add as_integer_ratio to PyInt

This commit is contained in:
Do Nhat Minh
2020-02-10 17:44:50 -05:00
parent 37fe20234d
commit 0c731e8a9b
2 changed files with 14 additions and 0 deletions

View File

@@ -61,6 +61,12 @@ assert (6).__rmod__(10) == 4
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

View File

@@ -521,6 +521,14 @@ impl PyInt {
size_of::<Self>() + ((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()