Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Handles subclasses more smoothly. Adds unit tests.
  • Loading branch information
lisroach committed Sep 13, 2018
commit 2028d976241a537b04bdba036c4811a205ec0d1e
19 changes: 19 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import sys

import enum
import random
import math
import array
Expand Down Expand Up @@ -1357,9 +1358,27 @@ def test_as_integer_ratio(self):
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))
assert(type(True.as_integer_ratio()[0]) == int)
assert(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))
assert(type(Foo.X.as_integer_ratio()[0] == int))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should be: self.assertEqual(type(Foo.X.as_integer_ratio()[0], int). Unittest doesn't use assertion statements.


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



Expand Down
12 changes: 7 additions & 5 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5282,11 +5282,13 @@ static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=c1aea0aa6fb85c28]*/
{
if (self == Py_True)
return PyTuple_Pack(2, _PyLong_One, _PyLong_One);
if (self == Py_False)
return PyTuple_Pack(2, _PyLong_Zero, _PyLong_One);
return PyTuple_Pack(2, self, _PyLong_One);
if PyLong_CheckExact(self)
return PyTuple_Pack(2, self, _PyLong_One);
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.

True.as_integer_ratio() will return (True, 1). It should return (1, 1).

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.

Hmm; good point. There's a precedent here in the form of True.numerator, which returns 1.

else {
PyObject *temp = PyNumber_Positive(self);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need another way to do this. The intent of this code is to construct a regular integer instance from an instance of an int subclass. The problem with PyNumber_Positive() is that the subclass can itself define pos() to return something other than an exact int. Elsewhere, we use _PyLong_Copy for this purpose.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason why PyNumber_Index does not call _PyLong_Copy?

Py_DECREF(temp);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The DECREF needs to occur after building the tuple; otherwise, the Python integer object can (and likely will) disappear before it gets used.

return PyTuple_Pack(2, temp, _PyLong_One);
}
Comment thread
rhettinger marked this conversation as resolved.
}

/*[clinic input]
Expand Down