Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Doc/library/signal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ Module contents
signal (SIG*), handler (:const:`SIG_DFL`, :const:`SIG_IGN`) and sigmask
(:const:`SIG_BLOCK`, :const:`SIG_UNBLOCK`, :const:`SIG_SETMASK`)
related constants listed below were turned into
:class:`enums <enum.IntEnum>`.
:class:`enums <enum.IntEnum>` (:class:`Handlers` and :class:`Sigmasks` respectively).
:func:`getsignal`, :func:`pthread_sigmask`, :func:`sigpending` and
:func:`sigwait` functions return human-readable
:class:`enums <enum.IntEnum>`.
:class:`enums <enum.IntEnum>` as :class:`Signals` objects.


The variables defined in the :mod:`signal` module are:
Expand Down Expand Up @@ -478,8 +478,8 @@ The :mod:`signal` module defines the following functions:

.. _signal-example:

Example
-------
Examples
--------

Here is a minimal example program. It uses the :func:`alarm` function to limit
the time spent waiting to open a file; this is useful if the file is for a
Expand All @@ -503,6 +503,21 @@ be sent, and the handler raises an exception. ::

signal.alarm(0) # Disable the alarm

:class:`enums <enum.IntEnum>` types can be used to convert from signal code to string, or the reverse::

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's no need to duplicate the first example here. We can enhance it instead of adding another one:

import signal, os

def handler(signum, frame):
    signame = signal.Signals(signum).name
    print(f'Signal handler called with signal {signame} ({signum})')
    raise OSError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
# An enum member can also be passed as first argument
signal.signal(signal.Signals['SIGALRM'], handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it would be nice to document enums at the documentation:

The signal module defines three enums:

.. class:: Signals

.. You may be able to use ``.. enum:: Signals`` instead. I don't know if it's supported now.

   Document it here.

   .. versionadded:: 3.5


import signal, os

def handler(signum, frame):
# signum is an integer code, get the signal name using the signal.Signal enum
signame = signal.Signals(signum).name
print(f'Signal handler called with signal {signame} of code {signum}')

# attach a handler for signal.SIGINT
signame = 'SIGINT'
signal.signal(signal.Signals[signame], handler)



Note on SIGPIPE
---------------

Expand Down