Ejemplo n.º 1
0
def infer_global(self, context=None):
    if context.lookupname is None:
        raise InferenceError()
    try:
        return _infer_stmts(self.root().getattr(context.lookupname), context)
    except NotFoundError:
        raise InferenceError()
Ejemplo n.º 2
0
def infer_from(self, context=None, asname=True):
    """infer a From nodes: return the imported module/object"""
    name = context.lookupname
    if name is None:
        raise InferenceError()
    if asname:
        name = self.real_name(name)
    module = self.do_import_module(self.modname)
    try:
        context = copy_context(context)
        context.lookupname = name
        return _infer_stmts(module.getattr(name), context)
    except NotFoundError:
        raise InferenceError(name)
Ejemplo n.º 3
0
 def do_import_module(self, modname):
     """return the ast for a module whose name is <modname> imported by <self>
     """
     # handle special case where we are on a package node importing a module
     # using the same name as the package, which may end in an infinite loop
     # on relative imports
     # XXX: no more needed ?
     mymodule = self.root()
     level = getattr(self, 'level', None)  # Import as no level
     if mymodule.absolute_modname(modname, level) == mymodule.name:
         # FIXME: I don't know what to do here...
         raise InferenceError('module importing itself: %s' % modname)
     try:
         return mymodule.import_module(modname, level=level)
     except (ASTNGBuildingException, SyntaxError):
         raise InferenceError(modname)
Ejemplo n.º 4
0
 def igetattr(self, name, context=None):
     """inferred getattr, need special treatment in class to handle
     descriptors
     """
     # set lookup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         for infered in _infer_stmts(self.getattr(name, context),
                                     context,
                                     frame=self):
             # yield YES object instead of descriptors when necessary
             if not isinstance(infered, Const) and isinstance(
                     infered, Instance):
                 try:
                     infered._proxied.getattr('__get__', context)
                 except NotFoundError:
                     yield infered
                 else:
                     yield YES
             else:
                 yield function_to_method(infered, self)
     except NotFoundError:
         if not name.startswith('__') and self.has_dynamic_getattr(context):
             # class handle some dynamic attributes, return a YES object
             yield YES
         else:
             raise InferenceError(name)
Ejemplo n.º 5
0
def infer_import(self, context=None, asname=True):
    """infer an Import node: return the imported module/object"""
    name = context.lookupname
    if name is None:
        raise InferenceError()
    if asname:
        yield self.do_import_module(self.real_name(name))
    else:
        yield self.do_import_module(name)
Ejemplo n.º 6
0
def infer_subscript(self, context=None):
    """infer simple subscription such as [1,2,3][0] or (1,2,3)[-1]"""
    if isinstance(self.slice, nodes.Index):
        index = self.slice.value.infer(context).next()
        if index is YES:
            yield YES
            return
        try:
            # suppose it's a Tuple/List node (attribute error else)
            assigned = self.value.getitem(index.value, context)
        except AttributeError:
            raise InferenceError()
        except (IndexError, TypeError):
            yield YES
            return
        for infered in assigned.infer(context):
            yield infered
    else:
        raise InferenceError()
Ejemplo n.º 7
0
 def igetattr(self, name, context=None):
     """inferred getattr"""
     # set lookup name since this is necessary to infer on import nodes for
     # instance
     context = copy_context(context)
     context.lookupname = name
     try:
         return _infer_stmts(self.getattr(name, context), context, frame=self)
     except NotFoundError:
         raise InferenceError(name)
Ejemplo n.º 8
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()
     for iface in unpack_infer(implements):
         if iface is YES:
             continue
         if not iface in found and handler_func(iface):
             found.add(iface)
             yield iface
     if not found:
         raise InferenceError()
Ejemplo n.º 9
0
def infer_arguments(self, context=None):
    name = context.lookupname
    if name is None:
        raise InferenceError()
    return _arguments_infer_argname(self, name, context)
Ejemplo n.º 10
0
 def infer_argument(self, funcnode, name, context):
     """infer a function argument value according to the call context"""
     # 1. search in named keywords
     try:
         return self.nargs[name].infer(context)
     except KeyError:
         # Function.args.args can be None in astng (means that we don't have
         # information on argnames)
         argindex = funcnode.args.find_argname(name)[0]
         if argindex is not None:
             # 2. first argument of instance/class method
             if argindex == 0 and funcnode.type in ('method',
                                                    'classmethod'):
                 if context.boundnode is not None:
                     boundnode = context.boundnode
                 else:
                     # XXX can do better ?
                     boundnode = funcnode.parent.frame()
                 if funcnode.type == 'method':
                     if not isinstance(boundnode, Instance):
                         boundnode = Instance(boundnode)
                     return iter((boundnode, ))
                 if funcnode.type == 'classmethod':
                     return iter((boundnode, ))
             # 2. search arg index
             try:
                 return self.args[argindex].infer(context)
             except IndexError:
                 pass
             # 3. search in *args (.starargs)
             if self.starargs is not None:
                 its = []
                 for infered in self.starargs.infer(context):
                     if infered is YES:
                         its.append((YES, ))
                         continue
                     try:
                         its.append(
                             infered.getitem(argindex,
                                             context).infer(context))
                     except (InferenceError, AttributeError):
                         its.append((YES, ))
                     except (IndexError, TypeError):
                         continue
                 if its:
                     return chain(*its)
     # 4. XXX search in **kwargs (.dstarargs)
     if self.dstarargs is not None:
         its = []
         for infered in self.dstarargs.infer(context):
             if infered is YES:
                 its.append((YES, ))
                 continue
             try:
                 its.append(infered.getitem(name, context).infer(context))
             except (InferenceError, AttributeError):
                 its.append((YES, ))
             except (IndexError, TypeError):
                 continue
         if its:
             return chain(*its)
     # 5. */** argument, (Tuple or Dict)
     if name == funcnode.args.vararg:
         return iter((nodes.const_factory(())))
     if name == funcnode.args.kwarg:
         return iter((nodes.const_factory({})))
     # 6. return default value if any
     try:
         return funcnode.args.default_value(name).infer(context)
     except NoDefault:
         raise InferenceError(name)