summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-10-07 20:40:09 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-10-07 20:40:09 (GMT)
commit00c94edea0cdc8c490a91dd5455150be8b19d3e2 (patch)
treea3071f8bf0ca2401def09c64d0f5074ddb6c4c96
parent001befaadcf562170039e085ebd4ae3318f322c2 (diff)
downloadcpython-00c94edea0cdc8c490a91dd5455150be8b19d3e2.zip
cpython-00c94edea0cdc8c490a91dd5455150be8b19d3e2.tar.gz
cpython-00c94edea0cdc8c490a91dd5455150be8b19d3e2.tar.bz2
#4069: aSet.remove(otherSet) would always report the empty frozenset([]) as the missing key.
Now it correctly refers to the initial otherSet. Backport of r66836.
-rw-r--r--Lib/test/test_set.py11
-rw-r--r--Misc/NEWS5
-rw-r--r--Objects/setobject.c11
3 files changed, 23 insertions, 4 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py
index 1b01954..499406f 100644
--- a/Lib/test/test_set.py
+++ b/Lib/test/test_set.py
@@ -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)
diff --git a/Misc/NEWS b/Misc/NEWS
index aba287c..ec96dc9 100644
--- 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.
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 075f8e7..ea3970e 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -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;
}