(original) (raw)

changeset: 103012:c3c4d8e4ca1a branch: 3.5 parent: 103006:a7ce98a4e9e4 user: Raymond Hettinger python@rcn.com date: Sat Sep 03 01:55:11 2016 -0700 files: Lib/test/test_builtin.py Lib/test/test_long.py Misc/NEWS Python/bltinmodule.c description: Issue 27936: Fix inconsistent round() behavior between float and int diff -r a7ce98a4e9e4 -r c3c4d8e4ca1a Lib/test/test_builtin.py --- a/Lib/test/test_builtin.py Thu Sep 01 23:29:04 2016 -0400 +++ b/Lib/test/test_builtin.py Sat Sep 03 01:55:11 2016 -0700 @@ -3,6 +3,8 @@ import ast import builtins import collections +import decimal +import fractions import io import locale import os @@ -1244,6 +1246,15 @@ self.assertEqual(round(5e15+2), 5e15+2) self.assertEqual(round(5e15+3), 5e15+3) + def test_bug_27936(self): + # Verify that ndigits=None means the same as passing in no argument + for x in [1234, + 1234.56, + decimal.Decimal('1234.56'), + fractions.Fraction(123456, 100)]: + self.assertEqual(round(x, None), round(x)) + self.assertEqual(type(round(x, None)), type(round(x))) + def test_setattr(self): setattr(sys, 'spam', 1) self.assertEqual(sys.spam, 1) diff -r a7ce98a4e9e4 -r c3c4d8e4ca1a Lib/test/test_long.py --- a/Lib/test/test_long.py Thu Sep 01 23:29:04 2016 -0400 +++ b/Lib/test/test_long.py Sat Sep 03 01:55:11 2016 -0700 @@ -967,7 +967,7 @@ self.assertIs(type(got), int) # bad second argument - bad_exponents = ('brian', 2.0, 0j, None) + bad_exponents = ('brian', 2.0, 0j) for e in bad_exponents: self.assertRaises(TypeError, round, 3, e) diff -r a7ce98a4e9e4 -r c3c4d8e4ca1a Misc/NEWS --- a/Misc/NEWS Thu Sep 01 23:29:04 2016 -0400 +++ b/Misc/NEWS Sat Sep 03 01:55:11 2016 -0700 @@ -18,6 +18,10 @@ ``m_methods`` field to be used to add module level functions to instances of non-module types returned from ``Py_create_mod``. Patch by Xiang Zhang. +- Issue #27936: The round() function accepted a second None argument + for some types but not for others. Fixed the inconsistency by + accepting None for all numeric types. + - Issue #27487: Warn if a submodule argument to "python -m" or runpy.run_module() is found in sys.modules after parent packages are imported, but before the submodule is executed. diff -r a7ce98a4e9e4 -r c3c4d8e4ca1a Python/bltinmodule.c --- a/Python/bltinmodule.c Thu Sep 01 23:29:04 2016 -0400 +++ b/Python/bltinmodule.c Sat Sep 03 01:55:11 2016 -0700 @@ -2039,7 +2039,7 @@ return NULL; } - if (ndigits == NULL) + if (ndigits == NULL || ndigits == Py_None) result = PyObject_CallFunctionObjArgs(round, NULL); else result = PyObject_CallFunctionObjArgs(round, ndigits, NULL); /python@rcn.com