Fix FanoutCache.__getattr__ breaking hasattr() with AssertionError - #374
Open
cristianchiriac wants to merge 1 commit into
Open
cristianchiriac wants to merge 1 commit into
cristianchiriac wants to merge 1 commit into
Conversation
…getattr__ hasattr() only treats AttributeError as "attribute not found"; any other exception propagates. FanoutCache.__getattr__ used a bare assert for an unknown attribute name, so hasattr(cache, name) raised instead of returning False for anything outside the known settings, breaking tools that probe attributes with hasattr() (e.g. pympler's asizeof). Fixes grantjenks#353
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.
Fixes #353.
Bug
FanoutCache.__getattr__uses a bareassertto reject unknown attribute names:hasattr()only swallowsAttributeError; any other exception propagates. Sohasattr(FanoutCache(), 'anything_unknown')raisesAssertionErrorinstead of returningFalse. This breaks code that probes objects withhasattr(), such as pympler'sasizeof, which crashed the pympler debug toolbar panel when it checkedhasattr(cache, '__slots__').Fix
Raise
AttributeErrorinstead of asserting, which is what__getattr__is expected to raise for an unknown attribute per the data model, and is whathasattr()actually checks for.Test plan
test_getattr_invalid_name_raises_attribute_errortotests/test_fanout.py, asserting both that accessing an invalid name raisesAttributeErrorand thathasattr()returnsFalserather than raising.AssertionErrorinstead ofAttributeError) and passes with the fix.pytest tests/test_fanout.py: 43 passed.pytest tests/(excluding the Django-backend tests, sincedjangoisn't installed in this environment): 137 passed.