[Python-bugs-list] [Bug #125598] Confusing KeyError-Message when key is tuple of size 1

noreply@sourceforge.net noreply@sourceforge.net
Fri, 19 Jan 2001 12:18:39 -0800


Bug #125598, was updated on 2000-Dec-13 02:38
Here is a current snapshot of the bug.

Project: Python
Category: Python Interpreter Core
Status: Open
Resolution: None
Bug Group: None
Priority: 5
Submitted by: murple
Assigned to : gvanrossum
Summary: Confusing KeyError-Message when key is tuple of size 1 

Details: Following caused some confusion for me:

>>> dic = {1:1,2:"bla"}
>>> dic[1]
1
>>> b = (1,)
#1000 lines of code
>>> dic[b]
Traceback (innermost last):
  File "<stdin>", line 1, in ?
KeyError: 1
# This should be KeyError: (1,) 
# because 1 is a valid key for dic 
>>> dic[(1,2)]
Traceback (innermost last):
  File "<stdin>", line 1, in ?
KeyError: (1, 2)
>>> 


Follow-Ups:

Date: 2001-Jan-19 12:18
By: bwarsaw

Comment:
Assigning back to Guido for final pronouncement, but I think this should be
resolved as "Won't Fix" and closed.
-------------------------------------------------------

Date: 2000-Dec-13 15:54
By: bwarsaw

Comment:
I don't remember the exact details, but this is a byproduct of the
backwards compatibility rules for Exception.__str__().

Specifically, if an exception is instantiated with a sequence of length 1,
then str(exc) will return str(exc.args[0]).  Note that exc.args contains
the length-1 tuple it was instantiated with.

This bites every built-in exception except EnvironmentError and
SyntaxError, which define their own __str__().  Changing this may have
unintended consequences, and I'm not sure if it's worth fixing.
-------------------------------------------------------

Date: 2000-Dec-13 06:09
By: gvanrossum

Comment:
This seems a problem in exception reporting.  I can reproduce it as
follows:

>>> raise KeyError, (1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 1
>>> 

Assigned to Barry since he's the master of this code.]

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

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=125598&group_id=5470