Skip to content

Commit e45225e

Browse files
vstinneradorilson
authored andcommitted
bpo-41713: Remove PyOS_InitInterrupts() function (pythonGH-23342)
Remove the undocumented PyOS_InitInterrupts() C function. * Rename PyOS_InitInterrupts() to _PySignal_Init(). It now installs other signal handlers, not only SIGINT. * Rename PyOS_FiniInterrupts() to _PySignal_Fini()
1 parent b63770d commit e45225e

File tree

6 files changed

+38
-40
lines changed

6 files changed

+38
-40
lines changed

Doc/whatsnew/3.10.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,8 @@ Removed
583583
``Py_END_ALLOW_RECURSION`` and the ``recursion_critical`` field of the
584584
:c:type:`PyInterpreterState` structure.
585585
(Contributed by Serhiy Storchaka in :issue:`41936`.)
586+
587+
* Removed the undocumented ``PyOS_InitInterrupts()`` function. Initializing
588+
Python already implicitly installs signal handlers: see
589+
:c:member:`PyConfig.install_signal_handlers`.
590+
(Contributed by Victor Stinner in :issue:`41713`.)

Include/internal/pycore_pylifecycle.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ extern void _PyFloat_Fini(PyThreadState *tstate);
6868
extern void _PySlice_Fini(PyThreadState *tstate);
6969
extern void _PyAsyncGen_Fini(PyThreadState *tstate);
7070

71-
extern void PyOS_FiniInterrupts(void);
71+
extern int _PySignal_Init(int install_signal_handlers);
72+
extern void _PySignal_Fini(void);
7273

7374
extern void _PyExc_Fini(PyThreadState *tstate);
7475
extern void _PyImport_Fini(void);

Include/intrcheck.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ extern "C" {
55
#endif
66

77
PyAPI_FUNC(int) PyOS_InterruptOccurred(void);
8-
PyAPI_FUNC(void) PyOS_InitInterrupts(void);
98
#ifdef HAVE_FORK
109
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03070000
1110
PyAPI_FUNC(void) PyOS_BeforeFork(void);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed the undocumented ``PyOS_InitInterrupts()`` function. Initializing
2+
Python already implicitly installs signal handlers: see
3+
:c:member:`PyConfig.install_signal_handlers`. Patch by Victor Stinner.

Modules/signalmodule.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,8 +1674,8 @@ PyInit__signal(void)
16741674
}
16751675

16761676

1677-
static void
1678-
finisignal(void)
1677+
void
1678+
_PySignal_Fini(void)
16791679
{
16801680
int i;
16811681
PyObject *func;
@@ -1792,19 +1792,32 @@ PyErr_SetInterrupt(void)
17921792
}
17931793
}
17941794

1795-
void
1796-
PyOS_InitInterrupts(void)
1795+
int
1796+
_PySignal_Init(int install_signal_handlers)
17971797
{
1798-
PyObject *m = PyImport_ImportModule("_signal");
1799-
if (m) {
1800-
Py_DECREF(m);
1798+
if (!install_signal_handlers) {
1799+
// Nothing to do
1800+
return 0;
18011801
}
1802-
}
18031802

1804-
void
1805-
PyOS_FiniInterrupts(void)
1806-
{
1807-
finisignal();
1803+
#ifdef SIGPIPE
1804+
PyOS_setsig(SIGPIPE, SIG_IGN);
1805+
#endif
1806+
#ifdef SIGXFZ
1807+
PyOS_setsig(SIGXFZ, SIG_IGN);
1808+
#endif
1809+
#ifdef SIGXFSZ
1810+
PyOS_setsig(SIGXFSZ, SIG_IGN);
1811+
#endif
1812+
1813+
// Import _signal to install the Python SIGINT handler
1814+
PyObject *module = PyImport_ImportModule("_signal");
1815+
if (!module) {
1816+
return -1;
1817+
}
1818+
Py_DECREF(module);
1819+
1820+
return 0;
18081821
}
18091822

18101823

Python/pylifecycle.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ static PyStatus add_main_module(PyInterpreterState *interp);
5454
static PyStatus init_import_site(void);
5555
static PyStatus init_set_builtins_open(void);
5656
static PyStatus init_sys_streams(PyThreadState *tstate);
57-
static PyStatus init_signals(PyThreadState *tstate);
5857
static void call_py_exitfuncs(PyThreadState *tstate);
5958
static void wait_for_thread_shutdown(PyThreadState *tstate);
6059
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
@@ -1047,11 +1046,8 @@ init_interp_main(PyThreadState *tstate)
10471046
}
10481047

10491048
if (is_main_interp) {
1050-
if (config->install_signal_handlers) {
1051-
status = init_signals(tstate);
1052-
if (_PyStatus_EXCEPTION(status)) {
1053-
return status;
1054-
}
1049+
if (_PySignal_Init(config->install_signal_handlers) < 0) {
1050+
return _PyStatus_ERR("can't initialize signals");
10551051
}
10561052

10571053
if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
@@ -1702,7 +1698,7 @@ Py_FinalizeEx(void)
17021698
}
17031699

17041700
/* Disable signal handling */
1705-
PyOS_FiniInterrupts();
1701+
_PySignal_Fini();
17061702

17071703
/* Collect garbage. This may call finalizers; it's nice to call these
17081704
* before all modules are destroyed.
@@ -2730,25 +2726,6 @@ Py_Exit(int sts)
27302726
exit(sts);
27312727
}
27322728

2733-
static PyStatus
2734-
init_signals(PyThreadState *tstate)
2735-
{
2736-
#ifdef SIGPIPE
2737-
PyOS_setsig(SIGPIPE, SIG_IGN);
2738-
#endif
2739-
#ifdef SIGXFZ
2740-
PyOS_setsig(SIGXFZ, SIG_IGN);
2741-
#endif
2742-
#ifdef SIGXFSZ
2743-
PyOS_setsig(SIGXFSZ, SIG_IGN);
2744-
#endif
2745-
PyOS_InitInterrupts(); /* May imply init_signals() */
2746-
if (_PyErr_Occurred(tstate)) {
2747-
return _PyStatus_ERR("can't import signal");
2748-
}
2749-
return _PyStatus_OK();
2750-
}
2751-
27522729

27532730
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
27542731
*

0 commit comments

Comments
 (0)