[Python-Dev] #2651 - KeyError does not round trip strings
Łukasz Langa
lukasz at langa.pl
Wed Aug 4 15:39:16 CEST 2010
Hi guys,
there's this 2 year old bug about making strings passed to KeyError round trip:
http://bugs.python.org/issue2651
There are three things I like you to present opinions on.
0. The moratorium.
Based on the old 2.x patch there I created a new one for py3k. It's been reviewed and it was actually quite close to it being committed when Georg reminded us that there's this moratorium situation. So, please -1 that change here or on the issue if you think it should be stopped until the moratorium ends. Georg, Antoine and Michael Foord seem to be +1 on it despite the moratorium. (guys, please correct me if I'm wrong)
1. The patch makes KeyError behave analogically to IOError so that the first arg is now a message and the second is the actual key.
>>> raise KeyError("Key not found", "a Scotsman on a horse")
Traceback (most recent call last):
...
KeyError: 'Key not found: a Scotsman on a horse'
This is backwards incompatible (which has been addressed for the stdlib in the patch). Now, for non-empty e.args, the key is stored in e.args[-1] whereas it used to in e.args[0]. We could swap the args to make it backwards compatible but then we lose consistency with IOError and the issue on the tracker was originally targetting consistency.
2. Some people suggest adding e.key to KeyError. I like the idea but in my opinion currently it is not implementable in a reliable way.
a) if the party raising the exception does not pass any arguments, what would the expected behaviour of e.key be? `None` is a valid key so returning this can be misleading.
b) if the party raising the exception passes one argument, how do we know it's a key and not a message? Take for instance "Set is empty" and such. Presenting e.key = "Set is empty" is just wrong.
c) if the party raising the exception passes two arguments, we already know which one is the key. So in this case it would work well but at the same time it would be somewhat redundant.
Antoine and Michael Foord suggest that we simply do a "best-effort thing" and present `None` if no args are passed and always treat the only argument as a key. It would be consistent with what's IOError is doing at the moment. I'm on the fence here myself.
--
Best regards,
Łukasz Langa
tel. +48 791 080 144
WWW http://lukasz.langa.pl/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100804/a7cef384/attachment-0001.html>
More information about the Python-Dev
mailing list