1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Tests some corner cases with isinstance() and issubclass(). While these 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# tests use new style classes and properties, they actually do whitebox 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# testing of error conditions uncovered when using extension types. 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestIsInstanceExceptions(unittest.TestCase): 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test to make sure that an AttributeError when accessing the instance's 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # class's bases is masked. This was actually a bug in Python 2.2 and 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 2.2.1 where the exception wasn't caught but it also wasn't being cleared 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (leading to an "undetected error" in the debug build). Set up is, 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # isinstance(inst, cls) where: 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # - inst isn't an InstanceType 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # - cls isn't a ClassType, a TypeType, or a TupleType 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # - cls has a __bases__ attribute 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # - inst has a __class__ attribute 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # - inst.__class__ as no __bases__ attribute 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Sounds complicated, I know, but this mimics a situation where an 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # extension type raises an AttributeError when its __bases__ attribute is 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # gotten. In that case, isinstance() should return False. 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_class_has_no_bases(self): 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class I(object): 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getclass(self): 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This must return an object that has no __bases__ attribute 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return None 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __class__ = property(getclass) 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return () 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(I(), C())) 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Like above except that inst.__class__.__bases__ raises an exception 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # other than AttributeError 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_bases_raises_other_than_attribute_error(self): 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class E(object): 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class I(object): 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getclass(self): 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return E() 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __class__ = property(getclass) 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return () 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, isinstance, I(), C()) 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Here's a situation where getattr(cls, '__bases__') raises an exception. 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # If that exception is not AttributeError, it should not get masked 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_dont_mask_non_attribute_error(self): 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class I: pass 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, isinstance, I(), C()) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Like above, except that getattr(cls, '__bases__') raises an 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # AttributeError, which /should/ get masked as a TypeError 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_mask_attribute_error(self): 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class I: pass 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise AttributeError 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, isinstance, I(), C()) 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# These tests are similar to above, but tickle certain code paths in 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# issubclass() instead of isinstance() -- really PyObject_IsSubclass() 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# vs. PyObject_IsInstance(). 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestIsSubclassExceptions(unittest.TestCase): 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_dont_mask_non_attribute_error(self): 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class S(C): pass 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, issubclass, C(), S()) 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_mask_attribute_error(self): 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise AttributeError 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class S(C): pass 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, issubclass, C(), S()) 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Like above, but test the second branch, where the __bases__ of the 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # second arg (the cls arg) is tested. This means the first arg must 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # return a valid __bases__, and it's okay for it to be a normal -- 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # unrelated by inheritance -- class. 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_dont_mask_non_attribute_error_in_cls_arg(self): 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class B: pass 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, issubclass, B, C()) 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_mask_attribute_error_in_cls_arg(self): 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class B: pass 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class C(object): 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise AttributeError 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(TypeError, issubclass, B, C()) 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# meta classes for creating abstract classes and instances 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractClass(object): 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, bases): 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.bases = bases 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getbases(self): 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.bases 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __bases__ = property(getbases) 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __call__(self): 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return AbstractInstance(self) 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass AbstractInstance(object): 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, klass): 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.klass = klass 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def getclass(self): 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.klass 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __class__ = property(getclass) 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# abstract classes 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepAbstractSuper = AbstractClass(bases=()) 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepAbstractChild = AbstractClass(bases=(AbstractSuper,)) 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# normal classes 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Super: 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Child(Super): 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# new-style classes 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewSuper(object): 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass NewChild(NewSuper): 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TestIsInstanceIsSubclass(unittest.TestCase): 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Tests to ensure that isinstance and issubclass work on abstract 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # classes and instances. Before the 2.2 release, TypeErrors were 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # raised when boolean values should have been returned. The bug was 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # triggered by mixing 'normal' classes and instances were with 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 'abstract' classes and instances. This case tries to test all 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # combinations. 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_isinstance_normal(self): 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # normal instances 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, isinstance(Super(), Super)) 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(Super(), Child)) 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(Super(), AbstractSuper)) 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(Super(), AbstractChild)) 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, isinstance(Child(), Super)) 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(Child(), AbstractSuper)) 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_isinstance_abstract(self): 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # abstract instances 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, isinstance(AbstractSuper(), AbstractSuper)) 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(AbstractSuper(), AbstractChild)) 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(AbstractSuper(), Super)) 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(AbstractSuper(), Child)) 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, isinstance(AbstractChild(), AbstractChild)) 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, isinstance(AbstractChild(), AbstractSuper)) 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(AbstractChild(), Super)) 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, isinstance(AbstractChild(), Child)) 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_subclass_normal(self): 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # normal classes 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Super, Super)) 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(Super, AbstractSuper)) 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(Super, Child)) 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Child, Child)) 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Child, Super)) 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(Child, AbstractSuper)) 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_subclass_abstract(self): 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # abstract classes 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(AbstractSuper, AbstractSuper)) 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(AbstractSuper, AbstractChild)) 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(AbstractSuper, Child)) 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(AbstractChild, AbstractChild)) 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(AbstractChild, AbstractSuper)) 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(AbstractChild, Super)) 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(AbstractChild, Child)) 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_subclass_tuple(self): 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # test with a tuple as the second argument classes 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Child, (Child,))) 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Child, (Super,))) 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(Super, (Child,))) 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Super, (Child, Super))) 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(Child, ())) 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(Super, (Child, (Super,)))) 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(NewChild, (NewChild,))) 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(NewChild, (NewSuper,))) 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(NewSuper, (NewChild,))) 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(NewSuper, (NewChild, NewSuper))) 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(False, issubclass(NewChild, ())) 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(NewSuper, (NewChild, (NewSuper,)))) 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(int, (long, (float, int)))) 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if test_support.have_unicode: 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(True, issubclass(str, (unicode, (Child, NewChild, basestring)))) 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_subclass_recursion_limit(self): 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # make sure that issubclass raises RuntimeError before the C stack is 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # blown 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, blowstack, issubclass, str, str) 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_isinstance_recursion_limit(self): 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # make sure that issubclass raises RuntimeError before the C stack is 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # blown 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(RuntimeError, blowstack, isinstance, '', str) 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef blowstack(fxn, arg, compare_to): 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Make sure that calling isinstance with a deeply nested tuple for its 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # argument will raise RuntimeError eventually. 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tuple_arg = (compare_to,) 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for cnt in xrange(sys.getrecursionlimit()+5): 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tuple_arg = (tuple_arg,) 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fxn(arg, tuple_arg) 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest( 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep TestIsInstanceExceptions, 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep TestIsSubclassExceptions, 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep TestIsInstanceIsSubclass 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ) 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == '__main__': 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 278