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/

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to