1from Cython.Compiler.Visitor import VisitorTransform
2from Cython.Compiler.Nodes import StatListNode
3
4class ExtractPxdCode(VisitorTransform):
5    """
6    Finds nodes in a pxd file that should generate code, and
7    returns them in a StatListNode.
8
9    The result is a tuple (StatListNode, ModuleScope), i.e.
10    everything that is needed from the pxd after it is processed.
11
12    A purer approach would be to seperately compile the pxd code,
13    but the result would have to be slightly more sophisticated
14    than pure strings (functions + wanted interned strings +
15    wanted utility code + wanted cached objects) so for now this
16    approach is taken.
17    """
18
19    def __call__(self, root):
20        self.funcs = []
21        self.visitchildren(root)
22        return (StatListNode(root.pos, stats=self.funcs), root.scope)
23
24    def visit_FuncDefNode(self, node):
25        self.funcs.append(node)
26        # Do not visit children, nested funcdefnodes will
27        # also be moved by this action...
28        return node
29
30    def visit_Node(self, node):
31        self.visitchildren(node)
32        return node
33