Commit 5765b020 authored by Stefan Behnel's avatar Stefan Behnel

safely replace all node references when injecting a literal set for set([...])

parent 43342ab9
...@@ -1775,7 +1775,8 @@ class InlineDefNodeCalls(Visitor.NodeRefCleanupMixin, Visitor.EnvTransform): ...@@ -1775,7 +1775,8 @@ class InlineDefNodeCalls(Visitor.NodeRefCleanupMixin, Visitor.EnvTransform):
return node return node
class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform): class OptimizeBuiltinCalls(Visitor.NodeRefCleanupMixin,
Visitor.MethodDispatcherTransform):
"""Optimize some common methods calls and instantiation patterns """Optimize some common methods calls and instantiation patterns
for builtin types *after* the type analysis phase. for builtin types *after* the type analysis phase.
...@@ -2061,17 +2062,18 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform): ...@@ -2061,17 +2062,18 @@ class OptimizeBuiltinCalls(Visitor.MethodDispatcherTransform):
temps.append(arg) temps.append(arg)
args.append(arg) args.append(arg)
result = ExprNodes.SetNode(node.pos, is_temp=1, args=args) result = ExprNodes.SetNode(node.pos, is_temp=1, args=args)
self.replace(node, result)
for temp in temps[::-1]: for temp in temps[::-1]:
result = UtilNodes.EvalWithTempExprNode(temp, result) result = UtilNodes.EvalWithTempExprNode(temp, result)
return result return result
else: else:
# PySet_New(it) is better than a generic Python call to set(it) # PySet_New(it) is better than a generic Python call to set(it)
return ExprNodes.PythonCapiCallNode( return self.replace(node, ExprNodes.PythonCapiCallNode(
node.pos, "PySet_New", node.pos, "PySet_New",
self.PySet_New_func_type, self.PySet_New_func_type,
args=pos_args, args=pos_args,
is_temp=node.is_temp, is_temp=node.is_temp,
py_name="set") py_name="set"))
PyFrozenSet_New_func_type = PyrexTypes.CFuncType( PyFrozenSet_New_func_type = PyrexTypes.CFuncType(
Builtin.frozenset_type, [ Builtin.frozenset_type, [
......
...@@ -144,6 +144,22 @@ def test_object_pop(s): ...@@ -144,6 +144,22 @@ def test_object_pop(s):
return s.pop() return s.pop()
def test_noop_pop():
"""
>>> test_noop_pop()
"""
set([0]).pop()
def test_noop_pop_exception():
"""
>>> try: test_noop_pop_exception()
... except KeyError: pass
... else: print("KeyError expected but not raised!")
"""
set([]).pop()
def test_set_discard(): def test_set_discard():
""" """
>>> type(test_set_discard()) is _set >>> type(test_set_discard()) is _set
......
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