('issubclass', "OO", "b", "PyObject_IsSubclass"),
('iter', "O", "O", "PyObject_GetIter"),
('len', "O", "Z", "PyObject_Length"),
+ ('locals', "", "O", "__pyx_locals"),
#('map', "", "", ""),
#('max', "", "", ""),
#('min', "", "", ""),
from ParseTreeTransforms import WithTransform, NormalizeTree, PostParse, PxdPostParse
from ParseTreeTransforms import AnalyseDeclarationsTransform, AnalyseExpressionsTransform
from ParseTreeTransforms import CreateClosureClasses, MarkClosureVisitor, DecoratorTransform
- from ParseTreeTransforms import InterpretCompilerDirectives
+ from ParseTreeTransforms import InterpretCompilerDirectives, TransformBuiltinMethods
from AutoDocTransforms import EmbedSignature
from Optimize import FlattenInListTransform, SwitchTransform, FinalOptimizePhase
from Buffer import IntroduceBufferAuxiliaryVars
WithTransform(self),
DecoratorTransform(self),
AnalyseDeclarationsTransform(self),
+ TransformBuiltinMethods(self),
IntroduceBufferAuxiliaryVars(self),
_check_c_classes,
AnalyseExpressionsTransform(self),
return property
class AnalyseExpressionsTransform(CythonTransform):
+
def visit_ModuleNode(self, node):
node.body.analyse_expressions(node.scope)
self.visitchildren(node)
return node
+class EnvTransform(CythonTransform):
+ """
+ This transformation keeps a stack of the environments.
+ """
+ def __call__(self, root):
+ self.env_stack = [root.scope]
+ return super(EnvTransform, self).__call__(root)
+
+ def visit_FuncDefNode(self, node):
+ self.env_stack.append(node.local_scope)
+ self.visitchildren(node)
+ self.env_stack.pop()
+ return node
+
+
+class TransformBuiltinMethods(EnvTransform):
+
+ def visit_SimpleCallNode(self, node):
+ self.visitchildren(node)
+ if isinstance(node.function, ExprNodes.NameNode):
+ if node.function.name == 'locals':
+ pos = node.pos
+ lenv = self.env_stack[-1]
+ items = [ExprNodes.DictItemNode(pos,
+ key=ExprNodes.IdentifierStringNode(pos, value=var),
+ value=ExprNodes.NameNode(pos, name=var)) for var in lenv.entries]
+ return ExprNodes.DictNode(pos, key_value_pairs=items)
+ return node
--- /dev/null
+__doc__ = """
+sage: get_locals(1,2,3)
+{'args': (2, 3), 'kwds': {}, 'x': 1, 'y': 'hi', 'z': 5}
+"""
+
+def get_locals(x, *args, **kwds):
+ y = "hi"
+ cdef int z = 5
+ return locals()