Commit 8e7816d2 authored by Stefan Behnel's avatar Stefan Behnel

implement read-only 0-arg vars() as locals()

parent 40fe046a
......@@ -2060,29 +2060,34 @@ class TransformBuiltinMethods(EnvTransform):
return node
def _inject_locals(self, node, func_name):
# locals()/dir() builtins
# locals()/dir()/vars() builtins
lenv = self.current_env()
entry = lenv.lookup_here(func_name)
if entry:
# not the builtin
return node
pos = node.pos
if func_name == 'locals':
if len(node.args) > 0:
if func_name in ('locals', 'vars'):
if func_name == 'locals' and len(node.args) > 0:
error(self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d"
% len(node.args))
return node
elif func_name == 'vars':
if len(node.args) > 1:
error(self.pos, "Builtin 'vars()' called with wrong number of args, expected 0-1, got %d"
% len(node.args))
if len(node.args) > 0:
return node # nothing to do
items = [ ExprNodes.DictItemNode(pos,
key=ExprNodes.StringNode(pos, value=var),
value=ExprNodes.NameNode(pos, name=var))
for var in lenv.entries ]
return ExprNodes.DictNode(pos, key_value_pairs=items)
else:
else: # dir()
if len(node.args) > 1:
error(self.pos, "Builtin 'dir()' called with wrong number of args, expected 0-1, got %d"
% len(node.args))
return node
elif len(node.args) == 1:
if len(node.args) > 0:
# optimised in Builtin.py
return node
items = [ ExprNodes.StringNode(pos, value=var) for var in lenv.entries ]
......@@ -2091,7 +2096,7 @@ class TransformBuiltinMethods(EnvTransform):
def visit_SimpleCallNode(self, node):
if isinstance(node.function, ExprNodes.NameNode):
func_name = node.function.name
if func_name in ('dir', 'locals'):
if func_name in ('dir', 'locals', 'vars'):
return self._inject_locals(node, func_name)
# cython.foo
......
......@@ -3,13 +3,22 @@
def get_locals(x, *args, **kwds):
"""
>>> sorted( get_locals(1,2,3, k=5) .items())
>>> sorted( get_locals(1,2,3, k=5).items() )
[('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
"""
cdef int z = 5
y = "hi"
return locals()
def get_vars(x, *args, **kwds):
"""
>>> sorted( get_vars(1,2,3, k=5).items() )
[('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
"""
cdef int z = 5
y = "hi"
return vars()
def get_dir(x, *args, **kwds):
"""
>>> sorted( get_dir(1,2,3, k=5) )
......@@ -45,6 +54,19 @@ def in_dir(x, *args, **kwds):
y = "hi"
return x in dir()
def in_vars(x, *args, **kwds):
"""
>>> in_vars('z')
True
>>> in_vars('args')
True
>>> in_vars('X')
False
"""
cdef int z = 5
y = "hi"
return x in vars()
def sorted(it):
l = list(it)
l.sort()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment