Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (revision 67223) +++ Modules/datetimemodule.c (working copy) @@ -1664,6 +1664,54 @@ } static PyObject * +divide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) +{ + PyObject *pyus_left; + PyObject *pyus_right; + PyObject *result; + + pyus_left = delta_to_microseconds(left); + if (pyus_left == NULL) + return NULL; + + pyus_right = delta_to_microseconds(right); + if (pyus_right == NULL) + { + Py_DECREF(pyus_left); + return NULL; + } + + result = PyNumber_Divide(pyus_left, pyus_right); + Py_DECREF(pyus_left); + Py_DECREF(pyus_right); + return result; +} + +static PyObject * +truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) +{ + PyObject *pyus_left; + PyObject *pyus_right; + PyObject *result; + + pyus_left = delta_to_microseconds(left); + if (pyus_left == NULL) + return NULL; + + pyus_right = delta_to_microseconds(right); + if (pyus_right == NULL) + { + Py_DECREF(pyus_left); + return NULL; + } + + result = PyNumber_TrueDivide(pyus_left, pyus_right); + Py_DECREF(pyus_left); + Py_DECREF(pyus_right); + return result; +} + +static PyObject * delta_add(PyObject *left, PyObject *right) { PyObject *result = Py_NotImplemented; @@ -1816,6 +1864,10 @@ result = divide_timedelta_int( (PyDateTime_Delta *)left, right); + if (PyDelta_Check(right)) + result = divide_timedelta_timedelta( + (PyDateTime_Delta *)left, + (PyDateTime_Delta *)right); } if (result == Py_NotImplemented) @@ -1823,6 +1875,23 @@ return result; } +static PyObject * +delta_truedivide(PyObject *left, PyObject *right) +{ + PyObject *result = Py_NotImplemented; + + if (PyDelta_Check(left)) { + if (PyDelta_Check(right)) + result = truedivide_timedelta_timedelta( + (PyDateTime_Delta *)left, + (PyDateTime_Delta *)right); + } + + if (result == Py_NotImplemented) + Py_INCREF(result); + return result; +} + /* Fold in the value of the tag ("seconds", "weeks", etc) component of a * timedelta constructor. sofar is the # of microseconds accounted for * so far, and there are factor microseconds per current unit, the number @@ -2155,7 +2224,7 @@ 0, /*nb_inplace_xor*/ 0, /*nb_inplace_or*/ delta_divide, /* nb_floor_divide */ - 0, /* nb_true_divide */ + delta_truedivide, /* nb_true_divide */ 0, /* nb_inplace_floor_divide */ 0, /* nb_inplace_true_divide */ }; Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (revision 67223) +++ Lib/test/test_datetime.py (working copy) @@ -234,6 +234,19 @@ eq(a//10, td(0, 7*24*360)) eq(a//3600000, td(0, 0, 7*24*1000)) + # test timdelta // timedelta division + eq(a//a, 1) + eq(b//td(0, 6), 10) + eq(c//td(0, 0, 1), 1000) + eq(a//td(0, 7*24*360), 10) + eq(a//td(0, 0, 7*24*1000), 3600000) + + # test timdelta / timedelta true division + truediv = td.__truediv__ + eq(truediv(a, td(2)), 3.5) + # XXX Is this desired behavior? + eq(int(truediv(td.min, td.min//3)), 2) + def test_disallowed_computations(self): a = timedelta(42)