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

Remove event.peek() behavior, minor doc and stub fixes #3122

Closed
Closed
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: ...
MyreMylar marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 3 additions & 1 deletion docs/reST/ref/event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ On Android, the following events can be generated

.. 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 +493,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
64 changes: 28 additions & 36 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 @@ -2258,44 +2258,36 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)

_pg_event_pump(dopump);

if (obj == NULL || obj == Py_None) {
res = PG_PEEP_EVENT_ALL(&event, 1, SDL_PEEKEVENT);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
return pgEvent_New(res ? &event : NULL);
}
else {
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
return NULL;
seq = _pg_eventtype_as_seq(obj, &len);
if (!seq)
Py_RETURN_FALSE;

for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
return NULL;
}
res = PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT, type);
if (res) {
Py_DECREF(seq);
for (loop = 0; loop < len; loop++) {
type = _pg_eventtype_from_seq(seq, loop);
if (type == -1) {
Py_DECREF(seq);
Py_RETURN_FALSE;
}
res = PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT, type);
if (res) {
Py_DECREF(seq);

if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
res = PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT,
_pg_pgevent_proxify(type));
if (res) {
Py_DECREF(seq);
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
res =
PG_PEEP_EVENT(&event, 1, SDL_PEEKEVENT, _pg_pgevent_proxify(type));
if (res) {
Py_DECREF(seq);

if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
if (res < 0)
return RAISE(pgExc_SDLError, SDL_GetError());
Py_RETURN_TRUE;
}
Py_DECREF(seq);
Py_RETURN_FALSE; /* No event type match. */
}
Py_DECREF(seq);
Py_RETURN_FALSE; /* No event type match. */
}

/* You might notice how we do event blocking stuff on proxy events and
Expand Down
4 changes: 2 additions & 2 deletions test/event_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def test_pump(self):

@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == pygame.NULL_VIDEODRIVER,
'requires the SDL_VIDEODRIVER to be a non-null value',
"requires the SDL_VIDEODRIVER to be a non-null value",
)
def test_set_grab__and_get_symmetric(self):
"""Ensure event grabbing can be enabled and disabled.
Expand Down Expand Up @@ -889,7 +889,7 @@ def test_get_blocked__event_sequence(self):

@unittest.skipIf(
os.environ.get("SDL_VIDEODRIVER") == pygame.NULL_VIDEODRIVER,
'requires the SDL_VIDEODRIVER to be a non-null value',
"requires the SDL_VIDEODRIVER to be a non-null value",
)
def test_get_grab(self):
"""Ensure get_grab() works as expected"""
Expand Down
Loading