shailesh / pyglet

Automatically exported from code.google.com/p/pyglet
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

set_handler bug and refactoring suggestion #763

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
There is bug in set_handler. E.g. pyglet crash with simple code below:
import pyglet

window = pyglet.window.Window()
def on_draw():
    pass

window.push_handlers(on_draw)
window.pop_handlers()
@window.event
def on_resize(w, h):
    pass
pyglet.app.run()

The reason is that in pop_handlers del self._event_stack[0] is used to delete 
events on top of stack and in set_handler inserting events is doing by  
self._event_stack[0][name] = handler without checking the length of 
event_stack. Fix is as simple as adding check, but personally I think it would 
be better to rewrite whole stack code.

First of all I don't understand why empty stack is defined as tuple and changed 
to list if event are to be added. It only complicates the code. 
Pushing new element on stack in done by
self._event_stack.insert(0, {})
self.set_handlers(*args, **kwargs)
while I think using append list method would be better (and probably faster)

pop_handlers:
now: del self._event_stack[0]
new: self._event_stack.pop()

I hope you get general idea. If you agree with above suggestions I can refactor 
code and provide patch. 

Original issue reported on code.google.com by fen...@gmail.com on 7 Sep 2014 at 7:58