FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-100268: Add is_integer method to int by hauntsaninja · Pull Request #100439 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 6 additions & 0 deletions Doc/library/stdtypes.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,12 @@ class`. In addition, it provides a few more methods:

.. versionadded:: 3.8

.. method:: int.is_integer()

Returns ``True``. Exists for duck type compatibility with :meth:`float.is_integer`.

.. versionadded:: 3.12

Additional Methods on Float
---------------------------

Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_long.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :meth:`int.is_integer` to improve duck type compatibility between :class:`int` and :class:`float`.
20 changes: 19 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.

14 changes: 14 additions & 0 deletions Objects/longobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -6168,6 +6168,19 @@ long_long_meth(PyObject *self, PyObject *Py_UNUSED(ignored))
return long_long(self);
}

/*[clinic input]
int.is_integer

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=7e41c4d4416e05f2]*/
{
Py_RETURN_TRUE;
}

static PyMethodDef long_methods[] = {
{"conjugate", long_long_meth, METH_NOARGS,
"Returns self, the complex conjugate of any int."},
Expand All @@ -6186,6 +6199,7 @@ static PyMethodDef long_methods[] = {
INT___GETNEWARGS___METHODDEF
INT___FORMAT___METHODDEF
INT___SIZEOF___METHODDEF
INT_IS_INTEGER_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down

Back | FazBrowse Home | New Git URL