Skip to content

Commit 4e6aad1

Browse files
Clean up float.as_integer_ratio().
1 parent 0d250bc commit 4e6aad1

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

Objects/floatobject.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,18 +1451,12 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
14511451
int exponent;
14521452
int i;
14531453

1454-
PyObject *prev;
14551454
PyObject *py_exponent = NULL;
14561455
PyObject *numerator = NULL;
14571456
PyObject *denominator = NULL;
14581457
PyObject *result_pair = NULL;
14591458
PyNumberMethods *long_methods = PyLong_Type.tp_as_number;
14601459

1461-
#define INPLACE_UPDATE(obj, call) \
1462-
prev = obj; \
1463-
obj = call; \
1464-
Py_DECREF(prev); \
1465-
14661460
CONVERT_TO_DOUBLE(v, self);
14671461

14681462
if (Py_IS_INFINITY(self)) {
@@ -1489,29 +1483,31 @@ float_as_integer_ratio(PyObject *v, PyObject *unused)
14891483
to be truncated by PyLong_FromDouble(). */
14901484

14911485
numerator = PyLong_FromDouble(float_part);
1492-
if (numerator == NULL) goto error;
1486+
if (numerator == NULL)
1487+
goto error;
1488+
denominator = PyLong_FromLong(1);
1489+
if (denominator == NULL)
1490+
goto error;
1491+
py_exponent = PyLong_FromLong(Py_ABS(exponent));
1492+
if (py_exponent == NULL)
1493+
goto error;
14931494

14941495
/* fold in 2**exponent */
1495-
denominator = PyLong_FromLong(1);
1496-
py_exponent = PyLong_FromLong(labs((long)exponent));
1497-
if (py_exponent == NULL) goto error;
1498-
INPLACE_UPDATE(py_exponent,
1499-
long_methods->nb_lshift(denominator, py_exponent));
1500-
if (py_exponent == NULL) goto error;
15011496
if (exponent > 0) {
1502-
INPLACE_UPDATE(numerator,
1503-
long_methods->nb_multiply(numerator, py_exponent));
1504-
if (numerator == NULL) goto error;
1497+
Py_SETREF(numerator,
1498+
long_methods->nb_lshift(numerator, py_exponent));
1499+
if (numerator == NULL)
1500+
goto error;
15051501
}
15061502
else {
1507-
Py_DECREF(denominator);
1508-
denominator = py_exponent;
1509-
py_exponent = NULL;
1503+
Py_SETREF(denominator,
1504+
long_methods->nb_lshift(denominator, py_exponent));
1505+
if (denominator == NULL)
1506+
goto error;
15101507
}
15111508

15121509
result_pair = PyTuple_Pack(2, numerator, denominator);
15131510

1514-
#undef INPLACE_UPDATE
15151511
error:
15161512
Py_XDECREF(py_exponent);
15171513
Py_XDECREF(denominator);

0 commit comments

Comments
 (0)