[Python-bugs-list] [Bug #112558] dictionary lookup does not check exceptions

noreply@sourceforge.net noreply@sourceforge.net
Thu, 31 Aug 2000 12:05:21 -0700


Bug #112558, was updated on 2000-Aug-23 07:24
Here is a current snapshot of the bug.

Project: Python
Category: Core
Status: Closed
Resolution: Fixed
Bug Group: None
Priority: 9
Summary: dictionary lookup does not check exceptions

Details: class BadDictKey:
    def __hash__(self):
        return hash(self.__class__)

    def __cmp__(self, other):
        if isinstance(other, self.__class__):
            print "raising error"
            raise RuntimeError, "gotcha"
        return other

The dict lookup code does not check for hash or cmp functions raising an exception.  This can lead to a variety of bogus behavior; e.g. the uncaught exception is noticed and raised for the next line.

>>> d = {}
>>> x1 = BadDictKey()
>>> x2 = BadDictKey()
>>> d[x1] = 1
>>> d[x2] = 2
raising error
>>> print d.keys()
Traceback (most recent call last):
  File "/tmp/dicterr.py", line 8, in __cmp__
    raise RuntimeError, "gotcha"
RuntimeError: gotcha


Follow-Ups:

Date: 2000-Aug-24 09:56
By: fdrake

Comment:
See patch #101277 for a proposed fix & discussion.
-------------------------------------------------------

Date: 2000-Aug-31 12:05
By: fdrake

Comment:
Fixed by patch #101277, checked in as dictobject.c revision 2.63.
-------------------------------------------------------

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