from Cython.TestUtils import CythonTest
from Cython.Compiler.TreeFragment import *
+from Cython.Compiler.Nodes import *
class TestTreeFragments(CythonTest):
def test_basic(self):
T = F.substitute({"x" : y})
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__":
import unittest
unittest.main()
# Clone
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):
self.substitute = substitute
return super(SubstitutionTransform, self).__call__(node)