]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #26020: Fix evaluation order for set literals
authorRaymond Hettinger <python@rcn.com>
Thu, 8 Sep 2016 21:40:36 +0000 (14:40 -0700)
committerRaymond Hettinger <python@rcn.com>
Thu, 8 Sep 2016 21:40:36 +0000 (14:40 -0700)
Lib/test/test_set.py
Misc/NEWS
Python/ceval.c

index 1a49edf23110ab02e76014afeda238205f75dd34..7594303132c6f08e8bd7bfe438ad55ee1b603b12 100644 (file)
@@ -388,6 +388,21 @@ class TestSet(TestJointOps, unittest.TestCase):
         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)
 
index 9125caf9fab16b6b3a4e01f53a15432bf9a6ea4d..1922432e0d06b569ec56d13631381cd2a1aaa67b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -20,6 +20,8 @@ Core and Builtins
   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.
index 8e396fd2bc6adfa163b6f2796f5a4dec9b2f9b86..451791c8b49f02b0e8918869ae807a0d236feffd 100644 (file)
@@ -2580,14 +2580,16 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
         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;