~ubuntu-branches/ubuntu/trusty/pylint/trusty

« back to all changes in this revision

Viewing changes to checkers/exceptions.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2009-03-27 09:45:39 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090327094539-xe2s86jkbvdv3xhb
Tags: 0.18.0-1
* New upstream release
* debian/copyright
  - added packaging copyright for the work I do
  - clearly separated copyright and license notices, indenting by 4 spaces
  - link to GPL-2 file, not to the generic GPL
* debian/control
  - updated Homepage field
  - bump versions for python-logilab-common and python-logilab-astng depends
  - bump Standard-Versions to 3.8.1 (no changes needed)
* debian/{control, rules}
  - switch from python-central to python-support
* debian/rules
  - 'build' is a dir, we need to clean with 'rm'

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from logilab.common.compat import enumerate
20
20
from logilab import astng
21
 
from logilab.astng.inference import unpack_infer
 
21
from logilab.astng.infutils import YES, Instance, unpack_infer
22
22
 
23
23
from pylint.checkers import BaseChecker
24
24
from pylint.checkers.utils import is_empty, is_raising
76
76
    def visit_raise(self, node):
77
77
        """visit raise possibly infering value"""
78
78
        # ignore empty raise
79
 
        if node.expr1 is None:
 
79
        if node.type is None:
80
80
            return
81
 
        expr = node.expr1
 
81
        expr = node.type
82
82
        if self._check_raise_value(node, expr):
83
83
            return
84
84
        else:
104
104
             isinstance(expr, (astng.List, astng.Dict, astng.Tuple, 
105
105
                                astng.Module, astng.Function)):
106
106
            self.add_message('E0702', node=node, args=expr.name)
107
 
        elif isinstance(expr, astng.Mod):
 
107
        elif isinstance(expr, astng.BinOp) and expr.op == '%':
108
108
            self.add_message('W0701', node=node)
109
 
        elif isinstance(expr, (astng.Instance, astng.Class)):
110
 
            if isinstance(expr, astng.Instance):
 
109
        elif isinstance(expr, (Instance, astng.Class)):
 
110
            if isinstance(expr, Instance):
111
111
                expr = expr._proxied
112
112
            if (isinstance(expr, astng.Class) and
113
113
                    not inherit_from_std_ex(expr) and
124
124
 
125
125
 
126
126
    def visit_tryexcept(self, node):
127
 
        """check for empty except
128
 
        """
 
127
        """check for empty except"""
129
128
        exceptions_classes = []
130
129
        nb_handlers = len(node.handlers)
131
130
        for index, handler  in enumerate(node.handlers):
132
 
            exc_type = handler[0]
133
 
            stmt = handler[2]
134
131
            # single except doing nothing but "pass" without else clause
135
 
            if nb_handlers == 1 and is_empty(stmt) and not node.else_:
136
 
                self.add_message('W0704', node=exc_type or stmt)
137
 
            if exc_type is None:
138
 
                if nb_handlers == 1 and not is_raising(stmt):
139
 
                    self.add_message('W0702', node=stmt.nodes[0])
 
132
            if nb_handlers == 1 and is_empty(handler.body) and not node.orelse:
 
133
                self.add_message('W0704', node=handler.type or handler.body[0])
 
134
            if handler.type is None:
 
135
                if nb_handlers == 1 and not is_raising(handler.body):
 
136
                    self.add_message('W0702', node=handler.body[0])
140
137
                # check if a "except:" is followed by some other
141
138
                # except
142
139
                elif index < (nb_handlers - 1):
144
141
                    self.add_message('E0701', node=node, args=msg)
145
142
            else:
146
143
                try:
147
 
                    excs = list(unpack_infer(exc_type))
 
144
                    excs = list(unpack_infer(handler.type))
148
145
                except astng.InferenceError:
149
146
                    continue
150
147
                for exc in excs:
151
148
                    # XXX skip other non class nodes 
152
 
                    if exc is astng.YES or not isinstance(exc, astng.Class):
 
149
                    if exc is YES or not isinstance(exc, astng.Class):
153
150
                        continue
154
151
                    exc_ancestors = [anc for anc in exc.ancestors()
155
152
                                     if isinstance(anc, astng.Class)]
157
154
                        if previous_exc in exc_ancestors:
158
155
                            msg = '%s is an ancestor class of %s' % (
159
156
                                previous_exc.name, exc.name)
160
 
                            self.add_message('E0701', node=exc_type, args=msg)
 
157
                            self.add_message('E0701', node=handler.type, args=msg)
161
158
                    if (exc.name == 'Exception'
162
159
                        and exc.root().name == 'exceptions'
163
 
                        and nb_handlers == 1 and not is_raising(stmt)):
164
 
                        self.add_message('W0703', node=exc_type)
 
160
                        and nb_handlers == 1 and not is_raising(handler.body)):
 
161
                        self.add_message('W0703', node=handler.type)
165
162
                exceptions_classes += excs
166
163
 
167
164
def inherit_from_std_ex(node):