[New-bugs-announce] [issue5830] heapq item comparison problematic with sched's events

Kay F. Jahnke report at bugs.python.org
Fri Apr 24 15:35:06 CEST 2009


New submission from Kay F. Jahnke <k_f_j at web.de>:

scheduler uses heapq to schedule it's events. Heapq uses plain >/< 
comparisons on the events. Now that comparisons of incomparable data 
are no longer valid, the comparison fails if two events are scheduled 
for the same time with the same priority, since the comparison 
continues with comparing the 'action' components ov the event's tuple. 
I suppose this behaviour is unwanted - at least I did not expect it and 
it took me some time to figure out what it was due to. I worked around 
it by assigning comparison functions to the event data type - since 
this is part of the standard library, maybe a general fix should be 
considered? Here's my humble snippet of code:

def evtlt ( self , other ):
	if self.time < other.time:
		return True
	elif self.time == other.time:
		return self.priority < other.priority
	return False

sched.Event.__lt__ = evtlt

def evtgt ( self , other ):
	if self.time > other.time:
		return True
	elif self.time == other.time:
		return self.priority > other.priority
	return False

sched.Event.__gt__ = evtgt

If anyone would care to reproduce the (?)bug, try:

import sched

def foo():
	pass

def bar():
	pass

s = sched.scheduler(None, None) 

s.enterabs ( 0 , 0 , foo , () )
s.enterabs ( 0 , 0 , bar , () )

this produces the following output:

Traceback (most recent call last):
  File "./schedbug.py", line 12, in <module>
    s.enterabs ( 0 , 0 , bar , () )
  File "c:\Programme\Python3.0\lib\sched.py", line 54, in enterabs
    heapq.heappush(self._queue, event)
TypeError: unorderable types: function() < function()

----------
components: Library (Lib)
messages: 86408
nosy: kfj
severity: normal
status: open
title: heapq item comparison problematic with sched's events
type: behavior
versions: Python 3.0

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5830>
_______________________________________


More information about the New-bugs-announce mailing list