Skip to content

Commit

Permalink
gh-120080: Accept None as a valid argument for direct call of the…
Browse files Browse the repository at this point in the history
… ``int.__round__`` (#120088)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
2 people authored and pull[bot] committed Oct 13, 2024
1 parent caad4db commit 1407512
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,12 @@ def test_None_ndigits(self):
self.assertEqual(x, 2)
self.assertIsInstance(x, int)

def test_round_with_none_arg_direct_call(self):
for val in [(1.0).__round__(None),
round(1.0),
round(1.0, None)]:
self.assertEqual(val, 1)
self.assertIs(type(val), int)

# Beginning with Python 2.6 float has cross platform compatible
# ways to create and represent inf and nan
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -5412,7 +5412,6 @@ def test_builtins_have_signatures(self):
'bytearray': {'count', 'endswith', 'find', 'hex', 'index', 'rfind', 'rindex', 'startswith'},
'bytes': {'count', 'endswith', 'find', 'hex', 'index', 'rfind', 'rindex', 'startswith'},
'dict': {'pop'},
'int': {'__round__'},
'memoryview': {'cast', 'hex'},
'str': {'count', 'endswith', 'find', 'index', 'maketrans', 'rfind', 'rindex', 'startswith'},
}
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ def test_issue31619(self):
self.assertEqual(int('1_2_3_4_5_6_7_8_9', 16), 0x123456789)
self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807)

def test_round_with_none_arg_direct_call(self):
for val in [(1).__round__(None),
round(1),
round(1, None)]:
self.assertEqual(val, 1)
self.assertIs(type(val), int)

class IntStrDigitLimitsTests(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Direct call to the :meth:`!int.__round__` now accepts ``None``
as a valid argument.
6 changes: 3 additions & 3 deletions Objects/clinic/longobject.c.h

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

6 changes: 3 additions & 3 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6045,7 +6045,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
/*[clinic input]
int.__round__
ndigits as o_ndigits: object = NULL
ndigits as o_ndigits: object = None
/
Rounding an Integral returns itself.
Expand All @@ -6055,7 +6055,7 @@ Rounding with an ndigits argument also returns an integer.

static PyObject *
int___round___impl(PyObject *self, PyObject *o_ndigits)
/*[clinic end generated code: output=954fda6b18875998 input=1614cf23ec9e18c3]*/
/*[clinic end generated code: output=954fda6b18875998 input=30c2aec788263144]*/
{
PyObject *temp, *result, *ndigits;

Expand All @@ -6073,7 +6073,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
*
* m - divmod_near(m, 10**n)[1].
*/
if (o_ndigits == NULL)
if (o_ndigits == Py_None)
return long_long(self);

ndigits = _PyNumber_Index(o_ndigits);
Expand Down

0 comments on commit 1407512

Please sign in to comment.