bpo-46417: signal: move siginfo_type to the module state (GH-30964)

This commit is contained in:
Victor Stinner 2022-01-27 21:21:50 +01:00 committed by GitHub
parent ace0aa2a27
commit 6c6a153dee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -136,7 +136,6 @@ typedef struct {
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
HANDLE sigint_event; HANDLE sigint_event;
#endif #endif
PyTypeObject *siginfo_type;
} signal_state_t; } signal_state_t;
// State shared by all Python interpreters // State shared by all Python interpreters
@ -152,6 +151,7 @@ typedef struct {
#ifdef PYHAVE_ITIMER_ERROR #ifdef PYHAVE_ITIMER_ERROR
PyObject *itimer_error; PyObject *itimer_error;
#endif #endif
PyTypeObject *siginfo_type;
} _signal_module_state; } _signal_module_state;
@ -1139,10 +1139,8 @@ static PyStructSequence_Desc struct_siginfo_desc = {
static PyObject * static PyObject *
fill_siginfo(siginfo_t *si) fill_siginfo(_signal_module_state *state, siginfo_t *si)
{ {
signal_state_t *state = &signal_global_state;
PyObject *result = PyStructSequence_New(state->siginfo_type); PyObject *result = PyStructSequence_New(state->siginfo_type);
if (!result) if (!result)
return NULL; return NULL;
@ -1206,7 +1204,8 @@ signal_sigwaitinfo_impl(PyObject *module, sigset_t sigset)
if (err == -1) if (err == -1)
return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL; return (!async_err) ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
return fill_siginfo(&si); _signal_module_state *state = get_signal_state(module);
return fill_siginfo(state, &si);
} }
#endif /* #ifdef HAVE_SIGWAITINFO */ #endif /* #ifdef HAVE_SIGWAITINFO */
@ -1274,7 +1273,8 @@ signal_sigtimedwait_impl(PyObject *module, sigset_t sigset,
} }
} while (1); } while (1);
return fill_siginfo(&si); _signal_module_state *state = get_signal_state(module);
return fill_siginfo(state, &si);
} }
#endif /* #ifdef HAVE_SIGTIMEDWAIT */ #endif /* #ifdef HAVE_SIGTIMEDWAIT */
@ -1661,8 +1661,15 @@ signal_module_exec(PyObject *m)
return -1; return -1;
} }
#endif #endif
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
if (PyModule_AddType(m, state->siginfo_type) < 0) { modstate->siginfo_type = PyStructSequence_NewType(&struct_siginfo_desc);
if (modstate->siginfo_type == NULL) {
return -1;
}
#endif
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
if (PyModule_AddType(m, modstate->siginfo_type) < 0) {
return -1; return -1;
} }
#endif #endif
@ -1683,16 +1690,18 @@ signal_module_exec(PyObject *m)
static int static int
_signal_module_traverse(PyObject *module, visitproc visit, void *arg) _signal_module_traverse(PyObject *module, visitproc visit, void *arg)
{ {
_signal_module_state *modstate = get_signal_state(module); _signal_module_state *state = get_signal_state(module);
Py_VISIT(modstate->itimer_error); Py_VISIT(state->itimer_error);
Py_VISIT(state->siginfo_type);
return 0; return 0;
} }
static int static int
_signal_module_clear(PyObject *module) _signal_module_clear(PyObject *module)
{ {
_signal_module_state *modstate = get_signal_state(module); _signal_module_state *state = get_signal_state(module);
Py_CLEAR(modstate->itimer_error); Py_CLEAR(state->itimer_error);
Py_CLEAR(state->siginfo_type);
return 0; return 0;
} }
@ -1760,7 +1769,6 @@ _PySignal_Fini(void)
Py_CLEAR(state->default_handler); Py_CLEAR(state->default_handler);
Py_CLEAR(state->ignore_handler); Py_CLEAR(state->ignore_handler);
Py_CLEAR(state->siginfo_type);
} }
@ -1968,13 +1976,6 @@ _PySignal_Init(int install_signal_handlers)
} }
#endif #endif
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
state->siginfo_type = PyStructSequence_NewType(&struct_siginfo_desc);
if (state->siginfo_type == NULL) {
return -1;
}
#endif
for (int signum = 1; signum < NSIG; signum++) { for (int signum = 1; signum < NSIG; signum++) {
_Py_atomic_store_relaxed(&Handlers[signum].tripped, 0); _Py_atomic_store_relaxed(&Handlers[signum].tripped, 0);
} }