起步
信号机制的初始化
[Python/pylifecycle.c]
_PyInitError
_Py_InitializeMainInterpreter(PyInterpreterState *interp,
const _PyMainInterpreterConfig *config)
{
...
if (interp->config.install_signal_handlers) {
err = initsigs(); /* Signal handling stuff, including initintr() */
if (_Py_INIT_FAILED(err)) {
return err;
}
}
...
}
[Python/pylifecycle.c]
static _PyInitError
initsigs(void)
{
#ifdef SIGPIPE
PyOS_setsig(SIGPIPE, SIG_IGN); // 忽略SIGPIPE
#endif
#ifdef SIGXFZ
PyOS_setsig(SIGXFZ, SIG_IGN); // 忽略SIGXFZ
#endif
#ifdef SIGXFSZ
PyOS_setsig(SIGXFSZ, SIG_IGN); // 忽略SIGXFSZ file size exceeded
#endif
PyOS_InitInterrupts(); /* May imply initsignal() */
if (PyErr_Occurred()) {
return _Py_INIT_ERR("can't import signal");
}
return _Py_INIT_OK();
}
[Modules/signalmodule.c]
void
PyOS_InitInterrupts(void)
{
PyObject *m = PyImport_ImportModule("_signal");
if (m) {
Py_DECREF(m);
}
}
[Modules/signalmodule.c]
PyMODINIT_FUNC
PyInit__signal(void)
{
PyObject *m, *d, *x;
int i;
main_thread = PyThread_get_thread_ident();
main_pid = getpid();
// 创建signal模块
m = PyModule_Create(&signalmodule);
if (m == NULL)
return NULL;
...
/* Add some symbolic constants to the module */
d = PyModule_GetDict(m);
// 将SIG_DFL、SIGIGN 转化成Python整数对象
x = DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL);
if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
goto finally;
x &