Beispiel #1
0
 def visit_raise(self, node):
     """visit raise possibly inferring value"""
     # ignore empty raise
     if node.exc is None:
         return
     expr = node.exc
     if self._check_raise_value(node, expr):
         return
     else:
         try:
             value = unpack_infer(expr).next()
         except astng.InferenceError:
             return
         self._check_raise_value(node, value)
Beispiel #2
0
 def visit_tryexcept(self, node):
     """check for empty except"""
     exceptions_classes = []
     nb_handlers = len(node.handlers)
     for index, handler in enumerate(node.handlers):
         # single except doing nothing but "pass" without else clause
         if nb_handlers == 1 and is_empty(handler.body) and not node.orelse:
             self.add_message('W0704', node=handler.type or handler.body[0])
         if handler.type is None:
             if nb_handlers == 1 and not is_raising(handler.body):
                 self.add_message('W0702', node=handler)
             # check if a "except:" is followed by some other
             # except
             elif index < (nb_handlers - 1):
                 msg = 'empty except clause should always appear last'
                 self.add_message('E0701', node=node, args=msg)
         else:
             try:
                 excs = list(unpack_infer(handler.type))
             except astng.InferenceError:
                 continue
             for exc in excs:
                 # XXX skip other non class nodes
                 if exc is YES or not isinstance(exc, astng.Class):
                     continue
                 exc_ancestors = [
                     anc for anc in exc.ancestors()
                     if isinstance(anc, astng.Class)
                 ]
                 for previous_exc in exceptions_classes:
                     if previous_exc in exc_ancestors:
                         msg = '%s is an ancestor class of %s' % (
                             previous_exc.name, exc.name)
                         self.add_message('E0701',
                                          node=handler.type,
                                          args=msg)
                 if (exc.name in self.config.overgeneral_exceptions
                         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)
             exceptions_classes += excs
Beispiel #3
0
    def visit_tryexcept(self, node):
        """check for empty except"""
        exceptions_classes = []
        nb_handlers = len(node.handlers)
        for index, handler  in enumerate(node.handlers):
            # single except doing nothing but "pass" without else clause
            if nb_handlers == 1 and is_empty(handler.body) and not node.orelse:
                self.add_message('W0704', node=handler.type or handler.body[0])
            if handler.type is None:
                if nb_handlers == 1 and not is_raising(handler.body):
                    self.add_message('W0702', node=handler)
                # check if a "except:" is followed by some other
                # except
                elif index < (nb_handlers - 1):
                    msg = 'empty except clause should always appear last'
                    self.add_message('E0701', node=node, args=msg)

            elif isinstance(handler.type, astng.BoolOp):
                self.add_message('W0711', node=handler, args=handler.type.op)
            else:
                try:
                    excs = list(unpack_infer(handler.type))
                except astng.InferenceError:
                    continue
                for exc in excs:
                    # XXX skip other non class nodes 
                    if exc is YES or not isinstance(exc, astng.Class):
                        continue
                    exc_ancestors = [anc for anc in exc.ancestors()
                                     if isinstance(anc, astng.Class)]
                    for previous_exc in exceptions_classes:
                        if previous_exc in exc_ancestors:
                            msg = '%s is an ancestor class of %s' % (
                                previous_exc.name, exc.name)
                            self.add_message('E0701', node=handler.type, args=msg)
                    if (exc.name in self.config.overgeneral_exceptions
                        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)
                exceptions_classes += excs