Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event.peek return bool, minor doc/stub fixes #3283

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions buildconfig/stubs/pygame/event.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ from pygame.typing import SequenceLike

@final
class Event:
type: int
@property
def type(self) -> int: ...
__dict__: dict[str, Any]
__hash__: None # type: ignore
def __init__(
Expand All @@ -17,20 +18,21 @@ class Event:

# this is at the bottom because mypy complains if this declaration comes
# before any uses of the dict[] typehinting because of the same naming
dict: dict[str, Any]
@property
def dict(self) -> dict[str, Any]: ...

_EventTypes = Union[int, SequenceLike[int]]

def pump() -> None: ...
def get(
eventtype: Optional[_EventTypes] = None,
pump: Any = True,
pump: bool = True,
exclude: Optional[_EventTypes] = None,
) -> list[Event]: ...
def poll() -> Event: ...
def wait(timeout: int = 0) -> Event: ...
def peek(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> bool: ...
def clear(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> None: ...
def peek(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> bool: ...
def clear(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> None: ...
def event_name(type: int, /) -> str: ...
def set_blocked(type: Optional[_EventTypes], /) -> None: ...
def set_allowed(type: Optional[_EventTypes], /) -> None: ...
Expand Down
7 changes: 6 additions & 1 deletion docs/reST/ref/event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,15 @@ On Android, the following events can be generated
queue. If a sequence of event types is passed, this will return ``True`` if
any of those events are on the queue.

When ``eventtype`` is not passed or ``None``, this function will return ``True`` if
there's any event on the queue, and return ``False`` if the queue is empty.

If ``pump`` is ``True`` (the default), then :func:`pygame.event.pump()` will be called.

.. versionchangedold:: 1.9.5 Added ``pump`` argument

.. versionchanged:: 2.5.3 no longer mistakenly returns an event when ``eventtype`` is None or not passed.

.. ## pygame.event.peek ##

.. function:: clear
Expand Down Expand Up @@ -491,7 +496,7 @@ On Android, the following events can be generated

Read-only. The event type identifier. For user created event
objects, this is the ``type`` argument passed to
:func:`pygame.event.Event()`.
:class:`pygame.event.Event()`.

For example, some predefined event identifiers are ``QUIT`` and
``MOUSEMOTION``.
Expand Down
8 changes: 4 additions & 4 deletions src_c/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -1591,9 +1591,9 @@ static PyTypeObject pgEvent_Type;
#define OFF(x) offsetof(pgEventObject, x)

static PyMemberDef pg_event_members[] = {
{"__dict__", T_OBJECT, OFF(dict), READONLY},
{"type", T_INT, OFF(type), READONLY},
{"dict", T_OBJECT, OFF(dict), READONLY},
{"__dict__", T_OBJECT, OFF(dict), READONLY, DOC_EVENT_EVENT_DICT},
{"type", T_INT, OFF(type), READONLY, DOC_EVENT_EVENT_TYPE},
{"dict", T_OBJECT, OFF(dict), READONLY, DOC_EVENT_EVENT_DICT},
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -2262,7 +2262,7 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)
res = PG_PEEP_EVENT_ALL(&event, 1, SDL_PEEKEVENT);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
return pgEvent_New(res ? &event : NULL);
return PyBool_FromLong(res);
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
Expand Down
34 changes: 25 additions & 9 deletions test/event_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,32 @@ def test_clear(self):
self.assertRaises(ValueError, pygame.event.clear, 0x0010FFFFF)
self.assertRaises(TypeError, pygame.event.get, ["a", "b", "c"])

def test_peek_no_arg(self):
pygame.event.clear()
# do a strict "is" check and not just a simple truthy-test to check that
# we have a boolean instance.
self.assertIs(pygame.event.peek(), False)
self.assertIs(pygame.event.peek(None), False)

# peek should return True if there's any event on the queue
pygame.event.post(pygame.event.Event(pygame.USEREVENT))
self.assertIs(pygame.event.peek(), True)
self.assertIs(pygame.event.peek(None), True)

def test_peek(self):
pygame.event.peek()
pygame.event.peek(None)
pygame.event.peek(None, True)

pygame.event.peek(pump=False)
pygame.event.peek(pump=True)
pygame.event.peek(eventtype=None)
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False)
self.assertIsInstance(pygame.event.peek(), bool)
self.assertIsInstance(pygame.event.peek(None), bool)
self.assertIsInstance(pygame.event.peek(None, True), bool)

self.assertIsInstance(pygame.event.peek(pump=False), bool)
self.assertIsInstance(pygame.event.peek(pump=True), bool)
self.assertIsInstance(pygame.event.peek(eventtype=None), bool)
self.assertIsInstance(
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN]), bool
)
self.assertIsInstance(
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False), bool
)

class Foo:
pass
Expand Down
Loading