Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0f1c23

Browse files
committed
Issue 27936: Fix inconsistent round() behavior between float and int
1 parent 22c108f commit f0f1c23

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import ast
44
import builtins
55
import collections
6+
import decimal
7+
import fractions
68
import io
79
import locale
810
import os
@@ -1244,6 +1246,15 @@ def test_round_large(self):
12441246
self.assertEqual(round(5e15+2), 5e15+2)
12451247
self.assertEqual(round(5e15+3), 5e15+3)
12461248

1249+
def test_bug_27936(self):
1250+
# Verify that ndigits=None means the same as passing in no argument
1251+
for x in [1234,
1252+
1234.56,
1253+
decimal.Decimal('1234.56'),
1254+
fractions.Fraction(123456, 100)]:
1255+
self.assertEqual(round(x, None), round(x))
1256+
self.assertEqual(type(round(x, None)), type(round(x)))
1257+
12471258
def test_setattr(self):
12481259
setattr(sys, 'spam', 1)
12491260
self.assertEqual(sys.spam, 1)

Lib/test/test_long.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ def test_round(self):
967967
self.assertIs(type(got), int)
968968

969969
# bad second argument
970-
bad_exponents = ('brian', 2.0, 0j, None)
970+
bad_exponents = ('brian', 2.0, 0j)
971971
for e in bad_exponents:
972972
self.assertRaises(TypeError, round, 3, e)
973973

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
``m_methods`` field to be used to add module level functions to instances
1919
of non-module types returned from ``Py_create_mod``. Patch by Xiang Zhang.
2020

21+
- Issue #27936: The round() function accepted a second None argument
22+
for some types but not for others. Fixed the inconsistency by
23+
accepting None for all numeric types.
24+
2125
- Issue #27487: Warn if a submodule argument to "python -m" or
2226
runpy.run_module() is found in sys.modules after parent packages are
2327
imported, but before the submodule is executed.

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,7 @@ builtin_round(PyObject *self, PyObject *args, PyObject *kwds)
20392039
return NULL;
20402040
}
20412041

2042-
if (ndigits == NULL)
2042+
if (ndigits == NULL || ndigits == Py_None)
20432043
result = PyObject_CallFunctionObjArgs(round, NULL);
20442044
else
20452045
result = PyObject_CallFunctionObjArgs(round, ndigits, NULL);

0 commit comments

Comments
 (0)