Skip to content

Commit

Permalink
pythongh-112062: Make _struct module thread-safe in --disable-gil
Browse files Browse the repository at this point in the history
… builds (python#112094)

* pythongh-112062: Make `_struct` module thread-safe in --disable-gil builds
  • Loading branch information
chgnrdv authored and Glyphack committed Jan 27, 2024
1 parent fb6fb57 commit 630ac12
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 630ac12

Please sign in to comment.