From df2df301e292d213d664e3ee44e14706821453e7 Mon Sep 17 00:00:00 2001 From: SeongWoo Chung <8859157+geesecross@users.noreply.github.com> Date: Mon, 10 Feb 2025 02:31:20 +0900 Subject: [PATCH] macOS: handle `HIDError` in `hidapi.hidapi_impl._match()` (#2804) * Fix: handle `HIDError` in `hidapi.hidapi_impl._match()` The `open_path()` function may raise `HIDError` but `_match()`, its caller, does not handle it, unlike other cases after opening the path. This affects to the device enumeration process in `hidapi.enumerate()`, causing some devices to be randomly undiscovered. * hidapi: revert to independent checking of long and short HID++ features with an extensible refactor * Refactor: too long line --- lib/hidapi/hidapi_impl.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/hidapi/hidapi_impl.py b/lib/hidapi/hidapi_impl.py index 044530a2..978d34fc 100644 --- a/lib/hidapi/hidapi_impl.py +++ b/lib/hidapi/hidapi_impl.py @@ -255,20 +255,30 @@ def _match( device["hidpp_short"] = False device["hidpp_long"] = False device_handle = None + + def check_hidpp_short(): + report = _get_input_report(device_handle, 0x10, 32) + if len(report) == 1 + 6 and report[0] == 0x10: + device["hidpp_short"] = True + + def check_hidpp_long(): + report = _get_input_report(device_handle, 0x11, 32) + if len(report) == 1 + 19 and report[0] == 0x11: + device["hidpp_long"] = True + try: device_handle = open_path(device["path"]) - try: - report = _get_input_report(device_handle, 0x10, 32) - if len(report) == 1 + 6 and report[0] == 0x10: - device["hidpp_short"] = True - except HIDError as e: - logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}") - try: - report = _get_input_report(device_handle, 0x11, 32) - if len(report) == 1 + 19 and report[0] == 0x11: - device["hidpp_long"] = True - except HIDError as e: - logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}") + + for check_func in (check_hidpp_short, check_hidpp_long): + try: + check_func() + except HIDError as e: + logger.info( + f"Error while {check_func.__name__}" + f"on device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}" + ) + except HIDError as e: + logger.info(f"Error opening device {device['path']} ({bus_id}/{vid:04X}/{pid:04X}) for hidpp check: {e}") finally: if device_handle: close(device_handle)