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