from inspect import getmro _sentinel = object() class _foo(object): __slots__ = ['foo'] slot_descriptor = type(_foo.foo) attribute_descriptor = type(type(open(__file__)).name) descriptor_types = (slot_descriptor, attribute_descriptor) def _check_instance(obj, attr): try: instance_dict = object.__getattribute__(obj, "__dict__") except AttributeError: # C types may not have a __dict__ or a __slots__ slots = getattr(obj, '__slots__', _sentinel) if slots is not _sentinel and attr in slots: try: # attempt to fetch the slot, we could use type(obj) # here, which would bypass __class__ proxying, but # we use obj.__class__ elsewhere *anyway* and # better to have consistent behaviour for objects # that lie about __class__ desc = getattr_static(obj.__class__, attr) except AttributeError: # this code path is only possible if __slots__ has # been tampered with, harmless anyway pass else: try: return desc.__get__(obj) except AttributeError: # arguably incorrect - as the instance member is # not set on this object return desc else: if attr in instance_dict: return instance_dict[attr] return _sentinel def getattr_static(obj, attr, default=_sentinel): """Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__. Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that getattr can't (like descriptors that raise AttributeError). It can also return descriptors instead of instance members in some cases. See the documentation for details. """ if not isinstance(obj, type): result = _check_instance(obj, attr) if result is not _sentinel: return result klass = obj.__class__ else: klass = obj for entry in getmro(klass): try: result = entry.__dict__[attr] except KeyError: pass else: if (klass is not obj and isinstance(result, descriptor_types)): try: return result.__get__(obj) except AttributeError: pass return result if obj is klass: # for types we check the metaclass too for entry in getmro(type(klass)): try: return entry.__dict__[attr] except KeyError: pass if default is not _sentinel: return default raise AttributeError(attr)