Skip to content

Commit f63fb7b

Browse files
committed
Accept unknown values in Scancode and KeySym enums.
Fixes a crash with the event handling.
1 parent c5514a7 commit f63fb7b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Fixed
12+
- *Scancode* and *KeySym* enums no longer crash when SDL returns an unexpected value.
1113

1214
12.7.1 - 2021-06-30
1315
-------------------

tcod/event.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,14 @@ def scancode(self) -> "Scancode":
15981598
"""
15991599
return self
16001600

1601+
@classmethod
1602+
def _missing_(cls, value: object) -> "Optional[Scancode]":
1603+
if not isinstance(value, int):
1604+
return None
1605+
result = cls(0)
1606+
result._value_ = value
1607+
return result
1608+
16011609
def __eq__(self, other: Any) -> bool:
16021610
if isinstance(other, KeySym):
16031611
raise TypeError(
@@ -2137,6 +2145,14 @@ def scancode(self) -> Scancode:
21372145
_init_sdl_video()
21382146
return Scancode(lib.SDL_GetScancodeFromKey(self.value))
21392147

2148+
@classmethod
2149+
def _missing_(cls, value: object) -> "Optional[KeySym]":
2150+
if not isinstance(value, int):
2151+
return None
2152+
result = cls(0)
2153+
result._value_ = value
2154+
return result
2155+
21402156
def __eq__(self, other: Any) -> bool:
21412157
if isinstance(other, Scancode):
21422158
raise TypeError(

0 commit comments

Comments
 (0)