Skip to content

Commit 56ef5c9

Browse files
authored
Merge pull request #3283 from pygame-community/ankith26-event-minor-fixes
event.peek return bool, minor doc/stub fixes
2 parents e5e2e5f + fa36460 commit 56ef5c9

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

buildconfig/stubs/pygame/event.pyi

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ from pygame.typing import SequenceLike
44

55
@final
66
class Event:
7-
type: int
7+
@property
8+
def type(self) -> int: ...
89
__dict__: dict[str, Any]
910
__hash__: None # type: ignore
1011
def __init__(
@@ -17,20 +18,21 @@ class Event:
1718

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

2224
_EventTypes = Union[int, SequenceLike[int]]
2325

2426
def pump() -> None: ...
2527
def get(
2628
eventtype: Optional[_EventTypes] = None,
27-
pump: Any = True,
29+
pump: bool = True,
2830
exclude: Optional[_EventTypes] = None,
2931
) -> list[Event]: ...
3032
def poll() -> Event: ...
3133
def wait(timeout: int = 0) -> Event: ...
32-
def peek(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> bool: ...
33-
def clear(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> None: ...
34+
def peek(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> bool: ...
35+
def clear(eventtype: Optional[_EventTypes] = None, pump: bool = True) -> None: ...
3436
def event_name(type: int, /) -> str: ...
3537
def set_blocked(type: Optional[_EventTypes], /) -> None: ...
3638
def set_allowed(type: Optional[_EventTypes], /) -> None: ...

docs/reST/ref/event.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,15 @@ On Android, the following events can be generated
324324
queue. If a sequence of event types is passed, this will return ``True`` if
325325
any of those events are on the queue.
326326

327+
When ``eventtype`` is not passed or ``None``, this function will return ``True`` if
328+
there's any event on the queue, and return ``False`` if the queue is empty.
329+
327330
If ``pump`` is ``True`` (the default), then :func:`pygame.event.pump()` will be called.
328331

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

334+
.. versionchanged:: 2.5.3 no longer mistakenly returns an event when ``eventtype`` is None or not passed.
335+
331336
.. ## pygame.event.peek ##
332337
333338
.. function:: clear
@@ -491,7 +496,7 @@ On Android, the following events can be generated
491496
492497
Read-only. The event type identifier. For user created event
493498
objects, this is the ``type`` argument passed to
494-
:func:`pygame.event.Event()`.
499+
:class:`pygame.event.Event()`.
495500

496501
For example, some predefined event identifiers are ``QUIT`` and
497502
``MOUSEMOTION``.

src_c/event.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1591,9 +1591,9 @@ static PyTypeObject pgEvent_Type;
15911591
#define OFF(x) offsetof(pgEventObject, x)
15921592

15931593
static PyMemberDef pg_event_members[] = {
1594-
{"__dict__", T_OBJECT, OFF(dict), READONLY},
1595-
{"type", T_INT, OFF(type), READONLY},
1596-
{"dict", T_OBJECT, OFF(dict), READONLY},
1594+
{"__dict__", T_OBJECT, OFF(dict), READONLY, DOC_EVENT_EVENT_DICT},
1595+
{"type", T_INT, OFF(type), READONLY, DOC_EVENT_EVENT_TYPE},
1596+
{"dict", T_OBJECT, OFF(dict), READONLY, DOC_EVENT_EVENT_DICT},
15971597
{NULL} /* Sentinel */
15981598
};
15991599

@@ -2262,7 +2262,7 @@ pg_event_peek(PyObject *self, PyObject *args, PyObject *kwargs)
22622262
res = PG_PEEP_EVENT_ALL(&event, 1, SDL_PEEKEVENT);
22632263
if (res < 0)
22642264
return RAISE(pgExc_SDLError, SDL_GetError());
2265-
return pgEvent_New(res ? &event : NULL);
2265+
return PyBool_FromLong(res);
22662266
}
22672267
else {
22682268
seq = _pg_eventtype_as_seq(obj, &len);

test/event_test.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,32 @@ def test_clear(self):
249249
self.assertRaises(ValueError, pygame.event.clear, 0x0010FFFFF)
250250
self.assertRaises(TypeError, pygame.event.get, ["a", "b", "c"])
251251

252+
def test_peek_no_arg(self):
253+
pygame.event.clear()
254+
# do a strict "is" check and not just a simple truthy-test to check that
255+
# we have a boolean instance.
256+
self.assertIs(pygame.event.peek(), False)
257+
self.assertIs(pygame.event.peek(None), False)
258+
259+
# peek should return True if there's any event on the queue
260+
pygame.event.post(pygame.event.Event(pygame.USEREVENT))
261+
self.assertIs(pygame.event.peek(), True)
262+
self.assertIs(pygame.event.peek(None), True)
263+
252264
def test_peek(self):
253-
pygame.event.peek()
254-
pygame.event.peek(None)
255-
pygame.event.peek(None, True)
256-
257-
pygame.event.peek(pump=False)
258-
pygame.event.peek(pump=True)
259-
pygame.event.peek(eventtype=None)
260-
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN])
261-
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False)
265+
self.assertIsInstance(pygame.event.peek(), bool)
266+
self.assertIsInstance(pygame.event.peek(None), bool)
267+
self.assertIsInstance(pygame.event.peek(None, True), bool)
268+
269+
self.assertIsInstance(pygame.event.peek(pump=False), bool)
270+
self.assertIsInstance(pygame.event.peek(pump=True), bool)
271+
self.assertIsInstance(pygame.event.peek(eventtype=None), bool)
272+
self.assertIsInstance(
273+
pygame.event.peek(eventtype=[pygame.KEYUP, pygame.KEYDOWN]), bool
274+
)
275+
self.assertIsInstance(
276+
pygame.event.peek(eventtype=pygame.USEREVENT, pump=False), bool
277+
)
262278

263279
class Foo:
264280
pass

0 commit comments

Comments
 (0)