blob: dc2d07467aa7fa889f142adf8bcf4cd1b1527fe2 [file] [log] [blame]
Barry Warsaw906569d2002-04-23 22:48:42 +00001# 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
5import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Brett Cannon4f653312004-03-20 22:52:14 +00007import sys
Barry Warsaw906569d2002-04-23 22:48:42 +00008
9
10
Neil Schemenauer3b04d632002-04-24 03:33:02 +000011class TestIsInstanceExceptions(unittest.TestCase):
Barry Warsaw906569d2002-04-23 22:48:42 +000012 # 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 Rossum13257902007-06-07 23:15:56 +000018 # - cls isn't a a type, or a tuple
Barry Warsaw906569d2002-04-23 22:48:42 +000019 # - 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 Murray6bb99892010-11-20 16:33:30 +000084 # 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 Peterson5c417872010-11-20 17:22:13 +000089 raise RuntimeError
90 __class__ = property(getclass)
R. David Murray6bb99892010-11-20 16:33:30 +000091
Benjamin Peterson5c417872010-11-20 17:22:13 +000092 c = C()
R. David Murray6bb99892010-11-20 16:33:30 +000093 self.assertRaises(RuntimeError, isinstance, c, bool)
94
95 # test another code path
96 class D: pass
97 self.assertRaises(RuntimeError, isinstance, c, D)
Barry Warsaw906569d2002-04-23 22:48:42 +000098
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 Schemenauer3b04d632002-04-24 03:33:02 +0000103class TestIsSubclassExceptions(unittest.TestCase):
Barry Warsaw906569d2002-04-23 22:48:42 +0000104 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 Peters8ac14952002-05-23 15:15:30 +0000121
Barry Warsaw906569d2002-04-23 22:48:42 +0000122 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 Schemenauer3b04d632002-04-24 03:33:02 +0000150# meta classes for creating abstract classes and instances
151class 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
162class 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
171AbstractSuper = AbstractClass(bases=())
172
173AbstractChild = AbstractClass(bases=(AbstractSuper,))
174
175# normal classes
176class Super:
177 pass
178
179class Child(Super):
180 pass
181
Walter Dörwald7e5c6a02002-12-12 19:14:08 +0000182# new-style classes
183class NewSuper(object):
184 pass
185
186class NewChild(NewSuper):
187 pass
188
Neil Schemenauer3b04d632002-04-24 03:33:02 +0000189
190
191class 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 Peters8ac14952002-05-23 15:15:30 +0000200 # normal instances
Neil Schemenauer3b04d632002-04-24 03:33:02 +0000201 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 Peters8ac14952002-05-23 15:15:30 +0000210 # abstract instances
Neil Schemenauer3b04d632002-04-24 03:33:02 +0000211 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 Peters8ac14952002-05-23 15:15:30 +0000220
Neil Schemenauer3b04d632002-04-24 03:33:02 +0000221 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 Peters8ac14952002-05-23 15:15:30 +0000241
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000242 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örwald7e5c6a02002-12-12 19:14:08 +0000249 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 Rossume2a383d2007-01-15 16:59:06 +0000258 self.assertEqual(True, issubclass(int, (int, (float, int))))
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000259 self.assertEqual(True, issubclass(str, (str, (Child, NewChild, str))))
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000260
Brett Cannon4f653312004-03-20 22:52:14 +0000261 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 Schemenauer3b04d632002-04-24 03:33:02 +0000265
Brett Cannon4f653312004-03-20 22:52:14 +0000266 def test_isinstance_recursion_limit(self):
267 # make sure that issubclass raises RuntimeError before the C stack is
Tim Peters27f88362004-07-08 04:22:35 +0000268 # blown
Brett Cannon4f653312004-03-20 22:52:14 +0000269 self.assertRaises(RuntimeError, blowstack, isinstance, '', str)
270
271def 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 Rossum805365e2007-05-07 22:24:25 +0000275 for cnt in range(sys.getrecursionlimit()+5):
Brett Cannon4f653312004-03-20 22:52:14 +0000276 tuple_arg = (tuple_arg,)
277 fxn(arg, tuple_arg)
Neil Schemenauer3b04d632002-04-24 03:33:02 +0000278
279
Barry Warsaw906569d2002-04-23 22:48:42 +0000280def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000281 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000282 TestIsInstanceExceptions,
283 TestIsSubclassExceptions,
284 TestIsInstanceIsSubclass
285 )
Barry Warsaw906569d2002-04-23 22:48:42 +0000286
287
288if __name__ == '__main__':
289 test_main()