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

raymond.hettinger python-checkins at python.org
Thu Sep 8 17:46:17 EDT 2016


https://hg.python.org/cpython/rev/0967531fc762
changeset: 103362:0967531fc762
branch: 3.5
parent: 103358:f6b62cf4a436
user: Raymond Hettinger <python at rcn.com>
date: Thu Sep 08 14:40:36 2016 -0700
summary:
 Issue #26020: Fix evaluation order for set literals
files:
 Lib/test/test_set.py | 15 +++++++++++++++
 Misc/NEWS | 2 ++
 Python/ceval.c | 6 ++++--
 3 files changed, 21 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
@@ -388,6 +388,21 @@
 t = {1,2,3}
 self.assertEqual(s, 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/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,8 @@
 after use of 'def' in _PyState_AddModule().
 Initial patch by Christian Heimes.
 
+- Issue #26020: set literal evaluation order did not match documented behaviour.
+
 - Issue #27782: Multi-phase extension module import now correctly allows the
 ``m_methods`` field to be used to add module level functions to instances
 of non-module types returned from ``Py_create_mod``. Patch by Xiang Zhang.
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2580,14 +2580,16 @@
 TARGET(BUILD_SET) {
 PyObject *set = PySet_New(NULL);
 int err = 0;
+ int i;
 if (set == NULL)
 goto error;
- while (--oparg >= 0) {
- PyObject *item = POP();
+ for (i = oparg; i > 0; i--) {
+ PyObject *item = PEEK(i);
 if (err == 0)
 err = PySet_Add(set, item);
 Py_DECREF(item);
 }
+ STACKADJ(-oparg);
 if (err != 0) {
 Py_DECREF(set);
 goto error;
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

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