summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmile Anclin <emile.anclin@logilab.fr>2009-03-06 11:16:44 +0100
committerEmile Anclin <emile.anclin@logilab.fr>2009-03-06 11:16:44 +0100
commit8cc24b856d88dfe252ee184fb18349c041ef92b4 (patch)
tree39e85466ca29d8bb23110665206d6ca69b6da77d
parentfb3a0dd20e6681223d811d5fd59d14ea0417c65b (diff)
downloadastroid-git-8cc24b856d88dfe252ee184fb18349c041ef92b4.tar.gz
fix sibling methods and tests
--HG-- branch : _ast_compat
-rw-r--r--nodes.py34
-rw-r--r--test/unittest_scoped_nodes.py3
2 files changed, 23 insertions, 14 deletions
diff --git a/nodes.py b/nodes.py
index c7915cbf..f738db21 100644
--- a/nodes.py
+++ b/nodes.py
@@ -191,22 +191,28 @@ class NodeNG:
return self
- def next_sibling(self, attr = "body"):# FIXME : what do we want ?
+ def next_sibling(self):
"""return the previous sibling statement
"""
- stmts = getattr(self.statement(), attr)
- for k, stmt in enumerate(stmts[:-1]):
- if self is stmt:
- return stmts[k+1]
-
+ while not self.is_statement or isinstance(self, Module):
+ self = self.parent
+ stmts = _get_child_sequence(self.parent, self)
+ index = stmts.index(self)
+ try:
+ return stmts[index +1]
+ except IndexError:
+ pass
- def previous_sibling(self, attr = "body"): # FIXME : what do we want ?
+ def previous_sibling(self):
"""return the next sibling statement
"""
- stmts = getattr(self.statement(), attr)
- for k, stmt in enumerate(stmts[1:]):
- if self is stmt:
- return stmts[k]
+ while not self.is_statement or isinstance(self, Module):
+ self = self.parent
+ stmts = _get_child_sequence(self.parent, self)
+ index = stmts.index(self)
+ if index >= 1:
+ return stmts[index -1]
+ return
def nearest(self, nodes):
"""return the node which is the nearest before this one in the
@@ -291,7 +297,7 @@ class NodeNG:
def as_string(self):
from logilab.astng.nodes_as_string import as_string
return as_string(self)
-
+
extend_class(Node, NodeNG)
@@ -324,7 +330,7 @@ CONST_CLS = {
tuple: Tuple,
dict: Dict,
}
-
+
def const_factory(value):
"""return an astng node for a python value"""
try:
@@ -577,6 +583,8 @@ class Generator(Proxy):
# additional nodes ##########################################################
+# XXX why not using Const node ? Or reintroduce Num / Str
+
class NoneType(Instance, NodeNG):
"""None value (instead of Name('None')"""
_astng_fields = ()
diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py
index 66ff2b0f..74400d44 100644
--- a/test/unittest_scoped_nodes.py
+++ b/test/unittest_scoped_nodes.py
@@ -92,8 +92,9 @@ class FunctionNodeTC(TestCase):
function = MODULE['global_access']
self.assertEquals(function.statement(), function)
l_sibling = function.previous_sibling()
+ # check taking parent if child is not a stmt
self.assertIsInstance(l_sibling, nodes.Assign)
- child = function.body[0] # XXX what do we want ?
+ child = function.args.args[0]
self.assert_(l_sibling is child.previous_sibling())
r_sibling = function.next_sibling()
self.assertIsInstance(r_sibling, nodes.Class)