From dd59eb08ef22f4c8d3146530d5c8e86e44c0baae Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 1 Aug 2013 21:16:06 +0300 Subject: Check for non-exception classes inside except clauses. --HG-- branch : catch --- checkers/exceptions.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'checkers/exceptions.py') diff --git a/checkers/exceptions.py b/checkers/exceptions.py index eb82d0dff..8ac00a5f0 100644 --- a/checkers/exceptions.py +++ b/checkers/exceptions.py @@ -46,7 +46,11 @@ MSGS = { 'notimplemented-raised', 'Used when NotImplemented is raised instead of \ NotImplementedError'), - + 'E0712': ('Catching an exception which doesn\'t inherit from BaseException: %s', + 'catching-non-exception', + 'Used when a class which doesn\'t inherit from \ + BaseException is used as an exception in an except clause.'), + 'W0701': ('Raising a string exception', 'raising-string', 'Used when a string exception is raised.'), @@ -160,13 +164,14 @@ class ExceptionsChecker(BaseChecker): value_found = False return value_found - @check_messages('W0712') def visit_excepthandler(self, node): """Visit an except handler block and check for exception unpacking.""" if isinstance(node.name, (astroid.Tuple, astroid.List)): self.add_message('W0712', node=node) - @check_messages('W0702', 'W0703', 'W0704', 'W0711', 'E0701') + + + @check_messages('W0702', 'W0703', 'W0704', 'W0711', 'E0701', 'catching-non-exception') def visit_tryexcept(self, node): """check for empty except""" exceptions_classes = [] @@ -206,6 +211,13 @@ class ExceptionsChecker(BaseChecker): and exc.root().name == EXCEPTIONS_MODULE and nb_handlers == 1 and not is_raising(handler.body)): self.add_message('W0703', args=exc.name, node=handler.type) + + if (not inherit_from_std_ex(exc) and + exc.root().name != BUILTINS_NAME): + self.add_message('catching-non-exception', + node=handler.type, + args=(exc.name, )) + exceptions_classes += excs -- cgit v1.2.1