Skip to content

Commit 71119a1

Browse files
authored
gh-129824: Fix data race in _PyBuiltins_AddExceptions with subinterpreters (gh-143446)
1 parent dd750b3 commit 71119a1

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

Objects/exceptions.c

Lines changed: 16 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,17 @@ _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, "IOError", PyExc_OSError)) {
4605+
return -1;
4606+
}
46134607
#ifdef MS_WINDOWS
4614-
INIT_ALIAS(WindowsError, OSError);
4608+
if (PyDict_SetItemString(mod_dict, "WindowsError", PyExc_OSError)) {
4609+
return -1;
4610+
}
46154611
#endif
4616-
4617-
#undef INIT_ALIAS
4618-
46194612
return 0;
46204613
}
46214614

0 commit comments

Comments
 (0)