diff options
author | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-01-08 22:13:57 +0100 |
---|---|---|
committer | Sylvain Th?nault <sylvain.thenault@logilab.fr> | 2014-01-08 22:13:57 +0100 |
commit | c23e3e4689a189a9b9b9ca4f222b5fc184bc3e5a (patch) | |
tree | a104e6f22c87a34dcf3402b265eb2e408d87a47f | |
parent | 616d72e5348f491c3106088eb6ac21553233c276 (diff) | |
parent | 42bafa2540b5ed5b3f5e78647b7fe781657aa925 (diff) | |
download | pylint-c23e3e4689a189a9b9b9ca4f222b5fc184bc3e5a.tar.gz |
merge
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | README | 4 | ||||
-rw-r--r-- | checkers/exceptions.py | 30 | ||||
-rw-r--r-- | checkers/strings.py | 4 | ||||
-rw-r--r-- | test/input/func_catching_non_exception.py | 15 | ||||
-rw-r--r-- | test/messages/func_catching_non_exception.txt | 6 |
6 files changed, 49 insertions, 12 deletions
@@ -57,8 +57,6 @@ ChangeLog for Pylint * Various documentation fixes and enhancements - * Added a __main__.py file so you can run "python -m pylint" - * Fix issue #55 (false-positive trailing-whitespace on Windows) @@ -15,8 +15,8 @@ It's a free software distributed under the GNU Public Licence. Development is hosted on bitbucket: https://bitbucket.org/logilab/pylint/ You can use the code-quality@python.org mailing list to discuss about -Pylint. Subscribe at http://lists.python.org/mailman/listinfo/code-quality -or read the archives at http://lists.python.org/pipermail/code-quality/ +Pylint. Subscribe at https://mail.python.org/mailman/listinfo/code-quality/ +or read the archives at https://mail.python.org/pipermail/code-quality/ Install ------- diff --git a/checkers/exceptions.py b/checkers/exceptions.py index 9843001..5bb07ac 100644 --- a/checkers/exceptions.py +++ b/checkers/exceptions.py @@ -25,6 +25,23 @@ from pylint.checkers import BaseChecker from pylint.checkers.utils import is_empty, is_raising, check_messages from pylint.interfaces import IAstroidChecker +def infer_bases(klass): + """ Fully infer the bases of the klass node. + + This doesn't use .ancestors(), because we need + the non-inferable nodes (YES nodes), + which can't be retrieved from .ancestors() + """ + for base in klass.bases: + try: + inferit = base.infer().next() + except astroid.InferenceError: + continue + if inferit is YES: + yield inferit + else: + for base in infer_bases(inferit): + yield base OVERGENERAL_EXCEPTIONS = ('Exception',) @@ -214,9 +231,16 @@ class ExceptionsChecker(BaseChecker): 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, )) + # try to see if the exception is based on a C based + # exception, by infering all the base classes and + # looking for inference errors + bases = infer_bases(exc) + fully_infered = all(inferit is not YES + for inferit in bases) + if fully_infered: + self.add_message('catching-non-exception', + node=handler.type, + args=(exc.name, )) exceptions_classes += excs diff --git a/checkers/strings.py b/checkers/strings.py index b2dd167..c6bf960 100644 --- a/checkers/strings.py +++ b/checkers/strings.py @@ -66,11 +66,11 @@ MSGS = { 'E1305': ("Too many arguments for format string", "too-many-format-args", "Used when a format string that uses unnamed conversion \ - specifiers is given too few arguments."), + specifiers is given too many arguments."), 'E1306': ("Not enough arguments for format string", "too-few-format-args", "Used when a format string that uses unnamed conversion \ - specifiers is given too many arguments"), + specifiers is given too few arguments"), } OTHER_NODES = (astroid.Const, astroid.List, astroid.Backquote, diff --git a/test/input/func_catching_non_exception.py b/test/input/func_catching_non_exception.py index e6bfc57..d8e0e2d 100644 --- a/test/input/func_catching_non_exception.py +++ b/test/input/func_catching_non_exception.py @@ -1,5 +1,6 @@ """test non-exceptions catched """ +import socket __revision__ = 1 @@ -19,6 +20,16 @@ class MySecondGoodException(MyGoodException): """ custom 'exception' """ pass +class SkipException(socket.error): + """ This gave false positives for Python 2, + but not for Python 3, due to exception + hierarchy rewrite. + """ + +class SecondSkipException(SkipException): + """ This too shouldn't give false + positives. """ + try: 1 + 1 except MyException: @@ -39,3 +50,7 @@ try: except (MyGoodException, MySecondGoodException): print "should work" +try: + 1 + 3 +except (SkipException, SecondSkipException): + print "should work" diff --git a/test/messages/func_catching_non_exception.txt b/test/messages/func_catching_non_exception.txt index 9dc23eb..19f22ab 100644 --- a/test/messages/func_catching_non_exception.txt +++ b/test/messages/func_catching_non_exception.txt @@ -1,3 +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 +E: 35: Catching an exception which doesn't inherit from BaseException: MyException +E: 40: Catching an exception which doesn't inherit from BaseException: MyException +E: 40: Catching an exception which doesn't inherit from BaseException: MySecondException
\ No newline at end of file |