Index: third_party/logilab/astng/inference.py |
diff --git a/third_party/logilab/astroid/inference.py b/third_party/logilab/astng/inference.py |
similarity index 72% |
rename from third_party/logilab/astroid/inference.py |
rename to third_party/logilab/astng/inference.py |
index 4186307a0e8dff0fe53d3fe14ffe1e22758f4bab..62bd7d962a076bc62899e1d247c858c335973f5c 100644 |
--- a/third_party/logilab/astroid/inference.py |
+++ b/third_party/logilab/astng/inference.py |
@@ -1,43 +1,43 @@ |
-# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
+# copyright 2003-2010 Sylvain Thenault, all rights reserved. |
+# contact mailto:thenault@gmail.com |
# |
-# This file is part of astroid. |
+# This file is part of logilab-astng. |
# |
-# astroid is free software: you can redistribute it and/or modify it |
+# logilab-astng is free software: you can redistribute it and/or modify it |
# under the terms of the GNU Lesser General Public License as published by the |
# Free Software Foundation, either version 2.1 of the License, or (at your |
# option) any later version. |
# |
-# astroid is distributed in the hope that it will be useful, but |
+# logilab-astng is distributed in the hope that it will be useful, but |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
# for more details. |
# |
# You should have received a copy of the GNU Lesser General Public License along |
-# with astroid. If not, see <http://www.gnu.org/licenses/>. |
-"""this module contains a set of functions to handle inference on astroid trees |
+# with logilab-astng. If not, see <http://www.gnu.org/licenses/>. |
+"""this module contains a set of functions to handle inference on astng trees |
""" |
__doctype__ = "restructuredtext en" |
from itertools import chain |
+import sys |
-from astroid import nodes |
+from logilab.astng import nodes |
-from astroid.manager import AstroidManager |
-from astroid.exceptions import (AstroidError, InferenceError, NoDefault, |
- NotFoundError, UnresolvableName) |
-from astroid.bases import (YES, Instance, InferenceContext, |
- _infer_stmts, copy_context, path_wrapper, |
- raise_if_nothing_infered) |
-from astroid.protocols import ( |
- _arguments_infer_argname, |
- BIN_OP_METHOD, UNARY_OP_METHOD) |
+from logilab.astng.manager import ASTNGManager |
+from logilab.astng.exceptions import (ASTNGBuildingException, ASTNGError, |
+ InferenceError, NoDefault, NotFoundError, UnresolvableName) |
+from logilab.astng.bases import YES, Instance, InferenceContext, Generator, \ |
+ _infer_stmts, copy_context, path_wrapper, raise_if_nothing_infered |
+from logilab.astng.protocols import _arguments_infer_argname |
-MANAGER = AstroidManager() |
+MANAGER = ASTNGManager() |
-class CallContext(object): |
+class CallContext: |
"""when inferring a function call, this class is used to remember values |
given as argument |
""" |
@@ -58,7 +58,7 @@ class CallContext(object): |
try: |
return self.nargs[name].infer(context) |
except KeyError: |
- # Function.args.args can be None in astroid (means that we don't have |
+ # 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: |
@@ -75,11 +75,6 @@ class CallContext(object): |
return iter((boundnode,)) |
if funcnode.type == 'classmethod': |
return iter((boundnode,)) |
- # if we have a method, extract one position |
- # from the index, so we'll take in account |
- # the extra parameter represented by `self` or `cls` |
- if funcnode.type in ('method', 'classmethod'): |
- argindex -= 1 |
# 2. search arg index |
try: |
return self.args[argindex].infer(context) |
@@ -134,51 +129,25 @@ def infer_end(self, context=None): |
"""inference's end for node such as Module, Class, Function, Const... |
""" |
yield self |
-nodes.Module._infer = infer_end |
-nodes.Class._infer = infer_end |
-nodes.Function._infer = infer_end |
-nodes.Lambda._infer = infer_end |
-nodes.Const._infer = infer_end |
-nodes.List._infer = infer_end |
-nodes.Tuple._infer = infer_end |
-nodes.Dict._infer = infer_end |
-nodes.Set._infer = infer_end |
- |
-def _higher_function_scope(node): |
- """ Search for the first function which encloses the given |
- scope. This can be used for looking up in that function's |
- scope, in case looking up in a lower scope for a particular |
- name fails. |
- |
- :param node: A scope node. |
- :returns: |
- ``None``, if no parent function scope was found, |
- otherwise an instance of :class:`astroid.scoped_nodes.Function`, |
- which encloses the given node. |
- """ |
- current = node |
- while current.parent and not isinstance(current.parent, nodes.Function): |
- current = current.parent |
- if current and current.parent: |
- return current.parent |
+nodes.Module.infer = infer_end |
+nodes.Class.infer = infer_end |
+nodes.Function.infer = infer_end |
+nodes.Lambda.infer = infer_end |
+nodes.Const.infer = infer_end |
+nodes.List.infer = infer_end |
+nodes.Tuple.infer = infer_end |
+nodes.Dict.infer = infer_end |
+ |
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) |
+ raise UnresolvableName(self.name) |
context = context.clone() |
context.lookupname = self.name |
return _infer_stmts(stmts, context, frame) |
-nodes.Name._infer = path_wrapper(infer_name) |
+nodes.Name.infer = path_wrapper(infer_name) |
nodes.AssName.infer_lhs = infer_name # won't work with a path wrapper |
@@ -198,7 +167,7 @@ def infer_callfunc(self, context=None): |
except InferenceError: |
## XXX log error ? |
continue |
-nodes.CallFunc._infer = path_wrapper(raise_if_nothing_infered(infer_callfunc)) |
+nodes.CallFunc.infer = path_wrapper(raise_if_nothing_infered(infer_callfunc)) |
def infer_import(self, context=None, asname=True): |
@@ -210,7 +179,7 @@ def infer_import(self, context=None, asname=True): |
yield self.do_import_module(self.real_name(name)) |
else: |
yield self.do_import_module(name) |
-nodes.Import._infer = path_wrapper(infer_import) |
+nodes.Import.infer = path_wrapper(infer_import) |
def infer_name_module(self, name): |
context = InferenceContext() |
@@ -226,14 +195,14 @@ def infer_from(self, context=None, asname=True): |
raise InferenceError() |
if asname: |
name = self.real_name(name) |
- module = self.do_import_module() |
+ module = self.do_import_module(self.modname) |
try: |
context = copy_context(context) |
context.lookupname = name |
return _infer_stmts(module.getattr(name, ignore_locals=module is self.root()), context) |
except NotFoundError: |
raise InferenceError(name) |
-nodes.From._infer = path_wrapper(infer_from) |
+nodes.From.infer = path_wrapper(infer_from) |
def infer_getattr(self, context=None): |
@@ -253,7 +222,7 @@ def infer_getattr(self, context=None): |
except AttributeError: |
# XXX method / function |
context.boundnode = None |
-nodes.Getattr._infer = path_wrapper(raise_if_nothing_infered(infer_getattr)) |
+nodes.Getattr.infer = path_wrapper(raise_if_nothing_infered(infer_getattr)) |
nodes.AssAttr.infer_lhs = raise_if_nothing_infered(infer_getattr) # # won't work with a path wrapper |
@@ -264,24 +233,19 @@ def infer_global(self, context=None): |
return _infer_stmts(self.root().getattr(context.lookupname), context) |
except NotFoundError: |
raise InferenceError() |
-nodes.Global._infer = path_wrapper(infer_global) |
+nodes.Global.infer = path_wrapper(infer_global) |
def infer_subscript(self, context=None): |
"""infer simple subscription such as [1,2,3][0] or (1,2,3)[-1]""" |
- value = self.value.infer(context).next() |
- if value is YES: |
- yield YES |
- return |
- |
- index = self.slice.infer(context).next() |
- if index is YES: |
- yield YES |
- return |
- |
- if isinstance(index, nodes.Const): |
+ if isinstance(self.slice, nodes.Index): |
+ index = self.slice.value.infer(context).next() |
+ if index is YES: |
+ yield YES |
+ return |
try: |
- assigned = value.getitem(index.value, context) |
+ # suppose it's a Tuple/List node (attribute error else) |
+ assigned = self.value.getitem(index.value, context) |
except AttributeError: |
raise InferenceError() |
except (IndexError, TypeError): |
@@ -291,9 +255,16 @@ def infer_subscript(self, context=None): |
yield infered |
else: |
raise InferenceError() |
-nodes.Subscript._infer = path_wrapper(infer_subscript) |
+nodes.Subscript.infer = path_wrapper(infer_subscript) |
nodes.Subscript.infer_lhs = raise_if_nothing_infered(infer_subscript) |
+ |
+UNARY_OP_METHOD = {'+': '__pos__', |
+ '-': '__neg__', |
+ '~': '__invert__', |
+ 'not': None, # XXX not '__nonzero__' |
+ } |
+ |
def infer_unaryop(self, context=None): |
for operand in self.operand.infer(context): |
try: |
@@ -314,7 +285,22 @@ def infer_unaryop(self, context=None): |
raise |
except: |
yield YES |
-nodes.UnaryOp._infer = path_wrapper(infer_unaryop) |
+nodes.UnaryOp.infer = path_wrapper(infer_unaryop) |
+ |
+ |
+BIN_OP_METHOD = {'+': '__add__', |
+ '-': '__sub__', |
+ '/': '__div__', |
+ '//': '__floordiv__', |
+ '*': '__mul__', |
+ '**': '__power__', |
+ '%': '__mod__', |
+ '&': '__and__', |
+ '|': '__or__', |
+ '^': '__xor__', |
+ '<<': '__lshift__', |
+ '>>': '__rshift__', |
+ } |
def _infer_binop(operator, operand1, operand2, context, failures=None): |
if operand1 is YES: |
@@ -344,7 +330,7 @@ def infer_binop(self, context=None): |
for rhs in self.right.infer(context): |
for val in _infer_binop(self.op, rhs, lhs, context): |
yield val |
-nodes.BinOp._infer = path_wrapper(infer_binop) |
+nodes.BinOp.infer = path_wrapper(infer_binop) |
def infer_arguments(self, context=None): |
@@ -352,7 +338,7 @@ def infer_arguments(self, context=None): |
if name is None: |
raise InferenceError() |
return _arguments_infer_argname(self, name, context) |
-nodes.Arguments._infer = infer_arguments |
+nodes.Arguments.infer = infer_arguments |
def infer_ass(self, context=None): |
@@ -364,8 +350,8 @@ def infer_ass(self, context=None): |
return stmt.infer(context) |
stmts = list(self.assigned_stmts(context=context)) |
return _infer_stmts(stmts, context) |
-nodes.AssName._infer = path_wrapper(infer_ass) |
-nodes.AssAttr._infer = path_wrapper(infer_ass) |
+nodes.AssName.infer = path_wrapper(infer_ass) |
+nodes.AssAttr.infer = path_wrapper(infer_ass) |
def infer_augassign(self, context=None): |
failures = [] |
@@ -376,7 +362,7 @@ def infer_augassign(self, context=None): |
for rhs in self.value.infer(context): |
for val in _infer_binop(self.op, rhs, lhs, context): |
yield val |
-nodes.AugAssign._infer = path_wrapper(infer_augassign) |
+nodes.AugAssign.infer = path_wrapper(infer_augassign) |
# no infer method on DelName and DelAttr (expected InferenceError) |
@@ -387,14 +373,10 @@ def infer_empty_node(self, context=None): |
yield YES |
else: |
try: |
- for infered in MANAGER.infer_ast_from_something(self.object, |
- context=context): |
+ for infered in MANAGER.infer_astng_from_something(self.object, |
+ context=context): |
yield infered |
- except AstroidError: |
+ except ASTNGError: |
yield YES |
-nodes.EmptyNode._infer = path_wrapper(infer_empty_node) |
- |
+nodes.EmptyNode.infer = path_wrapper(infer_empty_node) |
-def infer_index(self, context=None): |
- return self.value.infer(context) |
-nodes.Index._infer = infer_index |