Skip to content

Commit c1530c0

Browse files
committed
gh-129824: Fix data race in _PyBuiltins_AddExceptions with subinterpreters
Initialize the PyExc_OSError aliases statically to avoid data races.
1 parent 4f9a8d0 commit c1530c0

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

Objects/exceptions.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ class BaseExceptionGroup "PyBaseExceptionGroupObject *" "&PyExc_BaseExceptionGro
2525
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7c45e78cff8edc3]*/
2626

2727

28-
/* Compatibility aliases */
29-
PyObject *PyExc_EnvironmentError = NULL; // borrowed ref
30-
PyObject *PyExc_IOError = NULL; // borrowed ref
31-
#ifdef MS_WINDOWS
32-
PyObject *PyExc_WindowsError = NULL; // borrowed ref
33-
#endif
34-
35-
3628
static struct _Py_exc_state*
3729
get_exc_state(void)
3830
{
@@ -2528,6 +2520,13 @@ MiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError,
25282520
MiddlingExtendsException(PyExc_OSError, TimeoutError, OSError,
25292521
"Timeout expired.");
25302522

2523+
/* Compatibility aliases */
2524+
PyObject *PyExc_EnvironmentError = (PyObject *)&_PyExc_OSError; // borrowed ref
2525+
PyObject *PyExc_IOError = (PyObject *)&_PyExc_OSError; // borrowed ref
2526+
#ifdef MS_WINDOWS
2527+
PyObject *PyExc_WindowsError = (PyObject *)&_PyExc_OSError; // borrowed ref
2528+
#endif
2529+
25312530
/*
25322531
* EOFError extends Exception
25332532
*/
@@ -4599,23 +4598,20 @@ _PyBuiltins_AddExceptions(PyObject *bltinmod)
45994598
if (PyDict_SetItemString(mod_dict, "ExceptionGroup", PyExc_ExceptionGroup)) {
46004599
return -1;
46014600
}
4602-
4603-
#define INIT_ALIAS(NAME, TYPE) \
4604-
do { \
4605-
PyExc_ ## NAME = PyExc_ ## TYPE; \
4606-
if (PyDict_SetItemString(mod_dict, # NAME, PyExc_ ## TYPE)) { \
4607-
return -1; \
4608-
} \
4609-
} while (0)
4610-
4611-
INIT_ALIAS(EnvironmentError, OSError);
4612-
INIT_ALIAS(IOError, OSError);
4601+
if (PyDict_SetItemString(mod_dict, "EnvironmentError", PyExc_OSError)) {
4602+
return -1;
4603+
}
4604+
if (PyDict_SetItemString(mod_dict, "EnvironmentError", PyExc_OSError)) {
4605+
return -1;
4606+
}
4607+
if (PyDict_SetItemString(mod_dict, "IOError", PyExc_OSError)) {
4608+
return -1;
4609+
}
46134610
#ifdef MS_WINDOWS
4614-
INIT_ALIAS(WindowsError, OSError);
4611+
if (PyDict_SetItemString(mod_dict, "WindowsError", PyExc_OSError)) {
4612+
return -1;
4613+
}
46154614
#endif
4616-
4617-
#undef INIT_ALIAS
4618-
46194615
return 0;
46204616
}
46214617

0 commit comments

Comments
 (0)