8000 gh-106078: Move static variables initialized once to decimal module global state by CharlieZhao95 · Pull Request #106475 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-106078: Move static variables initialized once to decimal module global state #106475

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 4 commits into from
Jul 10, 2023
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 round_map to decimal module global state
  • Loading branch information
CharlieZhao95 committed Jul 6, 2023
commit 66f10461e3e4324b4a5ce429fc868db98e7163d4
27 changes: 15 additions & 12 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@

#include "docstrings.h"

#ifdef EXTRA_FUNCTIONALITY
#define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD
#else
#define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1)
#endif

typedef struct {
PyTypeObject *PyDecContextManager_Type;
PyTypeObject *PyDecContext_Type;
Expand All @@ -57,6 +63,8 @@ typedef struct {
/* Basic and extended context templates */
PyObject *basic_context_template;
PyObject *extended_context_template;

PyObject *round_map[_PY_DEC_ROUND_GUARD];
} decimal_state;

static decimal_state global_state;
Expand Down Expand Up @@ -216,13 +224,6 @@ static const char *dec_signal_string[MPD_NUM_FLAGS] = {
"Underflow",
};

#ifdef EXTRA_FUNCTIONALITY
#define _PY_DEC_ROUND_GUARD MPD_ROUND_GUARD
#else
#define _PY_DEC_ROUND_GUARD (MPD_ROUND_GUARD-1)
#endif
static PyObject *round_map[_PY_DEC_ROUND_GUARD];

static const char *invalid_rounding_err =
"valid values for rounding are:\n\
[ROUND_CEILING, ROUND_FLOOR, ROUND_UP, ROUND_DOWN,\n\
Expand Down Expand Up @@ -520,15 +521,16 @@ static int
getround(PyObject *v)
{
int i;
decimal_state *state = GLOBAL_STATE();

if (PyUnicode_Check(v)) {
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
if (v == round_map[i]) {
if (v == state->round_map[i]) {
return i;
}
}
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
if (PyUnicode_Compare(v, round_map[i]) == 0) {
if (PyUnicode_Compare(v, state->round_map[i]) == 0) {
return i;
}
}
Expand Down Expand Up @@ -754,8 +756,9 @@ static PyObject *
context_getround(PyObject *self, void *closure UNUSED)
{
int i = mpd_getround(CTX(self));
decimal_state *state = GLOBAL_STATE();

return Py_NewRef(round_map[i]);
return Py_NewRef(state->round_map[i]);
}

static PyObject *
Expand Down Expand Up @@ -6025,8 +6028,8 @@ PyInit__decimal(void)

/* Init string constants */
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(round_map[i])));
ASSIGN_PTR(state->round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(state->round_map[i])));
}

/* Add specification version number */
Expand Down
0