Skip to content

Commit 57ad769

Browse files
Eclips4sobolevn
andauthored
gh-120080: Accept None as a valid argument for direct call of the int.__round__ (#120088)
Co-authored-by: Nikita Sobolev <[email protected]>
1 parent bd826b9 commit 57ad769

File tree

6 files changed

+20
-7
lines changed

6 files changed

+20
-7
lines changed

Lib/test/test_float.py

+6
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,12 @@ def test_None_ndigits(self):
949949
self.assertEqual(x, 2)
950950
self.assertIsInstance(x, int)
951951

952+
def test_round_with_none_arg_direct_call(self):
953+
for val in [(1.0).__round__(None),
954+
round(1.0),
955+
round(1.0, None)]:
956+
self.assertEqual(val, 1)
957+
self.assertIs(type(val), int)
952958

953959
# Beginning with Python 2.6 float has cross platform compatible
954960
# ways to create and represent inf and nan

Lib/test/test_inspect/test_inspect.py

-1
Original file line numberDiff line numberDiff line change
@@ -5412,7 +5412,6 @@ def test_builtins_have_signatures(self):
54125412
'bytearray': {'count', 'endswith', 'find', 'hex', 'index', 'rfind', 'rindex', 'startswith'},
54135413
'bytes': {'count', 'endswith', 'find', 'hex', 'index', 'rfind', 'rindex', 'startswith'},
54145414
'dict': {'pop'},
5415-
'int': {'__round__'},
54165415
'memoryview': {'cast', 'hex'},
54175416
'str': {'count', 'endswith', 'find', 'index', 'maketrans', 'rfind', 'rindex', 'startswith'},
54185417
}

Lib/test/test_int.py

+6
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ def test_issue31619(self):
517517
self.assertEqual(int('1_2_3_4_5_6_7_8_9', 16), 0x123456789)
518518
self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807)
519519

520+
def test_round_with_none_arg_direct_call(self):
521+
for val in [(1).__round__(None),
522+
round(1),
523+
round(1, None)]:
524+
self.assertEqual(val, 1)
525+
self.assertIs(type(val), int)
520526

521527
class IntStrDigitLimitsTests(unittest.TestCase):
522528

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Direct call to the :meth:`!int.__round__` now accepts ``None``
2+
as a valid argument.

Objects/clinic/longobject.c.h

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/longobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -6045,7 +6045,7 @@ _PyLong_DivmodNear(PyObject *a, PyObject *b)
60456045
/*[clinic input]
60466046
int.__round__
60476047
6048-
ndigits as o_ndigits: object = NULL
6048+
ndigits as o_ndigits: object = None
60496049
/
60506050
60516051
Rounding an Integral returns itself.
@@ -6055,7 +6055,7 @@ Rounding with an ndigits argument also returns an integer.
60556055

60566056
static PyObject *
60576057
int___round___impl(PyObject *self, PyObject *o_ndigits)
6058-
/*[clinic end generated code: output=954fda6b18875998 input=1614cf23ec9e18c3]*/
6058+
/*[clinic end generated code: output=954fda6b18875998 input=30c2aec788263144]*/
60596059
{
60606060
PyObject *temp, *result, *ndigits;
60616061

@@ -6073,7 +6073,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits)
60736073
*
60746074
* m - divmod_near(m, 10**n)[1].
60756075
*/
6076-
if (o_ndigits == NULL)
6076+
if (o_ndigits == Py_None)
60776077
return long_long(self);
60786078

60796079
ndigits = _PyNumber_Index(o_ndigits);

0 commit comments

Comments
 (0)