[Python-checkins] cpython (2.7): Issue #26020: Fix evaluation order for set literals

raymond.hettinger python-checkins at python.org
Thu Sep 8 18:25:34 EDT 2016


https://hg.python.org/cpython/rev/a8d504600c18
changeset: 103375:a8d504600c18
branch: 2.7
parent: 103364:c5b8de10aac2
user: Raymond Hettinger <python at rcn.com>
date: Thu Sep 08 15:25:19 2016 -0700
summary:
 Issue #26020: Fix evaluation order for set literals
files:
 Lib/test/test_set.py | 15 +++++++++++++++
 Python/ceval.c | 6 ++++--
 2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -360,6 +360,21 @@
 t = self.thetype(s)
 self.assertNotEqual(id(s), id(t))
 
+ def test_set_literal_insertion_order(self):
+ # SF Issue #26020 -- Expect left to right insertion
+ s = {1, 1.0, True}
+ self.assertEqual(len(s), 1)
+ stored_value = s.pop()
+ self.assertEqual(type(stored_value), int)
+
+ def test_set_literal_evaluation_order(self):
+ # Expect left to right expression evaluation
+ events = []
+ def record(obj):
+ events.append(obj)
+ s = {record(1), record(2), record(3)}
+ self.assertEqual(events, [1, 2, 3])
+
 def test_hash(self):
 self.assertRaises(TypeError, hash, self.s)
 
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2477,14 +2477,16 @@
 
 TARGET(BUILD_SET)
 {
+ int i;
 x = PySet_New(NULL);
 if (x != NULL) {
- for (; --oparg >= 0;) {
- w = POP();
+ for (i = oparg; i > 0; i--) {
+ w = PEEK(i);
 if (err == 0)
 err = PySet_Add(x, w);
 Py_DECREF(w);
 }
+ STACKADJ(-oparg);
 if (err != 0) {
 Py_DECREF(x);
 break;
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /