Commit 9a9ee007 authored by Stefan Behnel's avatar Stefan Behnel

avoid list building when in-testing against dir() result

parent 85f97592
......@@ -2495,6 +2495,20 @@ class TransformBuiltinMethods(EnvTransform):
for var in local_names ]
return ExprNodes.ListNode(pos, args=items)
def visit_PrimaryCmpNode(self, node):
# special case: for in/not-in test, we do not need to sort locals()
self.visitchildren(node)
if node.operator in 'not_in': # in/not_in
if isinstance(node.operand2, ExprNodes.SortedDictKeysNode):
arg = node.operand2.arg
if isinstance(arg, ExprNodes.NoneCheckNode):
arg = arg.arg
node.operand2 = arg
return node
def visit_CascadedCmpNode(self, node):
return self.visit_PrimaryCmpNode(node)
def _inject_eval(self, node, func_name):
lenv = self.current_env()
entry = lenv.lookup_here(func_name)
......
......@@ -2,10 +2,13 @@
# ticket: 731
# tags: locals, vars, dir
cimport cython
LOCALS = locals()
GLOBALS = globals()
DIR_SAME = sorted(dir()) == sorted(globals().keys())
def test_module_locals_and_dir():
"""
>>> LOCALS is GLOBALS
......@@ -14,6 +17,7 @@ def test_module_locals_and_dir():
True
"""
def test_class_locals_and_dir():
"""
>>> klass = test_class_locals_and_dir()
......@@ -28,3 +32,33 @@ def test_class_locals_and_dir():
names = dir()
locs = locals()
return Foo
@cython.test_fail_if_path_exists('//SortedDictKeysNode')
def test_class_dir_contains():
"""
>>> klass = test_class_dir_contains()
True
False
True
False
True
False
True
True
True
"""
not_visible = 1234
class Foo:
visible = 4321
print('visible' in dir())
print('not_visible' in dir())
print('not_visible' not in dir())
print('locs' in dir())
print('visible' in locals())
print('locs' in locals())
locs = locals()
print('visible' in dir())
print('locs' in dir())
print('locs' in locals())
return Foo
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