summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-10-04 17:30:09 (GMT)
committerGitHub <noreply@github.com>2017-10-04 17:30:09 (GMT)
commitdb12ef7e8ac6a8a6f8b4830701558b9abfd108fc (patch)
treefdf64a22a6f94806bde09ea34f9cb8a16b241231 /Modules
parentc02a1f4ad8fcdbffad2911c5a31c71a17a89d713 (diff)
downloadcpython-db12ef7e8ac6a8a6f8b4830701558b9abfd108fc.zip
cpython-db12ef7e8ac6a8a6f8b4830701558b9abfd108fc.tar.gz
cpython-db12ef7e8ac6a8a6f8b4830701558b9abfd108fc.tar.bz2
Refactor multiplication and division of timedelta and float. (#3656)
Implementations of these operations are virtually identical.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_datetimemodule.c51
1 files changed, 10 insertions, 41 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 3dd7f82..fc2cdba 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -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;
@@ -1777,38 +1778,6 @@ truedivide_timedelta_timedelta(PyDateTime_Delta *left, PyDateTime_Delta *right)
}
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)
{
PyObject *result;
@@ -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);