]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missi...
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 7 Oct 2008 20:40:09 +0000 (20:40 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Tue, 7 Oct 2008 20:40:09 +0000 (20:40 +0000)
Now it correctly refers to the initial otherSet.

Backport of r66836.

Lib/test/test_set.py
Misc/NEWS
Objects/setobject.c

index 1b01954108e848ae5440a9503ddc2ffd15cb20b9..499406f3c266db6eeb4974ff49532960bd717517 100644 (file)
@@ -382,6 +382,17 @@ class TestSet(TestJointOps):
             else:
                 self.fail()
 
+    def test_remove_keyerror_set(self):
+        key = self.thetype([3, 4])
+        try:
+            self.s.remove(key)
+        except KeyError as e:
+            self.assert_(e.args[0] is key,
+                         "KeyError should be {0}, not {1}".format(key,
+                                                                  e.args[0]))
+        else:
+            self.fail()
+
     def test_discard(self):
         self.s.discard('a')
         self.assert_('a' not in self.s)
index aba287cd4b908d04538e76785acba527033369a9..ec96dc90d499f40eae6586a06dc227a34977e78b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 2.6.1 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #4069: When set.remove(element) is used with a set element, the element
+  is temporarily replaced with an equivalent frozenset.  But the eventual
+  KeyError would always report the empty frozenset([]) as the missing key. Now
+  it correctly refers to the initial element.
+
 - Fixed C99 style comments in several files. Python is now C89 compatible
   again.
 
index 075f8e770631f6522ef1d77f13f8fee8ff97ca2e..ea3970e9b70b2e47110b39374a58bffb04c05c6e 100644 (file)
@@ -1874,7 +1874,7 @@ PyDoc_STRVAR(contains_doc, "x.__contains__(y) <==> y in x.");
 static PyObject *
 set_remove(PySetObject *so, PyObject *key)
 {
-       PyObject *tmpkey, *result;
+       PyObject *tmpkey;
        int rv;
 
        rv = set_discard_key(so, key);
@@ -1886,11 +1886,14 @@ set_remove(PySetObject *so, PyObject *key)
                if (tmpkey == NULL)
                        return NULL;
                set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
-               result = set_remove(so, tmpkey);
+               rv = set_discard_key(so, tmpkey);
                set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
                Py_DECREF(tmpkey);
-               return result;
-       } else if (rv == DISCARD_NOTFOUND) {
+               if (rv == -1)
+                       return NULL;
+       } 
+
+       if (rv == DISCARD_NOTFOUND) {
                set_key_error(key);
                return NULL;
        }