Exemplo n.º 1
0
    def test_path_wrapper(self):
        def infer_default(self, *args):
            raise InferenceError

        infer_default = path_wrapper(infer_default)
        infer_end = path_wrapper(inference_infer_end)
        self.assertRaises(InferenceError, infer_default(1).next)
        self.assertEqual(infer_end(1).next(), 1)
Exemplo n.º 2
0
 def test_path_wrapper(self):
     def infer_default(self, *args):
         raise InferenceError
     infer_default = path_wrapper(infer_default)
     infer_end = path_wrapper(inference_infer_end)
     self.assertRaises(InferenceError,
                           infer_default(1).next)
     self.assertEqual(infer_end(1).next(), 1)
Exemplo n.º 3
0
    """infer a Name: use name lookup rules"""
    frame, stmts = self.lookup(self.name)
    if not stmts:
        # Try to see if the name is enclosed in a nested function
        # and use the higher (first function) scope for searching.
        # TODO: should this be promoted to other nodes as well?
        parent_function = _higher_function_scope(self.scope())
        if parent_function:
            _, stmts = parent_function.lookup(self.name)

        if not stmts:
            raise exceptions.UnresolvableName(self.name)
    context = context.clone()
    context.lookupname = self.name
    return bases._infer_stmts(stmts, context, frame)
nodes.Name._infer = bases.path_wrapper(infer_name)
nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper


@bases.path_wrapper
@bases.raise_if_nothing_inferred
def infer_call(self, context=None):
    """infer a Call node by trying to guess what the function returns"""
    callcontext = context.clone()
    callcontext.callcontext = contextmod.CallContext(args=self.args,
                                                     keywords=self.keywords)
    callcontext.boundnode = None
    for callee in self.func.infer(context):
        if callee is util.YES:
            yield callee
            continue
Exemplo n.º 4
0
def infer_name(self, context=None):
    """infer a Name: use name lookup rules"""
    frame, stmts = self.lookup(self.name)
    if not stmts:
        # Try to see if the name is enclosed in a nested function
        # and use the higher (first function) scope for searching.
        # TODO: should this be promoted to other nodes as well?
        parent_function = _higher_function_scope(self.scope())
        if parent_function:
            _, stmts = parent_function.lookup(self.name)

        if not stmts:
            raise UnresolvableName(self.name)
    return _infer_stmts(stmts, context, frame, self.name)
nodes.Name._infer = path_wrapper(infer_name)
nodes.AssName.infer_lhs = infer_name # won't work with a path wrapper


def infer_callfunc(self, context=None):
    """infer a CallFunc node by trying to guess what the function returns"""
    if context is None:
        context = InferenceContext()
    for callee in self.func.infer(context):
        with context.scope(
            callcontext=CallContext(self.args, self.starargs, self.kwargs),
            boundnode=None,
        ):
            if callee is YES:
                yield callee
                continue