Index: Lib/test/test_datetime.py =================================================================== --- Lib/test/test_datetime.py (Revision 62520) +++ Lib/test/test_datetime.py (Arbeitskopie) @@ -234,6 +234,9 @@ eq(a//10, td(0, 7*24*360)) eq(a//3600000, td(0, 0, 7*24*1000)) + eq(a//b, 10080) + eq(b/a, 1/10080) + def test_disallowed_computations(self): a = timedelta(42) Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (Revision 62520) +++ Modules/datetimemodule.c (Arbeitskopie) @@ -1656,6 +1656,30 @@ } static PyObject * +anydivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right, PyObject* (*operator)(PyObject*, PyObject*)) +{ + 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 = operator(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; @@ -1808,6 +1832,11 @@ result = divide_timedelta_int( (PyDateTime_Delta *)left, right); + if (PyDelta_Check(right)) + result = anydivide_timedelta_timedelta( + (PyDateTime_Delta *)left, + (PyDateTime_Delta *)right, + PyNumber_Divide); } if (result == Py_NotImplemented) @@ -1815,6 +1844,24 @@ return result; } +static PyObject * +delta_truedivide(PyObject *left, PyObject *right) +{ + PyObject *result = Py_NotImplemented; + + if (PyDelta_Check(left)) { + if (PyDelta_Check(right)) + result = anydivide_timedelta_timedelta( + (PyDateTime_Delta *)left, + (PyDateTime_Delta *)right, + PyNumber_TrueDivide); + } + + 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 @@ -2147,7 +2194,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 */ };