1 | n/a | "Test the functionality of Python classes implementing operators." |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | testmeths = [ |
---|
7 | n/a | |
---|
8 | n/a | # Binary operations |
---|
9 | n/a | "add", |
---|
10 | n/a | "radd", |
---|
11 | n/a | "sub", |
---|
12 | n/a | "rsub", |
---|
13 | n/a | "mul", |
---|
14 | n/a | "rmul", |
---|
15 | n/a | "matmul", |
---|
16 | n/a | "rmatmul", |
---|
17 | n/a | "truediv", |
---|
18 | n/a | "rtruediv", |
---|
19 | n/a | "floordiv", |
---|
20 | n/a | "rfloordiv", |
---|
21 | n/a | "mod", |
---|
22 | n/a | "rmod", |
---|
23 | n/a | "divmod", |
---|
24 | n/a | "rdivmod", |
---|
25 | n/a | "pow", |
---|
26 | n/a | "rpow", |
---|
27 | n/a | "rshift", |
---|
28 | n/a | "rrshift", |
---|
29 | n/a | "lshift", |
---|
30 | n/a | "rlshift", |
---|
31 | n/a | "and", |
---|
32 | n/a | "rand", |
---|
33 | n/a | "or", |
---|
34 | n/a | "ror", |
---|
35 | n/a | "xor", |
---|
36 | n/a | "rxor", |
---|
37 | n/a | |
---|
38 | n/a | # List/dict operations |
---|
39 | n/a | "contains", |
---|
40 | n/a | "getitem", |
---|
41 | n/a | "setitem", |
---|
42 | n/a | "delitem", |
---|
43 | n/a | |
---|
44 | n/a | # Unary operations |
---|
45 | n/a | "neg", |
---|
46 | n/a | "pos", |
---|
47 | n/a | "abs", |
---|
48 | n/a | |
---|
49 | n/a | # generic operations |
---|
50 | n/a | "init", |
---|
51 | n/a | ] |
---|
52 | n/a | |
---|
53 | n/a | # These need to return something other than None |
---|
54 | n/a | # "hash", |
---|
55 | n/a | # "str", |
---|
56 | n/a | # "repr", |
---|
57 | n/a | # "int", |
---|
58 | n/a | # "float", |
---|
59 | n/a | |
---|
60 | n/a | # These are separate because they can influence the test of other methods. |
---|
61 | n/a | # "getattr", |
---|
62 | n/a | # "setattr", |
---|
63 | n/a | # "delattr", |
---|
64 | n/a | |
---|
65 | n/a | callLst = [] |
---|
66 | n/a | def trackCall(f): |
---|
67 | n/a | def track(*args, **kwargs): |
---|
68 | n/a | callLst.append((f.__name__, args)) |
---|
69 | n/a | return f(*args, **kwargs) |
---|
70 | n/a | return track |
---|
71 | n/a | |
---|
72 | n/a | statictests = """ |
---|
73 | n/a | @trackCall |
---|
74 | n/a | def __hash__(self, *args): |
---|
75 | n/a | return hash(id(self)) |
---|
76 | n/a | |
---|
77 | n/a | @trackCall |
---|
78 | n/a | def __str__(self, *args): |
---|
79 | n/a | return "AllTests" |
---|
80 | n/a | |
---|
81 | n/a | @trackCall |
---|
82 | n/a | def __repr__(self, *args): |
---|
83 | n/a | return "AllTests" |
---|
84 | n/a | |
---|
85 | n/a | @trackCall |
---|
86 | n/a | def __int__(self, *args): |
---|
87 | n/a | return 1 |
---|
88 | n/a | |
---|
89 | n/a | @trackCall |
---|
90 | n/a | def __index__(self, *args): |
---|
91 | n/a | return 1 |
---|
92 | n/a | |
---|
93 | n/a | @trackCall |
---|
94 | n/a | def __float__(self, *args): |
---|
95 | n/a | return 1.0 |
---|
96 | n/a | |
---|
97 | n/a | @trackCall |
---|
98 | n/a | def __eq__(self, *args): |
---|
99 | n/a | return True |
---|
100 | n/a | |
---|
101 | n/a | @trackCall |
---|
102 | n/a | def __ne__(self, *args): |
---|
103 | n/a | return False |
---|
104 | n/a | |
---|
105 | n/a | @trackCall |
---|
106 | n/a | def __lt__(self, *args): |
---|
107 | n/a | return False |
---|
108 | n/a | |
---|
109 | n/a | @trackCall |
---|
110 | n/a | def __le__(self, *args): |
---|
111 | n/a | return True |
---|
112 | n/a | |
---|
113 | n/a | @trackCall |
---|
114 | n/a | def __gt__(self, *args): |
---|
115 | n/a | return False |
---|
116 | n/a | |
---|
117 | n/a | @trackCall |
---|
118 | n/a | def __ge__(self, *args): |
---|
119 | n/a | return True |
---|
120 | n/a | """ |
---|
121 | n/a | |
---|
122 | n/a | # Synthesize all the other AllTests methods from the names in testmeths. |
---|
123 | n/a | |
---|
124 | n/a | method_template = """\ |
---|
125 | n/a | @trackCall |
---|
126 | n/a | def __%s__(self, *args): |
---|
127 | n/a | pass |
---|
128 | n/a | """ |
---|
129 | n/a | |
---|
130 | n/a | d = {} |
---|
131 | n/a | exec(statictests, globals(), d) |
---|
132 | n/a | for method in testmeths: |
---|
133 | n/a | exec(method_template % method, globals(), d) |
---|
134 | n/a | AllTests = type("AllTests", (object,), d) |
---|
135 | n/a | del d, statictests, method, method_template |
---|
136 | n/a | |
---|
137 | n/a | class ClassTests(unittest.TestCase): |
---|
138 | n/a | def setUp(self): |
---|
139 | n/a | callLst[:] = [] |
---|
140 | n/a | |
---|
141 | n/a | def assertCallStack(self, expected_calls): |
---|
142 | n/a | actualCallList = callLst[:] # need to copy because the comparison below will add |
---|
143 | n/a | # additional calls to callLst |
---|
144 | n/a | if expected_calls != actualCallList: |
---|
145 | n/a | self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" % |
---|
146 | n/a | (expected_calls, actualCallList)) |
---|
147 | n/a | |
---|
148 | n/a | def testInit(self): |
---|
149 | n/a | foo = AllTests() |
---|
150 | n/a | self.assertCallStack([("__init__", (foo,))]) |
---|
151 | n/a | |
---|
152 | n/a | def testBinaryOps(self): |
---|
153 | n/a | testme = AllTests() |
---|
154 | n/a | # Binary operations |
---|
155 | n/a | |
---|
156 | n/a | callLst[:] = [] |
---|
157 | n/a | testme + 1 |
---|
158 | n/a | self.assertCallStack([("__add__", (testme, 1))]) |
---|
159 | n/a | |
---|
160 | n/a | callLst[:] = [] |
---|
161 | n/a | 1 + testme |
---|
162 | n/a | self.assertCallStack([("__radd__", (testme, 1))]) |
---|
163 | n/a | |
---|
164 | n/a | callLst[:] = [] |
---|
165 | n/a | testme - 1 |
---|
166 | n/a | self.assertCallStack([("__sub__", (testme, 1))]) |
---|
167 | n/a | |
---|
168 | n/a | callLst[:] = [] |
---|
169 | n/a | 1 - testme |
---|
170 | n/a | self.assertCallStack([("__rsub__", (testme, 1))]) |
---|
171 | n/a | |
---|
172 | n/a | callLst[:] = [] |
---|
173 | n/a | testme * 1 |
---|
174 | n/a | self.assertCallStack([("__mul__", (testme, 1))]) |
---|
175 | n/a | |
---|
176 | n/a | callLst[:] = [] |
---|
177 | n/a | 1 * testme |
---|
178 | n/a | self.assertCallStack([("__rmul__", (testme, 1))]) |
---|
179 | n/a | |
---|
180 | n/a | callLst[:] = [] |
---|
181 | n/a | testme @ 1 |
---|
182 | n/a | self.assertCallStack([("__matmul__", (testme, 1))]) |
---|
183 | n/a | |
---|
184 | n/a | callLst[:] = [] |
---|
185 | n/a | 1 @ testme |
---|
186 | n/a | self.assertCallStack([("__rmatmul__", (testme, 1))]) |
---|
187 | n/a | |
---|
188 | n/a | callLst[:] = [] |
---|
189 | n/a | testme / 1 |
---|
190 | n/a | self.assertCallStack([("__truediv__", (testme, 1))]) |
---|
191 | n/a | |
---|
192 | n/a | |
---|
193 | n/a | callLst[:] = [] |
---|
194 | n/a | 1 / testme |
---|
195 | n/a | self.assertCallStack([("__rtruediv__", (testme, 1))]) |
---|
196 | n/a | |
---|
197 | n/a | callLst[:] = [] |
---|
198 | n/a | testme // 1 |
---|
199 | n/a | self.assertCallStack([("__floordiv__", (testme, 1))]) |
---|
200 | n/a | |
---|
201 | n/a | |
---|
202 | n/a | callLst[:] = [] |
---|
203 | n/a | 1 // testme |
---|
204 | n/a | self.assertCallStack([("__rfloordiv__", (testme, 1))]) |
---|
205 | n/a | |
---|
206 | n/a | callLst[:] = [] |
---|
207 | n/a | testme % 1 |
---|
208 | n/a | self.assertCallStack([("__mod__", (testme, 1))]) |
---|
209 | n/a | |
---|
210 | n/a | callLst[:] = [] |
---|
211 | n/a | 1 % testme |
---|
212 | n/a | self.assertCallStack([("__rmod__", (testme, 1))]) |
---|
213 | n/a | |
---|
214 | n/a | |
---|
215 | n/a | callLst[:] = [] |
---|
216 | n/a | divmod(testme,1) |
---|
217 | n/a | self.assertCallStack([("__divmod__", (testme, 1))]) |
---|
218 | n/a | |
---|
219 | n/a | callLst[:] = [] |
---|
220 | n/a | divmod(1, testme) |
---|
221 | n/a | self.assertCallStack([("__rdivmod__", (testme, 1))]) |
---|
222 | n/a | |
---|
223 | n/a | callLst[:] = [] |
---|
224 | n/a | testme ** 1 |
---|
225 | n/a | self.assertCallStack([("__pow__", (testme, 1))]) |
---|
226 | n/a | |
---|
227 | n/a | callLst[:] = [] |
---|
228 | n/a | 1 ** testme |
---|
229 | n/a | self.assertCallStack([("__rpow__", (testme, 1))]) |
---|
230 | n/a | |
---|
231 | n/a | callLst[:] = [] |
---|
232 | n/a | testme >> 1 |
---|
233 | n/a | self.assertCallStack([("__rshift__", (testme, 1))]) |
---|
234 | n/a | |
---|
235 | n/a | callLst[:] = [] |
---|
236 | n/a | 1 >> testme |
---|
237 | n/a | self.assertCallStack([("__rrshift__", (testme, 1))]) |
---|
238 | n/a | |
---|
239 | n/a | callLst[:] = [] |
---|
240 | n/a | testme << 1 |
---|
241 | n/a | self.assertCallStack([("__lshift__", (testme, 1))]) |
---|
242 | n/a | |
---|
243 | n/a | callLst[:] = [] |
---|
244 | n/a | 1 << testme |
---|
245 | n/a | self.assertCallStack([("__rlshift__", (testme, 1))]) |
---|
246 | n/a | |
---|
247 | n/a | callLst[:] = [] |
---|
248 | n/a | testme & 1 |
---|
249 | n/a | self.assertCallStack([("__and__", (testme, 1))]) |
---|
250 | n/a | |
---|
251 | n/a | callLst[:] = [] |
---|
252 | n/a | 1 & testme |
---|
253 | n/a | self.assertCallStack([("__rand__", (testme, 1))]) |
---|
254 | n/a | |
---|
255 | n/a | callLst[:] = [] |
---|
256 | n/a | testme | 1 |
---|
257 | n/a | self.assertCallStack([("__or__", (testme, 1))]) |
---|
258 | n/a | |
---|
259 | n/a | callLst[:] = [] |
---|
260 | n/a | 1 | testme |
---|
261 | n/a | self.assertCallStack([("__ror__", (testme, 1))]) |
---|
262 | n/a | |
---|
263 | n/a | callLst[:] = [] |
---|
264 | n/a | testme ^ 1 |
---|
265 | n/a | self.assertCallStack([("__xor__", (testme, 1))]) |
---|
266 | n/a | |
---|
267 | n/a | callLst[:] = [] |
---|
268 | n/a | 1 ^ testme |
---|
269 | n/a | self.assertCallStack([("__rxor__", (testme, 1))]) |
---|
270 | n/a | |
---|
271 | n/a | def testListAndDictOps(self): |
---|
272 | n/a | testme = AllTests() |
---|
273 | n/a | |
---|
274 | n/a | # List/dict operations |
---|
275 | n/a | |
---|
276 | n/a | class Empty: pass |
---|
277 | n/a | |
---|
278 | n/a | try: |
---|
279 | n/a | 1 in Empty() |
---|
280 | n/a | self.fail('failed, should have raised TypeError') |
---|
281 | n/a | except TypeError: |
---|
282 | n/a | pass |
---|
283 | n/a | |
---|
284 | n/a | callLst[:] = [] |
---|
285 | n/a | 1 in testme |
---|
286 | n/a | self.assertCallStack([('__contains__', (testme, 1))]) |
---|
287 | n/a | |
---|
288 | n/a | callLst[:] = [] |
---|
289 | n/a | testme[1] |
---|
290 | n/a | self.assertCallStack([('__getitem__', (testme, 1))]) |
---|
291 | n/a | |
---|
292 | n/a | callLst[:] = [] |
---|
293 | n/a | testme[1] = 1 |
---|
294 | n/a | self.assertCallStack([('__setitem__', (testme, 1, 1))]) |
---|
295 | n/a | |
---|
296 | n/a | callLst[:] = [] |
---|
297 | n/a | del testme[1] |
---|
298 | n/a | self.assertCallStack([('__delitem__', (testme, 1))]) |
---|
299 | n/a | |
---|
300 | n/a | callLst[:] = [] |
---|
301 | n/a | testme[:42] |
---|
302 | n/a | self.assertCallStack([('__getitem__', (testme, slice(None, 42)))]) |
---|
303 | n/a | |
---|
304 | n/a | callLst[:] = [] |
---|
305 | n/a | testme[:42] = "The Answer" |
---|
306 | n/a | self.assertCallStack([('__setitem__', (testme, slice(None, 42), |
---|
307 | n/a | "The Answer"))]) |
---|
308 | n/a | |
---|
309 | n/a | callLst[:] = [] |
---|
310 | n/a | del testme[:42] |
---|
311 | n/a | self.assertCallStack([('__delitem__', (testme, slice(None, 42)))]) |
---|
312 | n/a | |
---|
313 | n/a | callLst[:] = [] |
---|
314 | n/a | testme[2:1024:10] |
---|
315 | n/a | self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))]) |
---|
316 | n/a | |
---|
317 | n/a | callLst[:] = [] |
---|
318 | n/a | testme[2:1024:10] = "A lot" |
---|
319 | n/a | self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10), |
---|
320 | n/a | "A lot"))]) |
---|
321 | n/a | callLst[:] = [] |
---|
322 | n/a | del testme[2:1024:10] |
---|
323 | n/a | self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))]) |
---|
324 | n/a | |
---|
325 | n/a | callLst[:] = [] |
---|
326 | n/a | testme[:42, ..., :24:, 24, 100] |
---|
327 | n/a | self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None), |
---|
328 | n/a | Ellipsis, |
---|
329 | n/a | slice(None, 24, None), |
---|
330 | n/a | 24, 100)))]) |
---|
331 | n/a | callLst[:] = [] |
---|
332 | n/a | testme[:42, ..., :24:, 24, 100] = "Strange" |
---|
333 | n/a | self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None), |
---|
334 | n/a | Ellipsis, |
---|
335 | n/a | slice(None, 24, None), |
---|
336 | n/a | 24, 100), "Strange"))]) |
---|
337 | n/a | callLst[:] = [] |
---|
338 | n/a | del testme[:42, ..., :24:, 24, 100] |
---|
339 | n/a | self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None), |
---|
340 | n/a | Ellipsis, |
---|
341 | n/a | slice(None, 24, None), |
---|
342 | n/a | 24, 100)))]) |
---|
343 | n/a | |
---|
344 | n/a | def testUnaryOps(self): |
---|
345 | n/a | testme = AllTests() |
---|
346 | n/a | |
---|
347 | n/a | callLst[:] = [] |
---|
348 | n/a | -testme |
---|
349 | n/a | self.assertCallStack([('__neg__', (testme,))]) |
---|
350 | n/a | callLst[:] = [] |
---|
351 | n/a | +testme |
---|
352 | n/a | self.assertCallStack([('__pos__', (testme,))]) |
---|
353 | n/a | callLst[:] = [] |
---|
354 | n/a | abs(testme) |
---|
355 | n/a | self.assertCallStack([('__abs__', (testme,))]) |
---|
356 | n/a | callLst[:] = [] |
---|
357 | n/a | int(testme) |
---|
358 | n/a | self.assertCallStack([('__int__', (testme,))]) |
---|
359 | n/a | callLst[:] = [] |
---|
360 | n/a | float(testme) |
---|
361 | n/a | self.assertCallStack([('__float__', (testme,))]) |
---|
362 | n/a | callLst[:] = [] |
---|
363 | n/a | oct(testme) |
---|
364 | n/a | self.assertCallStack([('__index__', (testme,))]) |
---|
365 | n/a | callLst[:] = [] |
---|
366 | n/a | hex(testme) |
---|
367 | n/a | self.assertCallStack([('__index__', (testme,))]) |
---|
368 | n/a | |
---|
369 | n/a | |
---|
370 | n/a | def testMisc(self): |
---|
371 | n/a | testme = AllTests() |
---|
372 | n/a | |
---|
373 | n/a | callLst[:] = [] |
---|
374 | n/a | hash(testme) |
---|
375 | n/a | self.assertCallStack([('__hash__', (testme,))]) |
---|
376 | n/a | |
---|
377 | n/a | callLst[:] = [] |
---|
378 | n/a | repr(testme) |
---|
379 | n/a | self.assertCallStack([('__repr__', (testme,))]) |
---|
380 | n/a | |
---|
381 | n/a | callLst[:] = [] |
---|
382 | n/a | str(testme) |
---|
383 | n/a | self.assertCallStack([('__str__', (testme,))]) |
---|
384 | n/a | |
---|
385 | n/a | callLst[:] = [] |
---|
386 | n/a | testme == 1 |
---|
387 | n/a | self.assertCallStack([('__eq__', (testme, 1))]) |
---|
388 | n/a | |
---|
389 | n/a | callLst[:] = [] |
---|
390 | n/a | testme < 1 |
---|
391 | n/a | self.assertCallStack([('__lt__', (testme, 1))]) |
---|
392 | n/a | |
---|
393 | n/a | callLst[:] = [] |
---|
394 | n/a | testme > 1 |
---|
395 | n/a | self.assertCallStack([('__gt__', (testme, 1))]) |
---|
396 | n/a | |
---|
397 | n/a | callLst[:] = [] |
---|
398 | n/a | testme != 1 |
---|
399 | n/a | self.assertCallStack([('__ne__', (testme, 1))]) |
---|
400 | n/a | |
---|
401 | n/a | callLst[:] = [] |
---|
402 | n/a | 1 == testme |
---|
403 | n/a | self.assertCallStack([('__eq__', (1, testme))]) |
---|
404 | n/a | |
---|
405 | n/a | callLst[:] = [] |
---|
406 | n/a | 1 < testme |
---|
407 | n/a | self.assertCallStack([('__gt__', (1, testme))]) |
---|
408 | n/a | |
---|
409 | n/a | callLst[:] = [] |
---|
410 | n/a | 1 > testme |
---|
411 | n/a | self.assertCallStack([('__lt__', (1, testme))]) |
---|
412 | n/a | |
---|
413 | n/a | callLst[:] = [] |
---|
414 | n/a | 1 != testme |
---|
415 | n/a | self.assertCallStack([('__ne__', (1, testme))]) |
---|
416 | n/a | |
---|
417 | n/a | |
---|
418 | n/a | def testGetSetAndDel(self): |
---|
419 | n/a | # Interfering tests |
---|
420 | n/a | class ExtraTests(AllTests): |
---|
421 | n/a | @trackCall |
---|
422 | n/a | def __getattr__(self, *args): |
---|
423 | n/a | return "SomeVal" |
---|
424 | n/a | |
---|
425 | n/a | @trackCall |
---|
426 | n/a | def __setattr__(self, *args): |
---|
427 | n/a | pass |
---|
428 | n/a | |
---|
429 | n/a | @trackCall |
---|
430 | n/a | def __delattr__(self, *args): |
---|
431 | n/a | pass |
---|
432 | n/a | |
---|
433 | n/a | testme = ExtraTests() |
---|
434 | n/a | |
---|
435 | n/a | callLst[:] = [] |
---|
436 | n/a | testme.spam |
---|
437 | n/a | self.assertCallStack([('__getattr__', (testme, "spam"))]) |
---|
438 | n/a | |
---|
439 | n/a | callLst[:] = [] |
---|
440 | n/a | testme.eggs = "spam, spam, spam and ham" |
---|
441 | n/a | self.assertCallStack([('__setattr__', (testme, "eggs", |
---|
442 | n/a | "spam, spam, spam and ham"))]) |
---|
443 | n/a | |
---|
444 | n/a | callLst[:] = [] |
---|
445 | n/a | del testme.cardinal |
---|
446 | n/a | self.assertCallStack([('__delattr__', (testme, "cardinal"))]) |
---|
447 | n/a | |
---|
448 | n/a | def testDel(self): |
---|
449 | n/a | x = [] |
---|
450 | n/a | |
---|
451 | n/a | class DelTest: |
---|
452 | n/a | def __del__(self): |
---|
453 | n/a | x.append("crab people, crab people") |
---|
454 | n/a | testme = DelTest() |
---|
455 | n/a | del testme |
---|
456 | n/a | import gc |
---|
457 | n/a | gc.collect() |
---|
458 | n/a | self.assertEqual(["crab people, crab people"], x) |
---|
459 | n/a | |
---|
460 | n/a | def testBadTypeReturned(self): |
---|
461 | n/a | # return values of some method are type-checked |
---|
462 | n/a | class BadTypeClass: |
---|
463 | n/a | def __int__(self): |
---|
464 | n/a | return None |
---|
465 | n/a | __float__ = __int__ |
---|
466 | n/a | __complex__ = __int__ |
---|
467 | n/a | __str__ = __int__ |
---|
468 | n/a | __repr__ = __int__ |
---|
469 | n/a | __bytes__ = __int__ |
---|
470 | n/a | __bool__ = __int__ |
---|
471 | n/a | __index__ = __int__ |
---|
472 | n/a | def index(x): |
---|
473 | n/a | return [][x] |
---|
474 | n/a | |
---|
475 | n/a | for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]: |
---|
476 | n/a | self.assertRaises(TypeError, f, BadTypeClass()) |
---|
477 | n/a | |
---|
478 | n/a | def testHashStuff(self): |
---|
479 | n/a | # Test correct errors from hash() on objects with comparisons but |
---|
480 | n/a | # no __hash__ |
---|
481 | n/a | |
---|
482 | n/a | class C0: |
---|
483 | n/a | pass |
---|
484 | n/a | |
---|
485 | n/a | hash(C0()) # This should work; the next two should raise TypeError |
---|
486 | n/a | |
---|
487 | n/a | class C2: |
---|
488 | n/a | def __eq__(self, other): return 1 |
---|
489 | n/a | |
---|
490 | n/a | self.assertRaises(TypeError, hash, C2()) |
---|
491 | n/a | |
---|
492 | n/a | |
---|
493 | n/a | def testSFBug532646(self): |
---|
494 | n/a | # Test for SF bug 532646 |
---|
495 | n/a | |
---|
496 | n/a | class A: |
---|
497 | n/a | pass |
---|
498 | n/a | A.__call__ = A() |
---|
499 | n/a | a = A() |
---|
500 | n/a | |
---|
501 | n/a | try: |
---|
502 | n/a | a() # This should not segfault |
---|
503 | n/a | except RecursionError: |
---|
504 | n/a | pass |
---|
505 | n/a | else: |
---|
506 | n/a | self.fail("Failed to raise RecursionError") |
---|
507 | n/a | |
---|
508 | n/a | def testForExceptionsRaisedInInstanceGetattr2(self): |
---|
509 | n/a | # Tests for exceptions raised in instance_getattr2(). |
---|
510 | n/a | |
---|
511 | n/a | def booh(self): |
---|
512 | n/a | raise AttributeError("booh") |
---|
513 | n/a | |
---|
514 | n/a | class A: |
---|
515 | n/a | a = property(booh) |
---|
516 | n/a | try: |
---|
517 | n/a | A().a # Raised AttributeError: A instance has no attribute 'a' |
---|
518 | n/a | except AttributeError as x: |
---|
519 | n/a | if str(x) != "booh": |
---|
520 | n/a | self.fail("attribute error for A().a got masked: %s" % x) |
---|
521 | n/a | |
---|
522 | n/a | class E: |
---|
523 | n/a | __eq__ = property(booh) |
---|
524 | n/a | E() == E() # In debug mode, caused a C-level assert() to fail |
---|
525 | n/a | |
---|
526 | n/a | class I: |
---|
527 | n/a | __init__ = property(booh) |
---|
528 | n/a | try: |
---|
529 | n/a | # In debug mode, printed XXX undetected error and |
---|
530 | n/a | # raises AttributeError |
---|
531 | n/a | I() |
---|
532 | n/a | except AttributeError as x: |
---|
533 | n/a | pass |
---|
534 | n/a | else: |
---|
535 | n/a | self.fail("attribute error for I.__init__ got masked") |
---|
536 | n/a | |
---|
537 | n/a | def testHashComparisonOfMethods(self): |
---|
538 | n/a | # Test comparison and hash of methods |
---|
539 | n/a | class A: |
---|
540 | n/a | def __init__(self, x): |
---|
541 | n/a | self.x = x |
---|
542 | n/a | def f(self): |
---|
543 | n/a | pass |
---|
544 | n/a | def g(self): |
---|
545 | n/a | pass |
---|
546 | n/a | def __eq__(self, other): |
---|
547 | n/a | return self.x == other.x |
---|
548 | n/a | def __hash__(self): |
---|
549 | n/a | return self.x |
---|
550 | n/a | class B(A): |
---|
551 | n/a | pass |
---|
552 | n/a | |
---|
553 | n/a | a1 = A(1) |
---|
554 | n/a | a2 = A(2) |
---|
555 | n/a | self.assertEqual(a1.f, a1.f) |
---|
556 | n/a | self.assertNotEqual(a1.f, a2.f) |
---|
557 | n/a | self.assertNotEqual(a1.f, a1.g) |
---|
558 | n/a | self.assertEqual(a1.f, A(1).f) |
---|
559 | n/a | self.assertEqual(hash(a1.f), hash(a1.f)) |
---|
560 | n/a | self.assertEqual(hash(a1.f), hash(A(1).f)) |
---|
561 | n/a | |
---|
562 | n/a | self.assertNotEqual(A.f, a1.f) |
---|
563 | n/a | self.assertNotEqual(A.f, A.g) |
---|
564 | n/a | self.assertEqual(B.f, A.f) |
---|
565 | n/a | self.assertEqual(hash(B.f), hash(A.f)) |
---|
566 | n/a | |
---|
567 | n/a | # the following triggers a SystemError in 2.4 |
---|
568 | n/a | a = A(hash(A.f)^(-1)) |
---|
569 | n/a | hash(a.f) |
---|
570 | n/a | |
---|
571 | n/a | if __name__ == '__main__': |
---|
572 | n/a | unittest.main() |
---|