]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-127945: make initialization of `error_object_name` thread safe in ctypes (#131896)
authorKumar Aditya <kumaraditya@python.org>
Sun, 30 Mar 2025 10:50:35 +0000 (16:20 +0530)
committerGitHub <noreply@github.com>
Sun, 30 Mar 2025 10:50:35 +0000 (10:50 +0000)
Modules/_ctypes/_ctypes.c
Modules/_ctypes/callproc.c

index 803d41630dd59ac5b61ecfffe74a6d1189280001..7536d3fdc2b882567f8259a0bfa3c68135a0c775 100644 (file)
@@ -6093,14 +6093,19 @@ _ctypes_mod_exec(PyObject *mod)
     }
 
 #ifdef WORDS_BIGENDIAN
-        st->swapped_suffix = PyUnicode_InternFromString("_le");
+    st->swapped_suffix = PyUnicode_InternFromString("_le");
 #else
-        st->swapped_suffix = PyUnicode_InternFromString("_be");
+    st->swapped_suffix = PyUnicode_InternFromString("_be");
 #endif
     if (st->swapped_suffix == NULL) {
         return -1;
     }
 
+    st->error_object_name = PyUnicode_InternFromString("ctypes.error_object");
+    if (st->error_object_name == NULL) {
+        return -1;
+    }
+
     if (_ctypes_add_types(mod) < 0) {
         return -1;
     }
index 80b66afb3625d1ff8b9fca3fa99a399ce45e6e33..158422fb9b0471545badecfe19c0d9514b6bd1f0 100644 (file)
@@ -164,12 +164,7 @@ _ctypes_get_errobj(ctypes_state *st, int **pspace)
                         "cannot get thread state");
         return NULL;
     }
-    if (st->error_object_name == NULL) {
-        st->error_object_name = PyUnicode_InternFromString("ctypes.error_object");
-        if (st->error_object_name == NULL) {
-            return NULL;
-        }
-    }
+    assert(st->error_object_name != NULL);
     if (PyDict_GetItemRef(dict, st->error_object_name, &errobj) < 0) {
         return NULL;
     }