classification
Title: set.remove raises confusing KeyError
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: amaury.forgeotdarc Nosy List: Carl.Friedrich.Bolz, amaury.forgeotdarc, rhettinger
Priority: normal Keywords: patch

Created on 2008-10-07 15:58 by Carl.Friedrich.Bolz, last changed 2008-10-07 20:44 by amaury.forgeotdarc. This issue is now closed.

Files
File name Uploaded Description Edit
set_remove.patch amaury.forgeotdarc, 2008-10-07 16:40
Messages (4)
msg74461 - (view) Author: Carl Friedrich Bolz-Tereick (Carl.Friedrich.Bolz) * Date: 2008-10-07 15:58
When trying to remove a set from a set, the KeyError that is raised is
confusing:

Python 2.6 (r26:66714, Oct  7 2008, 13:23:57)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = set([frozenset([1, 2]), frozenset([2, 3])])
>>> s.remove(set([3, 4]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: frozenset([])


I understand that s.remove(set(...)) turns the set into a frozenset, but
I was expecting this converted frozenset to be attached to the KeyError,
not an empty set.
msg74464 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-10-07 16:40
The KeyError initially contains the correct frozenset, but its content
is swapped with the original set object (yes, like C++ std::set::swap(),
this mutates the frozenset!).

Attached a patch with unit test. The exception now shows the original key:
    KeyError: {3, 4}
msg74467 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-07 17:56
Patch looks fine.  I see no need to backport to 2.5 though.
msg74484 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-10-07 20:44
Committed r66836 (trunk) and r66837 (2.6).
This will merge nicely into py3k.

Hello Carl Friedrich, and thanks for the report!
History
Date User Action Args
2008-10-07 20:44:19amaury.forgeotdarcsetstatus: open -> closed
resolution: accepted -> fixed
messages: + msg74484
2008-10-07 17:56:03rhettingersetkeywords: - needs review
assignee: rhettinger -> amaury.forgeotdarc
resolution: accepted
messages: + msg74467
2008-10-07 16:43:50rhettingersetassignee: rhettinger
nosy: + rhettinger
2008-10-07 16:40:24amaury.forgeotdarcsetkeywords: + needs review, patch
files: + set_remove.patch
messages: + msg74464
nosy: + amaury.forgeotdarc
2008-10-07 15:58:33Carl.Friedrich.Bolzcreate