Refactor multiplication and division of timedelta and float. (#3656)

Implementations of these operations are virtually identical.
This commit is contained in:
Serhiy Storchaka 2017-10-04 20:30:09 +03:00 committed by GitHub
parent c02a1f4ad8
commit db12ef7e8a
1 changed files with 10 additions and 41 deletions

View File

@ -1677,8 +1677,9 @@ get_float_as_integer_ratio(PyObject *floatobj)
return ratio;
}
/* op is 0 for multiplication, 1 for division */
static PyObject *
multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
multiply_truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *floatobj, int op)
{
PyObject *result = NULL;
PyObject *pyus_in = NULL, *temp, *pyus_out;
@ -1691,12 +1692,12 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
if (ratio == NULL) {
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 0));
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, op));
Py_DECREF(pyus_in);
pyus_in = NULL;
if (temp == NULL)
goto error;
pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 1));
pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, !op));
Py_DECREF(temp);
if (pyus_out == NULL)
goto error;
@ -1776,38 +1777,6 @@ truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right)
return result;
}
static PyObject *
truedivide_timedelta_float(PyDateTime_Delta *delta, PyObject *f)
{
PyObject *result = NULL;
PyObject *pyus_in = NULL, *temp, *pyus_out;
PyObject *ratio = NULL;
pyus_in = delta_to_microseconds(delta);
if (pyus_in == NULL)
return NULL;
ratio = get_float_as_integer_ratio(f);
if (ratio == NULL) {
goto error;
}
temp = PyNumber_Multiply(pyus_in, PyTuple_GET_ITEM(ratio, 1));
Py_DECREF(pyus_in);
pyus_in = NULL;
if (temp == NULL)
goto error;
pyus_out = divide_nearest(temp, PyTuple_GET_ITEM(ratio, 0));
Py_DECREF(temp);
if (pyus_out == NULL)
goto error;
result = microseconds_to_delta(pyus_out);
Py_DECREF(pyus_out);
error:
Py_XDECREF(pyus_in);
Py_XDECREF(ratio);
return result;
}
static PyObject *
truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i)
{
@ -1958,15 +1927,15 @@ delta_multiply(PyObject *left, PyObject *right)
result = multiply_int_timedelta(right,
(PyDateTime_Delta *) left);
else if (PyFloat_Check(right))
result = multiply_float_timedelta(right,
(PyDateTime_Delta *) left);
result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *) left, right, 0);
}
else if (PyLong_Check(left))
result = multiply_int_timedelta(left,
(PyDateTime_Delta *) right);
else if (PyFloat_Check(left))
result = multiply_float_timedelta(left,
(PyDateTime_Delta *) right);
result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *) right, left, 0);
if (result == Py_NotImplemented)
Py_INCREF(result);
@ -2006,8 +1975,8 @@ delta_truedivide(PyObject *left, PyObject *right)
(PyDateTime_Delta *)left,
(PyDateTime_Delta *)right);
else if (PyFloat_Check(right))
result = truedivide_timedelta_float(
(PyDateTime_Delta *)left, right);
result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *)left, right, 1);
else if (PyLong_Check(right))
result = truedivide_timedelta_int(
(PyDateTime_Delta *)left, right);