[ python-Bugs-1576657 ] dict keyerror formatting and tuples

SourceForge.net noreply at sourceforge.net
Thu Nov 2 17:18:29 CET 2006


Bugs item #1576657, was opened at 2006-10-13 16:00
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1576657&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
>Status: Closed
>Resolution: Fixed
Priority: 5
Private: No
Submitted By: M.-A. Lemburg (lemburg)
>Assigned to: Georg Brandl (gbrandl)
Summary: dict keyerror formatting and tuples

Initial Comment:
Probably just a minor glitch, but one which caused me
half an hour to track down:

>>> d = {1:'x'}
>>> v = (1,)
>>> d[v]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1

Note the formatting of the error message. It reads '1',
not '(1,)' as you would expect.

The example is constructed. In the code I was
debugging, I didn't know that v was a tuple and thought
it was the integer 1 - so the KeyError itself was
somewhat puzzling.

Only after printing the key I found that the lookup
failed due to a type error.

This happens in Python 2.4 and 2.5.


----------------------------------------------------------------------

>Comment By: Georg Brandl (gbrandl)
Date: 2006-11-02 16:18

Message:
Logged In: YES 
user_id=849994

Patch was committed as rev. 52535/6.

----------------------------------------------------------------------

Comment By: Georg Brandl (gbrandl)
Date: 2006-10-14 07:16

Message:
Logged In: YES 
user_id=849994

This is because a tuple as exception "argument" is
automatically unpacked as the arguments on NormalizeException.

Attaching patch that wraps all KeyErrors from dictionaries
in a tuple. (There may be other objects and exceptions where
this must be handled)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1576657&group_id=5470


More information about the Python-bugs-list mailing list