Commit 5e001476 authored by Dag Sverre Seljebotn's avatar Dag Sverre Seljebotn

TreeFragment fix: Replace enclosing ExprStatNode if statement is substituted

parent bddf62c2
from Cython.TestUtils import CythonTest from Cython.TestUtils import CythonTest
from Cython.Compiler.TreeFragment import * from Cython.Compiler.TreeFragment import *
from Cython.Compiler.Nodes import *
class TestTreeFragments(CythonTest): class TestTreeFragments(CythonTest):
def test_basic(self): def test_basic(self):
...@@ -21,6 +22,12 @@ class TestTreeFragments(CythonTest): ...@@ -21,6 +22,12 @@ class TestTreeFragments(CythonTest):
T = F.substitute({"x" : y}) T = F.substitute({"x" : y})
self.assertCode(u"y = 4", T) self.assertCode(u"y = 4", T)
def test_exprstat(self):
F = self.fragment(u"PASS")
pass_stat = PassStatNode(pos=None)
T = F.substitute({"PASS" : pass_stat})
self.assert_(T.body is pass_stat, T.body)
if __name__ == "__main__": if __name__ == "__main__":
import unittest import unittest
unittest.main() unittest.main()
...@@ -83,6 +83,14 @@ class SubstitutionTransform(VisitorTransform): ...@@ -83,6 +83,14 @@ class SubstitutionTransform(VisitorTransform):
# Clone # Clone
return self.visit_Node(node) return self.visit_Node(node)
def visit_ExprStatNode(self, node):
# If an expression-as-statement consists of only a replaceable
# NameNode, we replace the entire statement, not only the NameNode
if isinstance(node.expr, NameNode) and node.expr.name in self.substitute:
return self.substitute[node.expr.name]
else:
return self.visit_Node(node)
def __call__(self, node, substitute): def __call__(self, node, substitute):
self.substitute = substitute self.substitute = substitute
return super(SubstitutionTransform, self).__call__(node) return super(SubstitutionTransform, self).__call__(node)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment