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): ...@@ -2060,29 +2060,34 @@ class TransformBuiltinMethods(EnvTransform):
return node return node
def _inject_locals(self, node, func_name): def _inject_locals(self, node, func_name):
# locals()/dir() builtins # locals()/dir()/vars() builtins
lenv = self.current_env() lenv = self.current_env()
entry = lenv.lookup_here(func_name) entry = lenv.lookup_here(func_name)
if entry: if entry:
# not the builtin # not the builtin
return node return node
pos = node.pos pos = node.pos
if func_name == 'locals': if func_name in ('locals', 'vars'):
if len(node.args) > 0: if func_name == 'locals' and len(node.args) > 0:
error(self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d" error(self.pos, "Builtin 'locals()' called with wrong number of args, expected 0, got %d"
% len(node.args)) % len(node.args))
return node 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, items = [ ExprNodes.DictItemNode(pos,
key=ExprNodes.StringNode(pos, value=var), key=ExprNodes.StringNode(pos, value=var),
value=ExprNodes.NameNode(pos, name=var)) value=ExprNodes.NameNode(pos, name=var))
for var in lenv.entries ] for var in lenv.entries ]
return ExprNodes.DictNode(pos, key_value_pairs=items) return ExprNodes.DictNode(pos, key_value_pairs=items)
else: else: # dir()
if len(node.args) > 1: if len(node.args) > 1:
error(self.pos, "Builtin 'dir()' called with wrong number of args, expected 0-1, got %d" error(self.pos, "Builtin 'dir()' called with wrong number of args, expected 0-1, got %d"
% len(node.args)) % len(node.args))
return node if len(node.args) > 0:
elif len(node.args) == 1:
# optimised in Builtin.py # optimised in Builtin.py
return node return node
items = [ ExprNodes.StringNode(pos, value=var) for var in lenv.entries ] items = [ ExprNodes.StringNode(pos, value=var) for var in lenv.entries ]
...@@ -2091,7 +2096,7 @@ class TransformBuiltinMethods(EnvTransform): ...@@ -2091,7 +2096,7 @@ class TransformBuiltinMethods(EnvTransform):
def visit_SimpleCallNode(self, node): def visit_SimpleCallNode(self, node):
if isinstance(node.function, ExprNodes.NameNode): if isinstance(node.function, ExprNodes.NameNode):
func_name = node.function.name 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) return self._inject_locals(node, func_name)
# cython.foo # cython.foo
......
...@@ -3,13 +3,22 @@ ...@@ -3,13 +3,22 @@
def get_locals(x, *args, **kwds): 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)] [('args', (2, 3)), ('kwds', {'k': 5}), ('x', 1), ('y', 'hi'), ('z', 5)]
""" """
cdef int z = 5 cdef int z = 5
y = "hi" y = "hi"
return locals() 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): def get_dir(x, *args, **kwds):
""" """
>>> sorted( get_dir(1,2,3, k=5) ) >>> sorted( get_dir(1,2,3, k=5) )
...@@ -45,6 +54,19 @@ def in_dir(x, *args, **kwds): ...@@ -45,6 +54,19 @@ def in_dir(x, *args, **kwds):
y = "hi" y = "hi"
return x in dir() 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): def sorted(it):
l = list(it) l = list(it)
l.sort() 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