This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author amaury.forgeotdarc
Recipients amaury.forgeotdarc, rharris
Date 2008-04-18.09:36:14
SpamBayes Score 0.0035891824
Marked as misclassified No
Message-id <1208511377.56.0.892064553052.issue2651@psf.upfronthosting.co.za>
In-reply-to
Content
Here is another form of the same inconsistency:

>>> [].pop(0)
IndexError: pop from empty list
>>> {}.pop(0)
KeyError: 'pop(): dictionary is empty'

And my preferred one:
>>> unicodedata.lookup('"')
KeyError: 'undefined character name \'"\''

KeyError is special in that dict lookup raises the equivalent of
KeyError(key). Since the key may be any kind of (hashable) object, it's
preferable to repr() it.

I can see 3 solutions to the problem:

1- imitate IndexError for lists: the exception do not contain the key.

2- dict lookup builds the complete string message, and raise it
   raise KeyError("key not found: %r" % key)
then KeyError.__str__ can be removed.

3- like IOError, KeyError has "msg" and "key" attributes. then dict
lookup raises
   raise KeyError("key not found", key)
and KeyError.__str__ is something like:
   if self.key is not None:
       return "%s: %r" % (self.msg, self.key)
   else
       return str(self.msg)

Choice 1 is not an improvement.
Choice 2 has the correct behavior but leads to performance problems;
KeyErrors are very very common in the interpreter (namespace lookups...)
and formatting the message is costly.
Choice 3 may cause regression for code that use exception.args[0], but
otherwise seems the best to me. I'll try to come with a patch.
History
Date User Action Args
2008-04-18 09:36:18amaury.forgeotdarcsetspambayes_score: 0.00358918 -> 0.0035891824
recipients: + amaury.forgeotdarc, rharris
2008-04-18 09:36:17amaury.forgeotdarcsetspambayes_score: 0.00358918 -> 0.00358918
messageid: <1208511377.56.0.892064553052.issue2651@psf.upfronthosting.co.za>
2008-04-18 09:36:16amaury.forgeotdarclinkissue2651 messages
2008-04-18 09:36:14amaury.forgeotdarccreate