diff -r d801be922dc2 Lib/test/datetimetester.py --- a/Lib/test/datetimetester.py Mon Mar 18 10:59:41 2013 -0700 +++ b/Lib/test/datetimetester.py Mon Mar 18 14:17:52 2013 -0700 @@ -619,6 +619,10 @@ eq(td(hours=-.2/us_per_hour), td(0)) eq(td(days=-.4/us_per_day, hours=-.2/us_per_hour), td(microseconds=-1)) + # Test for a patch in Issue 8860 + eq(td(microseconds=0.5), 0.5*td(microseconds=1.0)) + eq(td(microseconds=0.5)//td.resolution, 0.5*td.resolution//td.resolution) + def test_massive_normalization(self): td = timedelta(microseconds=-1) self.assertEqual((td.days, td.seconds, td.microseconds), diff -r d801be922dc2 Modules/_datetimemodule.c --- a/Modules/_datetimemodule.c Mon Mar 18 10:59:41 2013 -0700 +++ b/Modules/_datetimemodule.c Mon Mar 18 14:17:52 2013 -0700 @@ -2148,7 +2148,31 @@ } if (leftover_us) { /* Round to nearest whole # of us, and add into x. */ - PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); + // PyObject *temp = PyLong_FromLong(round_to_long(leftover_us)); + double whole_us = round(leftover_us); + int x_is_odd; + PyObject *temp; + + whole_us = round(leftover_us); + if (fabs(whole_us - leftover_us) == 0.5) { + /* We're exactly halfway between two integers. In order to do + * round-half-to-even, we must determine whether x is odd. */ + temp = PyNumber_And(x, us_per_us); + if (temp == NULL) { + Py_DECREF(x); + goto Done; + } + x_is_odd = PyObject_IsTrue(temp); + Py_DECREF(temp); + if (x_is_odd == -1) { + Py_DECREF(x); + goto Done; + } + whole_us = 2.0 * round((leftover_us + x_is_odd) * 0.5) - x_is_odd; + } + + temp = PyLong_FromLong(whole_us); + if (temp == NULL) { Py_DECREF(x); goto Done;