Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ Other Language Changes
was lifted.
(Contributed by Serhiy Storchaka in :issue:`32489`.)

* The ``int`` type now has a new ``as_integer_ratio`` method compatible
with the existing ``float.as_integer_ratio`` method.
* The :class:`int` type now has a new :meth:`~int.as_integer_ratio` method
compatible with the existing :meth:`float.as_integer_ratio` method.
(Contributed by Lisa Roach in :issue:`33073`.)

* Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`.
Expand Down
34 changes: 6 additions & 28 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import sys

import enum
import random
import math
import array
Expand Down Expand Up @@ -1351,35 +1350,14 @@ def test_shift_bool(self):
self.assertEqual(type(value >> shift), int)

def test_as_integer_ratio(self):
tests = [10, 0, -10, 1]
class myint(int):
pass
tests = [10, 0, -10, 1, sys.maxsize + 1, True, False, myint(42)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: what about the enum.IntEnum tests? (I assume those can be derived from an int subclass but I just want to confirm).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum.IntEnum is just an int subclass.

for value in tests:
numerator, denominator = value.as_integer_ratio()
self.assertEqual((numerator, denominator), (value, 1))
self.assertIsInstance(numerator, int)
self.assertIsInstance(denominator, int)

def test_as_integer_ratio_maxint(self):
x = sys.maxsize + 1
self.assertEqual(x.as_integer_ratio()[0], x)

def test_as_integer_ratio_bool(self):
self.assertEqual(True.as_integer_ratio(), (1, 1))
self.assertEqual(False.as_integer_ratio(), (0, 1))
self.assertEqual(type(True.as_integer_ratio()[0]), int)
self.assertEqual(type(False.as_integer_ratio()[0]), int)

def test_as_integer_ratio_int_enum(self):
class Foo(enum.IntEnum):
X = 42
self.assertEqual(Foo.X.as_integer_ratio(), (42, 1))
self.assertEqual(type(Foo.X.as_integer_ratio()[0]), int)

def test_as_integer_ratio_int_flag(self):
class Foo(enum.IntFlag):
R = 1 << 2
self.assertEqual(Foo.R.as_integer_ratio(), (4, 1))
self.assertEqual(type(Foo.R.as_integer_ratio()[0]), int)

self.assertEqual((numerator, denominator), (int(value), 1))
self.assertEqual(type(numerator), int)
self.assertEqual(type(denominator), int)


if __name__ == "__main__":
Expand Down
7 changes: 1 addition & 6 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5280,13 +5280,8 @@ static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
{
PyObject *numerator;
PyObject *ratio_tuple;

if (PyLong_CheckExact(self)) {
return PyTuple_Pack(2, self, _PyLong_One);
}
numerator = _PyLong_Copy((PyLongObject *) self);
PyObject *numerator = long_long(self);
if (numerator == NULL) {
return NULL;
}
Expand Down