Skip to content

Commit 4744f59

Browse files
authored
gh-112062: Make _struct module thread-safe in --disable-gil builds (#112094)
* gh-112062: Make `_struct` module thread-safe in --disable-gil builds
1 parent 55f3cce commit 4744f59

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Modules/_struct.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -2250,12 +2250,6 @@ cache_struct_converter(PyObject *module, PyObject *fmt, PyStructObject **ptr)
22502250
return 1;
22512251
}
22522252

2253-
if (state->cache == NULL) {
2254-
state->cache = PyDict_New();
2255-
if (state->cache == NULL)
2256-
return 0;
2257-
}
2258-
22592253
s_object = PyDict_GetItemWithError(state->cache, fmt);
22602254
if (s_object != NULL) {
22612255
*ptr = (PyStructObject *)Py_NewRef(s_object);
@@ -2288,7 +2282,7 @@ static PyObject *
22882282
_clearcache_impl(PyObject *module)
22892283
/*[clinic end generated code: output=ce4fb8a7bf7cb523 input=463eaae04bab3211]*/
22902284
{
2291-
Py_CLEAR(get_struct_state(module)->cache);
2285+
PyDict_Clear(get_struct_state(module)->cache);
22922286
Py_RETURN_NONE;
22932287
}
22942288

@@ -2512,6 +2506,11 @@ _structmodule_exec(PyObject *m)
25122506
{
25132507
_structmodulestate *state = get_struct_state(m);
25142508

2509+
state->cache = PyDict_New();
2510+
if (state->cache == NULL) {
2511+
return -1;
2512+
}
2513+
25152514
state->PyStructType = PyType_FromModuleAndSpec(
25162515
m, &PyStructType_spec, NULL);
25172516
if (state->PyStructType == NULL) {

0 commit comments

Comments
 (0)