diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ccdddb..37b39fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 > breaking changes may be introduced > at any time without warning. +## [0.1.9] - 2024-06-07 + +### Fixed + +- Remove some methods or other constructs that were added in Python 3.12, + to ensure compatibility with Python 3.11. + ## [0.1.8] - 2024-06-05 ### Fixed diff --git a/src/libretro/core.py b/src/libretro/core.py index 0e24cac..39758e3 100644 --- a/src/libretro/core.py +++ b/src/libretro/core.py @@ -38,6 +38,8 @@ # TODO: Add a CorePhase enum that's updated when entering/leaving each phase. # (Some envcalls can only be called in certain phases, so this would be useful for error checking.) +_REGION_MEMBERS = Region.__members__.values() + class CoreInterface(Protocol): """ @@ -639,7 +641,7 @@ def get_region(self) -> Region | int: or as a plain ``int`` if not. """ region: int = self._core.retro_get_region() - return Region(region) if region in Region else region + return Region(region) if region in _REGION_MEMBERS else region def get_memory_data(self, id: int) -> c_void_p | None: """ diff --git a/src/libretro/drivers/environment/dict.py b/src/libretro/drivers/environment/dict.py index 85ab0dc..be95c05 100644 --- a/src/libretro/drivers/environment/dict.py +++ b/src/libretro/drivers/environment/dict.py @@ -10,6 +10,8 @@ EnvironmentCallbackFunction = Callable[[c_void_p], bool] +_ENVCALL_MEMBERS = EnvironmentCall.__members__.values() + class DictEnvironmentDriver( EnvironmentDriver, Mapping[EnvironmentCall, EnvironmentCallbackFunction] @@ -33,7 +35,7 @@ def __iter__(self) -> KeysView[EnvironmentCall]: @override def environment(self, cmd: int, data: c_void_p) -> bool: - if cmd not in EnvironmentCall: + if cmd not in _ENVCALL_MEMBERS: return False envcall = EnvironmentCall(cmd) diff --git a/src/libretro/drivers/input/generator.py b/src/libretro/drivers/input/generator.py index b168a62..e17df8e 100644 --- a/src/libretro/drivers/input/generator.py +++ b/src/libretro/drivers/input/generator.py @@ -31,6 +31,14 @@ from .driver import InputDriver +# Needed for Python 3.11 compatibility, +# as "int() in MyIntEnumSubclass" wasn't available until Python 3.12 +_DEVICEID_ANALOG_MEMBERS = DeviceIdAnalog.__members__.values() +_DEVICEID_JOYPAD_MEMBERS = DeviceIdJoypad.__members__.values() +_DEVICEID_LIGHTGUN_MEMBERS = DeviceIdLightgun.__members__.values() +_DEVICEID_MOUSE_MEMBERS = DeviceIdMouse.__members__.values() +_KEY_MEMBERS = Key.__members__.values() + @dataclass(order=True, slots=True) class Point: @@ -279,7 +287,7 @@ def _lookup_port_state( InputDevice.JOYPAD, _, id, - ) if id in DeviceIdJoypad: + ) if id in _DEVICEID_JOYPAD_MEMBERS: # When asking for a specific joypad button, # return 1 (True) if its pressed and 0 (False) if not # NOTE: id in DeviceInJoypad is perfectly valid @@ -327,7 +335,7 @@ def _lookup_port_state( InputDevice.ANALOG, DeviceIndexAnalog.LEFT, id, - ) if (id in DeviceIdAnalog): + ) if id in _DEVICEID_ANALOG_MEMBERS: analog_state: AnalogState return analog_state.lstick[id] case ( @@ -335,7 +343,7 @@ def _lookup_port_state( InputDevice.ANALOG, DeviceIndexAnalog.RIGHT, id, - ) if (id in DeviceIdAnalog): + ) if id in _DEVICEID_ANALOG_MEMBERS: analog_state: AnalogState return analog_state.rstick[id] case AnalogState(), _, _, _: @@ -349,7 +357,7 @@ def _lookup_port_state( InputDevice.MOUSE, _, id, - ) if id in DeviceIdMouse: + ) if id in _DEVICEID_MOUSE_MEMBERS: # When asking for a specific mouse button, # return 1 (True) if its pressed and 0 (False) if not mouse_state: MouseState @@ -382,7 +390,7 @@ def _lookup_port_state( InputDevice.KEYBOARD, _, id, - ) if id in Key: + ) if id in _KEY_MEMBERS: # KeyboardState overloads __getitem__ to return True for pressed keys # and False for unpressed or invalid keys. return keyboard_state[id] @@ -391,7 +399,7 @@ def _lookup_port_state( return 0 # Yielding a Key value will expose it as a key press on the keyboard device. - case Key(key), InputDevice.KEYBOARD, _, id if key == id and id in Key: + case Key(key), InputDevice.KEYBOARD, _, id if key == id and id in _KEY_MEMBERS: return 1 case Key(_), _, _, _: # When yielding a Key in all other cases, return 0 return 0 @@ -400,7 +408,7 @@ def _lookup_port_state( # with all other devices defaulting to 0. # Index is ignored. case LightGunState() as light_gun_state, InputDevice.LIGHTGUN, _, id if ( - id in DeviceIdLightgun + id in _DEVICEID_LIGHTGUN_MEMBERS ): light_gun_state: LightGunState return light_gun_state[id] diff --git a/src/libretro/drivers/vfs/interface.py b/src/libretro/drivers/vfs/interface.py index 02c6e43..c92eea3 100644 --- a/src/libretro/drivers/vfs/interface.py +++ b/src/libretro/drivers/vfs/interface.py @@ -35,6 +35,9 @@ retro_vfs_write_t, ) +_VFS_SEEK_POSITION_MEMBERS = VfsSeekPosition.__members__.values() +_VFS_MKDIR_RESULT_MEMBERS = VfsMkdirResult.__members__.values() + @runtime_checkable class FileHandle(Protocol): @@ -292,7 +295,7 @@ def __seek(self, stream: POINTER(retro_vfs_file_handle), offset: int, whence: in assert isinstance(offset, int) assert isinstance(whence, int) - if whence not in VfsSeekPosition: + if whence not in _VFS_SEEK_POSITION_MEMBERS: return -1 try: @@ -473,7 +476,7 @@ def __mkdir(self, path: bytes) -> int: try: ok = self.mkdir(path) - if ok not in VfsMkdirResult: + if ok not in _VFS_MKDIR_RESULT_MEMBERS: raise TypeError( f"Expected mkdir to return a VfsMkdirResult, got: {type(ok).__name__}" ) diff --git a/src/libretro/drivers/video/opengl/moderngl.py b/src/libretro/drivers/video/opengl/moderngl.py index 2c0031d..e415792 100644 --- a/src/libretro/drivers/video/opengl/moderngl.py +++ b/src/libretro/drivers/video/opengl/moderngl.py @@ -5,7 +5,7 @@ from copy import deepcopy from importlib import resources from sys import modules -from typing import final, override +from typing import final import moderngl @@ -30,6 +30,7 @@ ) from OpenGL import GL +from libretro._typing import override from libretro.api.av import retro_game_geometry, retro_system_av_info from libretro.api.video import ( HardwareContext,