Beispiel #1
0
    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 descriptor objects
           instead of instance members in some cases.  See the
           documentation for details.
        """
        instance_result = _sentinel
        if not _is_type(obj):
            _member = types.MemberDescriptorType
            klass = _typeof(obj)
            dict_attr = _shadowed_dict(klass)
            if dict_attr is _sentinel or isinstance(dict_attr, _member):
                instance_result = _check_instance(obj, attr)
        else:
            klass = obj

        klass_result = _check_class(klass, attr)

        if (instance_result is not _sentinel and
                klass_result is not _sentinel and
                _is_descriptor(klass_result)):
            return klass_result
        elif instance_result is not _sentinel:
            return instance_result
        elif klass_result is not _sentinel:
            return klass_result
        if obj is klass:
            if isinstance(obj, type):
                # for types we check the metaclass too
                meta_result = _check_class(type(klass), attr)
                if meta_result is not _sentinel:
                    return meta_result
            elif attr == '__name__':
                try:
                    return klass.__name__
                except AttributeError:
                    pass
        if default is not _sentinel:
            return default
        else:
            print('>>>', default)
            raise AttributeError(attr)
Beispiel #2
0
    def _static_getmro(klass):
        '''Get a reasonable method resolution order of a class.

        Works well for both old-style and new-style classes.

        '''
        try:
            old_class_type = types.ClassType
        except AttributeError:
            # Python 3.1 lacks both getattr_static and ClassType
            class ClassType(type):
                '''Impossible class type.'''
            old_class_type = ClassType
        if isinstance(klass, type):
            return type.__dict__['__mro__'].__get__(klass)
        elif isinstance(klass, old_class_type):
            res = []
            _safe_search_bases(klass, res)
            return res
        else:
            msg = "doesn't apply to '%s' object"
            raise TypeError(msg % _typeof(klass).__name__)