Skip to content
Merged
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'origin/master' into bpo-30696
  • Loading branch information
xdegaye committed Nov 5, 2017
commit 1516d4c81bd66369373a4b121ef993e26790209a
38 changes: 38 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2814,3 +2814,41 @@ def terminate(proc):
sel.modify(master, selectors.EVENT_READ)
proc.wait()
return proc.returncode, output

class SaveSignals:
"""
Save an restore signal handlers.

This class is only able to save/restore signal handlers registered
by the Python signal module: see bpo-13285 for "external" signal
handlers.
"""

def __init__(self):
import signal
self.signal = signal
self.signals = list(range(1, signal.NSIG))
# SIGKILL and SIGSTOP signals cannot be ignored nor catched
for signame in ('SIGKILL', 'SIGSTOP'):
try:
signum = getattr(signal, signame)
except AttributeError:
continue
self.signals.remove(signum)
self.handlers = {}

def save(self):
for signum in self.signals:
handler = self.signal.getsignal(signum)
if handler is None:
# getsignal() returns None if a signal handler was not
# registered by the Python signal module,
# and the handler is not SIG_DFL nor SIG_IGN.
#
# Ignore the signal: we cannot restore the handler.
continue
self.handlers[signum] = handler

def restore(self):
for signum, handler in self.handlers.items():
self.signal.signal(signum, handler)
You are viewing a condensed version of this merge commit. You can view the full changes here.