]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-120080: Accept ``None`` as a valid argument for direct call of the ``int.__round__...
authorKirill Podoprigora <kirill.bast9@mail.ru>
Fri, 7 Jun 2024 08:03:28 +0000 (11:03 +0300)
committerGitHub <noreply@github.com>
Fri, 7 Jun 2024 08:03:28 +0000 (10:03 +0200)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Lib/test/test_float.py
Lib/test/test_inspect/test_inspect.py
Lib/test/test_int.py
Misc/NEWS.d/next/Core and Builtins/2024-06-05-08-39-40.gh-issue-120080.DJFK11.rst [new file with mode: 0644]
Objects/clinic/longobject.c.h
Objects/longobject.c

index 5bd640617d687480696311598fd5f3942c753739..53695cefb8fded368969e8a607a5df70dd6a4d95 100644 (file)
@@ -949,6 +949,12 @@ class RoundTestCase(unittest.TestCase):
             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
index 011d42f34b646199cc9d7d76e2ddddd441615314..65007c16203c6dfafb22df76d22263f3ef9d9a35 100644 (file)
@@ -5412,7 +5412,6 @@ class TestSignatureDefinitions(unittest.TestCase):
             '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'},
         }
index ce9febd741bba2e63992b06a71976581860598a9..77221dfb6d5aa2fede27635e47a4cba371dba186 100644 (file)
@@ -517,6 +517,12 @@ class IntTestCases(unittest.TestCase):
         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):
 
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 (file)
index 0000000..8c5602f
--- /dev/null
@@ -0,0 +1,2 @@
+Direct call to the :meth:`!int.__round__` now accepts ``None``
+as a valid argument.
index 56bc3864582dcb95291a2829641644c991ffb1c4..90375b9a082ccae8df16d2bec4a3d219d6d67045 100644 (file)
@@ -116,7 +116,7 @@ exit:
 }
 
 PyDoc_STRVAR(int___round____doc__,
-"__round__($self, ndigits=<unrepresentable>, /)\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]*/
index ee0b2a038a2aab6185c2fdd1ff7aa1a3a2150b60..a3a59a20f0bb97781c56971b3d3b604636c72dc0 100644 (file)
@@ -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)
+    if (o_ndigits == Py_None)
         return long_long(self);
 
     ndigits = _PyNumber_Index(o_ndigits);