Repository navigation
Fix UnboundLocalError in hub creators when import fails non-trivially - #14985
Conversation
| except EnvironmentError as e: | ||
| log.w(f"unifi hub init failed: {e}") | ||
| return None | ||
| except BaseException as e: | ||
| log.w(f"unifi hub init failed: {e}") |
There was a problem hiding this comment.
UniFiSwitch() can't throw EnvironmentError nor BaseException, we can remove it or simplify to except Exception as e to exit cleanly
There was a problem hiding this comment.
Good catch — UniFiSwitch.__init__ only raises NoneFoundError. The EnvironmentError is raised at module import time (unifi.py:49 when UNIFI_SSH_PASSWORD is unset), so it's now caught by the import block. Removed the redundant except EnvironmentError / except BaseException after construction and kept a single except Exception as e that warns. Fixed in a5d5af9.
| return acroname.Acroname() | ||
| except ModuleNotFoundError: | ||
| return None | ||
| except acroname.NoneFoundError: |
There was a problem hiding this comment.
we have a similar bug here, if the import fails and is not caught by ModuleNotFoundError this will throw
There was a problem hiding this comment.
Right — _create_acroname had the same latent bug. Applied the same import/construction split: import failure other than ModuleNotFoundError now gets logged and returns None instead of falling through to except acroname.NoneFoundError and crashing on the unbound name. Fixed in a5d5af9.
| return None | ||
| except BaseException: | ||
| return None # no hub connected | ||
| except BaseException as e: |
There was a problem hiding this comment.
Let's validate we need BaseException and not Exception here - relevant for all uses
There was a problem hiding this comment.
Agreed — Exception is the right base here. BaseException would also swallow KeyboardInterrupt / SystemExit / GeneratorExit, which we don't want. Switched all BaseException to Exception across the three creators. Fixed in a5d5af9.
…eption to Exception, drop redundant EnvironmentError in _create_unifi
Summary
In `unit-tests/py/rspy/device_hub.py`, both `_create_ykush()` and `_create_unifi()` had a latent bug: the `except .NoneFoundError` clause references the locally-imported module name. If the `from rspy import ` line raises any exception other than `ModuleNotFoundError`, that exception falls through to the `except .NoneFoundError` clause — Python evaluates the unbound name and raises `UnboundLocalError`, masking the real error.
This was hit on Python 3.14 / Windows: paramiko 3.5.1's import-time `importlib.metadata.version("paramiko")` triggers a `TypeError` due to a 3.14 stdlib regression. The TypeError propagates out of `from rspy import unifi`, hits `except unifi.NoneFoundError:`, and crashes pytest collection with `UnboundLocalError` instead of returning `None` like the function intends.
Fix
Split the import from the construction:
This matches the original intent (silently return None when the hub isn't usable) while no longer masking unexpected errors and no longer hitting `UnboundLocalError` when the import fails non-trivially.
Test plan