beproudstandupcom / pyglet

Automatically exported from code.google.com/p/pyglet
0 stars 0 forks source link

clock.unschedule fails silently. #147

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. For t and x > 0, do something like:

clock.schedule(t, fn1); clock.schedule_interval(t-x, fn2)

2. Try to cancel the earlier function: clock.unschedule(fn2)
3. The clock silently ignores the request.  fn2 triggers forever, no way to
cancel it anymore.

i.e., if you schedule a function fn2 for a time earlier than that of an
already scheduled function, you can't cancel fn2 anymore.

Paste in the traceback or error message:

n/a

Paste in the output of tools/gl_info.py:

Irrelevant, other that this is revision 1262. Excuse me if this is fixed in
head.

Any additional info (platform/language/hardware) that may be relevant?

I narrowed it down to here (in clock.py:_schedule_item):

        # Insert in sort order
        for i, other in enumerate(self._schedule_interval_items):
            if other.next_ts > next_ts:
                self._schedule_interval_items.insert(i, item)
                return
        self._schedule_interval_items.append(item)

        # Add item to func mapping.
        [...]

If you schedule something for earlier than an existing scheduled action,
you reach the return.  The code below the "Add item to func mapping"
comment is never reached, so the function is never registered in first place.

I checked that the following fixes the error (changed lines marked with '!'):

        # Insert in sort order
        for i, other in enumerate(self._schedule_interval_items):
            if other.next_ts > next_ts:
                self._schedule_interval_items.insert(i, item)
!               break
!       else:
!           self._schedule_interval_items.append(item)

        # Add item to func mapping.
        [...]

BTW, I haven't looked too closely, but for this use you may want to use a
heapq?

http://docs.python.org/lib/module-heapq.html

Original issue reported on code.google.com by euccas...@gmail.com on 29 Sep 2007 at 8:03

GoogleCodeExporter commented 8 years ago
Thanks, applied your fix in r1299 (untested).

heapq would probably be an improvement, especially for the common case of 
reinserting a popped item in tick().  
Patches welcome :-)

Original comment by Alex.Hol...@gmail.com on 30 Sep 2007 at 12:38