summaryrefslogtreecommitdiff
path: root/infutils.py
diff options
context:
space:
mode:
authorSylvain Thénault <sylvain.thenault@logilab.fr>2009-03-17 18:10:56 +0100
committerSylvain Thénault <sylvain.thenault@logilab.fr>2009-03-17 18:10:56 +0100
commitbb8f7f0f6a7bf8440b16f5c17724a5b197b5e205 (patch)
tree553072cb6633f387e6f94984ee89e8b9f0e88006 /infutils.py
parentcd39af0ef8fb8c1d4f9c9c3cfe2cdf86760ef09a (diff)
downloadastroid-git-bb8f7f0f6a7bf8440b16f5c17724a5b197b5e205.tar.gz
pylint fixes
--HG-- branch : _ast_compat
Diffstat (limited to 'infutils.py')
-rw-r--r--infutils.py50
1 files changed, 49 insertions, 1 deletions
diff --git a/infutils.py b/infutils.py
index 365df00c..27d67061 100644
--- a/infutils.py
+++ b/infutils.py
@@ -25,7 +25,8 @@ __doctype__ = "restructuredtext en"
from logilab.common.compat import chain, imap
from logilab.astng._exceptions import InferenceError, NotFoundError, UnresolvableName
-from logilab.astng.nodes import Proxy_, List, Tuple, Function
+from logilab.astng.nodes import Proxy_, List, Tuple, Function, If, TryExcept
+
class Proxy(Proxy_):
"""a simple proxy object"""
@@ -76,6 +77,53 @@ class InferenceContext(object):
return clone
+def are_exclusive(stmt1, stmt2):
+ """return true if the two given statements are mutually exclusive
+
+ algorithm :
+ 1) index stmt1's parents
+ 2) climb among stmt2's parents until we find a common parent
+ 3) if the common parent is a If or TryExcept statement, look if nodes are
+ in exclusive branchs
+ """
+ # index stmt1's parents
+ stmt1_parents = {}
+ children = {}
+ node = stmt1.parent
+ previous = stmt1
+ while node:
+ stmt1_parents[node] = 1
+ children[node] = previous
+ previous = node
+ node = node.parent
+ # climb among stmt2's parents until we find a common parent
+ node = stmt2.parent
+ previous = stmt2
+ while node:
+ if stmt1_parents.has_key(node):
+ # if the common parent is a If or TryExcept statement, look if
+ # nodes are in exclusive branchs
+ if isinstance(node, If):
+ if (node.locate_child(previous)[1]
+ is not node.locate_child(children[node])[1]):
+ return True
+ elif isinstance(node, TryExcept):
+ c2attr, c2node = node.locate_child(previous)
+ c1attr, c1node = node.locate_child(children[node])
+ if c1node is not c2node:
+ if ((c2attr == 'body' and c1attr == 'handlers') or
+ (c2attr == 'handlers' and c1attr == 'body') or
+ (c2attr == 'handlers' and c1attr == 'orelse') or
+ (c2attr == 'orelse' and c1attr == 'handlers')):
+ return True
+ elif c2attr == 'handlers' and c1attr == 'handlers':
+ return previous is not children[node]
+ return False
+ previous = node
+ node = node.parent
+ return False
+
+
def unpack_infer(stmt, context=None):
"""return an iterator on nodes infered by the given statement if the infered
value is a list or a tuple, recurse on it to get values infered by its