diff --git a/buildconfig/stubs/pygame/_event.pyi b/buildconfig/stubs/pygame/_event.pyi index 2ba5a06637..bf8fdf87a0 100644 --- a/buildconfig/stubs/pygame/_event.pyi +++ b/buildconfig/stubs/pygame/_event.pyi @@ -1,31 +1,24 @@ -from typing import ( - Any, - List, - Optional, - Union, - Type, -) +from __future__ import annotations from ._common import Sequence, EventLike -_EventTypes = Union[int, Sequence[int]] +_EventTypes = int | Sequence[int] def pump(dopump: bool, /) -> None: ... def get( - eventtype: Optional[_EventTypes] = None, - pump: Any = True, - exclude: Optional[_EventTypes] = None, -) -> List[EventLike]: ... + eventtype: _EventTypes | None = None, + pump: bool = True, + exclude: _EventTypes | None = None, +) -> list[EventLike]: ... def poll() -> EventLike: ... def wait(timeout: int = 0) -> EventLike: ... -def peek(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> bool: ... -def clear(eventtype: Optional[_EventTypes] = None, pump: Any = True) -> None: ... -def set_blocked(type: Optional[_EventTypes], /) -> None: ... -def set_allowed(type: Optional[_EventTypes], /) -> None: ... -def get_blocked(type: _EventTypes, /) -> bool: ... +def peek(eventtype: _EventTypes | None = None, pump: bool = True) -> bool: ... +def clear(eventtype: _EventTypes | None = None, pump: bool = True) -> None: ... def set_grab(grab: bool, /) -> None: ... def get_grab() -> bool: ... def allowed_get(type: int, /) -> bool: ... -def allowed_set(type: int, val: bool, /): ... +def allowed_set(type: int, val: bool, /) -> None: ... def post(event: EventLike, /) -> bool: ... -def register_event_class(cls: Type[EventLike]): ... +def register_event_class(cls: type[EventLike]) -> None: ... +def _internal_mod_init() -> None: ... +def _internal_mod_quit() -> None: ... diff --git a/buildconfig/stubs/pygame/event.pyi b/buildconfig/stubs/pygame/event.pyi index 7a73c6ea6c..38ba2462a4 100644 --- a/buildconfig/stubs/pygame/event.pyi +++ b/buildconfig/stubs/pygame/event.pyi @@ -1,37 +1,33 @@ -from typing import ( - Any, - Dict, - List, - Optional, - Union, -) +from __future__ import annotations + +from typing import Any from ._common import Sequence, EventLike class Event(EventLike): ... -_EventTypes = Union[int, Sequence[int]] +_EventTypes = int | Sequence[int] def pump() -> None: ... def get( - eventtype: Optional[_EventTypes] = None, + eventtype: _EventTypes | None = None, pump: Any = True, - exclude: Optional[_EventTypes] = None, -) -> List[Event]: ... + exclude: _EventTypes | None = 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: _EventTypes | None = None, pump: Any = True) -> bool: ... +def clear(eventtype: _EventTypes | None = None, pump: Any = True) -> None: ... def event_name(type: int) -> str: ... -def set_blocked(type: Optional[_EventTypes], /) -> None: ... -def set_allowed(type: Optional[_EventTypes], /) -> None: ... +def set_blocked(type: _EventTypes | None, /) -> None: ... +def set_allowed(type: _EventTypes | None, /) -> None: ... def get_blocked(type: _EventTypes, /) -> bool: ... def set_grab(grab: bool, /) -> None: ... def get_grab() -> bool: ... def post(event: Event, /) -> bool: ... def custom_type() -> int: ... -def init(): ... -def quit(): ... +def init() -> None: ... +def quit() -> None: ... EventType = Event diff --git a/src_c/_event.c b/src_c/_event.c index 5c9246d1ab..8b3936a5eb 100644 --- a/src_c/_event.c +++ b/src_c/_event.c @@ -1878,42 +1878,11 @@ pg_event_post(PyObject *self, PyObject *obj) } } -// TODO static PyObject * -pg_event_set_allowed(PyObject *self, PyObject *obj) +pg_event_allowed_set(PyObject *self, PyObject *args) { - Py_ssize_t len; - int loop, type; - PyObject *seq; VIDEO_INIT_CHECK(); - if (obj == Py_None) { - int i; - for (i = SDL_FIRSTEVENT; i < SDL_LASTEVENT; i++) { - PG_SetEventEnabled(i, SDL_TRUE); - } - } - else { - seq = _pg_eventtype_as_seq(obj, &len); - if (!seq) - return NULL; - - for (loop = 0; loop < len; loop++) { - type = _pg_eventtype_from_seq(seq, loop); - if (type == -1) { - Py_DECREF(seq); - return NULL; - } - PG_SetEventEnabled(_pg_pgevent_proxify(type), SDL_TRUE); - } - Py_DECREF(seq); - } - Py_RETURN_NONE; -} - -static PyObject * -pg_event_allowed_set(PyObject *self, PyObject *args) -{ int e_type, e_flag; PyObject *e_flago; @@ -1946,6 +1915,8 @@ pg_event_allowed_set(PyObject *self, PyObject *args) static PyObject * pg_event_allowed_get(PyObject *self, PyObject *obj) { + VIDEO_INIT_CHECK(); + int e_type = PyLong_AsLong(obj); if (PyErr_Occurred()) @@ -1961,74 +1932,6 @@ pg_event_allowed_get(PyObject *self, PyObject *obj) Py_RETURN_FALSE; } -// TODO -static PyObject * -pg_event_set_blocked(PyObject *self, PyObject *obj) -{ - Py_ssize_t len; - int loop, type; - PyObject *seq; - VIDEO_INIT_CHECK(); - - if (obj == Py_None) { - int i; - /* Start at PGPOST_EVENTBEGIN */ - for (i = PGPOST_EVENTBEGIN; i < SDL_LASTEVENT; i++) { - PG_SetEventEnabled(i, SDL_FALSE); - } - } - else { - seq = _pg_eventtype_as_seq(obj, &len); - if (!seq) - return NULL; - - for (loop = 0; loop < len; loop++) { - type = _pg_eventtype_from_seq(seq, loop); - if (type == -1) { - Py_DECREF(seq); - return NULL; - } - PG_SetEventEnabled(_pg_pgevent_proxify(type), SDL_FALSE); - } - Py_DECREF(seq); - } - /* Never block SDL_WINDOWEVENT, we need them for translation */ - PG_SetEventEnabled(SDL_WINDOWEVENT, SDL_TRUE); - /* Never block PGE_KEYREPEAT too, its needed for pygame internal use */ - PG_SetEventEnabled(PGE_KEYREPEAT, SDL_TRUE); - Py_RETURN_NONE; -} - -// TODO -static PyObject * -pg_event_get_blocked(PyObject *self, PyObject *obj) -{ - Py_ssize_t len; - int loop, type, isblocked = 0; - PyObject *seq; - - VIDEO_INIT_CHECK(); - - seq = _pg_eventtype_as_seq(obj, &len); - if (!seq) - return NULL; - - for (loop = 0; loop < len; loop++) { - type = _pg_eventtype_from_seq(seq, loop); - if (type == -1) { - Py_DECREF(seq); - return NULL; - } - if (PG_EventEnabled(_pg_pgevent_proxify(type)) == SDL_FALSE) { - isblocked = 1; - break; - } - } - - Py_DECREF(seq); - return PyBool_FromLong(isblocked); -} - static PyObject * pg_event_register_event_class(PyObject *self, PyObject *obj) { @@ -2069,12 +1972,6 @@ static PyMethodDef _event_methods[] = { DOC_EVENT_PEEK}, {"post", (PyCFunction)pg_event_post, METH_O, DOC_EVENT_POST}, - {"set_allowed", (PyCFunction)pg_event_set_allowed, METH_O, - DOC_EVENT_SETALLOWED}, - {"set_blocked", (PyCFunction)pg_event_set_blocked, METH_O, - DOC_EVENT_SETBLOCKED}, - {"get_blocked", (PyCFunction)pg_event_get_blocked, METH_O, - DOC_EVENT_GETBLOCKED}, {"allowed_get", (PyCFunction)pg_event_allowed_get, METH_O}, {"allowed_set", (PyCFunction)pg_event_allowed_set, METH_VARARGS}, {"register_event_class", (PyCFunction)pg_event_register_event_class, diff --git a/src_py/event.py b/src_py/event.py index 21ad13cb74..24692efc90 100644 --- a/src_py/event.py +++ b/src_py/event.py @@ -1,18 +1,31 @@ from __future__ import annotations -from pygame._event import * # pylint: disable=wildcard-import,unused-wildcard-import; lgtm[py/polluting-import] +from collections.abc import Iterable +from typing import Any + from pygame._event import ( _internal_mod_init as _init, _internal_mod_quit as _quit, pump as _pump, + register_event_class as _register_event_class, + allowed_get as _allowed_get, + allowed_set as _allowed_set, + post, + get_grab, + set_grab, + clear, + peek, + wait, + poll, + get, ) -from pygame.constants import USEREVENT, NUMEVENTS + from pygame.base import error import pygame as pg _is_init = False -_custom_event = USEREVENT + 1 +_custom_event = pg.USEREVENT + 1 _NAMES_MAPPING = { pg.ACTIVEEVENT: "ActiveEvent", pg.APP_TERMINATING: "AppTerminating", @@ -92,7 +105,7 @@ def event_name(type: int) -> str: if type in _NAMES_MAPPING: return _NAMES_MAPPING[type] - if USEREVENT <= type < NUMEVENTS: + if pg.USEREVENT <= type < pg.NUMEVENTS: return "UserEvent" return "Unknown" @@ -104,14 +117,11 @@ class Event: pygame object for representing events """ - type: int - dict: dict[str, ...] - - def __init__(self, type: int, dict: dict[str, ...] | None = None, **kwargs): + def __init__(self, type: int, dict: dict[str, Any] | None = None, **kwargs: Any): if not isinstance(type, int): raise TypeError("event type must be an integer") - if not 0 <= type < NUMEVENTS: + if not 0 <= type < pg.NUMEVENTS: raise ValueError("event type out of range") dict = dict if dict is not None else {} @@ -123,7 +133,7 @@ def __init__(self, type: int, dict: dict[str, ...] | None = None, **kwargs): self._type = type self._dict = dict - def __new__(cls, *args, **kwargs): + def __new__(cls, *args: Any, **kwargs: Any): if "type" in kwargs: raise ValueError("redundant type field in event dict") return super().__new__(cls) @@ -134,7 +144,7 @@ def __int__(self): def __bool__(self): return self.type != pg.NOEVENT - def __eq__(self, other: Event): + def __eq__(self, other: Any): if not isinstance(other, Event): return NotImplemented return self.type == other.type and self.dict == other.dict @@ -150,23 +160,26 @@ def dict(self): def type(self): return self._type - def __getattr__(self, name): + def __getattr__(self, name: str) -> Any: return self._dict[name] - def __getattribute__(self, name): + def __getattribute__(self, name: str): if name == "__dict__": return super().__getattribute__("_dict") return super().__getattribute__(name) - def __setattr__(self, name, value): + def __setattr__(self, name: str, value: Any): if name in ("_type", "_dict", "type", "dict"): super().__setattr__(name, value) else: self._dict[name] = value + def __delattr__(self, name: str) -> None: + del self._dict[name] + EventType = Event -register_event_class(Event) +_register_event_class(Event) def init(): @@ -184,7 +197,7 @@ def quit(): # so we can have a unit test that checks if pygame.event.custom_type() stops # returning new types when they are finished, # without that test preventing further tests from getting a custom event type. - _custom_event = USEREVENT + 1 + _custom_event = pg.USEREVENT + 1 _quit() _is_init = False @@ -194,7 +207,7 @@ def custom_type(): """custom_type() -> int\nmake custom user event type""" global _custom_event - if _custom_event >= NUMEVENTS: + if _custom_event >= pg.NUMEVENTS: raise error("pygame.event.custom_type made too many event types.") _custom_event += 1 @@ -203,3 +216,72 @@ def custom_type(): def pump(): return _pump(True) + + +def _setter(val: bool, type: int | Iterable[int] | None, *args: int): + if type is None: + if args: + raise ValueError("Args aren't supported for type==None.") + for ev in range(pg.NUMEVENTS): + _allowed_set(ev, val) + return + + types: list[int] = [] + + if isinstance(type, Iterable): + types.extend(type) + else: + types.append(type) + + if args: + types.extend(args) + + for t in types: + _allowed_set(t, val) + + +def set_blocked(type: int | Iterable[int] | None, *args: int): + _setter(False, type, *args) + + +def set_allowed(type: int | Iterable[int] | None, *args: int): + _setter(True, type, *args) + + +def get_blocked(type: int | Iterable[int], *args: int): + types: list[int] = [] + + if isinstance(type, Iterable): + types.extend(type) + else: + types.append(type) + + if args: + types.extend(args) + + for t in types: + if not _allowed_get(t): + return True + return False + + +__all__ = [ + "Event", + "EventType", + "pump", + "get", + "poll", + "wait", + "peek", + "clear", + "event_name", + "set_blocked", + "set_allowed", + "get_blocked", + "set_grab", + "get_grab", + "post", + "custom_type", + "init", + "quit", +]