Commit f71c9006 authored by Stefan Behnel's avatar Stefan Behnel

fix creation order of set literals by first generating all items and only then...

fix creation order of set literals by first generating all items and only then adding them to the set (as CPython does it)

--HG--
rename : tests/run/set.pyx => tests/run/set_literals.py
parent 90cf6455
......@@ -6689,8 +6689,7 @@ class SetNode(ExprNode):
return False
def calculate_constant_result(self):
self.constant_result = set([
arg.constant_result for arg in self.args])
self.constant_result = set([arg.constant_result for arg in self.args])
def compile_time_value(self, denv):
values = [arg.compile_time_value(denv) for arg in self.args]
......@@ -6700,6 +6699,8 @@ class SetNode(ExprNode):
self.compile_time_value_error(e)
def generate_evaluation_code(self, code):
for arg in self.args:
arg.generate_evaluation_code(code)
self.allocate_temp_result(code)
code.putln(
"%s = PySet_New(0); %s" % (
......@@ -6707,7 +6708,6 @@ class SetNode(ExprNode):
code.error_goto_if_null(self.result(), self.pos)))
code.put_gotref(self.py_result())
for arg in self.args:
arg.generate_evaluation_code(code)
code.put_error_if_neg(
self.pos,
"PySet_Add(%s, %s)" % (self.result(), arg.py_result()))
......
......@@ -263,6 +263,7 @@ VER_DEP_MODULES = {
'run.yield_inside_lambda',
'run.test_dictviews',
'run.pyclass_special_methods',
'run.set_literals',
]),
# The next line should start (3,); but this is a dictionary, so
# we can only have one (3,) key. Since 2.7 is supposed to be the
......
......@@ -180,6 +180,25 @@ def test_set_sideeffect_unhashable_failure():
return L
def test_set_sideeffect_unhashable_failure_literal():
"""
>>> test_set_sideeffect_unhashable_failure_literal()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = {1,sideeffect(2),3,unhashable_value(4),sideeffect(5)}
except TypeError: pass
else: assert False, "expected exception not raised"
return L
def test_frozenset_sideeffect_unhashable_failure():
"""
>>> test_frozenset_sideeffect_unhashable_failure()
......
# Py2.7+ only
import sys
def test_set_literal():
"""
>>> type(test_set_literal()) is set
True
>>> sorted(test_set_literal())
['a', 'b', 1]
"""
s1 = {1, 'a', 1, 'b', 'a'}
return s1
def test_set_add():
"""
>>> type(test_set_add()) is set
True
>>> sorted(test_set_add())
['a', 1, (1, 2)]
"""
s1 = {1, (1, 2)}
s1.add(1)
s1.add('a')
s1.add(1)
s1.add((1, 2))
return s1
def test_set_comp():
"""
>>> type(test_set_comp()) is set
True
>>> sorted(test_set_comp())
[0, 1, 2]
"""
s1 = {i % 3 for i in range(5)}
return s1
def test_frozenset_set_comp():
"""
>>> type(test_frozenset_set_comp()) is frozenset
True
>>> sorted(test_frozenset_set_comp())
[0, 1, 2]
"""
s1 = frozenset({i % 3 for i in range(5)})
return s1
def test_set_sideeffect_unhashable_failure_literal():
"""
>>> test_set_sideeffect_unhashable_failure_literal()
[2, 4, 5]
"""
L = []
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
try:
s = {1, sideeffect(2), 3, unhashable_value(4), sideeffect(5)}
except TypeError: pass
else: assert False, "expected exception not raised"
return L
def test_set_comp_sideeffect_unhashable_failure():
"""
>>> test_set_comp_sideeffect_unhashable_failure()
(None, [2, 4])
"""
L = []
def value(x):
return x
def sideeffect(x):
L.append(x)
return x
def unhashable_value(x):
L.append(x)
return set()
s = None
try:
s = {f(i) for i, f in enumerate([value, sideeffect, value, unhashable_value, sideeffect], 1)}
except TypeError: pass
else: assert False, "expected exception not raised"
return s, L
def sorted(it):
# Py3 can't compare different types
chars = []
nums = []
tuples = []
for item in it:
if type(item) is int:
nums.append(item)
elif type(item) is tuple:
tuples.append(item)
else:
chars.append(item)
nums.sort()
chars.sort()
tuples.sort()
return chars+nums+tuples
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