Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-33073: Adding as_integer_ratio to ints. #8750

Merged
merged 18 commits into from
Sep 14, 2018
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
7 changes: 6 additions & 1 deletion Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ class`. In addition, it provides a few more methods:

.. versionadded:: 3.2

.. method:: int.as_integer_ratio()

Return a pair of integers whose ratio is exactly equal to the original integer
and with a positive denominator. The integer ratio of integers (whole numbers)
is always the integer as the numerator and 1 as the denominator.

Copy link
Contributor

Choose a reason for hiding this comment

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

A versionadded directive needs to be added. Also, the developer's guide states that reST files should use an indentation of 3 spaces.


Additional Methods on Float
---------------------------
Expand Down Expand Up @@ -4734,4 +4740,3 @@ types, where they are relevant. Some of these are not reported by the

.. [5] To format only a tuple you should therefore provide a singleton tuple whose only
element is the tuple to be formatted.

5 changes: 5 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,11 @@ def test_shift_bool(self):
self.assertEqual(type(value << shift), int)
self.assertEqual(type(value >> shift), int)

def test_as_integer_ratio(self):
tests = [10, 0, -10, 1, 3]
Copy link
Member

Choose a reason for hiding this comment

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

Why so much similar cases are needed?

Add tests for booleans and other int subclasses. Check types of numerator and denominator.

for value in tests:
self.assertEqual((value).as_integer_ratio(), (value, 1))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no parens needed around value



if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added as_integer_ratio to ints to make them more interoperable with floats.
34 changes: 33 additions & 1 deletion Objects/clinic/longobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5260,6 +5260,40 @@ long_is_finite(PyObject *v)
}
#endif

/*[clinic input]
int.as_integer_ratio

Return integer ratio.

Return a pair of integers, whose ratio is exactly equal to the original int
and with a positive denominator.

Raise OverflowError on infinities and a ValueError on NaNs.
Copy link
Contributor

Choose a reason for hiding this comment

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

An int can never be any of these things - remove this line

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. math.inf is float class


>>> (10).as_integer_ratio()
Copy link
Member

Choose a reason for hiding this comment

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

Do we need so much examples in a docstring?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's useful to have at least one positive and one negative example, so that it's obvious at a glance that the behaviour for negatives is to give (for example) (-5, 1) rather than (5, -1). I could imagine users thinking that 0 was somehow a special case, too, so I like that we have the 0 example there.

(10, 1)
>>> (0).as_integer_ratio()
(0, 1)
>>> (11).as_integer_ratio()
(11, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
[clinic start generated code]*/

static PyObject *
int_as_integer_ratio_impl(PyObject *self)
/*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/
{
PyObject *denominator = NULL;
PyObject *result_pair = NULL;

denominator = PyLong_FromLong(1);
result_pair = PyTuple_Pack(2, self, denominator);
Copy link
Member

Choose a reason for hiding this comment

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

If PyLong_FromLong returns NULL then PyTuple_Pack and Py_DECREF(denominator) will fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the whole function can be replaced with return PyTuple_Pack(2, self, _PyLong_One).

Copy link
Contributor

Choose a reason for hiding this comment

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

@pablogsal But PyLong_FromLong always receive the 1. How could it fail?

Copy link
Member

@pablogsal pablogsal Aug 14, 2018

Choose a reason for hiding this comment

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

The contract of PyLong_FromLong says that it can return NULL on failure. Even if the current implementation (that uses the array of small ints) makes it improbable/impossible to fail, this can change in the future without changing the external API.


Py_DECREF(denominator);
return result_pair;
}

/*[clinic input]
int.to_bytes

Expand Down Expand Up @@ -5392,6 +5426,7 @@ static PyMethodDef long_methods[] = {
#endif
INT_TO_BYTES_METHODDEF
INT_FROM_BYTES_METHODDEF
INT_AS_INTEGER_RATIO_METHODDEF
{"__trunc__", long_long_meth, METH_NOARGS,
"Truncating an Integral returns itself."},
{"__floor__", long_long_meth, METH_NOARGS,
Expand Down