From 42ea77b5898ea52a71b45e9d8c12c569bf6267a3 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Fri, 18 Sep 2009 08:25:16 +0200 Subject: [PATCH] support attribute value selection in TreePath --- Cython/Compiler/Tests/TestTreePath.py | 5 +++++ Cython/Compiler/TreePath.py | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Cython/Compiler/Tests/TestTreePath.py b/Cython/Compiler/Tests/TestTreePath.py index 64815409..296aff6a 100644 --- a/Cython/Compiler/Tests/TestTreePath.py +++ b/Cython/Compiler/Tests/TestTreePath.py @@ -24,6 +24,11 @@ class TestTreePath(TransformTest): self.assertEquals(1, len(find_all(t, "//ReturnStatNode"))) self.assertEquals(1, len(find_all(t, "//DefNode//ReturnStatNode"))) + def test_node_path_attribute(self): + t = self._build_tree() + self.assertEquals(2, len(find_all(t, "//NameNode/@name"))) + self.assertEquals(['fun', 'decorator'], find_all(t, "//NameNode/@name")) + def test_node_path_child(self): t = self._build_tree() self.assertEquals(1, len(find_all(t, "//DefNode/ReturnStatNode/NameNode"))) diff --git a/Cython/Compiler/TreePath.py b/Cython/Compiler/TreePath.py index 74aaeba7..81c0a574 100644 --- a/Cython/Compiler/TreePath.py +++ b/Cython/Compiler/TreePath.py @@ -136,10 +136,14 @@ def handle_attribute(next, token): if token[0]: raise ValueError("Expected attribute name") name = token[1] - token = next() value = None - if token[0] == '=': - value = parse_path_value(next) + try: + token = next() + except StopIteration: + pass + else: + if token[0] == '=': + value = parse_path_value(next) if value is None: def select(result): for node in result: -- 2.26.2