]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112062: Make `_struct` module thread-safe in `--disable-gil` builds (#112094)
authorRadislav Chugunov <52372310+chgnrdv@users.noreply.github.com>
Wed, 15 Nov 2023 04:00:34 +0000 (07:00 +0300)
committerGitHub <noreply@github.com>
Wed, 15 Nov 2023 04:00:34 +0000 (09:30 +0530)
* gh-112062: Make `_struct` module thread-safe in --disable-gil builds

Modules/_struct.c

index ff1bf4e96c5f214ef2b333a280f0f45f0baf864c..0116b03ea9511550c26c3bf4f33e277c5fedbcc3 100644 (file)
@@ -2250,12 +2250,6 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
         return 1;
     }
 
-    if (state->cache == NULL) {
-        state->cache = PyDict_New();
-        if (state->cache == NULL)
-            return 0;
-    }
-
     s_object = PyDict_GetItemWithError(state->cache, fmt);
     if (s_object != NULL) {
         *ptr = (PyStructObject *)Py_NewRef(s_object);
@@ -2288,7 +2282,7 @@ static PyObject *
 _clearcache_impl(PyObject *module)
 /*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
 {
-    Py_CLEAR(get_struct_state(module)->cache);
+    PyDict_Clear(get_struct_state(module)->cache);
     Py_RETURN_NONE;
 }
 
@@ -2512,6 +2506,11 @@ _structmodule_exec(PyObject *m)
 {
     _structmodulestate *state = get_struct_state(m);
 
+    state->cache = PyDict_New();
+    if (state->cache == NULL) {
+        return -1;
+    }
+
     state->PyStructType = PyType_FromModuleAndSpec(
         m, &PyStructType_spec, NULL);
     if (state->PyStructType == NULL) {