From 3dcdc2c728bc58329ad9f7c194dc1fb85edd142c Mon Sep 17 00:00:00 2001 From: glados-verma Date: Fri, 6 Sep 2019 15:39:25 -0700 Subject: [PATCH] Use default SIGINT handler if no tests are running. (#897) * Move signal.signal() registration from package __init__ to Test.execute * Revert "Move signal.signal() registration from package __init__ to Test.execute" This reverts commit 6f6729c55bd701a0da24b8b0997aec9b61594853. * Use default SIGINT handler if no tests are running. --- openhtf/__init__.py | 2 +- openhtf/core/test_descriptor.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/openhtf/__init__.py b/openhtf/__init__.py index 9035622aa..b35ae0f07 100644 --- a/openhtf/__init__.py +++ b/openhtf/__init__.py @@ -57,4 +57,4 @@ def get_version(): __version__ = get_version() # Register signal handler to stop all tests on SIGINT. -signal.signal(signal.SIGINT, Test.handle_sig_int) +Test.DEFAULT_SIGINT_HANDLER = signal.signal(signal.SIGINT, Test.handle_sig_int) diff --git a/openhtf/core/test_descriptor.py b/openhtf/core/test_descriptor.py index 9974746d7..68c8686b7 100644 --- a/openhtf/core/test_descriptor.py +++ b/openhtf/core/test_descriptor.py @@ -126,6 +126,7 @@ def PhaseTwo(test): TEST_INSTANCES = weakref.WeakValueDictionary() HANDLED_SIGINT_ONCE = False + DEFAULT_SIGINT_HANDLER = None def __init__(self, *phases, **metadata): # Some sanity checks on special metadata keys we automatically fill in. @@ -228,11 +229,14 @@ def configure(self, **kwargs): setattr(self._test_options, key, value) @classmethod - def handle_sig_int(cls, *_): - if cls.TEST_INSTANCES: - _LOG.error('Received SIGINT, stopping all tests.') - for test in cls.TEST_INSTANCES.values(): - test.abort_from_sig_int() + def handle_sig_int(cls, signalnum, handler): + if not cls.TEST_INSTANCES: + cls.DEFAULT_SIGINT_HANDLER(signalnum, handler) + return + + _LOG.error('Received SIGINT, stopping all tests.') + for test in cls.TEST_INSTANCES.values(): + test.abort_from_sig_int() if not cls.HANDLED_SIGINT_ONCE: cls.HANDLED_SIGINT_ONCE = True raise KeyboardInterrupt