commit 854338ca172b5a3fbeb588a905f20878556fe816 Author: Damian Johnson atagar@torproject.org Date: Wed Apr 10 11:16:38 2019 -0700
Move stacktrace signaling into test invocation
Functionally teor's patch is fine as test/__init__.py is only imported in testing contexts. That said, as a general practice it's better to invoke functional code (like signal listeners) when a module is invoked rather than imported.
All testing is invoked through run_tests.py's main method, so we can register our signal handlers there instead. --- run_tests.py | 16 ++++++++++++++++ test/__init__.py | 18 ------------------ 2 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/run_tests.py b/run_tests.py index c31e7b48..f3959d01 100755 --- a/run_tests.py +++ b/run_tests.py @@ -7,6 +7,7 @@ Runs unit and integration tests. For usage information run this with '--help'. """
import os +import signal import sys import threading import time @@ -70,6 +71,18 @@ New capabilities are: """
+def log_traceback(sig, frame): + """ + Signal handler that logs the present traceback, and aborts our process with + exit status -1 in the case of SIGABRT. + """ + + print('Signal %s received.\nTraceback:\n%s' % (sig, traceback.format_stack(frame))) + + if sig == signal.SIGABRT: + sys.exit(-1) + + def get_unit_tests(module_prefix = None): """ Provides the classes for our unit tests. @@ -124,6 +137,9 @@ def main(): println('%s\n' % exc) sys.exit(1)
+ signal.signal(signal.SIGABRT, log_traceback) + signal.signal(signal.SIGUSR1, log_traceback) + test_config = stem.util.conf.get_config('test') test_config.load(os.path.join(test.STEM_BASE, 'test', 'settings.cfg'))
diff --git a/test/__init__.py b/test/__init__.py index dc1ab559..fa9678af 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -16,28 +16,10 @@ Unit and integration tests for the stem library. Helpers include... import collections import itertools import os -import signal -import sys -import traceback
import stem.util.enum import stem.version
-# Install signal handlers before doing anything else -def log_traceback(sig, frame): - """Log a stack trace. exit(-1) if the signal was SIGABRT.""" - message = "Signal {} received.\nTraceback:\n".format(sig) - message += ''.join(traceback.format_stack(frame)) - print(message) - if sig == signal.SIGABRT: - sys.exit(-1) - -# Register handlers -# Log stack trace and exit -signal.signal(signal.SIGABRT, log_traceback) -# Log stack trace and continue -signal.signal(signal.SIGUSR1, log_traceback) - __all__ = [ 'network', 'output',
tor-commits@lists.torproject.org