8000 [3.6] bpo-31629: Add support.SaveSignals (#4183) by vstinner · Pull Request #4187 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.6] bpo-31629: Add support.SaveSignals (#4183) #4187

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

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
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
bpo-31629: Add support.SaveSignals (#4183)
test_curses now saves/restores signals. On FreeBSD, the curses module
sets handlers of some signals, but don't restore old handlers when
the module is deinitialized.

(cherry picked from commit 19f6830)
  • Loading branch information
vstinner committed Oct 31, 2017
commit b8609d683d8374bf9d758a00ffb6281ff7cfb1b9
39 changes: 39 additions & 0 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2621,3 +2621,42 @@ def disable_faulthandler():
finally:
if is_enabled:
faulthandler.enable(file=fd, all_threads=True)


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)
5 changes: 4 additions & 1 deletion Lib/test/test_curses.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import tempfile
import unittest

from test.support import requires, import_module, verbose
from test.support import requires, import_module, verbose, SaveSignals

# Optionally test curses module. This currently requires that the
# 'curses' resource be given on the regrtest command line using the -u
Expand Down Expand Up @@ -63,6 +63,8 @@ def tearDownClass(cls):
del cls.tmp

def setUp(self):
self.save_signals = SaveSignals()
self.save_signals.save()
if verbose:
# just to make the test output a little more readable
print()
Expand All @@ -72,6 +74,7 @@ def setUp(self):
def tearDown(self):
curses.resetty()
curses.endwin()
self.save_signals.restore()

def test_window_funcs(self):
"Test the methods of windows"
Expand Down
0