diff -r 07a6d4c30c4e Lib/test/test_float.py --- a/Lib/test/test_float.py Mon Dec 09 01:59:07 2013 +0100 +++ b/Lib/test/test_float.py Mon Dec 09 14:50:44 2013 +0800 @@ -769,6 +769,16 @@ test(sfmt, NAN, ' nan') test(sfmt, -NAN, ' nan') + def test_round_with_zero_ndigits_returns_integer(self): + x = round(1.23, 0) + self.assertEqual(x, 1) + # It should return 1 not 1.0 + self.assertIsInstance(x, int) + x = round(4.83, 0) + self.assertEqual(x, 5) + # It should return 5 not 5.0 + self.assertIsInstance(x, int) + # Beginning with Python 2.6 float has cross platform compatible # ways to create and represent inf and nan diff -r 07a6d4c30c4e Objects/floatobject.c --- a/Objects/floatobject.c Mon Dec 09 01:59:07 2013 +0100 +++ b/Objects/floatobject.c Mon Dec 09 14:50:44 2013 +0800 @@ -979,7 +979,12 @@ x = PyFloat_AsDouble(v); if (!PyArg_ParseTuple(args, "|O", &o_ndigits)) return NULL; - if (o_ndigits == NULL) { + + if (o_ndigits) + /* interpret second argument as a Py_ssize_t; clips on overflow */ + ndigits = PyNumber_AsSsize_t(o_ndigits, NULL); + + if (o_ndigits == NULL || ndigits == 0) { /* single-argument round: round to nearest integer */ rounded = round(x); if (fabs(x-rounded) == 0.5) @@ -988,8 +993,6 @@ return PyLong_FromDouble(rounded); } - /* interpret second argument as a Py_ssize_t; clips on overflow */ - ndigits = PyNumber_AsSsize_t(o_ndigits, NULL); if (ndigits == -1 && PyErr_Occurred()) return NULL;