Skip to content

Commit b2e2025

Browse files
serhiy-storchakavstinner
authored andcommitted
bpo-33073: Rework int.as_integer_ratio() implementation (GH-9303)
* Simplify the C code. * Simplify tests and make them more strict and robust. * Add references in the documentation.
1 parent b981fec commit b2e2025

File tree

3 files changed

+9
-36
lines changed

3 files changed

+9
-36
lines changed

Doc/whatsnew/3.8.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Other Language Changes
9191
was lifted.
9292
(Contributed by Serhiy Storchaka in :issue:`32489`.)
9393

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

9898
* Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`.

Lib/test/test_long.py

+6-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import sys
55

6-
import enum
76
import random
87
import math
98
import array
@@ -1354,35 +1353,14 @@ def test_shift_bool(self):
13541353
self.assertEqual(type(value >> shift), int)
13551354

13561355
def test_as_integer_ratio(self):
1357-
tests = [10, 0, -10, 1]
1356+
class myint(int):
1357+
pass
1358+
tests = [10, 0, -10, 1, sys.maxsize + 1, True, False, myint(42)]
13581359
for value in tests:
13591360
numerator, denominator = value.as_integer_ratio()
1360-
self.assertEqual((numerator, denominator), (value, 1))
1361-
self.assertIsInstance(numerator, int)
1362-
self.assertIsInstance(denominator, int)
1363-
1364-
def test_as_integer_ratio_maxint(self):
1365-
x = sys.maxsize + 1
1366-
self.assertEqual(x.as_integer_ratio()[0], x)
1367-
1368-
def test_as_integer_ratio_bool(self):
1369-
self.assertEqual(True.as_integer_ratio(), (1, 1))
1370-
self.assertEqual(False.as_integer_ratio(), (0, 1))
1371-
self.assertEqual(type(True.as_integer_ratio()[0]), int)
1372-
self.assertEqual(type(False.as_integer_ratio()[0]), int)
1373-
1374-
def test_as_integer_ratio_int_enum(self):
1375-
class Foo(enum.IntEnum):
1376-
X = 42
1377-
self.assertEqual(Foo.X.as_integer_ratio(), (42, 1))
1378-
self.assertEqual(type(Foo.X.as_integer_ratio()[0]), int)
1379-
1380-
def test_as_integer_ratio_int_flag(self):
1381-
class Foo(enum.IntFlag):
1382-
R = 1 << 2
1383-
self.assertEqual(Foo.R.as_integer_ratio(), (4, 1))
1384-
self.assertEqual(type(Foo.R.as_integer_ratio()[0]), int)
1385-
1361+
self.assertEqual((numerator, denominator), (int(value), 1))
1362+
self.assertEqual(type(numerator), int)
1363+
self.assertEqual(type(denominator), int)
13861364

13871365

13881366
if __name__ == "__main__":

Objects/longobject.c

+1-6
Original file line numberDiff line numberDiff line change
@@ -5280,13 +5280,8 @@ static PyObject *
52805280
int_as_integer_ratio_impl(PyObject *self)
52815281
/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/
52825282
{
5283-
PyObject *numerator;
52845283
PyObject *ratio_tuple;
5285-
5286-
if (PyLong_CheckExact(self)) {
5287-
return PyTuple_Pack(2, self, _PyLong_One);
5288-
}
5289-
numerator = _PyLong_Copy((PyLongObject *) self);
5284+
PyObject *numerator = long_long(self);
52905285
if (numerator == NULL) {
52915286
return NULL;
52925287
}

0 commit comments

Comments
 (0)