Esempio n. 1
0
def safe_get_attribute(obj, attr):
    """Gets attributes without triggering descriptors on new-style clases"""
    if is_new_style(obj):
        with AttrCleaner(obj):
            result = safe_get_attribute_new_style(obj, attr)
            if isinstance(result, member_descriptor):
                # will either be the same slot descriptor or the value
                return getattr(obj, attr)
            return result
    return getattr(obj, attr)
Esempio n. 2
0
def safe_get_attribute(obj, attr):
    """Gets attributes without triggering descriptors on new-style clases"""
    if is_new_style(obj):
        with AttrCleaner(obj):
            result = safe_get_attribute_new_style(obj, attr)
            if isinstance(result, member_descriptor):
                # will either be the same slot descriptor or the value
                return getattr(obj, attr)
            return result
    return getattr(obj, attr)
Esempio n. 3
0
 def test_is_new_style_py3(self):
     self.assertTrue(inspection.is_new_style(spam))
     self.assertTrue(inspection.is_new_style(Noncallable))
     self.assertTrue(inspection.is_new_style(OldNoncallable))
     self.assertTrue(inspection.is_new_style(Noncallable()))
     self.assertTrue(inspection.is_new_style(OldNoncallable()))
     self.assertTrue(inspection.is_new_style(None))
Esempio n. 4
0
 def test_is_new_style_py3(self):
     self.assertTrue(inspection.is_new_style(spam))
     self.assertTrue(inspection.is_new_style(Noncallable))
     self.assertTrue(inspection.is_new_style(OldNoncallable))
     self.assertTrue(inspection.is_new_style(Noncallable()))
     self.assertTrue(inspection.is_new_style(OldNoncallable()))
     self.assertTrue(inspection.is_new_style(None))
Esempio n. 5
0
def safe_get_attribute_new_style(obj, attr):
    """Returns approximately the attribute returned by getattr(obj, attr)

    The object returned ought to be callable if getattr(obj, attr) was.
    Fake callable objects may be returned instead, in order to avoid executing
    arbitrary code in descriptors.

    If the object is an instance of a class that uses __slots__, will return
    the member_descriptor object instead of the value.
    """
    if not is_new_style(obj):
        raise ValueError("%r is not a new-style class or object" % obj)
    to_look_through = (obj.__mro__ if isclass(obj) else
                       (obj, ) + type(obj).__mro__)

    for cls in to_look_through:
        if hasattr(cls, '__dict__') and attr in cls.__dict__:
            return cls.__dict__[attr]

    raise AttributeError()
Esempio n. 6
0
def safe_get_attribute_new_style(obj, attr):
    """Returns approximately the attribute returned by getattr(obj, attr)

    The object returned ought to be callable if getattr(obj, attr) was.
    Fake callable objects may be returned instead, in order to avoid executing
    arbitrary code in descriptors.

    If the object is an instance of a class that uses __slots__, will return
    the member_descriptor object instead of the value.
    """
    if not is_new_style(obj):
        raise ValueError("%r is not a new-style class or object" % obj)
    to_look_through = (obj.__mro__
                       if inspect.isclass(obj)
                       else (obj,) + type(obj).__mro__)

    for cls in to_look_through:
        if hasattr(cls, '__dict__') and attr in cls.__dict__:
            return cls.__dict__[attr]

    raise AttributeError()