Skip to content

gh-106078: Move external C-API functions to decimal module global state #106616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 24 additions & 23 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 All @@ -2322,8 +2322,8 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
uint32_t status = 0;
mpd_context_t maxctx;

#ifdef Py_DEBUG
decimal_state *state = GLOBAL_STATE();
#ifdef Py_DEBUG
assert(PyType_IsSubtype(type, state->PyDec_Type));
#endif
if (PyLong_Check(v)) {
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
6 changes: 0 additions & 6 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,6 @@ Modules/_cursesmodule.c - initialised -
Modules/_cursesmodule.c - initialised_setupterm -
Modules/_cursesmodule.c - initialisedcolors -
Modules/_cursesmodule.c - screen_encoding -
Modules/_decimal/_decimal.c - _py_long_multiply -
Modules/_decimal/_decimal.c - _py_long_floor_divide -
Modules/_decimal/_decimal.c - _py_long_power -
Modules/_decimal/_decimal.c - _py_float_abs -
Modules/_decimal/_decimal.c - _py_long_bit_length -
Modules/_decimal/_decimal.c - _py_float_as_integer_ratio -
Modules/_elementtree.c - expat_capi -
Modules/readline.c - libedit_append_replace_history_offset -
Modules/readline.c - using_libedit_emulation -
Expand Down