From 9b63403045974851ee9ddd10bc45688c33946e31 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 08:37:52 +0300 Subject: [PATCH 01/11] Accept None in int.__round__ --- Objects/longobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index ee0b2a038a2aab..e33fd68573fe12 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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 == NULL || o_ndigits == Py_None) return long_long(self); ndigits = _PyNumber_Index(o_ndigits); From b6a64effa1afca5853922f1d6f51e7fd38b8d723 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 08:42:52 +0300 Subject: [PATCH 02/11] Add a NEWS entry --- .../2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst new file mode 100644 index 00000000000000..e67a82ce4df53d --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst @@ -0,0 +1,2 @@ +Direct call to the :meth:`!int.__round__` now accepts ``None`` +as a valid argument. \ No newline at end of file From 6cc13fa03d8b312ec0aee8e911e16911c196751c Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 08:49:07 +0300 Subject: [PATCH 03/11] Add a test cases --- Lib/test/test_float.py | 2 ++ Lib/test/test_int.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 5bd640617d6874..5f6488f781b680 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -949,6 +949,8 @@ def test_None_ndigits(self): self.assertEqual(x, 2) self.assertIsInstance(x, int) + def test_round_with_none_arg_direct_call(self): + self.assertEqual((1.0).__round__(None), 1.0) # Beginning with Python 2.6 float has cross platform compatible # ways to create and represent inf and nan diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index ce9febd741bba2..0eb9a3e4e86846 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -517,6 +517,8 @@ 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): + self.assertEqual((1).__round__(None), 1) class IntStrDigitLimitsTests(unittest.TestCase): From 6aa8ded5a015efc146c3d8a6e115922e63dea53a Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 08:52:02 +0300 Subject: [PATCH 04/11] Add a missing newline --- .../2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst index e67a82ce4df53d..8c5602fcdb4ad2 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst @@ -1,2 +1,2 @@ Direct call to the :meth:`!int.__round__` now accepts ``None`` -as a valid argument. \ No newline at end of file +as a valid argument. From d000960bc1f15e690a76da6aece85c67d8d31818 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 09:18:24 +0300 Subject: [PATCH 05/11] Change clinic annotation and remove unecessary check --- Objects/longobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/longobject.c b/Objects/longobject.c index e33fd68573fe12..a3a59a20f0bb97 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -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. @@ -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; @@ -6073,7 +6073,7 @@ int___round___impl(PyObject *self, PyObject *o_ndigits) * * m - divmod_near(m, 10**n)[1]. */ - if (o_ndigits == NULL || o_ndigits == Py_None) + if (o_ndigits == Py_None) return long_long(self); ndigits = _PyNumber_Index(o_ndigits); From 59e859c74abbe93cc5af4601bfbe21d0dded0962 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 10:30:35 +0300 Subject: [PATCH 06/11] forgot to commit this.. --- Objects/clinic/longobject.c.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/clinic/longobject.c.h b/Objects/clinic/longobject.c.h index 56bc3864582dcb..90375b9a082cca 100644 --- a/Objects/clinic/longobject.c.h +++ b/Objects/clinic/longobject.c.h @@ -116,7 +116,7 @@ int___format__(PyObject *self, PyObject *arg) } PyDoc_STRVAR(int___round____doc__, -"__round__($self, ndigits=, /)\n" +"__round__($self, ndigits=None, /)\n" "--\n" "\n" "Rounding an Integral returns itself.\n" @@ -133,7 +133,7 @@ static PyObject * int___round__(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { PyObject *return_value = NULL; - PyObject *o_ndigits = NULL; + PyObject *o_ndigits = Py_None; if (!_PyArg_CheckPositional("__round__", nargs, 0, 1)) { goto exit; @@ -476,4 +476,4 @@ int_is_integer(PyObject *self, PyObject *Py_UNUSED(ignored)) { return int_is_integer_impl(self); } -/*[clinic end generated code: output=2ba2d8dcda9b99da input=a9049054013a1b77]*/ +/*[clinic end generated code: output=a53f5ba9a6c16737 input=a9049054013a1b77]*/ From 4d01917c5e24f351d886ba320f898a333559732a Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 10:33:32 +0300 Subject: [PATCH 07/11] Update test_inspect.py --- Lib/test/test_inspect/test_inspect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 011d42f34b6461..65007c16203c6d 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -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'}, } From 46a76924ff61dc0aab14e2fd68ff29ba320e1f58 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 11:19:28 +0300 Subject: [PATCH 08/11] Update Lib/test/test_float.py Co-authored-by: Nikita Sobolev --- Lib/test/test_float.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 5f6488f781b680..c505227f82b1be 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -950,7 +950,11 @@ def test_None_ndigits(self): self.assertIsInstance(x, int) def test_round_with_none_arg_direct_call(self): - self.assertEqual((1.0).__round__(None), 1.0) + 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 From dde13ead309435ce76da1c836a69ce64f65321a4 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 11:21:58 +0300 Subject: [PATCH 09/11] Fix indent --- Lib/test/test_float.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index c505227f82b1be..53695cefb8fded 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -953,8 +953,8 @@ 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) + 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 From e1e4999af36e36dd3cfecfd799b3bb16368739ee Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 11:22:57 +0300 Subject: [PATCH 10/11] Update Lib/test/test_int.py --- Lib/test/test_int.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 0eb9a3e4e86846..8f3eafae6f2506 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -518,7 +518,10 @@ def test_issue31619(self): self.assertEqual(int('1_2_3_4_5_6_7', 32), 1144132807) def test_round_with_none_arg_direct_call(self): - self.assertEqual((1).__round__(None), 1) + for val in [(1).__round__(None), + round(1), + round(1, None)]: + self.assertEqual(val, 1) class IntStrDigitLimitsTests(unittest.TestCase): From bd802482a022768bc8a75fd8aea135b6851e1dad Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Wed, 5 Jun 2024 11:27:53 +0300 Subject: [PATCH 11/11] Update Lib/test/test_int.py Co-authored-by: Nikita Sobolev --- Lib/test/test_int.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 8f3eafae6f2506..77221dfb6d5aa2 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -522,6 +522,7 @@ def test_round_with_none_arg_direct_call(self): round(1), round(1, None)]: self.assertEqual(val, 1) + self.assertIs(type(val), int) class IntStrDigitLimitsTests(unittest.TestCase):