From d80b01737c4691b3d42f49b3852913c21d1c1e8f Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 16:35:11 +0100 Subject: [PATCH 01/10] bpo-26680: Adds support for int.is_integer() for compatibility with float.is_integer(). The int.is_integer() method always returns True. --- Lib/test/test_long.py | 4 ++++ Objects/clinic/longobject.c.h | 20 +++++++++++++++++++- Objects/longobject.c | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index c97842b5bfd233..669826c0fa3c1b 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1381,6 +1381,10 @@ class myint(int): self.assertEqual(type(numerator), int) self.assertEqual(type(denominator), int) + def test_int_always_is_integer(self): + # Issue #26680: Incorporating number.is_integer into int + self.assertTrue(all(x.is_integer() for x in (-1, 0, 1, 42))) + if __name__ == "__main__": unittest.main() diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 4bd47b116f883c..740dc83c16c92a 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -121,6 +121,24 @@ int___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs) return return_value; } +PyDoc_STRVAR(int_is_integer__doc__, +"is_integer($self, /)\n" +"--\n" +"\n" +"Return True for all integers."); + +#define INT_IS_INTEGER_METHODDEF \ + {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__}, + +static PyObject * +int_is_integer_impl(PyObject *self); + +static PyObject * +int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + return int_is_integer_impl(self); +} + PyDoc_STRVAR(int___sizeof____doc__, "__sizeof__($self, /)\n" "--\n" @@ -367,4 +385,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=ea18e51af5b53591 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a68b8ed2c7b6e28f input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 92514d4154e2cb..055a72799dde1f 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5233,6 +5233,19 @@ int___round___impl(PyObject *self, PyObject *o_ndigits) return result; } +/*[clinic input] +int.is_integer + +Returns True for all integers. +[clinic start generated code]*/ + +static PyObject * +int_is_integer_impl(PyObject *self) +/*[clinic end generated code: output=90f8e794ce5430ef input=903121d57b734c35]*/ +{ + Py_RETURN_TRUE; +} + /*[clinic input] int.__sizeof__ -> Py_ssize_t @@ -5547,6 +5560,7 @@ static PyMethodDef long_methods[] = { {"__ceil__", long_long_meth, METH_NOARGS, "Ceiling of an Integral returns itself."}, INT___ROUND___METHODDEF + INT_IS_INTEGER_METHODDEF INT___GETNEWARGS___METHODDEF INT___FORMAT___METHODDEF INT___SIZEOF___METHODDEF From b46a9dcef39b889718dac6089ec9cf6bae23566f Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 16:42:44 +0100 Subject: [PATCH 02/10] bpo-26680: Adds a test to ensure that False.is_integer() and True.is_integer() are always True. --- Lib/test/test_bool.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index 7b3a3859e08932..bc201e10ff2671 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -354,6 +354,11 @@ def test_real_and_imag(self): self.assertIs(type(False.real), int) self.assertIs(type(False.imag), int) + def test_always_is_integer(self): + # Issue #26680: Incorporating number.is_integer into bool + self.assertTrue(all(b.is_integer() for b in (False, True))) + + def test_main(): support.run_unittest(BoolTest) From 3b1677fe7c7c41aa30b336ba1a970b3f68ac1e84 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 17:41:59 +0100 Subject: [PATCH 03/10] bpo-26680: Adds Real.is_integer() with a trivial implementation using conversion to int. This default implementation is intended to reduce the workload for subclass implementers. It is not robust in the presence of infinities or NaNs and may have suboptimal performance for other types. --- Lib/numbers.py | 13 ++++++++++++- Lib/test/test_numeric_tower.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Lib/numbers.py b/Lib/numbers.py index ed815ef41ebe12..65aecb81118e67 100644 --- a/Lib/numbers.py +++ b/Lib/numbers.py @@ -148,7 +148,7 @@ class Real(Complex): """To Complex, Real adds the operations that work on real numbers. In short, those are: a conversion to float, trunc(), divmod, - %, <, <=, >, and >=. + is_integer, %, <, <=, >, and >=. Real also provides defaults for the derived operations. """ @@ -242,6 +242,17 @@ def __le__(self, other): """self <= other""" raise NotImplementedError + def is_integer(self): + """Return True if the Real is integral; otherwise return False. + + This default implementation can be overridden in subclasses + for performance reasons or to deal with values such as NaN, + which would otherwise cause an exception to be raised. + """ + # Although __int__ is not defined at this level, the int + # constructor falls back to __trunc__, which we do have. + return self == int(self) + # Concrete implementations of Complex abstract methods. def __complex__(self): """complex(self) == complex(float(self), 0)""" diff --git a/Lib/test/test_numeric_tower.py b/Lib/test/test_numeric_tower.py index c54dedb8b793a0..917e2f6dedbbf6 100644 --- a/Lib/test/test_numeric_tower.py +++ b/Lib/test/test_numeric_tower.py @@ -6,6 +6,7 @@ import sys import operator +from numbers import Real from decimal import Decimal as D from fractions import Fraction as F @@ -197,6 +198,16 @@ def test_complex(self): self.assertRaises(TypeError, op, z, v) self.assertRaises(TypeError, op, v, z) +class IsIntegerTest(unittest.TestCase): + + def test_real_is_integer(self): + self.assertTrue(Real.is_integer(-1.0)) + self.assertTrue(Real.is_integer(0.0)) + self.assertTrue(Real.is_integer(1.0)) + self.assertTrue(Real.is_integer(42.0)) + + self.assertFalse(Real.is_integer(-0.5)) + self.assertFalse(Real.is_integer(4.2)) if __name__ == '__main__': unittest.main() From f78bf5c9d726801e67ff1bd8d13797d77d4967b6 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 18:15:45 +0100 Subject: [PATCH 04/10] bpo-26680: Adds Rational.is_integer which returns True if the denominator is one. This implementation assumes the Rational is represented in it's lowest form, as required by the class docstring. --- Lib/numbers.py | 4 ++++ Lib/test/test_numeric_tower.py | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Lib/numbers.py b/Lib/numbers.py index 65aecb81118e67..6027848a64dc54 100644 --- a/Lib/numbers.py +++ b/Lib/numbers.py @@ -301,6 +301,10 @@ def __float__(self): """ return self.numerator / self.denominator + def is_integer(self): + """Return True if the Rational is integral; otherwise return False.""" + return self.denominator == 1 + class Integral(Rational): """Integral adds a conversion to int and the bit-string operations.""" diff --git a/Lib/test/test_numeric_tower.py b/Lib/test/test_numeric_tower.py index 917e2f6dedbbf6..97ba92385fd3f0 100644 --- a/Lib/test/test_numeric_tower.py +++ b/Lib/test/test_numeric_tower.py @@ -6,7 +6,7 @@ import sys import operator -from numbers import Real +from numbers import Real, Rational, Integral from decimal import Decimal as D from fractions import Fraction as F @@ -198,6 +198,7 @@ def test_complex(self): self.assertRaises(TypeError, op, z, v) self.assertRaises(TypeError, op, v, z) + class IsIntegerTest(unittest.TestCase): def test_real_is_integer(self): @@ -209,5 +210,18 @@ def test_real_is_integer(self): self.assertFalse(Real.is_integer(-0.5)) self.assertFalse(Real.is_integer(4.2)) + def test_rational_is_integer(self): + self.assertTrue(Rational.is_integer(F(-1, 1))) + self.assertTrue(Rational.is_integer(F(0, 1))) + self.assertTrue(Rational.is_integer(F(1, 1))) + self.assertTrue(Rational.is_integer(F(42, 1))) + self.assertTrue(Rational.is_integer(F(2, 2))) + self.assertTrue(Rational.is_integer(F(8, 4))) + + self.assertFalse(Rational.is_integer(F(1, 2))) + self.assertFalse(Rational.is_integer(F(1, 3))) + self.assertFalse(Rational.is_integer(F(2, 3))) + + if __name__ == '__main__': unittest.main() From 6a905a8f95c2d52a10bfaebea0687c1514836375 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 18:55:42 +0100 Subject: [PATCH 05/10] bpo-26680: Adds Integral.is_integer which always returns True. --- Lib/numbers.py | 4 ++++ Lib/test/test_numeric_tower.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Lib/numbers.py b/Lib/numbers.py index 6027848a64dc54..0634f62ff123c4 100644 --- a/Lib/numbers.py +++ b/Lib/numbers.py @@ -401,4 +401,8 @@ def denominator(self): """Integers have a denominator of 1.""" return 1 + def is_integer(self): + """Return True; all Integrals represent an integral value.""" + return True + Integral.register(int) diff --git a/Lib/test/test_numeric_tower.py b/Lib/test/test_numeric_tower.py index 97ba92385fd3f0..4e46aacad82b6a 100644 --- a/Lib/test/test_numeric_tower.py +++ b/Lib/test/test_numeric_tower.py @@ -222,6 +222,12 @@ def test_rational_is_integer(self): self.assertFalse(Rational.is_integer(F(1, 3))) self.assertFalse(Rational.is_integer(F(2, 3))) + def test_integral_is_integer(self): + self.assertTrue(Integral.is_integer(-1)) + self.assertTrue(Integral.is_integer(0)) + self.assertTrue(Integral.is_integer(1)) + self.assertTrue(Integral.is_integer(1729)) + if __name__ == '__main__': unittest.main() From cc07ee5e857d0b10451b086a7412d20a9d6c8133 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Tue, 13 Mar 2018 19:01:37 +0100 Subject: [PATCH 06/10] bpo-26680: Adds tests for Fraction.is_integer called as an instance method. The tests for the Rational abstract base class use an unbound method to sidestep the inability to directly instantiate Rational. These tests check that everything works correct as an instance method. --- Lib/test/test_fractions.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 0845f7921c39ec..811b58fd8f56aa 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -724,6 +724,17 @@ def denominator(self): self.assertEqual(type(f.numerator), myint) self.assertEqual(type(f.denominator), myint) + def test_is_integer(self): + # Issue #26680: Incorporating number.is_integer into Fraction + self.assertTrue(F(-1, 1).is_integer()) + self.assertTrue(F(0, 1).is_integer()) + self.assertTrue(F(1, 1).is_integer()) + self.assertTrue(F(42, 1).is_integer()) + self.assertTrue(F(2, 2).is_integer()) + self.assertTrue(F(8, 4).is_integer()) + self.assertFalse(F(1, 2).is_integer()) + self.assertFalse(F(1, 3).is_integer()) + self.assertFalse(F(2, 3).is_integer()) if __name__ == '__main__': unittest.main() From 3ae9866a51d7d40d6a69308ad211aada8711949b Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Wed, 14 Mar 2018 15:02:37 +0100 Subject: [PATCH 07/10] bpo-26680: Updates documentation for Real.is_integer and built-ins int and float. The call x.is_integer() is now listed in the table of operations which apply to all numeric types except complex, with a reference to the full documentation for Real.is_integer(). Mention of is_integer() has been removed from the section 'Additional Methods on Float'. The documentation for Real.is_integer() describes its purpose, and mentions that it should be overridden for performance reasons, or to handle special values like NaN. --- Doc/library/numbers.rst | 26 +++++++++++++++++++------- Doc/library/stdtypes.rst | 14 ++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Doc/library/numbers.rst b/Doc/library/numbers.rst index 1b594952ead724..5d49f5eb96b7ad 100644 --- a/Doc/library/numbers.rst +++ b/Doc/library/numbers.rst @@ -49,19 +49,30 @@ The numeric tower numbers. In short, those are: a conversion to :class:`float`, :func:`math.trunc`, - :func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, ``//``, - ``%``, ``<``, ``<=``, ``>``, and ``>=``. + :func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, + :func:`~Real.is_integer`, ``//``, ``%``, ``<``, ``<=``, ``>``, and ``>=``. Real also provides defaults for :func:`complex`, :attr:`~Complex.real`, :attr:`~Complex.imag`, and :meth:`~Complex.conjugate`. + .. method:: is_integer() + + Returns :const:`True` if this number has a finite and integral value, + otherwise :const:`False`. This is a default implementation which + relies on successful conversion to :class:`int`. It may be overridden + in subclasses (such as it is in :class:`float`) for better performance, + or to handle special values such as NaN which are not + convertible to :class:`int`. + + .. versionadded:: 3.10 + .. class:: Rational Subtypes :class:`Real` and adds :attr:`~Rational.numerator` and :attr:`~Rational.denominator` properties, which - should be in lowest terms. With these, it provides a default for - :func:`float`. + should be in lowest terms. With these, it provides defaults for + :func:`float` and :func:`~Real.is_integer`. .. attribute:: numerator @@ -75,9 +86,10 @@ The numeric tower .. class:: Integral Subtypes :class:`Rational` and adds a conversion to :class:`int`. Provides - defaults for :func:`float`, :attr:`~Rational.numerator`, and - :attr:`~Rational.denominator`. Adds abstract methods for ``**`` and - bit-string operations: ``<<``, ``>>``, ``&``, ``^``, ``|``, ``~``. + defaults for :func:`float`, :attr:`~Rational.numerator`, + :attr:`~Rational.denominator`, and :func:`~Real.is_integer`. Adds abstract + methods for ``**`` and bit-string operations: ``<<``, ``>>``, ``&``, ``^``, + ``|``, ``~``. Notes for type implementors diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 2eee22c79af769..0d46c1cf872075 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -310,6 +310,10 @@ the operations, see :ref:`operator-summary`): +---------------------+---------------------------------+---------+--------------------+ | ``x ** y`` | *x* to the power *y* | \(5) | | +---------------------+---------------------------------+---------+--------------------+ +| ``x.is_integer()`` | ``True`` if *x* has a finite | | :func:`~numbers\ | +| | and integral value, otherwise | | .Real.is_integer` | +| | ``False``. | | | ++---------------------+---------------------------------+---------+--------------------+ .. index:: triple: operations on; numeric; types @@ -583,16 +587,6 @@ class`. float also has the following additional methods. :exc:`OverflowError` on infinities and a :exc:`ValueError` on NaNs. -.. method:: float.is_integer() - - Return ``True`` if the float instance is finite with integral - value, and ``False`` otherwise:: - - >>> (-2.0).is_integer() - True - >>> (3.2).is_integer() - False - Two methods support conversion to and from hexadecimal strings. Since Python's floats are stored internally as binary numbers, converting a float to or from a From 54ca820694afef68f71d1781167efce71e845ab2 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Wed, 14 Mar 2018 16:55:29 +0100 Subject: [PATCH 08/10] bpo-26680: Adds Decimal.is_integer to the Python and C implementations. The C implementation of Decimal already implements and uses mpd_isinteger internally, we just expose the existing function to Python. The Python implementation uses internal conversion to integer using to_integral_value(). In both cases, the corresponding context methods are also implemented. Tests and documentation are included. --- Doc/library/decimal.rst | 14 ++++++++++++++ Lib/_pydecimal.py | 25 +++++++++++++++++++++++++ Lib/test/decimaltestdata/extra.decTest | 18 ++++++++++++++++++ Lib/test/test_decimal.py | 24 ++++++++++++++++++++++++ Modules/_decimal/_decimal.c | 6 ++++-- Modules/_decimal/docstrings.h | 13 ++++++++++--- Objects/clinic/longobject.c.h | 4 ++-- Objects/longobject.c | 2 +- 8 files changed, 98 insertions(+), 8 deletions(-) diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index e194649e30d85c..7a6497305952f1 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -621,6 +621,13 @@ Decimal objects Return :const:`True` if the argument is either positive or negative infinity and :const:`False` otherwise. + .. method:: is_integer() + + Return :const:`True` if the argument is a finite integral value and + :const:`False` otherwise. + + .. versionadded:: 3.10 + .. method:: is_nan() Return :const:`True` if the argument is a (quiet or signaling) NaN and @@ -1215,6 +1222,13 @@ In addition to the three supplied contexts, new contexts can be created with the Returns ``True`` if *x* is infinite; otherwise returns ``False``. + .. method:: is_integer(x) + + Returns ``True`` if *x* is finite and integral; otherwise + returns ``False``. + + .. versionadded:: 3.10 + .. method:: is_nan(x) Returns ``True`` if *x* is a qNaN or sNaN; otherwise returns ``False``. diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index ab989e5206a9e9..8c0ef570922197 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -3164,6 +3164,12 @@ def is_zero(self): """Return True if self is a zero; otherwise return False.""" return not self._is_special and self._int == '0' + def is_integer(self): + """Return True is self is finite and integral; otherwise False.""" + if self._is_special: + return False + return self.to_integral_value(rounding=ROUND_FLOOR) == self + def _ln_exp_bound(self): """Compute a lower bound for the adjusted exponent of self.ln(). In other words, compute r such that self.ln() >= 10**r. Assumes @@ -4659,6 +4665,25 @@ def is_zero(self, a): a = _convert_other(a, raiseit=True) return a.is_zero() + def is_integer(self, a): + """Return True if the operand is integral; otherwise return False. + + >>> ExtendedContext.is_integer(Decimal('0')) + True + >>> ExtendedContext.is_integer(Decimal('2.50')) + False + >>> ExtendedContext.is_integer(Decimal('-0E+2')) + True + >>> ExtendedContext.is_integer(Decimal('-0.5')) + False + >>> ExtendedContext.is_integer(Decimal('NaN')) + False + >>> ExtendedContext.is_integer(10) + True + """ + a = _convert_other(a, raiseit=True) + return a.is_integer() + def ln(self, a): """Returns the natural (base e) logarithm of the operand. diff --git a/Lib/test/decimaltestdata/extra.decTest b/Lib/test/decimaltestdata/extra.decTest index b630d8e3f9d45e..2f0719ed22342a 100644 --- a/Lib/test/decimaltestdata/extra.decTest +++ b/Lib/test/decimaltestdata/extra.decTest @@ -2346,6 +2346,24 @@ bool2096 iszero sNaN -> 0 bool2097 iszero -sNaN -> 0 bool2098 iszero sNaN123 -> 0 bool2099 iszero -sNaN123 -> 0 +bool2100 is_integer -1.0 -> 1 +bool2101 is_integer 0.0 -> 1 +bool2102 is_integer 1.0 -> 1 +bool2103 is_integer 42 -> 1 +bool2104 is_integer 1e2 -> 1 +bool2105 is_integer 1.5 -> 0 +bool2106 is_integer 1e-2 -> 0 +bool2107 is_integer NaN -> 0 +bool2109 is_integer -NaN -> 0 +bool2110 is_integer NaN123 -> 0 +bool2111 is_integer -NaN123 -> 0 +bool2112 is_integer sNaN -> 0 +bool2113 is_integer -sNaN -> 0 +bool2114 is_integer sNaN123 -> 0 +bool2115 is_integer -sNaN123 -> 0 +bool2116 is_integer Infinity -> 0 +bool2117 is_integer -Infinity -> 0 + ------------------------------------------------------------------------ -- The following tests (pwmx0 through pwmx440) are for the -- diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index dbd58e8a6519b1..efb41fd4650566 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -276,6 +276,7 @@ def setUp(self): 'is_snan', 'is_subnormal', 'is_zero', + 'is_integer', 'same_quantum') def read_unlimited(self, v, context): @@ -2726,6 +2727,7 @@ def test_named_parameters(self): self.assertRaises(TypeError, D(1).is_snan, context=xc) self.assertRaises(TypeError, D(1).is_signed, context=xc) self.assertRaises(TypeError, D(1).is_zero, context=xc) + self.assertRaises(TypeError, D(1).is_integer, context=xc) self.assertFalse(D("0.01").is_normal(context=xc)) self.assertTrue(D("0.01").is_subnormal(context=xc)) @@ -3197,6 +3199,15 @@ def test_is_zero(self): self.assertEqual(c.is_zero(10), d) self.assertRaises(TypeError, c.is_zero, '10') + def test_is_integer(self): + Decimal = self.decimal.Decimal + Context = self.decimal.Context + + c = Context() + b = c.is_integer(Decimal(10)) + self.assertEqual(c.is_integer(10), b) + self.assertRaises(TypeError, c.is_integer, '10') + def test_ln(self): Decimal = self.decimal.Decimal Context = self.decimal.Context @@ -4360,6 +4371,19 @@ def test_implicit_context(self): self.assertTrue(Decimal("-1").is_signed()) self.assertTrue(Decimal("0").is_zero()) self.assertTrue(Decimal("0").is_zero()) + self.assertTrue(Decimal("-1").is_integer()) + self.assertTrue(Decimal("0").is_integer()) + self.assertTrue(Decimal("1").is_integer()) + self.assertTrue(Decimal("42").is_integer()) + self.assertTrue(Decimal("1e2").is_integer()) + self.assertFalse(Decimal("1.5").is_integer()) + self.assertFalse(Decimal("1e-2").is_integer()) + self.assertFalse(Decimal("NaN").is_integer()) + self.assertFalse(Decimal("-NaN").is_integer()) + self.assertFalse(Decimal("sNaN").is_integer()) + self.assertFalse(Decimal("-sNaN").is_integer()) + self.assertFalse(Decimal("Inf").is_integer()) + self.assertFalse(Decimal("-Inf").is_integer()) # Copy with localcontext() as c: diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index e7c44acba02fc2..5200b1a48e4bfa 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -4138,6 +4138,7 @@ Dec_BoolFunc(mpd_isqnan) Dec_BoolFunc(mpd_issnan) Dec_BoolFunc(mpd_issigned) Dec_BoolFunc(mpd_iszero) +Dec_BoolFunc(mpd_isinteger) /* Boolean functions, optional context arg */ Dec_BoolFuncVA(mpd_isnormal) @@ -4772,6 +4773,7 @@ static PyMethodDef dec_methods [] = { "is_snan", dec_mpd_issnan, METH_NOARGS, doc_is_snan }, { "is_signed", dec_mpd_issigned, METH_NOARGS, doc_is_signed }, { "is_zero", dec_mpd_iszero, METH_NOARGS, doc_is_zero }, + { "is_integer", dec_mpd_isinteger, METH_NOARGS, doc_is_integer}, /* Boolean functions, optional context arg */ { "is_normal", (PyCFunction)(void(*)(void))dec_mpd_isnormal, METH_VARARGS|METH_KEYWORDS, doc_is_normal }, @@ -5183,6 +5185,7 @@ DecCtx_BoolFunc_NO_CTX(mpd_isqnan) DecCtx_BoolFunc_NO_CTX(mpd_issigned) DecCtx_BoolFunc_NO_CTX(mpd_issnan) DecCtx_BoolFunc_NO_CTX(mpd_iszero) +DecCtx_BoolFunc_NO_CTX(mpd_isinteger) static PyObject * ctx_iscanonical(PyObject *context UNUSED, PyObject *v) @@ -5464,6 +5467,7 @@ static PyMethodDef context_methods [] = { "is_snan", ctx_mpd_issnan, METH_O, doc_ctx_is_snan }, { "is_subnormal", ctx_mpd_issubnormal, METH_O, doc_ctx_is_subnormal }, { "is_zero", ctx_mpd_iszero, METH_O, doc_ctx_is_zero }, + { "is_integer", ctx_mpd_isinteger, METH_O, doc_ctx_is_integer }, /* Functions with a single decimal argument */ { "_apply", PyDecContext_Apply, METH_O, NULL }, /* alias for apply */ @@ -6097,5 +6101,3 @@ PyInit__decimal(void) return NULL; /* GCOV_NOT_REACHED */ } - - diff --git a/Modules/_decimal/docstrings.h b/Modules/_decimal/docstrings.h index f7fd6e79529984..bd602ab278e0ed 100644 --- a/Modules/_decimal/docstrings.h +++ b/Modules/_decimal/docstrings.h @@ -260,6 +260,11 @@ Return True if the argument is a (positive or negative) zero and False\n\ otherwise.\n\ \n"); +PyDoc_STRVAR(doc_is_integer, +"is_integer($self, /)\n--\n\n\ +Return True if the argument is finite and integral, otherwise False.\n\ +\n"); + PyDoc_STRVAR(doc_ln, "ln($self, /, context=None)\n--\n\n\ Return the natural (base e) logarithm of the operand. The function always\n\ @@ -685,6 +690,11 @@ PyDoc_STRVAR(doc_ctx_is_zero, Return True if x is a zero, False otherwise.\n\ \n"); +PyDoc_STRVAR(doc_ctx_is_integer, +"is_integer($self, x, /)\n--\n\n\ ++Return True if x is finite and integral, False otherwise.\n\ ++\n"); + PyDoc_STRVAR(doc_ctx_ln, "ln($self, x, /)\n--\n\n\ Return the natural (base e) logarithm of x.\n\ @@ -879,6 +889,3 @@ Convert a number to a string using scientific notation.\n\ #endif /* DOCSTRINGS_H */ - - - diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 740dc83c16c92a..16e6f7e619e872 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -125,7 +125,7 @@ PyDoc_STRVAR(int_is_integer__doc__, "is_integer($self, /)\n" "--\n" "\n" -"Return True for all integers."); +"Returns True for all integers."); #define INT_IS_INTEGER_METHODDEF \ {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__}, @@ -385,4 +385,4 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=a68b8ed2c7b6e28f input=a9049054013a1b77]*/ +/*[clinic end generated code: output=022614978e2fcdf3 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 055a72799dde1f..bc5b49dcf8b56f 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5241,7 +5241,7 @@ Returns True for all integers. static PyObject * int_is_integer_impl(PyObject *self) -/*[clinic end generated code: output=90f8e794ce5430ef input=903121d57b734c35]*/ +/*[clinic end generated code: output=90f8e794ce5430ef input=1c1a86957301d26d]*/ { Py_RETURN_TRUE; } From a9e364d3a37aa5cbe6cd0874f3097322bded629d Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Thu, 15 Mar 2018 11:36:30 +0100 Subject: [PATCH 09/10] bpo-26680: Updates the ACKS file. --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index d5bdb084a1e1ed..8ba4acb9672e72 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1607,6 +1607,7 @@ Roman Skurikhin Ville Skyttä Michael Sloan Nick Sloan +Robert Smallshire Václav Šmilauer Allen W. Smith Christopher Smith From 11db7f890e089b475c4b136ca98b3828e0fd02c0 Mon Sep 17 00:00:00 2001 From: Robert Smallshire Date: Thu, 15 Mar 2018 11:58:01 +0100 Subject: [PATCH 10/10] bpo-26680: NEWS entries for int, the numeric ABCs and Decimal. --- .../Core and Builtins/2018-03-15-11-51-36.bpo-26680.wOWYps.rst | 2 ++ .../next/Library/2018-03-15-11-55-04.bpo-26680.eKAi85.rst | 3 +++ .../next/Library/2018-03-15-11-56-48.bpo-26680.Udkhn4.rst | 2 ++ 3 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2018-03-15-11-51-36.bpo-26680.wOWYps.rst create mode 100644 Misc/NEWS.d/next/Library/2018-03-15-11-55-04.bpo-26680.eKAi85.rst create mode 100644 Misc/NEWS.d/next/Library/2018-03-15-11-56-48.bpo-26680.Udkhn4.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-03-15-11-51-36.bpo-26680.wOWYps.rst b/Misc/NEWS.d/next/Core and Builtins/2018-03-15-11-51-36.bpo-26680.wOWYps.rst new file mode 100644 index 00000000000000..93325ffffcbfc6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2018-03-15-11-51-36.bpo-26680.wOWYps.rst @@ -0,0 +1,2 @@ +The int type now supports the x.is_integer() method for compatibility with +float. diff --git a/Misc/NEWS.d/next/Library/2018-03-15-11-55-04.bpo-26680.eKAi85.rst b/Misc/NEWS.d/next/Library/2018-03-15-11-55-04.bpo-26680.eKAi85.rst new file mode 100644 index 00000000000000..8b2e818383041a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-03-15-11-55-04.bpo-26680.eKAi85.rst @@ -0,0 +1,3 @@ +The x.is_integer() method is incorporated into the abstract types of the +numeric tower, Real, Rational and Integral, with appropriate default +implementations. diff --git a/Misc/NEWS.d/next/Library/2018-03-15-11-56-48.bpo-26680.Udkhn4.rst b/Misc/NEWS.d/next/Library/2018-03-15-11-56-48.bpo-26680.Udkhn4.rst new file mode 100644 index 00000000000000..df75e080fa6ee0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-03-15-11-56-48.bpo-26680.Udkhn4.rst @@ -0,0 +1,2 @@ +The d.is_integer() method is added to the Decimal type, for compatibility +with other number types.