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