Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Move external c-api functions
  • Loading branch information
CharlieZhao95 committed Jul 11, 2023
commit 0c480d90850c656aeb9a2e4b02f039f19d9a1e26
45 changes: 23 additions & 22 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ typedef struct {
PyObject *Rational;

PyObject *SignalTuple;

/* External C-API functions */
binaryfunc _py_long_multiply;
binaryfunc _py_long_floor_divide;
ternaryfunc _py_long_power;
unaryfunc _py_float_abs;
PyCFunction _py_long_bit_length;
PyCFunction _py_float_as_integer_ratio;
} decimal_state;

static decimal_state global_state;
Expand Down Expand Up @@ -2299,14 +2307,6 @@ PyDecType_FromLongExact(PyTypeObject *type, PyObject *v,
return dec;
}

/* External C-API functions */
static binaryfunc _py_long_multiply;
static binaryfunc _py_long_floor_divide;
static ternaryfunc _py_long_power;
static unaryfunc _py_float_abs;
static PyCFunction _py_long_bit_length;
static PyCFunction _py_float_as_integer_ratio;

/* Return a PyDecObject or a subtype from a PyFloatObject.
Conversion is exact. */
static PyObject *
Expand Down Expand Up @@ -2358,21 +2358,21 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
}

/* absolute value of the float */
tmp = _py_float_abs(v);
tmp = state->_py_float_abs(v);
if (tmp == NULL) {
return NULL;
}

/* float as integer ratio: numerator/denominator */
n_d = _py_float_as_integer_ratio(tmp, NULL);
n_d = state->_py_float_as_integer_ratio(tmp, NULL);
Py_DECREF(tmp);
if (n_d == NULL) {
return NULL;
}
n = PyTuple_GET_ITEM(n_d, 0);
d = PyTuple_GET_ITEM(n_d, 1);

tmp = _py_long_bit_length(d, NULL);
tmp = state->_py_long_bit_length(d, NULL);
if (tmp == NULL) {
Py_DECREF(n_d);
return NULL;
Expand Down Expand Up @@ -3660,14 +3660,14 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
goto error;
}

Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None));
Py_SETREF(exponent, state->_py_long_power(tmp, exponent, Py_None));
Py_DECREF(tmp);
if (exponent == NULL) {
goto error;
}

if (exp >= 0) {
Py_SETREF(numerator, _py_long_multiply(numerator, exponent));
Py_SETREF(numerator, state->_py_long_multiply(numerator, exponent));
if (numerator == NULL) {
goto error;
}
Expand All @@ -3683,12 +3683,12 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
if (tmp == NULL) {
goto error;
}
Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp));
Py_SETREF(numerator, state->_py_long_floor_divide(numerator, tmp));
if (numerator == NULL) {
Py_DECREF(tmp);
goto error;
}
Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp));
Py_SETREF(denominator, state->_py_long_floor_divide(denominator, tmp));
Py_DECREF(tmp);
if (denominator == NULL) {
goto error;
Expand Down Expand Up @@ -5834,13 +5834,14 @@ PyInit__decimal(void)
decimal_state *state = GLOBAL_STATE();

/* Init external C-API functions */
_py_long_multiply = PyLong_Type.tp_as_number->nb_multiply;
_py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide;
_py_long_power = PyLong_Type.tp_as_number->nb_power;
_py_float_abs = PyFloat_Type.tp_as_number->nb_absolute;
ASSIGN_PTR(_py_float_as_integer_ratio, cfunc_noargs(&PyFloat_Type,
"as_integer_ratio"));
ASSIGN_PTR(_py_long_bit_length, cfunc_noargs(&PyLong_Type, "bit_length"));
state->_py_long_multiply = PyLong_Type.tp_as_number->nb_multiply;
state->_py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide;
state->_py_long_power = PyLong_Type.tp_as_number->nb_power;
state->_py_float_abs = PyFloat_Type.tp_as_number->nb_absolute;
ASSIGN_PTR(state->_py_float_as_integer_ratio,
cfunc_noargs(&PyFloat_Type, "as_integer_ratio"));
ASSIGN_PTR(state->_py_long_bit_length,
cfunc_noargs(&PyLong_Type, "bit_length"));


/* Init types */
Expand Down