def parse(self): # Parse the parameters and body, then combine them. firstline = 'def f(%s): pass' % self.params tree = niceParse(firstline, '<function parameters>', 'exec') f = tree.node.nodes[0] body_code = niceParse(self.body, self.filename, 'exec') # Stitch the body code into the function. f.code.nodes = body_code.node.nodes f.name = self.name # Look for a docstring, if there are any nodes at all if len(f.code.nodes) > 0: stmt1 = f.code.nodes[0] if (isinstance(stmt1, ast.Discard) and isinstance(stmt1.expr, ast.Const) and isinstance(stmt1.expr.value, str)): f.doc = stmt1.expr.value # The caller may specify that certain variables are globals # so that they can be referenced before a local assignment. # The only known example is the variables context, container, # script, traverse_subpath in PythonScripts. if self.globals: f.code.nodes.insert(0, ast.Global(self.globals)) return tree
def _global(self, *names): for n in names: if not isinstance(n, Symbol): raise CompileError("invalid global") return ast.Global([str(n) for n in names])
def _global(self, *names): for n in names: if not isinstance(n, Symbol) or n.namespace != "": raise CompileError("invalid global: %s" % n) return ast.Global([n.name for n in names])
def _do_GlobalStatement(self, node): names = map(str, node.Names) return ast.Global(names)