Adds check for boolop being used as exception class. Closes #100707.

---
.../third_party/py/pylint/checkers/exceptions.py | 7 +++++++ .../third_party/py/pylint/test/input/func_w0711.py | 15 +++++++++++++++ .../py/pylint/test/messages/func_w0711.txt | 4 ++++ 3 files changed, 26 insertions(+), 0 deletions(-) create mode 100644 /test/input/func_w0711.py create mode 100644 /test/messages/func_w0711.txt
authorTim Hatch <tim@timhatch.com>
changesetb42533d9b6a5
branchdefault
phasepublic
hiddenno
parent revision#1985c7abb006 pylint fixes
child revision#c47d262572d7 amend ChangeLog
files modified by this revision
ChangeLog
checkers/exceptions.py
test/input/func_w0711.py
test/messages/func_w0711.txt
# HG changeset patch
# User Tim Hatch <tim@timhatch.com>
# Date 1346223926 -7200
# Wed Aug 29 09:05:26 2012 +0200
# Node ID b42533d9b6a5e3214841461d78c9ff664e4354c8
# Parent 1985c7abb0068c9ac397062da48c3afc27c7fee7
Adds check for boolop being used as exception class. Closes #100707.
diff --git a/ChangeLog b/ChangeLog
@@ -1,8 +1,13 @@
1  ChangeLog for PyLint
2  ====================
3 
4 +--
5 +    * #100707: check for boolop being used as exception class, introducing 
6 +      new W0711 message (patch by Tim Hatch)
7 +
8 +
9  2012-07-17  --  0.25.2
10      * #93591: Correctly emit warnings about clobbered variable names when an
11        except handler contains a tuple of names instead of a single name.
12        (patch by tmarek@google.com)
13 
diff --git a/checkers/exceptions.py b/checkers/exceptions.py
@@ -56,10 +56,14 @@
14                'Used when an except clause does nothing but "pass" and there is\
15                no "else" clause.'),
16      'W0710': ('Exception doesn\'t inherit from standard "Exception" class',
17                'Used when a custom exception class is raised but doesn\'t \
18                inherit from the builtin "Exception" class.'),
19 +    'W0711': ('Exception to catch is the result of a binary "%s" operation',
20 +              'Used when the exception to catch is of the form \
21 +              "except A or B:".  If intending to catch multiple, \
22 +              rewrite as "except (A, B):"'),
23      }
24 
25 
26  if sys.version_info < (3, 0):
27      EXCEPTIONS_MODULE = "exceptions"
@@ -155,10 +159,13 @@
28                  # check if a "except:" is followed by some other
29                  # except
30                  elif index < (nb_handlers - 1):
31                      msg = 'empty except clause should always appear last'
32                      self.add_message('E0701', node=node, args=msg)
33 +
34 +            elif isinstance(handler.type, astng.BoolOp):
35 +                self.add_message('W0711', node=handler, args=handler.type.op)
36              else:
37                  try:
38                      excs = list(unpack_infer(handler.type))
39                  except astng.InferenceError:
40                      continue
diff --git a/test/input/func_w0711.py b/test/input/func_w0711.py
@@ -0,0 +1,15 @@
41 +"""find binary operations used as exceptions
42 +"""
43 +
44 +__revision__ = 1
45 +
46 +try:
47 +    __revision__ += 1
48 +except Exception or StandardError:
49 +    print "caught1"
50 +except Exception and StandardError:
51 +    print "caught2"
52 +except (Exception or StandardError):
53 +    print "caught3"
54 +except (Exception or StandardError), exc:
55 +    print "caught4"
diff --git a/test/messages/func_w0711.txt b/test/messages/func_w0711.txt
@@ -0,0 +1,4 @@
56 +W:  8: Exception to catch is the result of a binary "or" operation
57 +W: 10: Exception to catch is the result of a binary "and" operation
58 +W: 12: Exception to catch is the result of a binary "or" operation
59 +W: 14: Exception to catch is the result of a binary "or" operation