Kaydet (Commit) db12ef7e authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

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

Implementations of these operations are virtually identical.
üst c02a1f4a
...@@ -1677,8 +1677,9 @@ get_float_as_integer_ratio(PyObject *floatobj) ...@@ -1677,8 +1677,9 @@ get_float_as_integer_ratio(PyObject *floatobj)
return ratio; return ratio;
} }
/* op is 0 for multiplication, 1 for division */
static PyObject * 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 *result = NULL;
PyObject *pyus_in = NULL, *temp, *pyus_out; PyObject *pyus_in = NULL, *temp, *pyus_out;
...@@ -1691,12 +1692,12 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta) ...@@ -1691,12 +1692,12 @@ multiply_float_timedelta(PyObject *floatobj, PyDateTime_Delta *delta)
if (ratio == NULL) { if (ratio == NULL) {
goto error; 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); Py_DECREF(pyus_in);
pyus_in = NULL; pyus_in = NULL;
if (temp == NULL) if (temp == NULL)
goto error; 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); Py_DECREF(temp);
if (pyus_out == NULL) if (pyus_out == NULL)
goto error; goto error;
...@@ -1776,38 +1777,6 @@ truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right) ...@@ -1776,38 +1777,6 @@ truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right)
return result; 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 * static PyObject *
truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i) truedivide_timedelta_int(PyDateTime_Delta *delta, PyObject *i)
{ {
...@@ -1958,15 +1927,15 @@ delta_multiply(PyObject *left, PyObject *right) ...@@ -1958,15 +1927,15 @@ delta_multiply(PyObject *left, PyObject *right)
result = multiply_int_timedelta(right, result = multiply_int_timedelta(right,
(PyDateTime_Delta *) left); (PyDateTime_Delta *) left);
else if (PyFloat_Check(right)) else if (PyFloat_Check(right))
result = multiply_float_timedelta(right, result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *) left); (PyDateTime_Delta *) left, right, 0);
} }
else if (PyLong_Check(left)) else if (PyLong_Check(left))
result = multiply_int_timedelta(left, result = multiply_int_timedelta(left,
(PyDateTime_Delta *) right); (PyDateTime_Delta *) right);
else if (PyFloat_Check(left)) else if (PyFloat_Check(left))
result = multiply_float_timedelta(left, result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *) right); (PyDateTime_Delta *) right, left, 0);
if (result == Py_NotImplemented) if (result == Py_NotImplemented)
Py_INCREF(result); Py_INCREF(result);
...@@ -2006,8 +1975,8 @@ delta_truedivide(PyObject *left, PyObject *right) ...@@ -2006,8 +1975,8 @@ delta_truedivide(PyObject *left, PyObject *right)
(PyDateTime_Delta *)left, (PyDateTime_Delta *)left,
(PyDateTime_Delta *)right); (PyDateTime_Delta *)right);
else if (PyFloat_Check(right)) else if (PyFloat_Check(right))
result = truedivide_timedelta_float( result = multiply_truedivide_timedelta_float(
(PyDateTime_Delta *)left, right); (PyDateTime_Delta *)left, right, 1);
else if (PyLong_Check(right)) else if (PyLong_Check(right))
result = truedivide_timedelta_int( result = truedivide_timedelta_int(
(PyDateTime_Delta *)left, right); (PyDateTime_Delta *)left, right);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment