Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_inspect.TestGetattrStatic Class Reference

List of all members.

Public Member Functions

def test_basic
def test_inherited
def test_instance_attr
def test_property
def test_descriptor_raises_AttributeError
def test_classAttribute
def test_inherited_classattribute
def test_slots
def test_metaclass
def test_no_dict_no_slots
def test_no_dict_no_slots_instance_member
def test_inherited_slots
def test_descriptor
def test_metaclass_with_descriptor
def test_class_as_property
def test_mro_as_property
def test_dict_as_property
def test_custom_object_dict
def test_metaclass_dict_as_property

Public Attributes

 x
 executed
 spam

Detailed Description

Definition at line 780 of file test_inspect.py.


Member Function Documentation

Definition at line 782 of file test_inspect.py.

00782 
00783     def test_basic(self):
00784         class Thing(object):
00785             x = object()
00786 
00787         thing = Thing()
00788         self.assertEqual(inspect.getattr_static(thing, 'x'), Thing.x)
00789         self.assertEqual(inspect.getattr_static(thing, 'x', None), Thing.x)
00790         with self.assertRaises(AttributeError):
00791             inspect.getattr_static(thing, 'y')
00792 
00793         self.assertEqual(inspect.getattr_static(thing, 'y', 3), 3)

Here is the call graph for this function:

Definition at line 928 of file test_inspect.py.

00928 
00929     def test_class_as_property(self):
00930         class Base(object):
00931             foo = 3
00932 
00933         class Something(Base):
00934             executed = False
00935             @property
00936             def __class__(self):
00937                 self.executed = True
00938                 return object
00939 
00940         instance = Something()
00941         self.assertEqual(inspect.getattr_static(instance, 'foo'), 3)
00942         self.assertFalse(instance.executed)
00943         self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)

Definition at line 831 of file test_inspect.py.

00831 
00832     def test_classAttribute(self):
00833         class Thing(object):
00834             x = object()
00835 
00836         self.assertEqual(inspect.getattr_static(Thing, 'x'), Thing.x)

Here is the call graph for this function:

Definition at line 975 of file test_inspect.py.

00975 
00976     def test_custom_object_dict(self):
00977         test = self
00978         test.called = False
00979 
00980         class Custom(dict):
00981             def get(self, key, default=None):
00982                 test.called = True
00983                 super().get(key, default)
00984 
00985         class Foo(object):
00986             a = 3
00987         foo = Foo()
00988         foo.__dict__ = Custom()
00989         self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
00990         self.assertFalse(test.called)

Here is the call graph for this function:

Definition at line 898 of file test_inspect.py.

00898 
00899     def test_descriptor(self):
00900         class descriptor(object):
00901             def __get__(self, instance, owner):
00902                 return 3
00903         class Foo(object):
00904             d = descriptor()
00905 
00906         foo = Foo()
00907 
00908         # for a non data descriptor we return the instance attribute
00909         foo.__dict__['d'] = 1
00910         self.assertEqual(inspect.getattr_static(foo, 'd'), 1)
00911 
00912         # if the descriptor is a data-desciptor we should return the
00913         # descriptor
00914         descriptor.__set__ = lambda s, i, v: None
00915         self.assertEqual(inspect.getattr_static(foo, 'd'), Foo.__dict__['d'])
00916 

Here is the call graph for this function:

Definition at line 821 of file test_inspect.py.

00821 
00822     def test_descriptor_raises_AttributeError(self):
00823         class descriptor(object):
00824             def __get__(*_):
00825                 raise AttributeError("I'm pretending not to exist")
00826         desc = descriptor()
00827         class Thing(object):
00828             x = desc
00829         thing = Thing()
00830         self.assertEqual(inspect.getattr_static(thing, 'x'), desc)

Here is the call graph for this function:

Definition at line 959 of file test_inspect.py.

00959 
00960     def test_dict_as_property(self):
00961         test = self
00962         test.called = False
00963 
00964         class Foo(dict):
00965             a = 3
00966             @property
00967             def __dict__(self):
00968                 test.called = True
00969                 return {}
00970 
00971         foo = Foo()
00972         foo.a = 4
00973         self.assertEqual(inspect.getattr_static(foo, 'a'), 3)
00974         self.assertFalse(test.called)

Here is the call graph for this function:

Definition at line 794 of file test_inspect.py.

00794 
00795     def test_inherited(self):
00796         class Thing(object):
00797             x = object()
00798         class OtherThing(Thing):
00799             pass
00800 
00801         something = OtherThing()
00802         self.assertEqual(inspect.getattr_static(something, 'x'), Thing.x)

Definition at line 837 of file test_inspect.py.

00837 
00838     def test_inherited_classattribute(self):
00839         class Thing(object):
00840             x = object()
00841         class OtherThing(Thing):
00842             pass
00843 
00844         self.assertEqual(inspect.getattr_static(OtherThing, 'x'), Thing.x)

Definition at line 885 of file test_inspect.py.

00885 
00886     def test_inherited_slots(self):
00887         # returns descriptor
00888         class Thing(object):
00889             __slots__ = ['x']
00890             def __init__(self):
00891                 self.x = 'foo'
00892 
00893         class OtherThing(Thing):
00894             pass
00895         # it would be nice if this worked...
00896         # we get the descriptor instead of the instance attribute
00897         self.assertEqual(inspect.getattr_static(OtherThing(), 'x'), Thing.x)

Here is the call graph for this function:

Definition at line 803 of file test_inspect.py.

00803 
00804     def test_instance_attr(self):
00805         class Thing(object):
00806             x = 2
00807             def __init__(self, x):
00808                 self.x = x
00809         thing = Thing(3)
00810         self.assertEqual(inspect.getattr_static(thing, 'x'), 3)
00811         del thing.x
00812         self.assertEqual(inspect.getattr_static(thing, 'x'), 2)

Here is the call graph for this function:

Definition at line 858 of file test_inspect.py.

00858 
00859     def test_metaclass(self):
00860         class meta(type):
00861             attr = 'foo'
00862         class Thing(object, metaclass=meta):
00863             pass
00864         self.assertEqual(inspect.getattr_static(Thing, 'attr'), 'foo')
00865 
00866         class sub(meta):
00867             pass
00868         class OtherThing(object, metaclass=sub):
00869             x = 3
00870         self.assertEqual(inspect.getattr_static(OtherThing, 'attr'), 'foo')
00871 
00872         class OtherOtherThing(OtherThing):
00873             pass
00874         # this test is odd, but it was added as it exposed a bug
00875         self.assertEqual(inspect.getattr_static(OtherOtherThing, 'x'), 3)

Here is the call graph for this function:

Definition at line 991 of file test_inspect.py.

00991 
00992     def test_metaclass_dict_as_property(self):
00993         class Meta(type):
00994             @property
00995             def __dict__(self):
00996                 self.executed = True
00997 
00998         class Thing(metaclass=Meta):
00999             executed = False
01000 
01001             def __init__(self):
01002                 self.spam = 42
01003 
01004         instance = Thing()
01005         self.assertEqual(inspect.getattr_static(instance, "spam"), 42)
01006         self.assertFalse(Thing.executed)

Here is the call graph for this function:

Definition at line 917 of file test_inspect.py.

00917 
00918     def test_metaclass_with_descriptor(self):
00919         class descriptor(object):
00920             def __get__(self, instance, owner):
00921                 return 3
00922         class meta(type):
00923             d = descriptor()
00924         class Thing(object, metaclass=meta):
00925             pass
00926         self.assertEqual(inspect.getattr_static(Thing, 'd'), meta.__dict__['d'])
00927 

Here is the call graph for this function:

Definition at line 944 of file test_inspect.py.

00944 
00945     def test_mro_as_property(self):
00946         class Meta(type):
00947             @property
00948             def __mro__(self):
00949                 return (object,)
00950 
00951         class Base(object):
00952             foo = 3
00953 
00954         class Something(Base, metaclass=Meta):
00955             pass
00956 
00957         self.assertEqual(inspect.getattr_static(Something(), 'foo'), 3)
00958         self.assertEqual(inspect.getattr_static(Something, 'foo'), 3)

Here is the call graph for this function:

Definition at line 876 of file test_inspect.py.

00876 
00877     def test_no_dict_no_slots(self):
00878         self.assertEqual(inspect.getattr_static(1, 'foo', None), None)
00879         self.assertNotEqual(inspect.getattr_static('foo', 'lower'), None)

Here is the call graph for this function:

Definition at line 880 of file test_inspect.py.

00880 
00881     def test_no_dict_no_slots_instance_member(self):
00882         # returns descriptor
00883         with open(__file__) as handle:
00884             self.assertEqual(inspect.getattr_static(handle, 'name'), type(handle).name)

Here is the call graph for this function:

Definition at line 813 of file test_inspect.py.

00813 
00814     def test_property(self):
00815         class Thing(object):
00816             @property
00817             def x(self):
00818                 raise AttributeError("I'm pretending not to exist")
00819         thing = Thing()
00820         self.assertEqual(inspect.getattr_static(thing, 'x'), Thing.x)

Here is the call graph for this function:

Definition at line 845 of file test_inspect.py.

00845 
00846     def test_slots(self):
00847         class Thing(object):
00848             y = 'bar'
00849             __slots__ = ['x']
00850             def __init__(self):
00851                 self.x = 'foo'
00852         thing = Thing()
00853         self.assertEqual(inspect.getattr_static(thing, 'x'), Thing.x)
00854         self.assertEqual(inspect.getattr_static(thing, 'y'), 'bar')
00855 
00856         del thing.x
00857         self.assertEqual(inspect.getattr_static(thing, 'x'), Thing.x)

Here is the call graph for this function:


Member Data Documentation

Definition at line 936 of file test_inspect.py.

Definition at line 1001 of file test_inspect.py.

Definition at line 807 of file test_inspect.py.


The documentation for this class was generated from the following file: