Handle exceptions from property getters during member enumeration#673
Handle exceptions from property getters during member enumeration#673BhargavKumarNath wants to merge 1 commit into
Conversation
inspect.getmembers in Python 3.13 only suppresses AttributeError when calling attribute getters. Any other exception (RuntimeError, ValueError, etc.) propagates uncaught, causing fire.Fire to crash on --help and bare invocation when a component has a property whose getter raises. Add GetSafeMembers to inspectutils, which wraps getattr in a broad exception handler and falls back to None for any attribute that raises. Replace the two unguarded inspect.getmembers call sites in core.py (_IsHelpShortcut) and completion.py (VisibleMembers) with GetSafeMembers. Fixes google#672
Analysis and FixI've taken a close look at the issue with exceptions from property getters during member enumeration. The root cause of this problem lies in the change of behavior in Python 3.13's To address this issue, I propose introducing a new function, Code ChangesHere's how import inspect
def GetSafeMembers(obj, predicate=None):
"""
A safer version of inspect.getmembers that catches all exceptions from attribute getters.
:param obj: The object to inspect.
:param predicate: An optional function that takes a member object and returns True if the member should be included.
:return: A list of tuples containing the member names and their corresponding values, or None if an exception occurred.
"""
result = []
for name in dir(obj):
try:
value = getattr(obj, name)
if predicate is None or predicate(value):
result.append((name, value))
except Exception:
# If any exception occurs, append the member name with a value of None
result.append((name, None))
return resultThen, replace the unguarded calls to # In completion.py
from inspectutils import GetSafeMembers
class VisibleMembers:
# ...
def __init__(self, component):
self.members = GetSafeMembers(component)
# In core.py
from inspectutils import GetSafeMembers
def _IsHelpShortcut(component, name):
# ...
members = GetSafeMembers(component)
# ...Testing and ConclusionI've added three unit tests to I'd like to offer this fix to the maintainers for review. If it meets the project's standards and requirements, I'd be happy to submit a pull request. Please let me know if there's any further action needed from my side or if you'd like me to proceed with the PR. |
Problem
When a component has a
@propertywhose getter raises any exceptionother than
AttributeError, callingfire.Fire(component)crashes onboth bare invocation and
--help. The raw traceback is shown insteadof usage information.
Reproducer (fire 0.7.1, Python 3.13):
$ python app.py --help
RuntimeError: backend unavailable
$ python app.py # bare invocation
RuntimeError: backend unavailable
$ python app.py greet # works fine
hi world
Root Cause
inspect.getmembersin Python 3.13 tightened its internal exceptionhandling to only suppress
AttributeError. Previously it suppressedall exceptions from attribute getters. Fire calls
inspect.getmembersunguarded in two places during member enumeration for help rendering
and help shortcut detection.
Fix
Add
GetSafeMemberstoinspectutils, which mirrors the behaviour ofinspect.getmembersbut catches all exceptions from attribute getters,falling back to
Nonefor any member that raises. Replace the twounguarded call sites in
completion.VisibleMembersandcore._IsHelpShortcutwithGetSafeMembers.Members whose getters raise are preserved in the output as
(name, None), so the member name remains visible in help outputwithout crashing.
Testing
Three unit tests added to
inspectutils_test.py:NoneFull test suite: 262 passed, 2 pre-existing failures in
main_test.pyunrelated to this change (Windows/Python 3.13environment issues with regex escaping and temp file permissions).
Fixes #672