Handle exceptions from property getters during member enumeration#673
Open
BhargavKumarNath wants to merge 1 commit into
Open
Handle exceptions from property getters during member enumeration#673BhargavKumarNath wants to merge 1 commit into
BhargavKumarNath 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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