8000 gh-106078: Move external C-API functions to decimal module global sta… · python/cpython@e5c32a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5c32a8

Browse files
gh-106078: Move external C-API functions to decimal module global state (#106616)
1 parent c0c041a commit e5c32a8

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

Modules/_decimal/_decimal.c

Lines changed: 24 additions & 23 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ typedef struct {
8181
PyObject *Rational;
8282

8383
PyObject *SignalTuple;
84+
85+
/* External C-API functions */
86+
binaryfunc _py_long_multiply;
87+
binaryfunc _py_long_floor_divide;
88+
ternaryfunc _py_long_power;
89+
unaryfunc _py_float_abs;
90+
PyCFunction _py_long_bit_length;
91+
PyCFunction _py_float_as_integer_ratio;
8492
} decimal_state;
8593

8694
static decimal_state global_state;
@@ -2299,14 +2307,6 @@ PyDecType_FromLongExact(PyTypeObject *type, PyObject *v,
22992307
return dec;
23002308
}
23012309

2302-
/* External C-API functions */
2303-
static binaryfunc _py_long_multiply;
2304-
static binaryfunc _py_long_floor_divide;
2305-
static ternaryfunc _py_long_power;
2306-
static unaryfunc _py_float_abs;
2307-
static PyCFunction _py_long_bit_length;
2308-
static PyCFunction _py_float_as_integer_ratio;
2309-
23102310
/* Return a PyDecObject or a subtype from a PyFloatObject.
23112311
Conversion is exact. */
23122312
static PyObject *
@@ -2322,8 +2322,8 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
23222322
uint32_t status = 0;
23232323
mpd_context_t maxctx;
23242324

2325-
#ifdef Py_DEBUG
23262325
decimal_state *state = GLOBAL_STATE();
2326+
#ifdef Py_DEBUG
23272327
assert(PyType_IsSubtype(type, state->PyDec_Type));
23282328
#endif
23292329
if (PyLong_Check(v)) {
@@ -2358,21 +2358,21 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
23582358
}
23592359

23602360
/* absolute value of the float */
2361-
tmp = _py_float_abs(v);
2361+
tmp = state->_py_float_abs(v);
23622362
if (tmp == NULL) {
23632363
return NULL;
23642364
}
23652365

23662366
/* float as integer ratio: numerator/denominator */
2367-
n_d = _py_float_as_integer_ratio(tmp, NULL);
2367+
n_d = state->_py_float_as_integer_ratio(tmp, NULL);
23682368
Py_DECREF(tmp);
23692369
if (n_d == NULL) {
23702370
return NULL;
23712371
}
23722372
n = PyTuple_GET_ITEM(n_d, 0);
23732373
d = PyTuple_GET_ITEM(n_d, 1);
23742374

2375-
tmp = _py_long_bit_length(d, NULL);
2375+
tmp = state->_py_long_bit_length(d, NULL);
23762376
if (tmp == NULL) {
23772377
Py_DECREF(n_d);
23782378
return NULL;
@@ -3660,14 +3660,14 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
36603660
goto error;
36613661
}
36623662

3663-
Py_SETREF(exponent, _py_long_power(tmp, exponent, Py_None));
3663+
Py_SETREF(exponent, state->_py_long_power(tmp, exponent, Py_None));
36643664
Py_DECREF(tmp);
36653665
if (exponent == NULL) {
36663666
goto error;
36673667
}
36683668

36693669
if (exp >= 0) {
3670-
Py_SETREF(numerator, _py_long_multiply(numerator, exponent));
3670+
Py_SETREF(numerator, state->_py_long_multiply(numerator, exponent));
36713671
if (numerator == NULL) {
36723672
goto error;
36733673
}
@@ -3683,12 +3683,12 @@ dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED)
36833683
if (tmp == NULL) {
36843684
goto error;
36853685
}
3686-
Py_SETREF(numerator, _py_long_floor_divide(numerator, tmp));
3686+
Py_SETREF(numerator, state->_py_long_floor_divide(numerator, tmp));
36873687
if (numerator == NULL) {
36883688
Py_DECREF(tmp);
36893689
goto error;
36903690
}
3691-
Py_SETREF(denominator, _py_long_floor_divide(denominator, tmp));
3691+
Py_SETREF(denominator, state->_py_long_floor_divide(denominator, tmp));
36923692
Py_DECREF(tmp);
36933693
if (denominator == NULL) {
36943694
goto error;
@@ -5834,13 +5834,14 @@ PyInit__decimal(void)
58345834
decimal_state *state = GLOBAL_STATE();
58355835

58365836
/* Init external C-API functions */
5837-
_py_long_multiply = PyLong_Type.tp_as_number->nb_multiply;
5838-
_py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide;
5839-
_py_long_power = PyLong_Type.tp_as_number->nb_power;
5840-
_py_float_abs = PyFloat_Type.tp_as_number->nb_absolute;
5841-
ASSIGN_PTR(_py_float_as_integer_ratio, cfunc_noargs(&PyFloat_Type,
5842-
"as_integer_ratio"));
5843-
ASSIGN_PTR(_py_long_bit_length, cfunc_noargs(&PyLong_Type, "bit_length"));
5837+
state->_py_long_multiply = PyLong_Type.tp_as_number->nb_multiply;
5838+
state->_py_long_floor_divide = PyLong_Type.tp_as_number->nb_floor_divide;
5839+
state->_py_long_power = PyLong_Type.tp_as_number->nb_power;
5840+
state->_py_float_abs = PyFloat_Type.tp_as_number->nb_absolute;
5841+
ASSIGN_PTR(state->_py_float_as_integer_ratio,
5842+
cfunc_noargs(&PyFloat_Type, "as_integer_ratio"));
5843+
ASSIGN_PTR(state->_py_long_bit_length,
5844+
cfunc_noargs(&PyLong_Type, "bit_length"));
58445845

58455846

58465847
/* Init types */

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,6 @@ Modules/_cursesmodule.c - initialised -
449449
Modules/_cursesmodule.c - initialised_setupterm -
450450
Modules/_cursesmodule.c - initialisedcolors -
451451
Modules/_cursesmodule.c - screen_encoding -
452-
Modules/_decimal/_decimal.c - _py_long_multiply -
453-
Modules/_decimal/_decimal.c - _py_long_floor_divide -
454-
Modules/_decimal/_decimal.c - _py_long_power -
455-
Modules/_decimal/_decimal.c - _py_float_abs -
456-
Modules/_decimal/_decimal.c - _py_long_bit_length -
457-
Modules/_decimal/_decimal.c - _py_float_as_integer_ratio -
458452
Modules/_elementtree.c - expat_capi -
459453
Modules/readline.c - libedit_append_replace_history_offset -
460454
Modules/readline.c - using_libedit_emulation -

0 commit comments

Comments
 (0)
0