Skip to content

Fix FanoutCache.__getattr__ breaking hasattr() with AssertionError - #374

Open
cristianchiriac wants to merge 1 commit into
grantjenks:masterfrom
cristianchiriac:fix/fanout-getattr-attributeerror
Open

cristianchiriac wants to merge 1 commit into
grantjenks:masterfrom
cristianchiriac:fix/fanout-getattr-attributeerror

Conversation

@cristianchiriac

Copy link
Copy Markdown

Fixes #353.

Bug

FanoutCache.__getattr__ uses a bare assert to reject unknown attribute names:

def __getattr__(self, name):
    safe_names = {'timeout', 'disk'}
    valid_name = name in DEFAULT_SETTINGS or name in safe_names
    assert valid_name, 'cannot access {} in cache shard'.format(name)
    return getattr(self._shards[0], name)

hasattr() only swallows AttributeError; any other exception propagates. So hasattr(FanoutCache(), 'anything_unknown') raises AssertionError instead of returning False. This breaks code that probes objects with hasattr(), such as pympler's asizeof, which crashed the pympler debug toolbar panel when it checked hasattr(cache, '__slots__').

Fix

Raise AttributeError instead of asserting, which is what __getattr__ is expected to raise for an unknown attribute per the data model, and is what hasattr() actually checks for.

Test plan

  • Added test_getattr_invalid_name_raises_attribute_error to tests/test_fanout.py, asserting both that accessing an invalid name raises AttributeError and that hasattr() returns False rather than raising.
  • Verified the new test fails against the unmodified code (AssertionError instead of AttributeError) and passes with the fix.
  • pytest tests/test_fanout.py: 43 passed.
  • pytest tests/ (excluding the Django-backend tests, since django isn't installed in this environment): 137 passed.

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssertionError on hasattr(FanoutCache(), 'foo')

1 participant