[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [gymnastics] Re: [pygame] BUG: pygame.event.peek



The "NoEvent" does evaluate to False in a Boolean test, so the examples actually do work as expected.

ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
import pygame
pygame.init()
(6, 0)
if pygame.event.peek():
...     print 'True'
... else:
...     print 'False'
...
False

But you are correct in that peek should return a bool as documented. My reply was geared more toward the general question of why peeking would be useful rather than an argument for continuing the current behavior of peek. A follow-up to your original post was:

"""
since peek is a kind of silly unthread safe function, maybe that
functionality should be removed.

Anyone have any ideas about this?  If not, I'll remove that functionality.
"""

which I (and others, I believe) took to mean that peek should be removed. Someone subsequently asked how peek could useful, hence my post.

Regards,
Taylor

----- Original Message ----- From: "Alex Holkner" <aholkner@xxxxxxxxxxxxxx>
To: <pygame-users@xxxxxxxx>
Sent: Monday, July 31, 2006 10:22 PM
Subject: [gymnastics] Re: [pygame] BUG: pygame.event.peek



Taylor Boon wrote:

This does not appear to be the case. Check out the "pygame+threading+windows" thread on this list. Other threads can't "get" an event behind your back because they cannot get an event. So, perhaps by accident rather than design, peek is thread safe.

peek is useful in order to service events while also doing other processing. Pseudocode:

while not done:
   #each part will take a second or two
   do_part_of_a_long_computation()

   #service events if there are any, otherwise do another part
   # of the long computation...
   if event.peek():
       for event in event.get():
           process_event(event)

This is equivalent to:

while not done:
  #each part will take a second or two
  do_part_of_a_long_computation()

  #service events if there are any, otherwise do another part
  # of the long computation...
  for event in event.get():
      process_event(event)

Another use that comes to mind is to yield time to other processes, something like this (pseudo code again):

while running:
   if not event.peek():
       #let something else run for a bit...
       sleep(1)
   else:
       process_events()

In the current implementation this will not do what you expect, as event.peek() returns an event, not a bool. If there are no events in the queue, an event filled with uninitialized data will be returned (not None, as you might hope).


A final example uses peek's abaility to just see if certain event types are in the event queue:

#assumes pygame.locals imported
while app.running:
   #process mouse events only
   if event.peek([MOUSEMOTION, MOUSEDOWN, MOUSEUP])
       do_mouse_things()
   #don't care about other events...

peek has its place.


You haven't demonstrated valid use of the undocumented form of peek: 'peek() -> Event', only the documented one: 'peek([events]) -> bool'. There's no reason to drop the documented behaviour, only the form returning Event, which is undocumented, is buggy, and should either be repaired or dropped.

Alex.

__________ NOD32 1.1685 (20060731) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com