[Python-checkins] GH-81057: Remove static state from arraymodule (#99409)

erlend-aasland webhook-mailer at python.org
Sat Nov 12 04:11:38 EST 2022


https://github.com/python/cpython/commit/504e12272b19395a3c59631a20a94e3323af78e5
commit: 504e12272b19395a3c59631a20a94e3323af78e5
branch: main
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2022-11-12T10:11:32+01:00
summary:

GH-81057: Remove static state from arraymodule (#99409)

files:
M Modules/arraymodule.c

diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index d60cf26788f5..8013c5ce6d78 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);



More information about the Python-checkins mailing list