From c0bf5f4291383c33b2069c52e4e5e3262523083f Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Tue, 1 Aug 2023 10:12:05 +0800 Subject: [PATCH 1/5] Suppress mpd_setminalloc warning --- Modules/_decimal/_decimal.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 585214cc45d6cd..390d72af556511 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -5898,7 +5898,13 @@ _decimal_exec(PyObject *m) mpd_reallocfunc = PyMem_Realloc; mpd_callocfunc = mpd_callocfunc_em; mpd_free = PyMem_Free; - mpd_setminalloc(_Py_DEC_MINALLOC); + + /* Suppress the warning caused by multi-phase initialization */ + static int minalloc_is_set = 0; + if (minalloc_is_set) { + mpd_setminalloc(_Py_DEC_MINALLOC); + minalloc_is_set = 1; + } decimal_state *state = get_module_state(m); From 473eed1e14166c3db78a34f14726670d531002ad Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Tue, 1 Aug 2023 10:20:57 +0800 Subject: [PATCH 2/5] Update ignored.tsv --- Tools/c-analyzer/cpython/ignored.tsv | 1 + 1 file changed, 1 insertion(+) diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index 099f20b5a1b433..40eac549448969 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -214,6 +214,7 @@ Modules/_decimal/_decimal.c - invalid_signals_err - Modules/_decimal/_decimal.c - signal_map_template - Modules/_decimal/_decimal.c - ssize_constants - Modules/_decimal/_decimal.c - INVALID_SIGNALDICT_ERROR_MSG - +Modules/_decimal/_decimal.c - minalloc_is_set - Modules/_elementtree.c - ExpatMemoryHandler - Modules/_hashopenssl.c - py_hashes - Modules/_hacl/Hacl_Hash_SHA1.c - _h0 - From 20ea76e9b693a76fb80fa8024781d68e2b5b3103 Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Tue, 1 Aug 2023 11:35:04 +0800 Subject: [PATCH 3/5] Add constructor attribute --- Modules/_decimal/_decimal.c | 13 ++++++------- Tools/c-analyzer/cpython/ignored.tsv | 1 - 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 390d72af556511..d6bba92215b420 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -5877,6 +5877,12 @@ cfunc_noargs(PyTypeObject *t, const char *name) } +/* Suppress the warning caused by multi-phase initialization */ +__attribute__((constructor)) void minalloc_init(void) +{ + mpd_setminalloc(_Py_DEC_MINALLOC); +} + static int _decimal_exec(PyObject *m) { @@ -5899,13 +5905,6 @@ _decimal_exec(PyObject *m) mpd_callocfunc = mpd_callocfunc_em; mpd_free = PyMem_Free; - /* Suppress the warning caused by multi-phase initialization */ - static int minalloc_is_set = 0; - if (minalloc_is_set) { - mpd_setminalloc(_Py_DEC_MINALLOC); - minalloc_is_set = 1; - } - decimal_state *state = get_module_state(m); /* Init external C-API functions */ diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index 40eac549448969..099f20b5a1b433 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -214,7 +214,6 @@ Modules/_decimal/_decimal.c - invalid_signals_err - Modules/_decimal/_decimal.c - signal_map_template - Modules/_decimal/_decimal.c - ssize_constants - Modules/_decimal/_decimal.c - INVALID_SIGNALDICT_ERROR_MSG - -Modules/_decimal/_decimal.c - minalloc_is_set - Modules/_elementtree.c - ExpatMemoryHandler - Modules/_hashopenssl.c - py_hashes - Modules/_hacl/Hacl_Hash_SHA1.c - _h0 - From ed8a2b3a1714cc7ee407c10b7eed2fb2f0814b2b Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Tue, 1 Aug 2023 15:31:44 +0800 Subject: [PATCH 4/5] Move `minalloc_is_set` outside --- Modules/_decimal/_decimal.c | 13 +++++++------ Tools/c-analyzer/cpython/ignored.tsv | 3 +++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index d6bba92215b420..e9fdd397cfbd6e 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -5876,12 +5876,7 @@ cfunc_noargs(PyTypeObject *t, const char *name) return NULL; } - -/* Suppress the warning caused by multi-phase initialization */ -__attribute__((constructor)) void minalloc_init(void) -{ - mpd_setminalloc(_Py_DEC_MINALLOC); -} +static int minalloc_is_set = 0; static int _decimal_exec(PyObject *m) @@ -5905,6 +5900,12 @@ _decimal_exec(PyObject *m) mpd_callocfunc = mpd_callocfunc_em; mpd_free = PyMem_Free; + /* Suppress the warning caused by multi-phase initialization */ + if (minalloc_is_set) { + mpd_setminalloc(_Py_DEC_MINALLOC); + minalloc_is_set = 1; + } + decimal_state *state = get_module_state(m); /* Init external C-API functions */ diff --git a/Tools/c-analyzer/cpython/ignored.tsv b/Tools/c-analyzer/cpython/ignored.tsv index 099f20b5a1b433..3bbe0e6e7cc599 100644 --- a/Tools/c-analyzer/cpython/ignored.tsv +++ b/Tools/c-analyzer/cpython/ignored.tsv @@ -159,6 +159,9 @@ Python/pylifecycle.c fatal_error reentrant - # explicitly protected, internal-only Modules/_xxinterpchannelsmodule.c - _globals - +# set once during module init +Modules/_decimal/_decimal.c - minalloc_is_set - + ################################## ## not significant From ff0054b966ea780911dc1707407f5fc366e3565b Mon Sep 17 00:00:00 2001 From: CharlieZhao Date: Wed, 9 Aug 2023 09:18:06 +0800 Subject: [PATCH 5/5] Fix suppression --- Modules/_decimal/_decimal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index e9fdd397cfbd6e..216ddeffa26738 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -5901,7 +5901,7 @@ _decimal_exec(PyObject *m) mpd_free = PyMem_Free; /* Suppress the warning caused by multi-phase initialization */ - if (minalloc_is_set) { + if (!minalloc_is_set) { mpd_setminalloc(_Py_DEC_MINALLOC); minalloc_is_set = 1; }