Skip to content

bpo-35021: Fix assertion failures in _datetimemodule.c. #9958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,16 @@ def test_issue31752(self):
class BadInt(int):
def __mul__(self, other):
return Prod()
def __rmul__(self, other):
return Prod()
def __floordiv__(self, other):
return Prod()
def __rfloordiv__(self, other):
return Prod()

class Prod:
def __add__(self, other):
return Sum()
def __radd__(self, other):
return Sum()

Expand All @@ -906,6 +914,9 @@ def __divmod__(self, other):
timedelta(microseconds=BadInt(1))
timedelta(hours=BadInt(1))
timedelta(weeks=BadInt(1))
timedelta(1) * BadInt(1)
BadInt(1) * timedelta(1)
timedelta(1) // BadInt(1)


#############################################################################
Expand Down
26 changes: 23 additions & 3 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,26 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
#define microseconds_to_delta(pymicros) \
microseconds_to_delta_ex(pymicros, &PyDateTime_DeltaType)

/* Don't use PyNumber_Multiply() because of possible
overridden operators in int subclasses. */
static PyObject *
integer_multiply(PyObject *a, PyObject *b)
{
assert(PyLong_Check(a));
assert(PyLong_Check(b));
return PyLong_Type.tp_as_number->nb_multiply(a, b);
}

/* Don't use PyNumber_FloorDivide() because of possible
overridden operators in int subclasses. */
static PyObject *
integer_divide(PyObject *a, PyObject *b)
{
assert(PyLong_Check(a));
assert(PyLong_Check(b));
return PyLong_Type.tp_as_number->nb_floor_divide(a, b);
}

static PyObject *
multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
{
Expand All @@ -1868,7 +1888,7 @@ multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
if (pyus_in == NULL)
return NULL;

pyus_out = PyNumber_Multiply(pyus_in, intobj);
pyus_out = integer_multiply(pyus_in, intobj);
Py_DECREF(pyus_in);
if (pyus_out == NULL)
return NULL;
Expand Down Expand Up @@ -1949,7 +1969,7 @@ divide_timedelta_int(PyDateTime_Delta *delta, PyObject *intobj)
if (pyus_in == NULL)
return NULL;

pyus_out = PyNumber_FloorDivide(pyus_in, intobj);
pyus_out = integer_divide(pyus_in, intobj);
Py_DECREF(pyus_in);
if (pyus_out == NULL)
return NULL;
Expand Down Expand Up @@ -2309,7 +2329,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
assert(num != NULL);

if (PyLong_Check(num)) {
prod = PyNumber_Multiply(factor, num);
prod = integer_multiply(num, factor);
if (prod == NULL)
return NULL;
assert(PyLong_CheckExact(prod));
Expand Down