From 504e12272b19395a3c59631a20a94e3323af78e5 Mon Sep 17 00:00:00 2001
From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Date: Sat, 12 Nov 2022 14:41:32 +0530
Subject: [PATCH] GH-81057: Remove static state from arraymodule (#99409)

---
 Modules/arraymodule.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index d60cf26788f..8013c5ce6d7 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -58,6 +58,8 @@ typedef struct {
     PyTypeObject *ArrayType;
     PyTypeObject *ArrayIterType;
 
+    PyObject *array_reconstructor;
+
     PyObject *str_read;
     PyObject *str_write;
     PyObject *str___dict__;
@@ -2191,17 +2193,17 @@ array_array___reduce_ex___impl(arrayobject *self, PyTypeObject *cls,
     PyObject *array_str;
     int typecode = self->ob_descr->typecode;
     int mformat_code;
-    static PyObject *array_reconstructor = NULL;
     long protocol;
 
     array_state *state = get_array_state_by_class(cls);
     assert(state != NULL);
 
-    if (array_reconstructor == NULL) {
-        array_reconstructor = _PyImport_GetModuleAttrString(
+    if (state->array_reconstructor == NULL) {
+        state->array_reconstructor = _PyImport_GetModuleAttrString(
                 "array", "_array_reconstructor");
-        if (array_reconstructor == NULL)
+        if (state->array_reconstructor == NULL) {
             return NULL;
+        }
     }
 
     if (!PyLong_Check(value)) {
@@ -2252,8 +2254,10 @@ array_array___reduce_ex___impl(arrayobject *self, PyTypeObject *cls,
         Py_DECREF(dict);
         return NULL;
     }
+
+    assert(state->array_reconstructor != NULL);
     result = Py_BuildValue(
-        "O(OCiN)O", array_reconstructor, Py_TYPE(self), typecode,
+        "O(OCiN)O", state->array_reconstructor, Py_TYPE(self), typecode,
         mformat_code, array_str, dict);
     Py_DECREF(dict);
     return result;
@@ -3013,6 +3017,7 @@ array_traverse(PyObject *module, visitproc visit, void *arg)
     array_state *state = get_array_state(module);
     Py_VISIT(state->ArrayType);
     Py_VISIT(state->ArrayIterType);
+    Py_VISIT(state->array_reconstructor);
     return 0;
 }
 
@@ -3022,6 +3027,7 @@ array_clear(PyObject *module)
     array_state *state = get_array_state(module);
     Py_CLEAR(state->ArrayType);
     Py_CLEAR(state->ArrayIterType);
+    Py_CLEAR(state->array_reconstructor);
     Py_CLEAR(state->str_read);
     Py_CLEAR(state->str_write);
     Py_CLEAR(state->str___dict__);
@@ -3066,6 +3072,7 @@ array_modexec(PyObject *m)
     PyObject *typecodes;
     const struct arraydescr *descr;
 
+    state->array_reconstructor = NULL;
     /* Add interned strings */
     ADD_INTERNED(state, read);
     ADD_INTERNED(state, write);
-- 
GitLab