blob: fcac5c5ea93dffb6b2a6de56853eced7eddc0ab1 [file] [log] [blame]
Thomas Wouters1d75a792000-08-17 22:37:32 +00001"Test the functionality of Python classes implementing operators."
2
Thomas Woutersed03b412007-08-28 21:37:11 +00003import unittest
Thomas Woutersed03b412007-08-28 21:37:11 +00004
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Thomas Wouters1d75a792000-08-17 22:37:32 +00006
7testmeths = [
8
9# Binary operations
10 "add",
11 "radd",
12 "sub",
13 "rsub",
14 "mul",
15 "rmul",
Serhiy Storchakac2ccce72015-03-12 22:01:30 +020016 "matmul",
17 "rmatmul",
Neal Norwitzbcc0db82006-03-24 08:14:36 +000018 "truediv",
19 "rtruediv",
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +020020 "floordiv",
21 "rfloordiv",
Thomas Wouters1d75a792000-08-17 22:37:32 +000022 "mod",
23 "rmod",
24 "divmod",
25 "rdivmod",
26 "pow",
27 "rpow",
28 "rshift",
29 "rrshift",
30 "lshift",
31 "rlshift",
32 "and",
33 "rand",
34 "or",
35 "ror",
36 "xor",
37 "rxor",
38
39# List/dict operations
40 "contains",
41 "getitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000042 "setitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000043 "delitem",
Thomas Wouters1d75a792000-08-17 22:37:32 +000044
45# Unary operations
46 "neg",
47 "pos",
48 "abs",
Thomas Wouters1d75a792000-08-17 22:37:32 +000049
50# generic operations
51 "init",
Thomas Wouters1d75a792000-08-17 22:37:32 +000052 ]
53
54# These need to return something other than None
Thomas Wouters1d75a792000-08-17 22:37:32 +000055# "hash",
56# "str",
57# "repr",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000058# "int",
Neil Schemenauer3a313e32004-07-19 16:29:17 +000059# "float",
Thomas Wouters1d75a792000-08-17 22:37:32 +000060
61# These are separate because they can influence the test of other methods.
62# "getattr",
63# "setattr",
64# "delattr",
65
Thomas Woutersed03b412007-08-28 21:37:11 +000066callLst = []
67def trackCall(f):
68 def track(*args, **kwargs):
69 callLst.append((f.__name__, args))
70 return f(*args, **kwargs)
71 return track
Thomas Wouters1d75a792000-08-17 22:37:32 +000072
Thomas Woutersed03b412007-08-28 21:37:11 +000073statictests = """
74@trackCall
75def __hash__(self, *args):
76 return hash(id(self))
Thomas Wouters1d75a792000-08-17 22:37:32 +000077
Thomas Woutersed03b412007-08-28 21:37:11 +000078@trackCall
79def __str__(self, *args):
80 return "AllTests"
Thomas Wouters1d75a792000-08-17 22:37:32 +000081
Thomas Woutersed03b412007-08-28 21:37:11 +000082@trackCall
83def __repr__(self, *args):
84 return "AllTests"
Neil Schemenauer3a313e32004-07-19 16:29:17 +000085
Thomas Woutersed03b412007-08-28 21:37:11 +000086@trackCall
87def __int__(self, *args):
88 return 1
Guido van Rossumcd16bf62007-06-13 18:07:49 +000089
Thomas Woutersed03b412007-08-28 21:37:11 +000090@trackCall
91def __index__(self, *args):
92 return 1
Neil Schemenauer3a313e32004-07-19 16:29:17 +000093
Thomas Woutersed03b412007-08-28 21:37:11 +000094@trackCall
95def __float__(self, *args):
96 return 1.0
Thomas Wouters1d75a792000-08-17 22:37:32 +000097
Thomas Woutersed03b412007-08-28 21:37:11 +000098@trackCall
Thomas Woutersed03b412007-08-28 21:37:11 +000099def __eq__(self, *args):
100 return True
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000101
Thomas Woutersed03b412007-08-28 21:37:11 +0000102@trackCall
103def __ne__(self, *args):
104 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000105
Thomas Woutersed03b412007-08-28 21:37:11 +0000106@trackCall
107def __lt__(self, *args):
108 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000109
Thomas Woutersed03b412007-08-28 21:37:11 +0000110@trackCall
111def __le__(self, *args):
112 return True
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000113
Thomas Woutersed03b412007-08-28 21:37:11 +0000114@trackCall
115def __gt__(self, *args):
116 return False
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000117
Thomas Woutersed03b412007-08-28 21:37:11 +0000118@trackCall
119def __ge__(self, *args):
120 return True
121"""
Barry Warsaw07d8d642001-08-20 20:29:07 +0000122
Thomas Woutersed03b412007-08-28 21:37:11 +0000123# Synthesize all the other AllTests methods from the names in testmeths.
Tim Peters01705212001-12-11 19:28:47 +0000124
125method_template = """\
Thomas Woutersed03b412007-08-28 21:37:11 +0000126@trackCall
127def __%s__(self, *args):
128 pass
Tim Peters01705212001-12-11 19:28:47 +0000129"""
130
Thomas Wouters4cdada92006-04-15 09:19:16 +0000131d = {}
Thomas Woutersed03b412007-08-28 21:37:11 +0000132exec(statictests, globals(), d)
Thomas Wouters1d75a792000-08-17 22:37:32 +0000133for method in testmeths:
Thomas Woutersed03b412007-08-28 21:37:11 +0000134 exec(method_template % method, globals(), d)
135AllTests = type("AllTests", (object,), d)
136del d, statictests, method, method_template
Thomas Wouters1d75a792000-08-17 22:37:32 +0000137
Thomas Woutersed03b412007-08-28 21:37:11 +0000138class ClassTests(unittest.TestCase):
139 def setUp(self):
140 callLst[:] = []
Thomas Wouters1d75a792000-08-17 22:37:32 +0000141
Thomas Woutersed03b412007-08-28 21:37:11 +0000142 def assertCallStack(self, expected_calls):
143 actualCallList = callLst[:] # need to copy because the comparison below will add
144 # additional calls to callLst
145 if expected_calls != actualCallList:
146 self.fail("Expected call list:\n %s\ndoes not match actual call list\n %s" %
147 (expected_calls, actualCallList))
Thomas Wouters1d75a792000-08-17 22:37:32 +0000148
Thomas Woutersed03b412007-08-28 21:37:11 +0000149 def testInit(self):
150 foo = AllTests()
151 self.assertCallStack([("__init__", (foo,))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000152
Thomas Woutersed03b412007-08-28 21:37:11 +0000153 def testBinaryOps(self):
154 testme = AllTests()
155 # Binary operations
Thomas Wouters1d75a792000-08-17 22:37:32 +0000156
Thomas Woutersed03b412007-08-28 21:37:11 +0000157 callLst[:] = []
158 testme + 1
159 self.assertCallStack([("__add__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000160
Thomas Woutersed03b412007-08-28 21:37:11 +0000161 callLst[:] = []
162 1 + testme
163 self.assertCallStack([("__radd__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000164
Thomas Woutersed03b412007-08-28 21:37:11 +0000165 callLst[:] = []
166 testme - 1
167 self.assertCallStack([("__sub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000168
Thomas Woutersed03b412007-08-28 21:37:11 +0000169 callLst[:] = []
170 1 - testme
171 self.assertCallStack([("__rsub__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000172
Thomas Woutersed03b412007-08-28 21:37:11 +0000173 callLst[:] = []
174 testme * 1
175 self.assertCallStack([("__mul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000176
Thomas Woutersed03b412007-08-28 21:37:11 +0000177 callLst[:] = []
178 1 * testme
179 self.assertCallStack([("__rmul__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000180
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200181 callLst[:] = []
Serhiy Storchakac2ccce72015-03-12 22:01:30 +0200182 testme @ 1
183 self.assertCallStack([("__matmul__", (testme, 1))])
184
185 callLst[:] = []
186 1 @ testme
187 self.assertCallStack([("__rmatmul__", (testme, 1))])
188
189 callLst[:] = []
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200190 testme / 1
191 self.assertCallStack([("__truediv__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000192
193
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200194 callLst[:] = []
195 1 / testme
196 self.assertCallStack([("__rtruediv__", (testme, 1))])
197
198 callLst[:] = []
199 testme // 1
200 self.assertCallStack([("__floordiv__", (testme, 1))])
201
202
203 callLst[:] = []
204 1 // testme
205 self.assertCallStack([("__rfloordiv__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000206
Thomas Woutersed03b412007-08-28 21:37:11 +0000207 callLst[:] = []
208 testme % 1
209 self.assertCallStack([("__mod__", (testme, 1))])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000210
Thomas Woutersed03b412007-08-28 21:37:11 +0000211 callLst[:] = []
212 1 % testme
213 self.assertCallStack([("__rmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000214
215
Thomas Woutersed03b412007-08-28 21:37:11 +0000216 callLst[:] = []
217 divmod(testme,1)
218 self.assertCallStack([("__divmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000219
Thomas Woutersed03b412007-08-28 21:37:11 +0000220 callLst[:] = []
221 divmod(1, testme)
222 self.assertCallStack([("__rdivmod__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000223
Thomas Woutersed03b412007-08-28 21:37:11 +0000224 callLst[:] = []
225 testme ** 1
226 self.assertCallStack([("__pow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000227
Thomas Woutersed03b412007-08-28 21:37:11 +0000228 callLst[:] = []
229 1 ** testme
230 self.assertCallStack([("__rpow__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000231
Thomas Woutersed03b412007-08-28 21:37:11 +0000232 callLst[:] = []
233 testme >> 1
234 self.assertCallStack([("__rshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000235
Thomas Woutersed03b412007-08-28 21:37:11 +0000236 callLst[:] = []
237 1 >> testme
238 self.assertCallStack([("__rrshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000239
Thomas Woutersed03b412007-08-28 21:37:11 +0000240 callLst[:] = []
241 testme << 1
242 self.assertCallStack([("__lshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000243
Thomas Woutersed03b412007-08-28 21:37:11 +0000244 callLst[:] = []
245 1 << testme
246 self.assertCallStack([("__rlshift__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000247
Thomas Woutersed03b412007-08-28 21:37:11 +0000248 callLst[:] = []
249 testme & 1
250 self.assertCallStack([("__and__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000251
Thomas Woutersed03b412007-08-28 21:37:11 +0000252 callLst[:] = []
253 1 & testme
254 self.assertCallStack([("__rand__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000255
Thomas Woutersed03b412007-08-28 21:37:11 +0000256 callLst[:] = []
257 testme | 1
258 self.assertCallStack([("__or__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000259
Thomas Woutersed03b412007-08-28 21:37:11 +0000260 callLst[:] = []
261 1 | testme
262 self.assertCallStack([("__ror__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000263
Thomas Woutersed03b412007-08-28 21:37:11 +0000264 callLst[:] = []
265 testme ^ 1
266 self.assertCallStack([("__xor__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000267
Thomas Woutersed03b412007-08-28 21:37:11 +0000268 callLst[:] = []
269 1 ^ testme
270 self.assertCallStack([("__rxor__", (testme, 1))])
Thomas Wouters1d75a792000-08-17 22:37:32 +0000271
Thomas Woutersed03b412007-08-28 21:37:11 +0000272 def testListAndDictOps(self):
273 testme = AllTests()
274
275 # List/dict operations
276
277 class Empty: pass
278
279 try:
280 1 in Empty()
281 self.fail('failed, should have raised TypeError')
282 except TypeError:
283 pass
284
285 callLst[:] = []
286 1 in testme
287 self.assertCallStack([('__contains__', (testme, 1))])
288
289 callLst[:] = []
290 testme[1]
291 self.assertCallStack([('__getitem__', (testme, 1))])
292
293 callLst[:] = []
294 testme[1] = 1
295 self.assertCallStack([('__setitem__', (testme, 1, 1))])
296
297 callLst[:] = []
298 del testme[1]
299 self.assertCallStack([('__delitem__', (testme, 1))])
300
301 callLst[:] = []
302 testme[:42]
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000303 self.assertCallStack([('__getitem__', (testme, slice(None, 42)))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000304
305 callLst[:] = []
306 testme[:42] = "The Answer"
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000307 self.assertCallStack([('__setitem__', (testme, slice(None, 42),
308 "The Answer"))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000309
310 callLst[:] = []
311 del testme[:42]
Thomas Woutersd2cf20e2007-08-30 22:57:53 +0000312 self.assertCallStack([('__delitem__', (testme, slice(None, 42)))])
Thomas Woutersed03b412007-08-28 21:37:11 +0000313
314 callLst[:] = []
315 testme[2:1024:10]
316 self.assertCallStack([('__getitem__', (testme, slice(2, 1024, 10)))])
317
318 callLst[:] = []
319 testme[2:1024:10] = "A lot"
320 self.assertCallStack([('__setitem__', (testme, slice(2, 1024, 10),
321 "A lot"))])
322 callLst[:] = []
323 del testme[2:1024:10]
324 self.assertCallStack([('__delitem__', (testme, slice(2, 1024, 10)))])
325
326 callLst[:] = []
327 testme[:42, ..., :24:, 24, 100]
328 self.assertCallStack([('__getitem__', (testme, (slice(None, 42, None),
329 Ellipsis,
330 slice(None, 24, None),
331 24, 100)))])
332 callLst[:] = []
333 testme[:42, ..., :24:, 24, 100] = "Strange"
334 self.assertCallStack([('__setitem__', (testme, (slice(None, 42, None),
335 Ellipsis,
336 slice(None, 24, None),
337 24, 100), "Strange"))])
338 callLst[:] = []
339 del testme[:42, ..., :24:, 24, 100]
340 self.assertCallStack([('__delitem__', (testme, (slice(None, 42, None),
341 Ellipsis,
342 slice(None, 24, None),
343 24, 100)))])
344
Thomas Woutersed03b412007-08-28 21:37:11 +0000345 def testUnaryOps(self):
346 testme = AllTests()
Neil Schemenauer3a313e32004-07-19 16:29:17 +0000347
Thomas Woutersed03b412007-08-28 21:37:11 +0000348 callLst[:] = []
349 -testme
350 self.assertCallStack([('__neg__', (testme,))])
351 callLst[:] = []
352 +testme
353 self.assertCallStack([('__pos__', (testme,))])
354 callLst[:] = []
355 abs(testme)
356 self.assertCallStack([('__abs__', (testme,))])
357 callLst[:] = []
358 int(testme)
359 self.assertCallStack([('__int__', (testme,))])
360 callLst[:] = []
361 float(testme)
362 self.assertCallStack([('__float__', (testme,))])
363 callLst[:] = []
364 oct(testme)
365 self.assertCallStack([('__index__', (testme,))])
366 callLst[:] = []
367 hex(testme)
368 self.assertCallStack([('__index__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000369
370
Thomas Woutersed03b412007-08-28 21:37:11 +0000371 def testMisc(self):
372 testme = AllTests()
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000373
Thomas Woutersed03b412007-08-28 21:37:11 +0000374 callLst[:] = []
375 hash(testme)
376 self.assertCallStack([('__hash__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000377
Thomas Woutersed03b412007-08-28 21:37:11 +0000378 callLst[:] = []
379 repr(testme)
380 self.assertCallStack([('__repr__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000381
Thomas Woutersed03b412007-08-28 21:37:11 +0000382 callLst[:] = []
383 str(testme)
384 self.assertCallStack([('__str__', (testme,))])
Guido van Rossum2c9590f2002-10-29 19:08:29 +0000385
Thomas Woutersed03b412007-08-28 21:37:11 +0000386 callLst[:] = []
387 testme == 1
388 self.assertCallStack([('__eq__', (testme, 1))])
389
390 callLst[:] = []
391 testme < 1
392 self.assertCallStack([('__lt__', (testme, 1))])
393
394 callLst[:] = []
395 testme > 1
396 self.assertCallStack([('__gt__', (testme, 1))])
397
398 callLst[:] = []
399 testme != 1
400 self.assertCallStack([('__ne__', (testme, 1))])
401
402 callLst[:] = []
403 1 == testme
404 self.assertCallStack([('__eq__', (1, testme))])
405
406 callLst[:] = []
407 1 < testme
408 self.assertCallStack([('__gt__', (1, testme))])
409
410 callLst[:] = []
411 1 > testme
412 self.assertCallStack([('__lt__', (1, testme))])
413
414 callLst[:] = []
415 1 != testme
416 self.assertCallStack([('__ne__', (1, testme))])
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000417
418
Thomas Woutersed03b412007-08-28 21:37:11 +0000419 def testGetSetAndDel(self):
420 # Interfering tests
421 class ExtraTests(AllTests):
422 @trackCall
423 def __getattr__(self, *args):
424 return "SomeVal"
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000425
Thomas Woutersed03b412007-08-28 21:37:11 +0000426 @trackCall
427 def __setattr__(self, *args):
428 pass
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000429
Thomas Woutersed03b412007-08-28 21:37:11 +0000430 @trackCall
431 def __delattr__(self, *args):
432 pass
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000433
Thomas Woutersed03b412007-08-28 21:37:11 +0000434 testme = ExtraTests()
435
436 callLst[:] = []
437 testme.spam
438 self.assertCallStack([('__getattr__', (testme, "spam"))])
439
440 callLst[:] = []
441 testme.eggs = "spam, spam, spam and ham"
442 self.assertCallStack([('__setattr__', (testme, "eggs",
443 "spam, spam, spam and ham"))])
444
445 callLst[:] = []
446 del testme.cardinal
447 self.assertCallStack([('__delattr__', (testme, "cardinal"))])
448
449 def testDel(self):
450 x = []
451
452 class DelTest:
453 def __del__(self):
454 x.append("crab people, crab people")
455 testme = DelTest()
456 del testme
457 import gc
458 gc.collect()
Ezio Melottib3aedd42010-11-20 19:04:17 +0000459 self.assertEqual(["crab people, crab people"], x)
Thomas Woutersed03b412007-08-28 21:37:11 +0000460
461 def testBadTypeReturned(self):
462 # return values of some method are type-checked
463 class BadTypeClass:
464 def __int__(self):
465 return None
466 __float__ = __int__
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200467 __complex__ = __int__
Thomas Woutersed03b412007-08-28 21:37:11 +0000468 __str__ = __int__
469 __repr__ = __int__
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200470 __bytes__ = __int__
471 __bool__ = __int__
472 __index__ = __int__
473 def index(x):
474 return [][x]
Thomas Woutersed03b412007-08-28 21:37:11 +0000475
Serhiy Storchakaa60c2fe2015-03-12 21:56:08 +0200476 for f in [float, complex, str, repr, bytes, bin, oct, hex, bool, index]:
Thomas Woutersed03b412007-08-28 21:37:11 +0000477 self.assertRaises(TypeError, f, BadTypeClass())
478
479 def testHashStuff(self):
480 # Test correct errors from hash() on objects with comparisons but
481 # no __hash__
482
483 class C0:
484 pass
485
486 hash(C0()) # This should work; the next two should raise TypeError
487
Thomas Woutersed03b412007-08-28 21:37:11 +0000488 class C2:
489 def __eq__(self, other): return 1
490
491 self.assertRaises(TypeError, hash, C2())
492
493
494 def testSFBug532646(self):
495 # Test for SF bug 532646
496
497 class A:
498 pass
499 A.__call__ = A()
500 a = A()
501
502 try:
503 a() # This should not segfault
504 except RuntimeError:
505 pass
506 else:
507 self.fail("Failed to raise RuntimeError")
508
509 def testForExceptionsRaisedInInstanceGetattr2(self):
510 # Tests for exceptions raised in instance_getattr2().
511
512 def booh(self):
513 raise AttributeError("booh")
514
515 class A:
516 a = property(booh)
517 try:
518 A().a # Raised AttributeError: A instance has no attribute 'a'
519 except AttributeError as x:
520 if str(x) != "booh":
521 self.fail("attribute error for A().a got masked: %s" % x)
522
523 class E:
524 __eq__ = property(booh)
525 E() == E() # In debug mode, caused a C-level assert() to fail
526
527 class I:
528 __init__ = property(booh)
529 try:
530 # In debug mode, printed XXX undetected error and
531 # raises AttributeError
532 I()
533 except AttributeError as x:
534 pass
535 else:
536 self.fail("attribute error for I.__init__ got masked")
537
538 def testHashComparisonOfMethods(self):
539 # Test comparison and hash of methods
540 class A:
541 def __init__(self, x):
542 self.x = x
543 def f(self):
544 pass
545 def g(self):
546 pass
547 def __eq__(self, other):
548 return self.x == other.x
549 def __hash__(self):
550 return self.x
551 class B(A):
552 pass
553
554 a1 = A(1)
555 a2 = A(2)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000556 self.assertEqual(a1.f, a1.f)
557 self.assertNotEqual(a1.f, a2.f)
558 self.assertNotEqual(a1.f, a1.g)
559 self.assertEqual(a1.f, A(1).f)
560 self.assertEqual(hash(a1.f), hash(a1.f))
561 self.assertEqual(hash(a1.f), hash(A(1).f))
Thomas Woutersed03b412007-08-28 21:37:11 +0000562
Ezio Melottib3aedd42010-11-20 19:04:17 +0000563 self.assertNotEqual(A.f, a1.f)
564 self.assertNotEqual(A.f, A.g)
565 self.assertEqual(B.f, A.f)
566 self.assertEqual(hash(B.f), hash(A.f))
Thomas Woutersed03b412007-08-28 21:37:11 +0000567
568 # the following triggers a SystemError in 2.4
Christian Heimes4a22b5d2007-11-25 09:39:14 +0000569 a = A(hash(A.f)^(-1))
Thomas Woutersed03b412007-08-28 21:37:11 +0000570 hash(a.f)
571
572def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000573 support.run_unittest(ClassTests)
Thomas Woutersed03b412007-08-28 21:37:11 +0000574
575if __name__=='__main__':
576 test_main()