diff options
author | cpopa <devnull@localhost> | 2013-08-01 21:16:06 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2013-08-01 21:16:06 +0300 |
commit | d51aae3461f5505ef64157b3d07fba2072358b7c (patch) | |
tree | 49c463895529c3840db2637e45b84bd63a2fdf5e /checkers/exceptions.py | |
parent | f1b0ea4a88aeaa100865a056f9321c9646565bb9 (diff) | |
download | pylint-d51aae3461f5505ef64157b3d07fba2072358b7c.tar.gz |
Check for non-exception classes inside except clauses.
Diffstat (limited to 'checkers/exceptions.py')
-rw-r--r-- | checkers/exceptions.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/checkers/exceptions.py b/checkers/exceptions.py index eb82d0d..8ac00a5 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 |