Python interpreter bug

Steve Holden steve at holdenweb.com
Fri Oct 7 15:22:12 CEST 2005


alainpoint at yahoo.fr wrote:
> Hello,
> 
> I came accross what i think is a serious bug in the python interpreter.
> 
> Membership testing seems not to work for list of objects when these
> objects have a user-defined __cmp__ method.
> It is present in Python 2.3 and 2.4. I don't know about other versions.
> The following code illustrates the bug:
> from random import choice
> class OBJ:
> 	def __init__(self,identifier):
> 		self.id=identifier
> 		self.allocated=0
> 	def __cmp__(self,other):
> 		return cmp(other.allocated,self.allocated)
> mylist=[OBJ(i) for i in range(20)]
> excluded=[obj for obj in mylist if obj.id>choice(range(20))]
> for obj in mylist:
> 	if obj in excluded:
> 		assert obj.id in [objt.id for objt in excluded]
> 		continue
> 
I presume you just put the "continue" in there for fun?

 >>> for obj in mylist:
...   print obj in excluded
...
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
 >>> OBJ(0) == OBJ(1)
True

> Running the above snippet will trigger the assert. The culprit seems to
> be the __cmp__ method which sorts on a key with constant value.

Well indeed. As far as I can see your objects will all test equal. Did 
you mean the __cmp__ method to return cmp(other.id, self.id)?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list