Skip to content

gh-120080: Accept None as a valid argument for direct call of the int.__round__ #120088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 7, 2024
Merged
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
Loading