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