From 930121eccde6f59bde8a923199b131bdd2e58b6b Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 22 Dec 2022 15:53:39 -0600 Subject: [PATCH 1/5] gh-100268: Add is_integer method to int --- Lib/test/test_long.py | 5 +++++ Objects/clinic/longobject.c.h | 20 +++++++++++++++++++- Objects/longobject.c | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 77b37ca1fa4afe..569ab15820e302 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -1553,6 +1553,11 @@ def test_from_bytes_small(self): b = i.to_bytes(2, signed=True) self.assertIs(int.from_bytes(b, signed=True), i) + def test_is_integer(self): + self.assertTrue((-1).is_integer()) + self.assertTrue((0).is_integer()) + self.assertTrue((1).is_integer()) + def test_access_to_nonexistent_digit_0(self): # http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that # ob_digit[0] was being incorrectly accessed for instances of a diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index dde49099cf9592..97c9a81567d15f 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -467,4 +467,22 @@ int_from_bytes(PyTypeObject *type, PyObject *const *args, Py_ssize_t nargs, PyOb exit: return return_value; } -/*[clinic end generated code: output=bf6074ecf2f32cf4 input=a9049054013a1b77]*/ + +PyDoc_STRVAR(int_is_integer__doc__, +"is_integer($self, /)\n" +"--\n" +"\n" +"Returns True."); + +#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); +} +/*[clinic end generated code: output=d6f555c14b0b534e input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 8596ce9797b5a6..77e729ab67c2de 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6168,6 +6168,19 @@ long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) return long_long(self); } +/*[clinic input] +int.is_integer + +Returns True. +[clinic start generated code]*/ + +static PyObject * +int_is_integer_impl(PyObject *self) +/*[clinic end generated code: output=90f8e794ce5430ef input=5987f0abb5d0e177]*/ +{ + Py_RETURN_TRUE; +} + static PyMethodDef long_methods[] = { {"conjugate", long_long_meth, METH_NOARGS, "Returns self, the complex conjugate of any int."}, @@ -6186,6 +6199,7 @@ static PyMethodDef long_methods[] = { INT___GETNEWARGS___METHODDEF INT___FORMAT___METHODDEF INT___SIZEOF___METHODDEF + INT_IS_INTEGER_METHODDEF {NULL, NULL} /* sentinel */ }; From fdf6bbd3565c839e7207b1f3725d7cfa6ef38769 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 22 Dec 2022 21:56:11 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst new file mode 100644 index 00000000000000..73d04c19d1cccc --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-22-21-56-08.gh-issue-100268.xw_phB.rst @@ -0,0 +1 @@ +Add :meth:`int.is_integer` to improve duck type compatibility between :class:`int` and :class:`float`. From 0d2dfa9225c7b773703c7008afffb05894e13a35 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 22 Dec 2022 15:58:32 -0600 Subject: [PATCH 3/5] docs --- Doc/library/stdtypes.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 624284f7092f60..0ed59e1a16214b 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -609,6 +609,12 @@ class`. In addition, it provides a few more methods: .. versionadded:: 3.8 +.. method:: int.is_integer() + + Returns ``True``. + + .. versionadded:: 3.12 + Additional Methods on Float --------------------------- From d5f67e179fabd3302f4f9b7e8a76cd3838224d9d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 23 Dec 2022 00:03:20 -0600 Subject: [PATCH 4/5] mention float in docs --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 0ed59e1a16214b..0ef03035a572e5 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -611,7 +611,7 @@ class`. In addition, it provides a few more methods: .. method:: int.is_integer() - Returns ``True``. + Returns ``True``. Exists for duck type compatibility with :meth:`float.is_integer`. .. versionadded:: 3.12 From 6b0a02eb05592471307814143f85227253678b6e Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 23 Dec 2022 15:46:32 -0600 Subject: [PATCH 5/5] update __doc__ --- Objects/clinic/longobject.c.h | 4 ++-- Objects/longobject.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 97c9a81567d15f..206bffdd086a5c 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -472,7 +472,7 @@ PyDoc_STRVAR(int_is_integer__doc__, "is_integer($self, /)\n" "--\n" "\n" -"Returns True."); +"Returns True. Exists for duck type compatibility with float.is_integer."); #define INT_IS_INTEGER_METHODDEF \ {"is_integer", (PyCFunction)int_is_integer, METH_NOARGS, int_is_integer__doc__}, @@ -485,4 +485,4 @@ int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) { return int_is_integer_impl(self); } -/*[clinic end generated code: output=d6f555c14b0b534e input=a9049054013a1b77]*/ +/*[clinic end generated code: output=e518fe2b5d519322 input=a9049054013a1b77]*/ diff --git a/Objects/longobject.c b/Objects/longobject.c index 77e729ab67c2de..0df3b9a9d564e0 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -6171,12 +6171,12 @@ long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored)) /*[clinic input] int.is_integer -Returns True. +Returns True. Exists for duck type compatibility with float.is_integer. [clinic start generated code]*/ static PyObject * int_is_integer_impl(PyObject *self) -/*[clinic end generated code: output=90f8e794ce5430ef input=5987f0abb5d0e177]*/ +/*[clinic end generated code: output=90f8e794ce5430ef input=7e41c4d4416e05f2]*/ { Py_RETURN_TRUE; }