From a1395c7cb393a6ca8561172f0bc12691235f90ff Mon Sep 17 00:00:00 2001 From: Takis Panagopoulos Date: Fri, 10 Apr 2020 23:18:22 +0300 Subject: [PATCH] Add a more straightforward validation of throws arg --- dramatiq/actor.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/dramatiq/actor.py b/dramatiq/actor.py index be6b1963..acb1713a 100644 --- a/dramatiq/actor.py +++ b/dramatiq/actor.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . +import inspect import re import time @@ -44,16 +45,16 @@ class Actor: """ def __init__(self, fn, *, broker, actor_name, queue_name, priority, options, throws): - if throws: + if throws is not None: # Validate throws can be used in an except statement - try: - try: - raise Exception() - except throws: - pass - except Exception: - pass - except TypeError: + def is_exception(exc): + return inspect.isclass(exc) and issubclass(exc, BaseException) + if ( + not ( + is_exception(throws) or + (isinstance(throws, tuple) and all([is_exception(exc) for exc in throws])) + ) + ): raise TypeError("'throws' must be a subclass of BaseException or a tuple of such.") self.logger = get_logger(fn.__module__, actor_name) self.fn = fn