From 451575de7acf941b14ba293ba41513511c8a2cb9 Mon Sep 17 00:00:00 2001 From: Guido Trotter Date: Wed, 19 Aug 2009 10:18:19 +0200 Subject: [PATCH] Add a new SignalHandled decorator This decorator can be used to call a function holding with protection from a given signal. The function should accept a dict of utils.SignalHandler, indexed by signal number, and make use of it. Signed-off-by: Guido Trotter Reviewed-by: Iustin Pop --- lib/utils.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/utils.py b/lib/utils.py index 046b91e..d5ca93d 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -2068,6 +2068,43 @@ class FileLock(object): "Failed to unlock %s" % self.filename) +def SignalHandled(signums): + """Signal Handled decoration. + + This special decorator installs a signal handler and then calls the target + function. The function must accept a 'signal_handlers' keyword argument, + which will contain a dict indexed by signal number, with SignalHandler + objects as values. + + The decorator can be safely stacked with iself, to handle multiple signals + with different handlers. + + @type signums: list + @param signums: signals to intercept + + """ + def wrap(fn): + def sig_function(*args, **kwargs): + assert 'signal_handlers' not in kwargs or \ + kwargs['signal_handlers'] is None or \ + isinstance(kwargs['signal_handlers'], dict), \ + "Wrong signal_handlers parameter in original function call" + if 'signal_handlers' in kwargs and kwargs['signal_handlers'] is not None: + signal_handlers = kwargs['signal_handlers'] + else: + signal_handlers = {} + kwargs['signal_handlers'] = signal_handlers + sighandler = SignalHandler(signums) + try: + for sig in signums: + signal_handlers[sig] = sighandler + return fn(*args, **kwargs) + finally: + sighandler.Reset() + return sig_function + return wrap + + class SignalHandler(object): """Generic signal handler class. -- 1.7.10.4