Exemplo n.º 1
0
 def test_unpack_infer_empty_tuple(self):
     node = test_utils.extract_node('''
     ()
     ''')
     inferred = next(node.infer())
     with self.assertRaises(InferenceError) as cm:
         unpacked = list(node_classes.unpack_infer(inferred))
Exemplo n.º 2
0
def excepthandler_assigned_stmts(self, node=None, context=None, assign_path=None):
    for assigned in node_classes.unpack_infer(self.type):
        if isinstance(assigned, nodes.ClassDef):
            assigned = objects.ExceptionInstance(assigned)

        yield assigned
    return dict(node=self, unknown=node, assign_path=assign_path, context=context)
Exemplo n.º 3
0
    def test_bad_import_inference(self):
        # Explication of bug
        '''When we import PickleError from nonexistent, a call to the infer
        method of this From node will be made by unpack_infer.
        inference.infer_from will try to import this module, which will fail and
        raise a InferenceException (by mixins.do_import_module). The infer_name
        will catch this exception and yield and YES instead.
        '''

        code = '''try:
    from pickle import PickleError
except ImportError:
    from nonexistent import PickleError

try:
    pass
except PickleError:
    pass
        '''

        astroid = abuilder.string_build(code)
        from_node = astroid.body[1].handlers[0].body[0]
        handler_type = astroid.body[1].handlers[0].type

        excs = list(unpack_infer(handler_type))
Exemplo n.º 4
0
 def test_unpack_infer_empty_tuple(self):
     node = builder.extract_node('''
     ()
     ''')
     inferred = next(node.infer())
     with self.assertRaises(InferenceError) as cm:
         unpacked = list(node_classes.unpack_infer(inferred))
Exemplo n.º 5
0
    def test_bad_import_inference(self):
        # Explication of bug
        """When we import PickleError from nonexistent, a call to the infer
        method of this From node will be made by unpack_infer.
        inference.infer_from will try to import this module, which will fail and
        raise a InferenceException (by mixins.do_import_module). The infer_name
        will catch this exception and yield and Uninferable instead.
        """

        code = """
            try:
                from pickle import PickleError
            except ImportError:
                from nonexistent import PickleError

            try:
                pass
            except PickleError:
                pass
        """
        module = builder.parse(code)
        handler_type = module.body[1].handlers[0].type

        excs = list(node_classes.unpack_infer(handler_type))
        # The number of returned object can differ on Python 2
        # and Python 3. In one version, an additional item will
        # be returned, from the _pickle module, which is not
        # present in the other version.
        self.assertIsInstance(excs[0], nodes.ClassDef)
        self.assertEqual(excs[0].name, "PickleError")
        self.assertIs(excs[-1], util.Uninferable)
Exemplo n.º 6
0
    def test_bad_import_inference(self):
        # Explication of bug
        '''When we import PickleError from nonexistent, a call to the infer
        method of this From node will be made by unpack_infer.
        inference.infer_from will try to import this module, which will fail and
        raise a InferenceException (by mixins.do_import_module). The infer_name
        will catch this exception and yield and YES instead.
        '''

        code = '''try:
    from pickle import PickleError
except ImportError:
    from nonexistent import PickleError

try:
    pass
except PickleError:
    pass
        '''

        astroid = abuilder.string_build(code)
        from_node = astroid.body[1].handlers[0].body[0]
        handler_type = astroid.body[1].handlers[0].type

        excs = list(unpack_infer(handler_type))
Exemplo n.º 7
0
    def test_bad_import_inference(self):
        # Explication of bug
        '''When we import PickleError from nonexistent, a call to the infer
        method of this From node will be made by unpack_infer.
        inference.infer_from will try to import this module, which will fail and
        raise a InferenceException (by mixins.do_import_module). The infer_name
        will catch this exception and yield and Uninferable instead.
        '''

        code = '''
            try:
                from pickle import PickleError
            except ImportError:
                from nonexistent import PickleError

            try:
                pass
            except PickleError:
                pass
        '''
        module = builder.parse(code)
        handler_type = module.body[1].handlers[0].type

        excs = list(node_classes.unpack_infer(handler_type))
        # The number of returned object can differ on Python 2
        # and Python 3. In one version, an additional item will
        # be returned, from the _pickle module, which is not
        # present in the other version.
        self.assertIsInstance(excs[0], nodes.ClassDef)
        self.assertEqual(excs[0].name, 'PickleError')
        self.assertIs(excs[-1], util.Uninferable)
Exemplo n.º 8
0
    def test_bad_import_inference(self):
        # Explication of bug
        '''When we import PickleError from nonexistent, a call to the infer
        method of this From node will be made by unpack_infer.
        inference.infer_from will try to import this module, which will fail and
        raise a InferenceException (by mixins.do_import_module). The infer_name
        will catch this exception and yield and YES instead.
        '''

        code = '''
            try:
                from pickle import PickleError
            except ImportError:
                from nonexistent import PickleError

            try:
                pass
            except PickleError:
                pass
        '''
        astroid = test_utils.build_module(code)
        handler_type = astroid.body[1].handlers[0].type

        excs = list(unpack_infer(handler_type))
        # The number of returned object can differ on Python 2
        # and Python 3. In one version, an additional item will
        # be returned, from the _pickle module, which is not
        # present in the other version.
        self.assertIsInstance(excs[0], nodes.Class)
        self.assertEqual(excs[0].name, 'PickleError')
        self.assertIs(excs[-1], bases.YES)
Exemplo n.º 9
0
 def test_unpack_infer_empty_tuple(self):
     node = builder.extract_node("""
     ()
     """)
     inferred = next(node.infer())
     with self.assertRaises(InferenceError):
         list(node_classes.unpack_infer(inferred))
Exemplo n.º 10
0
def excepthandler_assigned_stmts(self, node=None, context=None, assign_path=None):
    for assigned in node_classes.unpack_infer(self.type):
        if isinstance(assigned, nodes.ClassDef):
            assigned = objects.ExceptionInstance(assigned)

        yield assigned
    return dict(node=self, unknown=node, assign_path=assign_path, context=context)
Exemplo n.º 11
0
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None):
    for assigned in node_classes.unpack_infer(self.type):
        if isinstance(assigned, nodes.ClassDef):
            assigned = objects.ExceptionInstance(assigned)

        yield assigned
    # Explicit StopIteration to return error information, see comment
    # in raise_if_nothing_inferred.
    return dict(node=self, unknown=node, assign_path=asspath, context=context)
Exemplo n.º 12
0
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None):
    for assigned in node_classes.unpack_infer(self.type):
        if isinstance(assigned, nodes.ClassDef):
            assigned = bases.Instance(assigned)
        yield assigned
    # Explicit StopIteration to return error information, see comment
    # in raise_if_nothing_inferred.
    raise StopIteration(dict(node=self, unknown=node,
                             assign_path=asspath, context=context))
Exemplo n.º 13
0
 def test_unpack_infer_uninferable_nodes(self):
     node = test_utils.extract_node('''
     x = [A] * 1
     f = [x, [A] * 2]
     f
     ''')
     inferred = next(node.infer())
     unpacked = list(node_classes.unpack_infer(inferred))
     self.assertEqual(len(unpacked), 3)
     self.assertTrue(all(elt is astroid_util.YES for elt in unpacked))
Exemplo n.º 14
0
 def test_unpack_infer_uninferable_nodes(self):
     node = builder.extract_node('''
     x = [A] * 1
     f = [x, [A] * 2]
     f
     ''')
     inferred = next(node.infer())
     unpacked = list(node_classes.unpack_infer(inferred))
     self.assertEqual(len(unpacked), 3)
     self.assertTrue(all(elt is astroid_util.Uninferable
                         for elt in unpacked))
Exemplo n.º 15
0
 def test_unpack_infer_uninferable_nodes(self):
     node = builder.extract_node("""
     x = [A] * 1
     f = [x, [A] * 2]
     f
     """)
     inferred = next(node.infer())
     unpacked = list(node_classes.unpack_infer(inferred))
     self.assertEqual(len(unpacked), 3)
     self.assertTrue(
         all(elt is astroid_util.Uninferable for elt in unpacked))
def interfaces(node, herited=True, handler_func=_iface_hdlr):
    """Return an iterator on interfaces implemented by the given class node."""
    try:
        implements = bases.Instance(node).getattr("__implements__")[0]
    except exceptions.NotFoundError:
        return
    if not herited and implements.frame() is not node:
        return
    found = set()
    missing = False
    for iface in node_classes.unpack_infer(implements):
        if iface is astroid.Uninferable:
            missing = True
            continue
        if iface not in found and handler_func(iface):
            found.add(iface)
            yield iface
    if missing:
        raise exceptions.InferenceError()
Exemplo n.º 17
0
def interfaces(node, herited=True, handler_func=_iface_hdlr):
    """Return an iterator on interfaces implemented by the given class node."""
    # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
    try:
        implements = bases.Instance(node).getattr('__implements__')[0]
    except exceptions.NotFoundError:
        return
    if not herited and implements.frame() is not node:
        return
    found = set()
    missing = False
    for iface in node_classes.unpack_infer(implements):
        if iface is astroid.YES:
            missing = True
            continue
        if iface not in found and handler_func(iface):
            found.add(iface)
            yield iface
    if missing:
        raise exceptions.InferenceError()
Exemplo n.º 18
0
 def interfaces(self, herited=True, handler_func=_iface_hdlr):
     """return an iterator on interfaces implemented by the given
     class node
     """
     # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
     try:
         implements = Instance(self).getattr('__implements__')[0]
     except NotFoundError:
         return
     if not herited and not implements.frame() is self:
         return
     found = set()
     missing = False
     for iface in unpack_infer(implements):
         if iface is YES:
             missing = True
             continue
         if not iface in found and handler_func(iface):
             found.add(iface)
             yield iface
     if missing:
         raise InferenceError()
Exemplo n.º 19
0
 def interfaces(self, herited=True, handler_func=_iface_hdlr):
     """return an iterator on interfaces implemented by the given
     class node
     """
     # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
     try:
         implements = Instance(self).getattr("__implements__")[0]
     except NotFoundError:
         return
     if not herited and not implements.frame() is self:
         return
     found = set()
     missing = False
     for iface in unpack_infer(implements):
         if iface is YES:
             missing = True
             continue
         if not iface in found and handler_func(iface):
             found.add(iface)
             yield iface
     if missing:
         raise InferenceError()
Exemplo n.º 20
0
def excepthandler_assigned_stmts(self, node, context=None, asspath=None):
    for assigned in unpack_infer(self.type):
        if isinstance(assigned, nodes.Class):
            assigned = Instance(assigned)
        yield assigned
Exemplo n.º 21
0
def excepthandler_assigned_stmts(self, node=None, context=None, asspath=None):
    for assigned in node_classes.unpack_infer(self.type):
        if isinstance(assigned, nodes.ClassDef):
            assigned = bases.Instance(assigned)
        yield assigned