diff options
author | Sylvain Th?nault <thenault@gmail.com> | 2013-09-02 17:53:54 +0200 |
---|---|---|
committer | Sylvain Th?nault <thenault@gmail.com> | 2013-09-02 17:53:54 +0200 |
commit | 9efb13e79121fd8370219d1d80d75e141774a3f5 (patch) | |
tree | 1b7345fae4367c902e1c718437d01de3d4bbed5b | |
parent | 92bbafc7af894a0139064f812f153b3e5eeee4bb (diff) | |
parent | 5568fd997ac89c003fb88f6142457414ac613f9a (diff) | |
download | pylint-9efb13e79121fd8370219d1d80d75e141774a3f5.tar.gz |
Merged in PCManticore/pylint/catch (pull request #47)
Check for non-exception classes inside except clauses.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | checkers/exceptions.py | 18 | ||||
-rw-r--r-- | test/input/func_catching_non_exception.py | 41 | ||||
-rw-r--r-- | test/messages/func_catching_non_exception.txt | 3 |
4 files changed, 63 insertions, 4 deletions
@@ -1,7 +1,11 @@ ChangeLog for Pylint ==================== +-- + * Check for non-exception classes inside an except clause + 2013-08-06 -- 1.0.0 + * Add check for the use of 'exec' function * New --msg-template option to control output, deprecating "msvc" and @@ -108,7 +112,6 @@ ChangeLog for Pylint modules - 2013-04-25 -- 0.28.0 * bitbucket #1: fix "dictionary changed size during iteration" crash 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 diff --git a/test/input/func_catching_non_exception.py b/test/input/func_catching_non_exception.py new file mode 100644 index 0000000..e6bfc57 --- /dev/null +++ b/test/input/func_catching_non_exception.py @@ -0,0 +1,41 @@ +"""test non-exceptions catched +""" + +__revision__ = 1 + +class MyException(object): + """ custom 'exception' """ + pass + +class MySecondException(object): + """ custom 'exception' """ + pass + +class MyGoodException(Exception): + """ custom 'exception' """ + pass + +class MySecondGoodException(MyGoodException): + """ custom 'exception' """ + pass + +try: + 1 + 1 +except MyException: + print "oups" + +try: + 1 + 2 +except (MyException, MySecondException): + print "oups" + +try: + 1 + 3 +except MyGoodException: + print "should work" + +try: + 1 + 3 +except (MyGoodException, MySecondGoodException): + print "should work" + diff --git a/test/messages/func_catching_non_exception.txt b/test/messages/func_catching_non_exception.txt new file mode 100644 index 0000000..9dc23eb --- /dev/null +++ b/test/messages/func_catching_non_exception.txt @@ -0,0 +1,3 @@ +E: 24: Catching an exception which doesn't inherit from BaseException: MyException +E: 29: Catching an exception which doesn't inherit from BaseException: MyException +E: 29: Catching an exception which doesn't inherit from BaseException: MySecondException
\ No newline at end of file |