@@ -505,29 +505,73 @@ Signal Handling
505505 single: SIGINT
506506 single: KeyboardInterrupt (built-in exception)
507507
508- This function interacts with Python's signal handling. It checks whether a
509- signal has been sent to the processes and if so, invokes the corresponding
510- signal handler. If the :mod:`signal` module is supported, this can invoke a
511- signal handler written in Python. In all cases, the default effect for
512- :const :`SIGINT` is to raise the :exc:`KeyboardInterrupt` exception. If an
513- exception is raised the error indicator is set and the function returns ``-1``;
514- otherwise the function returns ``0 ``. The error indicator may or may not be
515- cleared if it was previously set.
508+ This function interacts with Python's signal handling.
509+
510+ If the function is called from the main thread and under the main Python
511+ interpreter, it checks whether a signal has been sent to the processes
512+ and if so, invokes the corresponding signal handler. If the :mod:`signal`
513+ module is supported, this can invoke a signal handler written in Python.
514+
515+ The function attemps to handle all pending signals, and then returns ``0``.
516+ However, if a Python signal handler raises an exception, the error
517+ indicator is set and the function returns ``-1`` immediately (such that
518+ other pending signals may not have been handled yet: they will be on the
519+ next :c:func: `PyErr_CheckSignals() ` invocation).
520+
521+ If the function is called from a non-main thread, or under a non-main
522+ Python interpreter, it does nothing and returns ``0``.
523+
524+ This function can be called by long-running C code that wants to
525+ be interruptible by user requests (such as by pressing Ctrl-C).
526+
527+ .. note::
528+ The default Python signal handler for :const :`SIGINT` raises the
529+ :exc:`KeyboardInterrupt` exception.
516530
517531
518532.. c:function:: void PyErr_SetInterrupt()
519533
520534 .. index::
535+ module: signal
521536 single: SIGINT
522537 single: KeyboardInterrupt (built-in exception)
523538
524- Simulate the effect of a :const :`SIGINT` signal arriving. The next time
539+ Simulate the effect of a :const :`SIGINT` signal arriving.
540+ This is equivalent to ``PyErr_SetInterruptEx(SIGINT)``.
541+
542+ .. note::
543+ This function is async-signal-safe. It can be called without
544+ the :term:`GIL` and from a C signal handler.
545+
546+
547+ .. c:function:: int PyErr_SetInterruptEx(int signum)
548+
549+ .. index::
550+ module: signal
551+ single: KeyboardInterrupt (built-in exception)
552+
553+ Simulate the effect of a signal arriving. The next time
525554 :c:func:`PyErr_CheckSignals` is called, the Python signal handler for
526- :const :`SIGINT` will be called.
555+ the given signal number will be called.
556+
557+ This function can be called by C code that sets up its own signal handling
558+ and wants Python signal handlers to be invoked as expected when an
559+ interruption is requested (for example when the user presses Ctrl-C
560+ to interrupt an operation).
561+
562+ If the given signal isn't handled by Python (it was set to
563+ :data: `signal.SIG_DFL ` or :data: `signal.SIG_IGN `), it will be ignored.
564+
565+ If *signum* is outside of the allowed range of signal numbers, ``-1``
566+ is returned. Otherwise, ``0`` is returned. The error indicator is
567+ never changed by this function.
568+
569+ .. note::
570+ This function is async-signal-safe. It can be called without
571+ the :term:`GIL` and from a C signal handler.
572+
573+ .. versionadded:: 3.10
527574
528- If :const :`SIGINT` isn't handled by Python (it was set to
529- :data: `signal.SIG_DFL ` or :data: `signal.SIG_IGN `), this function does
530- nothing.
531575
532576.. c:function:: int PySignal_SetWakeupFd(int fd)
533577
0 commit comments