-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-1635741 port signalmodule to multi-phase init (PEP 489) #22049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Port the :mod:`signalmodule` extension module to multi-phase initialization | ||
(:pep:`489`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1365,77 +1365,60 @@ ITIMER_PROF -- decrements both when the process is executing and\n\ | |
A signal handler function is called with two arguments:\n\ | ||
the first is the signal number, the second is the interrupted stack frame."); | ||
|
||
static struct PyModuleDef signalmodule = { | ||
PyModuleDef_HEAD_INIT, | ||
"_signal", | ||
module_doc, | ||
-1, | ||
signal_methods, | ||
NULL, | ||
NULL, | ||
NULL, | ||
NULL | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__signal(void) | ||
{ | ||
PyObject *m, *d; | ||
int i; | ||
|
||
/* Create the module and add the functions */ | ||
m = PyModule_Create(&signalmodule); | ||
if (m == NULL) | ||
return NULL; | ||
|
||
static int | ||
signal_exec(PyObject *m) | ||
{ | ||
/* add the functions */ | ||
#if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT) | ||
if (!initialized) { | ||
if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be nice to convert the static type SiginfoType to a heap type, but it can be done later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vstinner what is the procedure for types defined using PyStructSequence_InitType2? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know. |
||
return NULL; | ||
return -1; | ||
} | ||
Py_INCREF((PyObject*) &SiginfoType); | ||
PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for error. On error, DECREF is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shihai1991 good idea, this simplifies the error handling too. |
||
initialized = 1; | ||
#endif | ||
|
||
/* Add some symbolic constants to the module */ | ||
d = PyModule_GetDict(m); | ||
PyObject *d = PyModule_GetDict(m); | ||
|
||
DefaultHandler = PyLong_FromVoidPtr((void *)SIG_DFL); | ||
if (!DefaultHandler || | ||
PyDict_SetItemString(d, "SIG_DFL", DefaultHandler) < 0) { | ||
goto finally; | ||
return -1; | ||
} | ||
|
||
IgnoreHandler = PyLong_FromVoidPtr((void *)SIG_IGN); | ||
if (!IgnoreHandler || | ||
PyDict_SetItemString(d, "SIG_IGN", IgnoreHandler) < 0) { | ||
goto finally; | ||
return -1; | ||
} | ||
|
||
if (PyModule_AddIntMacro(m, NSIG)) | ||
goto finally; | ||
return -1; | ||
|
||
#ifdef SIG_BLOCK | ||
if (PyModule_AddIntMacro(m, SIG_BLOCK)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIG_UNBLOCK | ||
if (PyModule_AddIntMacro(m, SIG_UNBLOCK)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIG_SETMASK | ||
if (PyModule_AddIntMacro(m, SIG_SETMASK)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
|
||
IntHandler = PyDict_GetItemString(d, "default_int_handler"); | ||
if (!IntHandler) | ||
goto finally; | ||
return -1; | ||
Py_INCREF(IntHandler); | ||
|
||
_Py_atomic_store_relaxed(&Handlers[0].tripped, 0); | ||
for (i = 1; i < NSIG; i++) { | ||
for (int i = 1; i < NSIG; i++) { | ||
void (*t)(int); | ||
t = PyOS_getsig(i); | ||
_Py_atomic_store_relaxed(&Handlers[i].tripped, 0); | ||
|
@@ -1456,187 +1439,187 @@ PyInit__signal(void) | |
|
||
#ifdef SIGHUP | ||
if (PyModule_AddIntMacro(m, SIGHUP)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGINT | ||
if (PyModule_AddIntMacro(m, SIGINT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGBREAK | ||
if (PyModule_AddIntMacro(m, SIGBREAK)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGQUIT | ||
if (PyModule_AddIntMacro(m, SIGQUIT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGILL | ||
if (PyModule_AddIntMacro(m, SIGILL)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGTRAP | ||
if (PyModule_AddIntMacro(m, SIGTRAP)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGIOT | ||
if (PyModule_AddIntMacro(m, SIGIOT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGABRT | ||
if (PyModule_AddIntMacro(m, SIGABRT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGEMT | ||
if (PyModule_AddIntMacro(m, SIGEMT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGFPE | ||
if (PyModule_AddIntMacro(m, SIGFPE)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGKILL | ||
if (PyModule_AddIntMacro(m, SIGKILL)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGBUS | ||
if (PyModule_AddIntMacro(m, SIGBUS)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGSEGV | ||
if (PyModule_AddIntMacro(m, SIGSEGV)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGSYS | ||
if (PyModule_AddIntMacro(m, SIGSYS)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGPIPE | ||
if (PyModule_AddIntMacro(m, SIGPIPE)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGALRM | ||
if (PyModule_AddIntMacro(m, SIGALRM)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGTERM | ||
if (PyModule_AddIntMacro(m, SIGTERM)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGUSR1 | ||
if (PyModule_AddIntMacro(m, SIGUSR1)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGUSR2 | ||
if (PyModule_AddIntMacro(m, SIGUSR2)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGCLD | ||
if (PyModule_AddIntMacro(m, SIGCLD)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGCHLD | ||
if (PyModule_AddIntMacro(m, SIGCHLD)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGPWR | ||
if (PyModule_AddIntMacro(m, SIGPWR)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGIO | ||
if (PyModule_AddIntMacro(m, SIGIO)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGURG | ||
if (PyModule_AddIntMacro(m, SIGURG)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGWINCH | ||
if (PyModule_AddIntMacro(m, SIGWINCH)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGPOLL | ||
if (PyModule_AddIntMacro(m, SIGPOLL)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGSTOP | ||
if (PyModule_AddIntMacro(m, SIGSTOP)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGTSTP | ||
if (PyModule_AddIntMacro(m, SIGTSTP)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGCONT | ||
if (PyModule_AddIntMacro(m, SIGCONT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGTTIN | ||
if (PyModule_AddIntMacro(m, SIGTTIN)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGTTOU | ||
if (PyModule_AddIntMacro(m, SIGTTOU)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGVTALRM | ||
if (PyModule_AddIntMacro(m, SIGVTALRM)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGPROF | ||
if (PyModule_AddIntMacro(m, SIGPROF)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGXCPU | ||
if (PyModule_AddIntMacro(m, SIGXCPU)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGXFSZ | ||
if (PyModule_AddIntMacro(m, SIGXFSZ)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGRTMIN | ||
if (PyModule_AddIntMacro(m, SIGRTMIN)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGRTMAX | ||
if (PyModule_AddIntMacro(m, SIGRTMAX)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef SIGINFO | ||
if (PyModule_AddIntMacro(m, SIGINFO)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
|
||
#ifdef ITIMER_REAL | ||
if (PyModule_AddIntMacro(m, ITIMER_REAL)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef ITIMER_VIRTUAL | ||
if (PyModule_AddIntMacro(m, ITIMER_VIRTUAL)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
#ifdef ITIMER_PROF | ||
if (PyModule_AddIntMacro(m, ITIMER_PROF)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
|
||
#if defined (HAVE_SETITIMER) || defined (HAVE_GETITIMER) | ||
ItimerError = PyErr_NewException("signal.ItimerError", | ||
PyExc_OSError, NULL); | ||
if (!ItimerError || | ||
PyDict_SetItemString(d, "ItimerError", ItimerError) < 0) { | ||
goto finally; | ||
return -1; | ||
} | ||
#endif | ||
|
||
#ifdef CTRL_C_EVENT | ||
if (PyModule_AddIntMacro(m, CTRL_C_EVENT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
|
||
#ifdef CTRL_BREAK_EVENT | ||
if (PyModule_AddIntMacro(m, CTRL_BREAK_EVENT)) | ||
goto finally; | ||
return -1; | ||
#endif | ||
|
||
#ifdef MS_WINDOWS | ||
|
@@ -1645,12 +1628,30 @@ PyInit__signal(void) | |
#endif | ||
|
||
if (PyErr_Occurred()) { | ||
Py_DECREF(m); | ||
m = NULL; | ||
return -1; | ||
} | ||
|
||
finally: | ||
return m; | ||
return 0; | ||
} | ||
|
||
static PyModuleDef_Slot signal_slots[] = { | ||
{Py_mod_exec, signal_exec}, | ||
{0, NULL} | ||
}; | ||
|
||
static struct PyModuleDef signalmodule = { | ||
PyModuleDef_HEAD_INIT, | ||
"_signal", | ||
.m_doc = module_doc, | ||
.m_size = 0, | ||
.m_methods = signal_methods, | ||
.m_slots = signal_slots | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
PyInit__signal(void) | ||
{ | ||
return PyModuleDef_Init(&signalmodule); | ||
} | ||
|
||
static void | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The module is called "_signal":