From ff0790ef3a5a454a694b9745508932616c53d94a Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Sun, 12 Aug 2018 16:09:35 -0700 Subject: [PATCH 01/17] bpo:33073 adding as_integer_ratio to ints. --- Doc/library/stdtypes.rst | 7 +++- Lib/test/test_long.py | 5 +++ .../2018-08-12-16-03-58.bpo-33073.XWu1Jh.rst | 1 + Objects/clinic/longobject.c.h | 34 +++++++++++++++++- Objects/longobject.c | 35 +++++++++++++++++++ 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-08-12-16-03-58.bpo-33073.XWu1Jh.rst diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 128c1bd24a4f49..af59d722deb218 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -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. + Additional Methods on Float --------------------------- @@ -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. - diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 8472889d48bade..fb77ce6d5736b4 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -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] + for value in tests: + self.assertEqual((value).as_integer_ratio(), (value, 1)) + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-08-12-16-03-58.bpo-33073.XWu1Jh.rst b/Misc/NEWS.d/next/Core and Builtins/2018-08-12-16-03-58.bpo-33073.XWu1Jh.rst new file mode 100644 index 00000000000000..ce9b6129f96e80 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-08-12-16-03-58.bpo-33073.XWu1Jh.rst @@ -0,0 +1 @@ +Added as_integer_ratio to ints to make them more interoperable with floats. diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 14f5515c7a626e..3c87ba1d2bc549 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -118,6 +118,38 @@ int_bit_length(PyObject *self, PyObject *Py_UNUSED(ignored)) return int_bit_length_impl(self); } +PyDoc_STRVAR(int_as_integer_ratio__doc__, +"as_integer_ratio($self, /)\n" +"--\n" +"\n" +"Return integer ratio.\n" +"\n" +"Return a pair of integers, whose ratio is exactly equal to the original int\n" +"and with a positive denominator.\n" +"\n" +"Raise OverflowError on infinities and a ValueError on NaNs.\n" +"\n" +">>> (10).as_integer_ratio()\n" +"(10, 1)\n" +">>> (0).as_integer_ratio()\n" +"(0, 1)\n" +">>> (11).as_integer_ratio()\n" +"(11, 1)\n" +">>> (-10).as_integer_ratio()\n" +"(-10, 1)"); + +#define INT_AS_INTEGER_RATIO_METHODDEF \ + {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__}, + +static PyObject * +int_as_integer_ratio_impl(PyObject *self); + +static PyObject * +int_as_integer_ratio(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_as_integer_ratio_impl(self); +} + PyDoc_STRVAR(int_to_bytes__doc__, "to_bytes($self, /, length, byteorder, *, signed=False)\n" "--\n" @@ -211,4 +243,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=fd64beb83bd16df3 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=d86862742f1a2709 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 399d3542709901..7118caefaa0d2b 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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. + +>>> (10).as_integer_ratio() +(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); + + Py_DECREF(denominator); + return result_pair; +} + /*[clinic input] int.to_bytes @@ -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, From d1daadcd21b4e9516314661e50adddcfc90bf3b3 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Wed, 15 Aug 2018 17:15:56 -0700 Subject: [PATCH 02/17] Updates as_integer_ratio to be one line. --- Doc/library/stdtypes.rst | 7 ++++--- Lib/test/test_long.py | 2 +- Objects/longobject.c | 11 +---------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index af59d722deb218..0aec4826c691da 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -541,10 +541,11 @@ class`. In addition, it provides a few more methods: .. 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. + 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. + .. versionadded:: 3.8 Additional Methods on Float --------------------------- diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index fb77ce6d5736b4..19e2958b730f41 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1352,7 +1352,7 @@ def test_shift_bool(self): def test_as_integer_ratio(self): tests = [10, 0, -10, 1, 3] for value in tests: - self.assertEqual((value).as_integer_ratio(), (value, 1)) + self.assertEqual(value.as_integer_ratio(), (value, 1)) if __name__ == "__main__": diff --git a/Objects/longobject.c b/Objects/longobject.c index 7118caefaa0d2b..b5acc5b14e91a2 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5268,8 +5268,6 @@ 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. - >>> (10).as_integer_ratio() (10, 1) >>> (0).as_integer_ratio() @@ -5284,14 +5282,7 @@ 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); - - Py_DECREF(denominator); - return result_pair; + return PyTuple_Pack(2, self, _PyLong_One) } /*[clinic input] From 3b89b6f9c5b5ea96a549d87873245445e1680a67 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Wed, 22 Aug 2018 16:58:05 -0400 Subject: [PATCH 03/17] Adds ;, fixes spacing. --- Objects/longobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index b5acc5b14e91a2..f8246c414476c6 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5282,7 +5282,7 @@ static PyObject * int_as_integer_ratio_impl(PyObject *self) /*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/ { - return PyTuple_Pack(2, self, _PyLong_One) + return PyTuple_Pack(2, self, _PyLong_One); } /*[clinic input] From 48c7dd002652d1fc54da9fcfca77eab17cc656cf Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 12:39:07 +0100 Subject: [PATCH 04/17] Update test_doctest expected output to include builtins.int.as_integer_ratio. --- Lib/test/test_doctest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 83941c129f4463..0b06428a5d773a 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -675,6 +675,7 @@ def non_Python_modules(): r""" 2 builtins.float.hex 1 builtins.hex 1 builtins.int + 4 builtins.int.as_integer_ratio 2 builtins.int.bit_length 1 builtins.oct From 4ca566a9b9910728e6aac14b565a6aadbb889198 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 12:52:53 +0100 Subject: [PATCH 05/17] Regenerate clinic files. --- Objects/clinic/longobject.c.h | 4 +--- Objects/longobject.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 3c87ba1d2bc549..f40f9a05fa257b 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -127,8 +127,6 @@ PyDoc_STRVAR(int_as_integer_ratio__doc__, "Return a pair of integers, whose ratio is exactly equal to the original int\n" "and with a positive denominator.\n" "\n" -"Raise OverflowError on infinities and a ValueError on NaNs.\n" -"\n" ">>> (10).as_integer_ratio()\n" "(10, 1)\n" ">>> (0).as_integer_ratio()\n" @@ -243,4 +241,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=d86862742f1a2709 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=acfef50c38f681a0 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index f8246c414476c6..07f008e4a539a6 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5280,7 +5280,7 @@ and with a positive denominator. static PyObject * int_as_integer_ratio_impl(PyObject *self) -/*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/ +/*[clinic end generated code: output=e60803ae1cc8621a input=c1aea0aa6fb85c28]*/ { return PyTuple_Pack(2, self, _PyLong_One); } From 2336cb4407ad83d9979be071f4d630dbed763797 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 13:06:33 +0100 Subject: [PATCH 06/17] Rewrap text to keep within the line length limit. --- Doc/library/stdtypes.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 0aec4826c691da..31c58a4b8b2351 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -541,9 +541,10 @@ class`. In addition, it provides a few more methods: .. 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. + 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. .. versionadded:: 3.8 From 673a0cabd0582714c358a7e183a6ca3352c4e4fd Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 13:22:48 +0100 Subject: [PATCH 07/17] One more test_doctest fix. --- Lib/test/test_doctest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 0b06428a5d773a..2de79401d03cac 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -665,7 +665,7 @@ def non_Python_modules(): r""" True >>> real_tests = [t for t in tests if len(t.examples) > 0] >>> len(real_tests) # objects that actually have doctests - 8 + 9 >>> for t in real_tests: ... print('{} {}'.format(len(t.examples), t.name)) ... From 51025893d540cb23a32f11fb2fad7acd785f6991 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 13:58:50 +0100 Subject: [PATCH 08/17] Add 'what's new' entry. --- Doc/whatsnew/3.8.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index ef5455a9f6e2a5..02551295fc837a 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -94,6 +94,10 @@ Other Language Changes * Added support of ``\N{name}`` escapes in :mod:`regular expressions `. (Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.) +* The ``int`` type now has a new ``as_integer_ratio`` method compatible + with the existing ``float.as_integer_ratio`` method. + (Contributed by Lisa Roach in :issue:`33073`.) + New Modules =========== From a935a7b3b96d4357ea37a7fdd172d5721ce24edf Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Aug 2018 14:09:36 +0100 Subject: [PATCH 09/17] Add Misc/ACKS entry. --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 8c8d954a312cb4..1d180168df5cc3 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1347,6 +1347,7 @@ Juan M. Bello Rivas Mohd Sanad Zaki Rizvi Davide Rizzo Anthony Roach +Lisa Roach Carl Robben Ben Roberts Mark Roberts From 3cd076c01428c89ac75ffc4e2ff1c5664576fb08 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Mon, 10 Sep 2018 11:58:44 -0700 Subject: [PATCH 10/17] Adds boolean support. --- Doc/library/stdtypes.rst | 7 ++++--- Doc/whatsnew/3.8.rst | 4 ++++ Lib/test/test_doctest.py | 3 ++- Lib/test/test_long.py | 12 ++++++++++-- Misc/ACKS | 1 + Objects/clinic/longobject.c.h | 4 +--- Objects/longobject.c | 8 ++++++-- 7 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 0aec4826c691da..31c58a4b8b2351 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -541,9 +541,10 @@ class`. In addition, it provides a few more methods: .. 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. + 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. .. versionadded:: 3.8 diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index ef5455a9f6e2a5..02551295fc837a 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -94,6 +94,10 @@ Other Language Changes * Added support of ``\N{name}`` escapes in :mod:`regular expressions `. (Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.) +* The ``int`` type now has a new ``as_integer_ratio`` method compatible + with the existing ``float.as_integer_ratio`` method. + (Contributed by Lisa Roach in :issue:`33073`.) + New Modules =========== diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 83941c129f4463..2de79401d03cac 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -665,7 +665,7 @@ def non_Python_modules(): r""" True >>> real_tests = [t for t in tests if len(t.examples) > 0] >>> len(real_tests) # objects that actually have doctests - 8 + 9 >>> for t in real_tests: ... print('{} {}'.format(len(t.examples), t.name)) ... @@ -675,6 +675,7 @@ def non_Python_modules(): r""" 2 builtins.float.hex 1 builtins.hex 1 builtins.int + 4 builtins.int.as_integer_ratio 2 builtins.int.bit_length 1 builtins.oct diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 19e2958b730f41..0c435afe105251 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1350,9 +1350,17 @@ def test_shift_bool(self): self.assertEqual(type(value >> shift), int) def test_as_integer_ratio(self): - tests = [10, 0, -10, 1, 3] + tests = [10, 0, -10, 1] for value in tests: - self.assertEqual(value.as_integer_ratio(), (value, 1)) + 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_bool(self): + self.assertEqual(True.as_integer_ratio(), (1, 1)) + self.assertEqual(False.as_integer_ratio(), (0, 1)) + if __name__ == "__main__": diff --git a/Misc/ACKS b/Misc/ACKS index 8c8d954a312cb4..1d180168df5cc3 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1347,6 +1347,7 @@ Juan M. Bello Rivas Mohd Sanad Zaki Rizvi Davide Rizzo Anthony Roach +Lisa Roach Carl Robben Ben Roberts Mark Roberts diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 3c87ba1d2bc549..f40f9a05fa257b 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -127,8 +127,6 @@ PyDoc_STRVAR(int_as_integer_ratio__doc__, "Return a pair of integers, whose ratio is exactly equal to the original int\n" "and with a positive denominator.\n" "\n" -"Raise OverflowError on infinities and a ValueError on NaNs.\n" -"\n" ">>> (10).as_integer_ratio()\n" "(10, 1)\n" ">>> (0).as_integer_ratio()\n" @@ -243,4 +241,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=d86862742f1a2709 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=acfef50c38f681a0 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index f8246c414476c6..9a9d2c251728f0 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5280,9 +5280,13 @@ and with a positive denominator. static PyObject * int_as_integer_ratio_impl(PyObject *self) -/*[clinic end generated code: output=e60803ae1cc8621a input=ce9c7768a1287fb9]*/ +/*[clinic end generated code: output=e60803ae1cc8621a input=c1aea0aa6fb85c28]*/ { - return PyTuple_Pack(2, self, _PyLong_One); + 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); } /*[clinic input] From 2028d976241a537b04bdba036c4811a205ec0d1e Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Thu, 13 Sep 2018 13:26:13 -0700 Subject: [PATCH 11/17] Handles subclasses more smoothly. Adds unit tests. --- Lib/test/test_long.py | 19 +++++++++++++++++++ Objects/longobject.c | 12 +++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 0c435afe105251..9d3e3429a15b87 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -3,6 +3,7 @@ import sys +import enum import random import math import array @@ -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)) + + 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) diff --git a/Objects/longobject.c b/Objects/longobject.c index 9a9d2c251728f0..c2315e2d53d619 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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); + else { + PyObject *temp = PyNumber_Positive(self); + Py_DECREF(temp); + return PyTuple_Pack(2, temp, _PyLong_One); + } } /*[clinic input] From 89a0d9d9e75bcfd78bd749722b340692279befe2 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Thu, 13 Sep 2018 13:32:40 -0700 Subject: [PATCH 12/17] Fixes docstring and doc format. --- Doc/library/stdtypes.rst | 2 +- Objects/longobject.c | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 31c58a4b8b2351..72b816af897c28 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -543,7 +543,7 @@ class`. In addition, it provides a few more methods: 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 + (whole numbers) is always the integer as the numerator and ``1`` as the denominator. .. versionadded:: 3.8 diff --git a/Objects/longobject.c b/Objects/longobject.c index c2315e2d53d619..308a799b1470b0 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5270,12 +5270,10 @@ and with a positive denominator. >>> (10).as_integer_ratio() (10, 1) ->>> (0).as_integer_ratio() -(0, 1) ->>> (11).as_integer_ratio() -(11, 1) >>> (-10).as_integer_ratio() (-10, 1) +>>> (0).as_integer_ratio() +(0, 1) [clinic start generated code]*/ static PyObject * From 148225c06fe507dbc2eabc5973f8133f84c1fe40 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Thu, 13 Sep 2018 13:35:08 -0700 Subject: [PATCH 13/17] Regenerates argument clinic. --- Objects/clinic/longobject.c.h | 10 ++++------ Objects/longobject.c | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index f40f9a05fa257b..0e70fe5d8c4404 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -129,12 +129,10 @@ PyDoc_STRVAR(int_as_integer_ratio__doc__, "\n" ">>> (10).as_integer_ratio()\n" "(10, 1)\n" -">>> (0).as_integer_ratio()\n" -"(0, 1)\n" -">>> (11).as_integer_ratio()\n" -"(11, 1)\n" ">>> (-10).as_integer_ratio()\n" -"(-10, 1)"); +"(-10, 1)\n" +">>> (0).as_integer_ratio()\n" +"(0, 1)"); #define INT_AS_INTEGER_RATIO_METHODDEF \ {"as_integer_ratio", (PyCFunction)int_as_integer_ratio, METH_NOARGS, int_as_integer_ratio__doc__}, @@ -241,4 +239,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=acfef50c38f681a0 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=6d5e92d7dc803751 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 308a799b1470b0..f945ad178cb245 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5278,7 +5278,7 @@ and with a positive denominator. static PyObject * int_as_integer_ratio_impl(PyObject *self) -/*[clinic end generated code: output=e60803ae1cc8621a input=c1aea0aa6fb85c28]*/ +/*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ { if PyLong_CheckExact(self) return PyTuple_Pack(2, self, _PyLong_One); From ac0f11fa5997d7bf3e80fa3131387d7aea8ab5e2 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 13 Sep 2018 22:22:55 -0700 Subject: [PATCH 14/17] Move whatsnew entry to resolve a merge conflict --- Doc/whatsnew/3.8.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 02551295fc837a..864c702404f104 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -91,13 +91,13 @@ Other Language Changes was lifted. (Contributed by Serhiy Storchaka in :issue:`32489`.) -* Added support of ``\N{name}`` escapes in :mod:`regular expressions `. - (Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.) - * The ``int`` type now has a new ``as_integer_ratio`` method compatible with the existing ``float.as_integer_ratio`` method. (Contributed by Lisa Roach in :issue:`33073`.) +* Added support of ``\N{name}`` escapes in :mod:`regular expressions `. + (Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.) + New Modules =========== From cb9c9784055b3ec6911723386429732e10fabdfd Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 13 Sep 2018 22:42:49 -0700 Subject: [PATCH 15/17] Restored bogus whitespace redaction --- Doc/library/stdtypes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 72b816af897c28..f10579d31da277 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -4742,3 +4742,4 @@ 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. + From d311b83dd1370b7b31868dda43456b79c2d8c61e Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Thu, 13 Sep 2018 22:56:29 -0700 Subject: [PATCH 16/17] Updates the function call. Fixes tests. --- Lib/test/test_long.py | 8 ++++---- Objects/longobject.c | 13 +++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 9d3e3429a15b87..7c883baebb410e 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1365,20 +1365,20 @@ def test_as_integer_ratio_maxint(self): 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) + 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)) - assert(type(Foo.X.as_integer_ratio()[0] == int)) + 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)) - assert(type(Foo.R.as_integer_ratio()[0]) == int) + self.assertEqual(type(Foo.R.as_integer_ratio()[0]), int) diff --git a/Objects/longobject.c b/Objects/longobject.c index f945ad178cb245..98ff9a8c265bc5 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5280,12 +5280,13 @@ static PyObject * int_as_integer_ratio_impl(PyObject *self) /*[clinic end generated code: output=e60803ae1cc8621a input=55ce3058e15de393]*/ { - if PyLong_CheckExact(self) - return PyTuple_Pack(2, self, _PyLong_One); - else { - PyObject *temp = PyNumber_Positive(self); - Py_DECREF(temp); - return PyTuple_Pack(2, temp, _PyLong_One); + if PyLong_CheckExact(self) { + return PyTuple_Pack(2, self, _PyLong_One); + } else { + PyObject *numerator = _PyLong_Copy(self); + PyObject *ratio_tuple = PyTuple_Pack(2, numerator, _PyLong_One); + Py_DECREF(numerator); + return ratio_tuple; } } From d3ef8439501de7694c3919df8441ee716d31e5e7 Mon Sep 17 00:00:00 2001 From: Lisa Roach Date: Thu, 13 Sep 2018 23:17:06 -0700 Subject: [PATCH 17/17] Fixing failing Travis build. --- Lib/test/test_doctest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index 2de79401d03cac..797bdb8471904b 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -675,7 +675,7 @@ def non_Python_modules(): r""" 2 builtins.float.hex 1 builtins.hex 1 builtins.int - 4 builtins.int.as_integer_ratio + 3 builtins.int.as_integer_ratio 2 builtins.int.bit_length 1 builtins.oct